def main(): d = 5 ps = rand(d) ps /= norm(ps) distribution = Bernoulli(ps) num_history = 100 Z = distribution.sample(num_history).samples threshold = 0.8 spread = 0.2 gamma = 0.2 kernel = HypercubeKernel(gamma) mcmc_sampler = DiscreteKameleon(distribution, kernel, Z, threshold, spread) start = zeros(distribution.dimension, dtype=numpy.bool8) mcmc_params = MCMCParams(start=start, num_iterations=1000) chain = MCMCChain(mcmc_sampler, mcmc_params) chain.append_mcmc_output(StatisticsOutput(plot_times=True)) chain.append_mcmc_output(DiscretePlottingOutput(plot_from=0, lag=100)) chain.run() print "ps", ps print "empirical", mean(chain.samples, 0)
def main(): d = 5 ps = rand(d) ps /= norm(ps) distribution = Bernoulli(ps) num_history = 100 Z = distribution.sample(num_history).samples threshold = 0.8 spread = 0.2 gamma = 0.2 kernel = HypercubeKernel(gamma) mcmc_sampler = DiscreteKameleon(distribution, kernel, Z, threshold, spread) start = zeros(distribution.dimension, dtype=numpy.bool8) mcmc_params = MCMCParams(start=start, num_iterations=1000) chain = MCMCChain(mcmc_sampler, mcmc_params) chain.append_mcmc_output(StatisticsOutput(plot_times=True)) chain.append_mcmc_output(DiscretePlottingOutput(plot_from=0, lag=100)) chain.run() print "ps", ps print "empirical", mean(chain.samples, 0)
def test_chain_bernoulli(self): # runs the sampler on a distribution of infdependent bernoulli variables # and compares the mean d = 5 ps = rand(d) ps /= norm(ps) distribution = Bernoulli(ps) num_history = 100 Z = distribution.sample(num_history).samples threshold = 0.8 spread = 0.2 gamma = 0.2 kernel = HypercubeKernel(gamma) mcmc_sampler = DiscreteKameleon(distribution, kernel, Z, threshold, spread) start = zeros(distribution.dimension, dtype=numpy.bool8) mcmc_params = MCMCParams(start=start, num_iterations=1000) chain = MCMCChain(mcmc_sampler, mcmc_params) chain.run() self.assertAlmostEqual(norm(mean(chain.samples, 0) - ps), 0, delta=0.2)
def test_sample_dim(self): n = 3 d = 2 p = asarray(rand(d)) b = Bernoulli(p) s = b.sample(n) self.assertEqual(s.samples.shape, (n, d))
def test_sample_mean_values(self): n = 10000 d = 3 runs = 100 for _ in range(runs): ps = asarray(rand(d)) b = Bernoulli(ps) s = b.sample(n) for i in range(d): self.assertAlmostEqual(mean(s.samples[:, i]), ps[i], delta=0.05)
def test_chain_bernoulli(self): # runs the sampler on a distribution of infdependent bernoulli variables # and compares the mean d = 5 ps = rand(d) ps /= norm(ps) distribution = Bernoulli(ps) num_history = 100 Z = distribution.sample(num_history).samples threshold = 0.8 spread = 0.2 gamma = 0.2 kernel = HypercubeKernel(gamma) mcmc_sampler = DiscreteKameleon(distribution, kernel, Z, threshold, spread) start = zeros(distribution.dimension, dtype=numpy.bool8) mcmc_params = MCMCParams(start=start, num_iterations=1000) chain = MCMCChain(mcmc_sampler, mcmc_params) chain.run() self.assertAlmostEqual(norm(mean(chain.samples, 0) - ps), 0, delta=0.2)
def test_sample_samples_dtype(self): p = asarray([0.5]) b = Bernoulli(p) self.assertEqual(b.sample(1).samples.dtype, numpy.bool8)
def test_sample_type(self): p = asarray([0.5]) b = Bernoulli(p) self.assertTrue(isinstance(b.sample(1), Sample))