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 __init__(self, opts, num_chains, max_temp, min_temp=1, swap_period=20): self.options = opts self.max_temp = max_temp self.min_temp = min_temp self.swap_period = swap_period self.iter = 0 """Current step iteration (runs to ``nsteps``).""" self.chains = [] """The set of chains in the temperature series.""" # Calculate the temperature series temps = np.logspace(np.log10(min_temp), np.log10(max_temp), num_chains) # Initialize each of the chains for i, temp in enumerate(temps): chain = MCMC(opts) chain.options.T_init = temp chain.initialize() chain.iter = chain.start_iter self.chains.append(chain) # Initialize arrays for storing swap info num_swaps = self.options.nsteps / swap_period self.swap_proposals = np.zeros((num_swaps, 2)) """Each swap proposal is stored as [i, j] row in this array.""" self.pi_xi = np.zeros(num_swaps) """The posterior of chain i at the position of i.""" self.pi_xj = np.zeros(num_swaps) """The posterior of chain i at the position of j.""" self.pj_xi = np.zeros(num_swaps) """The posterior of chain j at the position of i.""" self.pj_xj = np.zeros(num_swaps) """The posterior of chain j at the position of j.""" self.delta_test_posteriors = np.zeros(num_swaps) """The posterior probability ratio for swap/noswap.""" self.swap_alphas = np.zeros(num_swaps) """The random number used to accept/reject the swap.""" self.swap_accepts = np.zeros(num_swaps, dtype=bool) """Booleans indicating accepted swaps.""" self.swap_rejects = np.zeros(num_swaps, dtype=bool) """Booleans indicating rejected swaps.""" self.swap_iter = 0 """Current swap iteration (runs to ``nsteps / swap_period``)"""
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
# 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()