def fit_beta(nsteps): """Fit a beta distribution by MCMC.""" num_dimensions = 1 # Create the dummy model b = BetaFit(0.5, 0.5) # Create the options opts = MCMCOpts() opts.model = b opts.estimate_params = b.parameters opts.initial_values = [1.001] opts.nsteps = nsteps opts.anneal_length = nsteps/10 opts.T_init = 100 opts.use_hessian = False opts.seed = 1 opts.norm_step_size = 0.01 opts.likelihood_fn = b.likelihood opts.step_fn = step # Create the MCMC object mcmc = MCMC(opts) mcmc.initialize() mcmc.estimate() mcmc.prune(nsteps/10, 1) plt.ion() for i in range(mcmc.num_estimate): plt.figure() (heights, points, lines) = plt.hist(mcmc.positions[:,i], bins=100, normed=True) plt.plot(points, beta.pdf(points, b.a, b.b), 'r') return mcmc
def fit_gaussian(nsteps): num_dimensions = 1 # Create the dummy model means = 10 ** np.random.rand(num_dimensions) variances = np.random.rand(num_dimensions) g = GaussianFit(means, variances) # Create the options opts = MCMCOpts() opts.model = g opts.estimate_params = g.parameters opts.initial_values = [1] opts.nsteps = nsteps opts.anneal_length = nsteps/10 opts.T_init = 1 opts.use_hessian = False opts.seed = 1 opts.norm_step_size = 1 opts.likelihood_fn = g.likelihood opts.step_fn = step # Create the MCMC object mcmc = MCMC(opts) mcmc.initialize() mcmc.estimate() mcmc.prune(nsteps/10, 1) plt.ion() for i in range(mcmc.num_estimate): mean_i = np.mean(mcmc.positions[:,i]) var_i = np.var(mcmc.positions[:, i]) print "True mean: %f" % means[i] print "Sampled mean: %f" % mean_i print "True variance: %f" % variances[i] print "Sampled variance: %f" % var_i plt.figure() (heights, points, lines) = plt.hist(mcmc.positions[:,i], bins=50, normed=True) plt.plot(points, norm.pdf(points, loc=means[i], scale=np.sqrt(variances[i])), 'r') mean_err = np.zeros(len(mcmc.positions)) var_err = np.zeros(len(mcmc.positions)) for i in range(len(mcmc.positions)): mean_err[i] = (means[0] - np.mean(mcmc.positions[:i+1,0])) var_err[i] = (variances[0] - np.var(mcmc.positions[:i+1,0])) plt.figure() plt.plot(mean_err) plt.plot(var_err) return mcmc
def fit_twod_gaussians(nsteps): means_x = [ 0.1, 0.5, 0.9, 0.1, 0.5, 0.9, 0.1, 0.5, 0.9] means_y = [0.1, 0.1, 0.1, 0.5, 0.5, 0.5, 0.9, 0.9, 0.9] sd = 0.01 tdg = TwoDGaussianFit(means_x, means_y, sd ** 2) # Create the options opts = MCMCOpts() opts.model = tdg opts.estimate_params = tdg.parameters opts.initial_values = [1.001, 1.001] opts.nsteps = nsteps opts.anneal_length = nsteps/10 opts.T_init = 1 opts.use_hessian = False opts.seed = 1 opts.norm_step_size = 0.1 opts.likelihood_fn = tdg.likelihood opts.step_fn = step mcmc = MCMC(opts) mcmc.initialize() mcmc.estimate() plt.ion() fig = plt.figure() mcmc.prune(0, 20) plt.scatter(mcmc.positions[:,0], mcmc.positions[:,1]) ax = fig.gca() for x, y in zip(means_x, means_y): circ = plt.Circle((x, y), radius=2*sd, color='r', fill=False) ax.add_patch(circ) plt.xlim((-0.5, 1.5)) plt.ylim((-0.5, 1.5)) plt.show() return mcmc