def test_three_variances(self):
     model, options, parameters, data = gf.setup_mcmc()
     CP = CovarianceProcedures()
     CP._qcov = np.atleast_2d([0.2, 0.3, 0.4])
     CP._CovarianceProcedures__setup_R_based_on_variances(parind=[0, 1, 2])
     self.assertTrue(np.array_equal(CP._R,
                                    np.diagflat(np.sqrt([0.2, 0.3, 0.4]))),
                     msg=str('Expect sqrt of variances: {}'.format(CP._R)))
     self.assertTrue(np.array_equal(CP._qcovorig,
                                    np.diagflat([0.2, 0.3, 0.4])),
                     msg='Arrays should match')
    def test_print_these_none(self):
        model, options, parameters, data = gf.setup_mcmc()
        CP = CovarianceProcedures()
        CP._initialize_covariance_settings(parameters=parameters,
                                           options=options)

        print_these = CP.display_covariance_settings(print_these=None)
        self.assertEqual(print_these, [
            'qcov', 'R', 'RDR', 'invR', 'last_index_since_adaptation',
            'covchain'
        ],
                         msg='Default print keys')
 def test_3x3_cov_mtx(self):
     model, options, parameters, data = gf.setup_mcmc()
     CP = CovarianceProcedures()
     testmtx = np.atleast_2d([[0.2, 0.01, 0.05], [0.01, 0.3, 0.024],
                              [0.05, 0.024, 0.04]])
     CP._qcov = testmtx
     CP._CovarianceProcedures__setup_R_based_on_covariance_matrix(
         parind=[0, 1, 2])
     self.assertTrue(np.array_equal(CP._R,
                                    np.linalg.cholesky(testmtx).T),
                     msg='Expect sqrt of variance')
     self.assertTrue(np.array_equal(CP._qcovorig, testmtx),
                     msg='Expect sqrt of variance')
 def test_2x2_cov_mtx(self):
     model, options, parameters, data = gf.setup_mcmc()
     CP = CovarianceProcedures()
     CP._qcov = np.atleast_2d([[0.2, 0.1], [0.1, 0.3]])
     CP._CovarianceProcedures__setup_R_based_on_covariance_matrix(
         parind=[0, 1])
     self.assertTrue(np.array_equal(
         CP._R,
         np.linalg.cholesky(np.atleast_2d([[0.2, 0.1], [0.1, 0.3]])).T),
                     msg='Expect sqrt of variance')
     self.assertTrue(np.array_equal(CP._qcovorig,
                                    np.atleast_2d([[0.2, 0.1], [0.1,
                                                                0.3]])),
                     msg='Expect sqrt of variance')
Пример #5
0
def setup_run_adapt_settings():
    __, options, parameters, data = gf.setup_mcmc()
    CP = CovarianceProcedures()
    CP._initialize_covariance_settings(parameters=parameters, options=options)
    rejected = {'in_adaptation_interval': 4, 'total': 10, 'outside_bounds': 1}
    isimu = 100
    iiadapt = 100
    chain = np.zeros([100, 2])
    chain[:, 0] = np.linspace(1, 100, 100)
    chain[:, 1] = np.linspace(1, 100, 100)
    chainind = 100
    u = np.random.random_sample(size=(1, 2))
    npar = 2
    alpha = 0.78
    return CP, options, isimu, iiadapt, rejected, chain, chainind, u, npar, alpha
    def test_update_cov_wsum_none(self):
        __, options, parameters, data = gf.setup_mcmc()
        CP = CovarianceProcedures()
        CP._initialize_covariance_settings(parameters=parameters,
                                           options=options)

        theta = np.array([2., 5.])
        CP._wsum = None
        CP._covchain = 1
        CP._meanchain = 2
        CP._qcov = 0
        CP._update_covariance_settings(parameter_set=theta)

        CPD = CP.__dict__
        self.assertEqual(CPD['_covchain'], 1, msg='_covchain unchanged.')
        self.assertEqual(CPD['_meanchain'], 2, msg='_meanchain unchanged.')
    def test_print_these_not_none(self):
        model, options, parameters, data = gf.setup_mcmc()
        CP = CovarianceProcedures()
        CP._initialize_covariance_settings(parameters=parameters,
                                           options=options)

        print_these = [
            'qcov', 'R', 'RDR', 'invR', 'last_index_since_adaptation',
            'covchain'
        ]
        for __, ptii in enumerate(print_these):
            self.assertEqual(
                CP.display_covariance_settings(print_these=[ptii]), [ptii],
                msg='Specified print keys')


# -------------------------------------------
    def test_update_cov_wsum_not_none(self):
        model, options, parameters, data = gf.setup_mcmc()
        CP = CovarianceProcedures()
        CP._initialize_covariance_settings(parameters=parameters,
                                           options=options)

        theta = np.array([2., 5.])
        CP._wsum = 10
        CP._covchain = 1
        CP._meanchain = 2
        CP._qcov = 0
        CP._update_covariance_settings(parameter_set=theta)

        CPD = CP.__dict__
        self.assertEqual(CPD['_covchain'], 0, msg='_covchain = _qcov.')
        self.assertTrue(np.array_equal(CPD['_meanchain'], theta),
                        msg='_meanchain = parameter_set.')
Пример #9
0
    def test_unpack_covariance(self):
        model, options, parameters, data = gf.setup_mcmc()
        CP = CovarianceProcedures()
        CP._initialize_covariance_settings(parameters=parameters,
                                           options=options)

        last_index_since_adaptation, R, oldcovchain, oldmeanchain, oldwsum, no_adapt_index, qcov_scale, qcov = unpack_covariance_settings(
            covariance=CP)

        out = {
            'last_index_since_adaptation': last_index_since_adaptation,
            'R': R,
            'oldcovchain': oldcovchain,
            'oldmeanchain': oldmeanchain,
            'oldwsum': oldwsum,
            'no_adapt_index': no_adapt_index,
            'qcov_scale': qcov_scale,
            'qcov': qcov
        }

        defaults = {
            'last_index_since_adaptation': 0,
            'R': np.array([[0.1, 0.], [0., 0.25]]),
            'oldcovchain': None,
            'oldmeanchain': None,
            'oldwsum': None,
            'no_adapt_index': np.array([False, False]),
            'qcov_scale': 2.4 / np.sqrt(2),
            'qcov': np.square(np.array([[0.1, 0.], [0., 0.25]]))
        }

        check_these = [
            'last_index_since_adaptation', 'oldcovchain', 'oldmeanchain',
            'oldwsum'
        ]
        for item in check_these:
            self.assertEqual(out[item],
                             defaults[item],
                             msg=str('Expected {} = {} ? {}'.format(
                                 item, defaults[item], out[item])))

        check_these = ['R', 'no_adapt_index', 'qcov_scale', 'qcov']
        for item in check_these:
            self.assertTrue(np.array_equal(out[item], defaults[item]),
                            msg=str('Expected {} = {} ? {}'.format(
                                item, defaults[item], out[item])))
    def test_update_cov(self):
        __, options, parameters, __ = gf.setup_mcmc()
        CP = CovarianceProcedures()
        CP._initialize_covariance_settings(parameters=parameters,
                                           options=options)
        check = {
            'R': np.random.random_sample(size=(2, 2)),
            'covchain': np.random.random_sample(size=(2, 2)),
            'meanchain': np.random.random_sample(size=(1, 2)),
            'wsum': np.random.random_sample(size=(2, 1)),
            'last_index_since_adaptation': 0,
            'iiadapt': 100
        }
        CP._update_covariance_from_adaptation(**check)
        CPD = CP.__dict__
        items = ['last_index_since_adaptation', 'iiadapt']
        for __, ai in enumerate(items):
            self.assertEqual(CPD[str('_{}'.format(ai))],
                             check[ai],
                             msg=str('{}: {} != {}'.format(
                                 ai, CPD[str('_{}'.format(ai))], check[ai])))

        array_items = ['R', 'covchain', 'meanchain', 'wsum']
        for __, ai in enumerate(array_items):
            self.assertTrue(np.array_equal(CPD[str('_{}'.format(ai))],
                                           check[ai]),
                            msg=str('{}: {} != {}'.format(
                                ai, CPD[str('_{}'.format(ai))], check[ai])))
 def test_init_CP_original_cov(self):
     CP = CovarianceProcedures()
     __, options, parameters, __ = gf.setup_mcmc()
     CP = CovarianceProcedures()
     CP._initialize_covariance_settings(parameters=parameters,
                                        options=options)
     self.assertTrue(hasattr(CP, '_qcov_original'),
                     msg='Has assigned original covariance matrix')
     original_covariance = CP._qcov_original.copy()
     check = {
         'R': np.random.random_sample(size=(2, 2)),
         'covchain': np.random.random_sample(size=(2, 2)),
         'meanchain': np.random.random_sample(size=(1, 2)),
         'wsum': np.random.random_sample(size=(2, 1)),
         'last_index_since_adaptation': 0,
         'iiadapt': 100
     }
     CP._update_covariance_from_adaptation(**check)
     self.assertTrue(
         np.array_equal(CP._qcov_original, original_covariance),
         msg='Expect original cov. mtx. unchanged after update.')
    def test_update_cov(self):
        __, options, parameters, __ = gf.setup_mcmc()
        CP = CovarianceProcedures()
        CP._initialize_covariance_settings(parameters=parameters,
                                           options=options)

        check = {
            'RDR': np.random.random_sample(size=(2, 2)),
            'invR': np.random.random_sample(size=(2, 2)),
        }

        CP._update_covariance_for_delayed_rejection_from_adaptation(**check)
        CPD = CP.__dict__
        array_items = ['RDR', 'invR']
        for __, ai in enumerate(array_items):
            self.assertTrue(np.array_equal(CPD[str('_{}'.format(ai))],
                                           check[ai]),
                            msg=str('{}: {} != {}'.format(
                                ai, CPD[str('_{}'.format(ai))], check[ai])))
 def test_init_CP(self):
     CP = CovarianceProcedures()
     self.assertTrue(hasattr(CP, 'description'))
 def test_one_variance(self):
     model, options, parameters, data = gf.setup_mcmc()
     CP = CovarianceProcedures()
     CP._qcov = np.atleast_2d([0.2])
     CP._CovarianceProcedures__setup_R_based_on_variances(parind=[0])
     self.assertEqual(CP._R, np.sqrt(0.2), msg='Expect sqrt of variance')