def rvs(theta): n1.mean = theta k = n1.rvs() k[0] = np.abs(k[0]) return k n2 = normal(np.zeros(blr.n), 3 * np.eye(blr.n)) def pdf(x, y): n2.mean = x return n2.pdf(y) prop = ProposalDistribution(pdf, rvs) mcmc = Mcmc(prop, blr) first_sample = blr.generate_first_sample() samples = mcmc.sample(10000, first_sample) utils.plot(xs, ys, samples, blr.n) ss, hs, sigmas = utils.individual_samples(samples, blr.n) smeans = np.mean(ss, 1) hmeans = np.mean(hs, 1)
n3 = normal(0, 2) def rvs(x): n3.mean = x return [n3.rvs()] def pdf(x, y): n3.mean = x return n3.pdf(y) stationary = ProposalDistribution(stat, stat) proposal = ProposalDistribution(pdf, rvs) normative, err = quad(stat, -10, 10) mcmc = Mcmc(proposal, stationary) samples = mcmc.sample(10000, [0]) samples = [sample[0] for sample in samples] xs = np.linspace(-10, 10, 1000) ys = [stat(x) / normative for x in xs] plt.plot(xs, ys, 'r') plt.hist(samples, normed=True, bins=100) plt.savefig('images/mcmc_2normals.png') plt.gcf().clear()