def launch(sampler, prior, alpha, eps, ratio_min=1e-2, surveypath=None, pool=None, plotting=False): """ Launches pools Could become implemented in abc-pmc itself""" pools = [] for pool in sampler.sample(prior, eps, pool): eps_str = ", ".join(["{0:>.4f}".format(e) for e in pool.eps]) print("T: {0}, eps: [{1}], ratio: {2:>.4f}".format(pool.t, eps_str, pool.ratio)) for i, (mean, std) in enumerate(zip(*abcpmc.weighted_avg_and_std(pool.thetas, pool.ws, axis=0))): print(" theta[{0}]: {1:>.4f} +- {2:>.4f}".format(i, mean, std)) eps.eps = np.percentile(pool.dists, alpha, axis=0) # reduce eps value pools.append(pool) iom.pickleObject(pools, surveypath, 'launch_pools', append=False) """ Creates plots on the fly """ if plotting: plot_abctraces(pools, surveypath) if pool.ratio < ratio_min: print('Ended abc-pmc because ratio_min < %.3e' % (ratio_min)) break sampler.close() return pools
def test_weighted_avg_and_std(): values = np.random.normal(size=1000) weights = np.ones((1000)) avg, std = abcpmc.weighted_avg_and_std(values, weights) assert np.allclose(avg, np.average(values)) assert np.allclose(std, np.std(values))
def abc(pewl, name=None, niter=None, npart=None, restart=None): if restart is not None: # read pool theta_init = np.loadtxt( os.path.join(abc_dir, 'theta.t%i.dat' % restart)) rho_init = np.loadtxt( os.path.join(abc_dir, 'rho.t%i.dat' % restart)) w_init = np.loadtxt( os.path.join(abc_dir, 'w.t%i.dat' % restart)) init_pool = abcpmc.PoolSpec(restart, None, None, theta_init, rho_init, w_init) npart = len(theta_init) print('%i particles' % npart) else: init_pool = None #--- inference with ABC-PMC below --- # prior prior = abcpmc.TophatPrior(prior_min, prior_max) # sampler abcpmc_sampler = abcpmc.Sampler( N=npart, # N_particles Y=x_obs, # data postfn=_sumstat_model_wrap, # simulator dist=_distance_metric_wrap, # distance metric pool=pewl, postfn_kwargs={'dem': dem}#, dist_kwargs={'method': 'L2', 'phi_err': phi_err} ) # threshold eps = abcpmc.ConstEps(niter, eps0) print('eps0', eps.eps) for pool in abcpmc_sampler.sample(prior, eps, pool=init_pool): eps_str = ", ".join(["{0:>.4f}".format(e) for e in pool.eps]) print("T: {0}, eps: [{1}], ratio: {2:>.4f}".format(pool.t, eps_str, pool.ratio)) for i, (mean, std) in enumerate(zip(*abcpmc.weighted_avg_and_std(pool.thetas, pool.ws, axis=0))): print(u" theta[{0}]: {1:>.4f} \u00B1 {2:>.4f}".format(i, mean,std)) print('dist', pool.dists) # write out theta, weights, and distances to file dustInfer.writeABC('eps', pool, abc_dir=abc_dir) dustInfer.writeABC('theta', pool, abc_dir=abc_dir) dustInfer.writeABC('w', pool, abc_dir=abc_dir) dustInfer.writeABC('rho', pool, abc_dir=abc_dir) # update epsilon based on median thresholding eps.eps = np.median(pool.dists, axis=0) print('eps%i' % pool.t, eps.eps) print('----------------------------------------') #if pool.ratio <0.2: break abcpmc_sampler.close() return None
def launch(): eps = abcpmc.ConstEps(T, eps_start) pools = [] for pool in sampler.sample(prior, eps): print("T: {0}, eps: {1:>.4f}, ratio: {2:>.4f}".format(pool.t, eps(pool.eps), pool.ratio)) np.savetxt("theta"+str(pool.t)+".dat" , pool.thetas) np.savetxt("weights"+str(pool.t)+".dat" , pool.ws) for i, (mean, std) in enumerate(zip(*abcpmc.weighted_avg_and_std(pool.thetas, pool.ws, axis=0))): print(u" theta[{0}]: {1:>.4f} \u00B1 {2:>.4f}".format(i, mean,std)) eps.eps = np.percentile(pool.dists, alpha) # reduce eps value pools.append(pool) sampler.close() return pools
def launch(threads): eps = abcpmc.ConstEps(T, eps_start) pools = [] # pool is a namedtuple representing the values of one iteration for pool in sampler.sample(prior, eps): print("T: {0}, eps: {1:>.4f}, ratio: {2:>.4f}".format( pool.t, eps(pool.eps), pool.ratio)) for i, (mean, std) in enumerate( zip(*abcpmc.weighted_avg_and_std(pool.thetas, pool.ws, axis=0))): print(u" theta[{0}]: {1:>.4f} \u00B1 {2:>.4f}".format( i, mean, std)) # reduce the eps value to the alpha-th percentile of the sorted distances eps.eps = np.percentile(pool.dists, alpha) pools.append(pool) sampler.close() return pools
def postprocessing(pools): """Check the estimated value of theta1 and theta2 """ for i in range(len([theta1_fid, theta2_fid])): moments = np.array([ abcpmc.weighted_avg_and_std(pool.thetas[:, i], pool.ws, axis=0) for pool in pools ]) plt.errorbar(range(T), moments[:, 0], moments[:, 1], label='Theta %i' % i) plt.hlines([theta1_fid, theta2_fid], 0, T, linestyle='dotted', linewidth=0.7) plt.xlim([-.5, T]) plt.xlabel("Iteration") plt.ylabel("Value") plt.legend() plt.savefig( './Figures/multivariate_gaussian/estimated_thetas_vs_iterations.png') # plt.show() plt.close() """Check the distribution of distances for the posterior""" distances = np.array([pool.dists for pool in pools]).flatten() # If we are close to the true posterior, we expect to have a very high # bin count around the values we have found in the earlier distribution plot sns.distplot(distances, axlabel='distance') plt.savefig( './Figures/multivariate_gaussian/distance_distribution_posterior.png') # plt.show() plt.close() """Check the epsilon value as function of iteration """ eps_values = np.array([pool.eps for pool in pools]) plt.plot(eps_values, label=r'$\epsilon$ values') plt.xlabel('Iteration') plt.ylabel(r'$\epsilon$') plt.legend() plt.savefig('./Figures/multivariate_gaussian/epsilon_vs_iterations.png') # plt.show() plt.close() """ Check the acceptance ratio""" acc_ratios = np.array([pool.ratio for pool in pools]) plt.plot(acc_ratios, label='Acceptance ratio') plt.ylim([0, 1]) plt.xlabel('Iteration') plt.ylabel('Acceptance ratio') plt.legend() plt.savefig('./Figures/multivariate_gaussian/acceptance_vs_iterations.png') # plt.show() plt.close() """Plot the posterior, visualize with 'corner' package """ samples = np.vstack([pool.thetas for pool in pools]) fig = corner.corner(samples, truths=[theta1_fid, theta2_fid]) # plt.show() # theres probably a nicer package for this that rotates the Y plt.savefig('./Figures/multivariate_gaussian/posterior_all_iterations.png') plt.close() """ Plot the posterior, omitting the first iterations """ idx = -1 samples = pools[idx].thetas fig = corner.corner(samples, weights=pools[idx].ws, truths=[theta1_fid, theta2_fid]) for mean, std in zip( *abcpmc.weighted_avg_and_std(samples, pools[idx].ws, axis=0)): print(u"mean: {0:>.4f} \u00B1 {1:>.4f}".format(mean, std)) plt.savefig('./Figures/multivariate_gaussian/posterior.png') # plt.show() plt.close()