def setup_dr(self): model, options, parameters, data, covariance, __, __, __, __ = gf.setup_mcmc_case_dr( ) RDR = covariance._RDR invR = covariance._invR old_set = ParameterSet(theta=np.random.rand(2), ss=np.array([10.2]), sigma2=np.array([0.5]), prior=np.array([0.5])) new_set = ParameterSet(theta=np.random.rand(2), ss=np.array([8.2]), sigma2=np.array([0.5]), prior=np.array([0.5])) priorobj = PriorFunction( priorfun=model.prior_function, mu=parameters._thetamu[parameters._parind[:]], sigma=parameters._thetasigma[parameters._parind[:]]) sosobj = SumOfSquares(model, data, parameters) DR = DelayedRejection() DR._initialize_dr_metrics(options=options) accept, out_set, outbound = DR.run_delayed_rejection( old_set=old_set, new_set=new_set, RDR=RDR, ntry=2, parameters=parameters, invR=invR, sosobj=sosobj, priorobj=priorobj) return accept, out_set, outbound
def test_alphafun(self): invR = [] invR.append(np.array([[0.4, 0.1], [0., 0.2]])) invR.append(np.array([[0.4, 0.1], [0., 0.2]]) / 4) invR.append(np.array([[0.4, 0.1], [0., 0.2]]) / 5) trypath = [] trypath.append( ParameterSet(theta=0.1, ss=np.array([10.2]), sigma2=np.array([0.5]), prior=np.array([0.5]))) trypath.append( ParameterSet(theta=0.2, ss=np.array([8.2]), sigma2=np.array([0.5]), prior=np.array([0.5]))) trypath.append( ParameterSet(theta=0.2, ss=np.array([8.2]), sigma2=np.array([0.5]), prior=np.array([0.5]))) trypath.append( ParameterSet(theta=0.2, ss=np.array([8.2]), sigma2=np.array([0.5]), prior=np.array([0.5]))) __, options, __, __ = gf.setup_mcmc() DR = DelayedRejection() DR._initialize_dr_metrics(options=options) alpha = DR.alphafun(trypath=trypath, invR=invR) self.assertIsInstance(alpha, np.ndarray, msg='Expect numpy array return') self.assertEqual(alpha.size, 1, msg='Expect single element array')
def test_set_based_on_accept_true(self): next_set = ParameterSet(theta=np.random.random_sample(size=(2, 1)), ss=0.4) old_set = ParameterSet(theta=np.random.random_sample(size=(2, 1)), ss=0.6) out_set = update_set_based_on_acceptance(accept=True, old_set=old_set, next_set=next_set) self.assertEqual(out_set, next_set)
def setup_trypath(self, iq=0): trypath = [] trypath.append(ParameterSet(theta=0.1)) trypath.append(ParameterSet(theta=0.2)) trypath.append(ParameterSet(theta=0.3)) trypath.append(ParameterSet(theta=0.4)) stage = len(trypath) - 2 y1, y2, y3, y4 = extract_state_elements(iq=iq, stage=stage, trypath=trypath) return trypath, y1, y2, y3, y4
def test_logposteriorratio_with_2d(self): trypath = [] trypath.append( ParameterSet(theta=0.1, ss=np.array([10.2, 5.1]), sigma2=np.array([0.5, 0.6]), prior=np.array([0.5, 0.75]))) trypath.append( ParameterSet(theta=0.2, ss=np.array([8.2, 7.5]), sigma2=np.array([0.5, 1.2]), prior=np.array([0.5, 0.25]))) zq = log_posterior_ratio(x1=trypath[0], x2=trypath[-1]) self.assertTrue(isinstance(zq, float), msg='Expect float return')
def test_chain_not_accepted_within_bounds(self): accept = 0 outsidebounds = 0 CL = { 'theta': np.array([1.0, 2.0]), 'ss': 1.0, 'prior': 0.0, 'sigma2': 0.0 } parset = ParameterSet(theta=CL['theta'], ss=CL['ss'], prior=CL['prior'], sigma2=CL['sigma2']) mcstat = gf.setup_initialize_chains(CL) mcstat._MCMC__rejected = { 'total': 10, 'in_adaptation_interval': 4, 'outside_bounds': 1 } mcstat._MCMC__update_chain(accept=accept, new_set=parset, outsidebounds=outsidebounds) self.assertTrue(np.array_equal(mcstat._MCMC__chain[-1, :], mcstat._MCMC__old_set.theta), msg=str('theta added to end of chain - {}'.format( mcstat._MCMC__chain[-1, :])))
def test_set_outsidebounds(self): next_set = ParameterSet() next_set, outbound = set_outside_bounds(next_set = next_set) self.assertEqual(next_set.alpha, 0, msg = 'next_set.alpha should be 0') self.assertEqual(next_set.prior, 0, msg = 'next_set.prior should be 0') self.assertEqual(next_set.ss, np.inf, msg = 'next_set.ss should be np.inf') self.assertEqual(outbound, 1, msg = 'outbound should be 1')
def test_nth_stage_logpropratio(self): iq = 0 trypath = [] trypath.append( ParameterSet(theta=0.1, ss=np.array([10.2]), sigma2=np.array([0.5]), prior=np.array([0.5]))) trypath.append( ParameterSet(theta=0.2, ss=np.array([8.2]), sigma2=np.array([0.5]), prior=np.array([0.5]))) zq = nth_stage_log_proposal_ratio(iq=iq, trypath=trypath, invR=None) self.assertTrue(np.array_equal(zq, np.zeros([1])), msg='Expect arrays to match')
def test_unpack_set(self): CL = {'theta': 1.0, 'ss': 1.0, 'prior': 0.0, 'sigma2': 0.0} parset = ParameterSet(theta=CL['theta'], ss=CL['ss'], prior=CL['prior'], sigma2=CL['sigma2']) MA = Metropolis() oldpar, ss, oldprior, sigma2 = MA.unpack_set(parset) NL = {'theta': oldpar, 'ss': ss, 'prior': oldprior, 'sigma2': sigma2} self.assertDictEqual(CL, NL)
def test_nth_stage_logpropratio_invR(self): iq = 0 invR = [np.array([[0.4, 0.1], [0., 0.2]])] trypath = [] trypath.append( ParameterSet(theta=0.1, ss=np.array([10.2]), sigma2=np.array([0.5]), prior=np.array([0.5]))) trypath.append( ParameterSet(theta=0.2, ss=np.array([8.2]), sigma2=np.array([0.5]), prior=np.array([0.5]))) trypath.append( ParameterSet(theta=0.2, ss=np.array([8.2]), sigma2=np.array([0.5]), prior=np.array([0.5]))) zq = nth_stage_log_proposal_ratio(iq=iq, trypath=trypath, invR=invR) self.assertTrue(isinstance(zq, float), msg='Expect float return')
def setup_rms(cls, CL): sos_object, prior_object, parameters = gf.setup_mcmc_case_mh() MS = Metropolis() R = np.array([[0.4, 0.2], [0, 0.3]]) parset = ParameterSet(theta=CL['theta'], ss=CL['ss'], prior=CL['prior'], sigma2=CL['sigma2']) accept, _, outbound, npar_sample_from_normal = MS.run_metropolis_step( old_set=parset, parameters=parameters, R=R, prior_object=prior_object, sos_object=sos_object) return accept, outbound
def setup_initialize_chains(CL, updatesigma=True, nsos=1): mcstat = setup_mcmc_case_cp() mcstat.simulation_options.updatesigma = updatesigma mcstat.model_settings.nsos = nsos mcstat._MCMC__old_set = ParameterSet(theta=CL['theta'], ss=CL['ss'], prior=CL['prior'], sigma2=CL['sigma2']) mcstat._MCMC__chain_index = mcstat.simulation_options.nsimu - 1 mcstat._MCMC__initialize_chains( chainind=0, nsimu=mcstat.simulation_options.nsimu, npar=mcstat.parameters.npar, nsos=mcstat.model_settings.nsos, updatesigma=mcstat.simulation_options.updatesigma, sigma2=mcstat.model_settings.sigma2) return mcstat
def test_chain_accepted(self): accept = 1 outsidebounds = 0 CL = { 'theta': np.array([1.0, 2.0]), 'ss': 1.0, 'prior': 0.0, 'sigma2': 0.0 } parset = ParameterSet(theta=CL['theta'], ss=CL['ss'], prior=CL['prior'], sigma2=CL['sigma2']) mcstat = gf.setup_initialize_chains(CL) mcstat._MCMC__update_chain(accept=accept, new_set=parset, outsidebounds=outsidebounds) self.assertTrue(np.array_equal(mcstat._MCMC__chain[-1, :], parset.theta), msg=str('theta added to end of chain - {}'.format( mcstat._MCMC__chain[-1, :]))) self.assertEqual(mcstat._MCMC__old_set, parset, msg='old_set updated to new set')
def test_PS_default_match(self): PS = ParameterSet() PSD = PS.__dict__ for (k,v) in PSD.items(): self.assertEqual(v, None, msg = str('Default {} is None'.format(k)))
def test_PS_set_theta(self): x = 1.2 key = ['theta'] PS = ParameterSet(theta = x) self.standard_check(key, x, PS)
def test_PS_set_ss(self): x = 1.2 key = ['ss'] PS = ParameterSet(ss = x) self.standard_check(key, x, PS)
def test_PS_set_prior(self): x = 1.2 key = ['prior'] PS = ParameterSet(prior = x) self.standard_check(key, x, PS)
def test_PS_set_theta_and_prior(self): x = 1.2 keys = ['theta', 'prior'] PS = ParameterSet(theta = x, prior = x) self.standard_check(keys, x, PS)
def test_PS_set_alpha(self): x = 1.2 key = ['alpha'] PS = ParameterSet(alpha = x) self.standard_check(key, x, PS)
def test_PS_set_sigma2(self): x = 1.2 key = ['sigma2'] PS = ParameterSet(sigma2 = x) self.standard_check(key, x, PS)