def test_step_continuous(self): start, model, (mu, C) = mv_simple() unc = np.diag(C)**.5 check = (('x', np.mean, mu, unc / 10.), ('x', np.std, unc, unc / 10.)) with model: steps = ( Slice(), HamiltonianMC(scaling=C, is_cov=True, blocked=False), NUTS(scaling=C, is_cov=True, blocked=False), Metropolis(S=C, proposal_dist=MultivariateNormalProposal, blocked=True), Slice(blocked=True), HamiltonianMC(scaling=C, is_cov=True), NUTS(scaling=C, is_cov=True), CompoundStep([ HamiltonianMC(scaling=C, is_cov=True), HamiltonianMC(scaling=C, is_cov=True, blocked=False) ]), ) for step in steps: trace = sample(8000, step=step, start=start, model=model, random_seed=1) yield self.check_stat, check, trace, step.__class__.__name__
def test_non_blocked(): """Test that samplers correctly create non-blocked compound steps. """ start, model = simple_2model() with model: # Metropolis and Slice are non-blocked by default mh = Metropolis() assert isinstance(mh, CompoundStep) slicer = Slice() assert isinstance(slicer, CompoundStep) hmc = HamiltonianMC(blocked=False) assert isinstance(hmc, CompoundStep) nuts = NUTS(blocked=False) assert isinstance(nuts, CompoundStep) mh_blocked = Metropolis(blocked=True) assert isinstance(mh_blocked, Metropolis) slicer_blocked = Slice(blocked=True) assert isinstance(slicer_blocked, Slice) hmc_blocked = HamiltonianMC() assert isinstance(hmc_blocked, HamiltonianMC) nuts_blocked = NUTS() assert isinstance(nuts_blocked, NUTS) compound = CompoundStep([hmc_blocked, mh_blocked])
def test_step_continuous(self): start, model, (mu, C) = mv_simple() unc = np.diag(C)**0.5 check = (("x", np.mean, mu, unc / 10.0), ("x", np.std, unc, unc / 10.0)) with model: steps = ( Slice(), HamiltonianMC(scaling=C, is_cov=True, blocked=False), NUTS(scaling=C, is_cov=True, blocked=False), Metropolis(S=C, proposal_dist=MultivariateNormalProposal, blocked=True), Slice(blocked=True), HamiltonianMC(scaling=C, is_cov=True), NUTS(scaling=C, is_cov=True), CompoundStep([ HamiltonianMC(scaling=C, is_cov=True), HamiltonianMC(scaling=C, is_cov=True, blocked=False), ]), ) for step in steps: trace = sample( 0, tune=8000, chains=1, discard_tuned_samples=False, step=step, start=start, model=model, #random_seed=1, cores=1) self.check_stat(check, trace, step.__class__.__name__)
def test_step_continuous(): start, model, (mu, C) = mv_simple() with model: mh = Metropolis() slicer = Slice() hmc = HamiltonianMC(scaling=C, is_cov=True, blocked=False) nuts = NUTS(scaling=C, is_cov=True, blocked=False) mh_blocked = Metropolis(S=C, proposal_dist=MultivariateNormalProposal, blocked=True) slicer_blocked = Slice(blocked=True) hmc_blocked = HamiltonianMC(scaling=C, is_cov=True) nuts_blocked = NUTS(scaling=C, is_cov=True) compound = CompoundStep([hmc_blocked, mh_blocked]) steps = [ slicer, hmc, nuts, mh_blocked, hmc_blocked, slicer_blocked, nuts_blocked, compound ] unc = np.diag(C)**.5 check = [('x', np.mean, mu, unc / 10.), ('x', np.std, unc, unc / 10.)] for st in steps: h = sample(8000, st, start, model=model, random_seed=1) for (var, stat, val, bound) in check: yield check_stat, repr(st), h, var, stat, val, bound