Package: approxposterior
approxposteriorA Python package for approximate Bayesian inference with computationally-expensive modelsOverviewapproxposterioris a Python package for efficient approximate Bayesian
inference and Bayesian optimization of computationally-expensive models.approxposteriortrains a Gaussian process (GP) surrogate for the computationally-expensive model
and employs an active learning approach to iteratively improve the GPs predictive
performance while minimizing the number of calls to the expensive model required
to generate the GP's training set.approxposteriorimplements both theBayesian Active Learning for Posterior Estimation (BAPE, Kandasamy et al. (2017))andAdaptive Gaussian process approximation for Bayesian inference with expensive likelihood functions (AGP, Wang & Li (2018))algorithms for estimating posterior probability distributions for use with inference problems with computationally-expensive models. In such situations,
the goal is to infer posterior probability distributions for model parameters, given some data, with the additional constraint of minimizing the number of forward model evaluations given the model's assumed large computational cost.approxposteriortrains a Gaussian Process (GP) surrogate model for the likelihood evaluation by modeling the covariances in logprobability (logprior + loglikelihood) space.approxposteriorthen uses this GP within an MCMC sampler for each likelihood evaluation to perform the inference.approxposterioriteratively improves the GP's predictive performance by leveraging the inherent uncertainty in the GP's predictions to identify high-likelihood regions in parameter space where the GP is uncertain.approxposteriorthen evaluates the forward model at these points to expand the training set in relevant regions of parameter space, re-training the GP to maximize its predictive ability while minimizing the size of the training set.  Check outthe BAPE paperby Kandasamy et al. (2017) andthe AGP paperby Wang & Li (2018) for in-depth descriptions of the respective algorithms.DocumentationCheck out the documentation athttps://dflemin3.github.io/approxposterior/for a more in-depth explanation about the code, detailed API notes, numerous examples with figures.InstallationUsing conda:condainstall-cconda-forgeapproxposteriorUsing pip:pipinstallapproxposteriorThis step can fail if george (the Python Gaussian Process package) is not properly installed and compiled.
To install george, runcondainstall-cconda-forgegeorgeFrom source:gitclonehttps://github.com/dflemin3/approxposterior.gitcdapproxposterior
pythonsetup.pyinstallA simple exampleBelow is a simple application ofapproxposteriorbased on the Wang & Li (2017) example.fromapproxposteriorimportapprox,gpUtils,likelihoodaslh,utilityasutimportnumpyasnp# Define algorithm parametersm0=50# Initial size of training setm=20# Number of new points to find each iterationnmax=2# Maximum number of iterationsbounds=[(-5,5),(-5,5)]# Prior boundsalgorithm="bape"# Use the Kandasamy et al. (2017) formalismseed=57# RNG seednp.random.seed(seed)# emcee MCMC parameterssamplerKwargs={"nwalkers":20}# emcee.EnsembleSampler parametersmcmcKwargs={"iterations":int(2.0e4)}# emcee.EnsembleSampler.run_mcmc parameters# Sample design points from priortheta=lh.rosenbrockSample(m0)# Evaluate forward model log likelihood + lnprior for each thetay=np.zeros(len(theta))foriiinrange(len(theta)):y[ii]=lh.rosenbrockLnlike(theta[ii])+lh.rosenbrockLnprior(theta[ii])# Default GP with an ExpSquaredKernelgp=gpUtils.defaultGP(theta,y,white_noise=-12)# Initialize object using the Wang & Li (2018) Rosenbrock function exampleap=approx.ApproxPosterior(theta=theta,y=y,gp=gp,lnprior=lh.rosenbrockLnprior,lnlike=lh.rosenbrockLnlike,priorSample=lh.rosenbrockSample,bounds=bounds,algorithm=algorithm)# Run!ap.run(m=m,nmax=nmax,estBurnin=True,nGPRestarts=3,mcmcKwargs=mcmcKwargs,cache=False,samplerKwargs=samplerKwargs,verbose=True,thinChains=False,onlyLastMCMC=True)# Check out the final posterior distribution!importcorner# Load in chain from last iterationsamples=ap.sampler.get_chain(discard=ap.iburns[-1],flat=True,thin=ap.ithins[-1])# Corner plot!fig=corner.corner(samples,quantiles=[0.16,0.5,0.84],show_titles=True,scale_hist=True,plot_contours=True)# Plot where forward model was evaluated - uncomment to plot!fig.axes[2].scatter(ap.theta[m0:,0],ap.theta[m0:,1],s=10,color="red",zorder=20)# Save figurefig.savefig("finalPosterior.png",bbox_inches="tight")The final distribution will look something like this:The red points were selected byapproxposteriorby maximizing the BAPE utility function.
At each red point,approxposteriorran the forward model to evaluate the true likelihood
and added this input-likelihood pair to the GP's training set. We retrain the GP each time
to improve its predictive ability. Note how the points are selected in regions of
high posterior density, precisely where we would want to maximize the GP's predictive ability! By using the
BAPE point selection scheme,approxposteriordoes not waste computational resources by
evaluating the forward model in low likelihood regions.Check out theexamplesdirectory for Jupyter Notebook examples and explanations. Check out the fulldocumentationfor a more in-depth explanation of classes, methods, variables, and how to use the code.ContributionIf you would like to contribute to this code, please feel free to fork the repository, make some edits, and open a pull request.
If you find a bug, have a suggestion, etc, please open up an issue!CitationIf you use this code, please cite the following:Fleming and VanderPlas (2018):@ARTICLE{Fleming2018,author={{Fleming},D.~P.and{VanderPlas},J.},title="{approxposterior: Approximate Posterior Distributions in Python}",journal={TheJournalofOpenSourceSoftware},year=2018,month=sep,volume=3,pages={781},doi={10.21105/joss.00781},adsurl={http://adsabs.harvard.edu/abs/2018JOSS....3..781P},adsnote={ProvidedbytheSAO/NASAAstrophysicsDataSystem}}Kandasamy et al. (2017):@article{Kandasamy2017,title="Query efficient posterior estimation in scientific experiments via Bayesian active learning",journal="Artificial Intelligence",volume="243",pages="45 - 56",year="2017",issn="0004-3702",doi="https://doi.org/10.1016/j.artint.2016.11.002",url="http://www.sciencedirect.com/science/article/pii/S0004370216301394",author="Kirthevasan Kandasamy and Jeff Schneider and Barnabás Póczos",keywords="Posterior estimation, Active learning, Gaussian processes"}Wang & Li (2018):@article{Wang2018,author={Wang,HongqiaoandLi,Jinglai},title={AdaptiveGaussianProcessApproximationforBayesianInferencewithExpensiveLikelihoodFunctions},journal={NeuralComputation},volume={30},number={11},pages={3072-3094},year={2018},doi={10.1162/neco\_a\_01127},URL={https://doi.org/10.1162/neco_a_01127},eprint={https://doi.org/10.1162/neco_a_01127}}