def sample_bimodal_gauss(mcmc_algo, comm): mu_gold_a, std_dev_gold_a = -8.0, 1.0 mu_gold_b, std_dev_gold_b = 10.0, 1.0 def log_like_fn(theta, data=None): return np.log( (1 / 6.) * stats.norm.pdf(theta[0], loc=mu_gold_a, scale=std_dev_gold_a) + (5 / 6.) * stats.norm.pdf(theta[0], loc=mu_gold_b, scale=std_dev_gold_b)) \ - log_prior(theta) def log_prior(theta): if (-100 < theta[0] < 100): return 0 else: return -np.inf if comm.rank == 0: print("========== SAMPLE BIMODAL GAUSSI ===========") theta_0 = np.array([1.0]) n_chains = comm.size*6 my_mcmc = DreamMpi(log_like_fn, theta_0, n_chains=n_chains, varepsilon=1e-7, mpi_comm=comm, burnin_gen=0) my_mcmc.run_mcmc(1000 * n_chains) # my_mcmc = DeMcMpi(log_like_fn, theta_0, n_chains=comm.size*n_chains, varepsilon=1e-7, mpi_comm=comm, burnin_gen=0) #my_mcmc = DeMc(log_like_fn, n_chains=comm.size*n_chains, inflate=1e1, mpi_comm=comm, burnin_gen=0) #my_mcmc.run_mcmc(5000 * n_chains, theta_0) # view results theta_est, sig_est, chain = my_mcmc.param_est(n_burn=1000) theta_est_, sig_est_, full_chain = my_mcmc.param_est(n_burn=0) if comm.rank == 0: print("Esimated mu: %s" % str(theta_est)) print("Estimated sigma: %s " % str(sig_est)) print("Acceptance fraction: %f" % my_mcmc.acceptance_fraction) print("Expected Acceptance fraction: %f" % (0.36)) sys.stdout.flush() # vis the parameter estimates mc_plot.plot_mcmc_params(chain, ["$\mu_b$"], savefig='gauss_bi_mu_mcmc_ex.png', truths=[(1/6.)*(-8.)+(5/6.)*(10.)]) # vis the full chain mc_plot.plot_mcmc_indep_chains(full_chain, n_chains, ["$\mu_b$"], savefig='gauss_bi_mu_chain_ex.png', truths=[(1/6.)*(-8.)+(5/6.)*(10.)], scatter=True) else: pass
def sample_gauss(mcmc_algo, comm): """! @brief Sample from a gaussian distribution """ mu_gold, std_dev_gold = 5.0, 0.5 def log_like_fn(theta, data=None): return np.log(stats.norm.pdf(theta[0], loc=mu_gold, scale=std_dev_gold)) - log_prior(theta) def log_prior(theta): if -100 < theta[0] < 100: return 0 else: return -np.inf if comm.rank == 0: print("========== SAMPLE GAUSSI ===========") theta_0 = np.array([1.0]) n_chains = comm.size*6 my_mcmc = DreamMpi(log_like_fn, theta_0, n_chains=n_chains, mpi_comm=comm) my_mcmc.run_mcmc(4000) # view results theta_est, sig_est, chain = my_mcmc.param_est(n_burn=1000) theta_est_, sig_est_, full_chain = my_mcmc.param_est(n_burn=0) if comm.rank == 0: print("Esimated mu: %s" % str(theta_est)) print("Estimated sigma: %s " % str(sig_est)) print("Acceptance fraction: %f" % my_mcmc.acceptance_fraction) sys.stdout.flush() # vis the parameter estimates mc_plot.plot_mcmc_params(chain, ["$\mu$"], savefig='gauss_mu_mcmc_ex.png', truths=[5.0]) # vis the full chain mc_plot.plot_mcmc_indep_chains(full_chain, n_chains, ["$\mu$"], savefig='gauss_mu_chain_ex.png', truths=[5.0], scatter=True) else: pass
def fit_line(mcmc_algo, comm): """! @brief Example data from http://dfm.io/emcee/current/user/line/ For example/testing only. """ # Choose the "true" parameters. m_true = -0.9594 b_true = 4.294 f_true = 0.534 # Generate some synthetic data from the model. N = 50 x = np.sort(10 * np.random.rand(N)) yerr = 0.1 + 0.5 * np.random.rand(N) y = m_true * x + b_true y += np.abs(f_true * y) * np.random.randn(N) y += yerr * np.random.randn(N) # from http://dfm.io/emcee/current/user/line/ def lnlike(theta, x, y, yerr): m, b, lnf = theta model = m * x + b inv_sigma2 = 1.0/(yerr**2 + model**2*np.exp(2*lnf)) return -0.5*(np.sum((y-model)**2*inv_sigma2 - np.log(inv_sigma2))) def lnprior(theta): m, b, lnf = theta if -5.0 < m < 0.5 and 0.0 < b < 10.0 and -10.0 < lnf < 1.0: return 0.0 return -np.inf def lnprob(theta, x, y, yerr): lp = lnprior(theta) if not np.isfinite(lp): return -np.inf return lp + lnlike(theta, x, y, yerr) # custom prior (ignore the unknown var term) def log_prior(theta): if (-50 < theta[0] < 50) and (-50 < theta[1] < 50): return 0. else: return -np.inf def model_fn(theta): return theta[0] + theta[1] * x def log_like_fn(theta, data): sigma = 1.0 log_like = -0.5 * (np.sum((data - model_fn(theta)) ** 2 / sigma \ - np.log(1./sigma)) + log_prior(theta)) return log_like # === EXAMPLE 1 === if comm.rank == 0: print("========== FIT LIN MODEL 1 ===========") theta_0 = np.array([4.0, -0.5]) n_chains = comm.size*6 my_mcmc = DreamMpi(log_like_fn, theta_0, n_chains=n_chains, mpi_comm=comm, inflate=1e1, ln_kwargs={'data': y}) my_mcmc.run_mcmc(500 * 100) # view results theta_est, sig_est, chain = my_mcmc.param_est(n_burn=10000) theta_est_, sig_est_, full_chain = my_mcmc.param_est(n_burn=0) if comm.rank == 0: print("Esimated params: %s" % str(theta_est)) print("Estimated params sigma: %s " % str(sig_est)) print("Acceptance fraction: %f" % my_mcmc.acceptance_fraction) # vis the parameter estimates mc_plot.plot_mcmc_params(chain, labels=["$y_0$", "m"], savefig='line_mcmc_ex.png', truths=[4.294, -0.9594]) # vis the full chain mc_plot.plot_mcmc_indep_chains(full_chain, n_chains, labels=["$y_0$", "m"], savefig='lin_chain_ex.png', truths=[4.294, -0.9594], scatter=True) # === EXAMPLE 2 === comm.Barrier() if comm.rank == 0: print("========== FIT LIN MODEL 2 ===========") theta_0 = np.array([-0.8, 4.5, 0.2]) n_chains = comm.size*6 my_mcmc = DreamMpi(lnprob, theta_0, n_chains=n_chains, mpi_comm=comm, ln_kwargs={'x': x, 'y': y, 'yerr': yerr}, inflate=1e1) my_mcmc.run_mcmc(500 * 100) theta_est, sig_est, chain = my_mcmc.param_est(n_burn=10000) theta_est_, sig_est_, full_chain = my_mcmc.param_est(n_burn=0) if comm.rank == 0: print("Esimated params: %s" % str(theta_est)) print("Estimated params sigma: %s " % str(sig_est)) print("Acceptance fraction: %f" % my_mcmc.acceptance_fraction) # vis the parameter estimates mc_plot.plot_mcmc_params(chain, labels=["m", "$y_0$", "$\mathrm{ln}(f)$"], savefig='line_mcmc_ex_2.png', truths=[-0.9594, 4.294, np.log(f_true)]) # vis the full chain mc_plot.plot_mcmc_indep_chains(full_chain, n_chains, labels=["m", "$y_0$", "$\mathrm{ln}(f)$"], savefig='lin_chain_ex_2.png', truths=[-0.9594, 4.294, np.log(f_true)], scatter=True)
def fit_exp_data(theta_0, mcmc_algo="DE-MC"): """! @brief Fit an exponential model to some data """ # get data t_data, y_data = read_data() sigma_0 = 1e-4 theta_0 = np.array(list(theta_0) + [sigma_0]) varepsilon = np.asarray([1e-2, 1e-2, 1e-3, 1e-8, 1e-9]) * 1e-2 # Run MCMC n_chains = 6 my_mcmc = DreamMpi(lnprob, theta_0, n_chains=n_chains, mpi_comm=comm, n_cr_gen=200, burnin_gen=4000, varepsilon=varepsilon, ln_kwargs={ 'y_data': y_data, 't': t_data }) my_mcmc.run_mcmc(1000 * 100, suffle=True, flip=0.5) # Run Emcee MCMC if comm.rank == 0: ndim, nwalkers = 5, 100 pos = [theta_0 + 1e-6 * np.random.randn(ndim) for i in range(nwalkers)] sampler = EnsembleSampler(nwalkers, ndim, lnprob, args=(t_data, y_data)) sampler.run_mcmc(pos, 1000) # 100 * 1000 tot samples samples = sampler.chain[:, 400:, :].reshape((-1, ndim)) fig = corner.corner( samples, labels=["$\tau$", "$c_\infty$", "$c_0$", "l", r"$\sigma$"]) fig.savefig("exp_emcee_out.png") print("=== EMCEE ===") print("Emcee mean acceptance fraction: {0:.3f}".format( np.mean(sampler.acceptance_fraction))) # view results print("=== Opti values by Bipymc MCMC ===") print("[tau, c_inf, c_0, leakage]:") theta_est, sig_est, chain = my_mcmc.param_est(n_burn=400 * 100) theta_est_, sig_est_, full_chain = my_mcmc.param_est(n_burn=0) if comm.rank == 0: print("MCMC Esimated params: %s" % str(theta_est)) print("MCMC Estimated params sigma: %s " % str(sig_est)) print("Acceptance fraction: %f" % my_mcmc.acceptance_fraction) print("P_cr: %s" % str(my_mcmc.p_cr)) # vis the parameter estimates mc_plot.plot_mcmc_params( chain, labels=[r"$\tau$", "$c_\infty$", "$c_0$", "leak", r"$\sigma$"], savefig='exp_mcmc_out.png', ) # vis the full chain mc_plot.plot_mcmc_indep_chains( full_chain, n_chains, labels=[r"$\tau$", "$c_\infty$", "$c_0$", "leak", r"$\sigma$"], savefig='exp_chain_out.png', ) # plot trajectories xdata, ydata = read_data() plt.close() i = 0 reduced_model = lambda t, tau, c_inf, c_0, set_leak, sigma: exp_c1_model_full( tau, c_inf, c_0, set_leak, t) for sample in chain: i += 1 plt.plot(xdata, np.abs(reduced_model(xdata, *sample) - sample[1]) / sample[1], lw=0.2, alpha=0.02, c='b') if i > 1000: break plt.title("MCMC Fit") plt.plot(xdata, np.abs(reduced_model(xdata, *theta_est) - theta_est[1]) / theta_est[1], lw=1.0, alpha=0.8, label="MCMC", c='b') plt.scatter(xdata, np.abs(ydata - theta_est[1]) / theta_est[1], s=2, alpha=0.9, c='r', label="data") plt.ylabel("$|C_t - c_\infty| /C_\infty$") plt.xlabel("time") plt.legend() plt.savefig("mcmc_trajectories.png", dpi=160) # plot the slopes at the end pass # compute and plot c_o/c_inf c_inf_samples = chain[:, 1] c_0_samples = chain[:, 2] c_ratio = c_0_samples / c_inf_samples c_ratio_avg, c_ratio_sd = np.mean(c_ratio), np.std(c_ratio) print("c_0/c_inf estimate: %0.3e +/- %0.3e" % (c_ratio_avg, c_ratio_sd))
def test_samplers(self): """ Test ability of each mcmc method to draw samples from the 2d bimodal_gauss distribution. """ n_samples = 100000 n_burn = 40000 for sampler_name, (my_mcmc, n_chains) in iteritems(self.sampler_dict): t0 = time.time() try: my_mcmc.run_mcmc(n_samples) except: my_mcmc.run_mcmc(n_samples, [0.0, 0.0]) t1 = time.time() theta_est, sig_est, chain = my_mcmc.param_est(n_burn=n_burn) theta_est_, sig_est_, full_chain = my_mcmc.param_est(n_burn=0) if self.comm.rank == 0: print("=== " + str(sampler_name) + " ===") print("Sampler walltime: %d (s)" % int(t1 - t0)) print("Esimated params: %s" % str(theta_est)) print("Estimated params sigma: %s " % str(sig_est)) print("Acceptance fraction: %f" % my_mcmc.acceptance_fraction) try: print("P_cr: %s" % str(my_mcmc.p_cr)) except: pass y1, y2 = chain[:, 0], chain[:, 1] if sampler_name == 'demc' or sampler_name == 'dream': self.assertAlmostEqual(1.5, theta_est[0], delta=0.1) self.assertAlmostEqual(1.5, theta_est[1], delta=0.1) elif sampler_name == 'dram': self.assertNotAlmostEqual(1.5, theta_est[0], delta=0.1) self.assertNotAlmostEqual(1.5, theta_est[1], delta=0.1) else: pass # plot mcmc samples plt.figure() plt.scatter(y1, y2, s=2, alpha=0.10) plt.grid(ls='--', alpha=0.5) plt.xlim(-2, 4) plt.ylim(-2, 4) plt.axvline(theta_est[0], -10, 10, c='r') plt.axhline(theta_est[1], -10, 10, c='r') plt.xlabel("x1") plt.ylabel("x2") plt.title(sampler_name + " " + r"$ \mu=(%0.2f,%0.2f)$" % (theta_est[0], theta_est[1])) plt.savefig(str(sampler_name) + "_bimodal_gauss_sample.png") plt.close() # plot mcmc chains mc_plot.plot_mcmc_indep_chains(full_chain, n_chains, labels=["x1", "x2"], savefig=str(sampler_name) + "_chains.png", scatter=True, nburn=int(n_burn))