def test_mixture_list_of_poissons(self): with Model() as model: w = Dirichlet("w", floatX(np.ones_like(self.pois_w)), shape=self.pois_w.shape) mu = Gamma("mu", 1.0, 1.0, shape=self.pois_w.size) Mixture( "x_obs", w, [Poisson.dist(mu[0]), Poisson.dist(mu[1])], observed=self.pois_x) step = Metropolis() trace = sample(5000, step, random_seed=self.random_seed, progressbar=False, chains=1) assert_allclose(np.sort(trace["w"].mean(axis=0)), np.sort(self.pois_w), rtol=0.1, atol=0.1) assert_allclose(np.sort(trace["mu"].mean(axis=0)), np.sort(self.pois_mu), rtol=0.1, atol=0.1)
def test_plots_multidimensional(): # Test single trace from .models import multidimensional_model start, model, _ = multidimensional_model() with model as model: h = np.diag(find_hessian(start)) step = Metropolis(model.vars, h) trace = sample(3000, step, start) traceplot(trace)
def test_normal_mixture(self): with Model() as model: w = Dirichlet("w", floatX(np.ones_like(self.norm_w)), shape=self.norm_w.size) mu = Normal("mu", 0.0, 10.0, shape=self.norm_w.size) tau = Gamma("tau", 1.0, 1.0, shape=self.norm_w.size) NormalMixture("x_obs", w, mu, tau=tau, observed=self.norm_x) step = Metropolis() trace = sample(5000, step, random_seed=self.random_seed, progressbar=False, chains=1) assert_allclose(np.sort(trace["w"].mean(axis=0)), np.sort(self.norm_w), rtol=0.1, atol=0.1) assert_allclose( np.sort(trace["mu"].mean(axis=0)), np.sort(self.norm_mu), rtol=0.1, atol=0.1 )
def test_multichain_plots(): from pymc.examples import disaster_model as dm with dm.model as model: # Run sampler step1 = Slice([dm.early_mean, dm.late_mean]) step2 = Metropolis([dm.switchpoint]) start = {'early_mean': 2., 'late_mean': 3., 'switchpoint': 50} ptrace = psample(1000, [step1, step2], start, threads=2) forestplot(ptrace, vars=['early_mean', 'late_mean']) autocorrplot(ptrace, vars=['switchpoint'])
def test_plots(): # Test single trace from pymc.examples import arbitrary_stochastic as asmod with asmod.model as model: start = model.test_point h = find_hessian(start) step = Metropolis(model.vars, h) trace = sample(3000, step, start) forestplot(trace) autocorrplot(trace)
def simple_init(): start, model, moments = simple_model() step = Metropolis(model.vars, np.diag([1.]), model=model) return model, start, step, moments
def __init__(self, stochastic, cov=None, scale=1., proposal_sd=None, verbose=None, tally=True): Metropolis.__init__(self, stochastic, scale=scale, proposal_sd=proposal_sd, proposal_distribution="Normal", verbose=verbose, tally=tally) self.Cov = cov