def fit_beta_by_pt(nsteps): """Fit a beta distribution by parallel tempering.""" 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 = [10 ** 0.5] opts.nsteps = nsteps opts.anneal_length = 0 opts.T_init = 1 opts.use_hessian = False opts.seed = 1 opts.norm_step_size = 0.5 opts.likelihood_fn = b.likelihood opts.step_fn = step # Create the MCMC object num_temps = 8 pt = PT_MCMC(opts, num_temps, 10) pt.estimate() plt.ion() for chain in pt.chains: fig = plt.figure() chain.prune(nsteps/10, 1) (heights, points, lines) = plt.hist(chain.positions, bins=100, normed=True) plt.plot(points, beta.pdf(points, b.a, b.b), 'r') plt.ylim((0,10)) plt.xlim((0, 1)) return pt
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_by_pt(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 = 0 # necessary so cooling does not occur 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 # Create the PT object num_temps = 8 pt = PT_MCMC(opts, num_temps, 100) pt.estimate() plt.ion() for chain in pt.chains: fig = plt.figure() chain.prune(nsteps/10, 1) plt.scatter(chain.positions[:,0], chain.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.title('Temp = %.2f' % chain.options.T_init) plt.show() return pt
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
# Create temperature array based on number of workers (excluding master) temps = np.logspace(np.log10(min_temp), np.log10(max_temp), num_chains-1) # 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 = 0 # necessary so cooling does not occur opts.T_init = temps[rank - 1] # Use the temperature for this worker opts.use_hessian = False opts.seed = 1 opts.norm_step_size = 0.1 opts.likelihood_fn = tdg.likelihood mcmc = MCMC(opts) mcmc.initialize() # The master coordinates when swaps occur --------- if rank == 0: pt = PT_MPI_Master(comm, rank, opts, swap_period, num_chains) pt.run() # Everyone else runs MCMC steps and swaps when told ----------- else: pt = PT_MPI_Worker(comm, rank, mcmc, swap_period) pt.run()
sd = 0.01 tdg = TwoDGaussianFit(means_x, means_y, sd**2) # Create temperature array based on number of workers (excluding master) temps = np.logspace(np.log10(min_temp), np.log10(max_temp), num_chains - 1) # 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 = 0 # necessary so cooling does not occur opts.T_init = temps[rank - 1] # Use the temperature for this worker opts.use_hessian = False opts.seed = 1 opts.norm_step_size = 0.1 opts.likelihood_fn = tdg.likelihood mcmc = MCMC(opts) mcmc.initialize() # The master coordinates when swaps occur --------- if rank == 0: pt = PT_MPI_Master(comm, rank, opts, swap_period, num_chains) pt.run() # Everyone else runs MCMC steps and swaps when told ----------- else: pt = PT_MPI_Worker(comm, rank, mcmc, swap_period) pt.run()