dflemin3 / approxposterior

A Python package for approximate Bayesian inference and optimization using Gaussian processes
https://dflemin3.github.io/approxposterior/
MIT License
41 stars 9 forks source link

Adding basic functionality for object-oriented priors #69

Closed jlustigy closed 4 years ago

jlustigy commented 4 years ago

This pull request adds a new file priors.py to approxposterior. It defines the Prior base class, which is subclassed to create the UniformPrior and GaussianPrior classes (demonstrated below). Other priors can easily be implemented following these as examples. Note that other than importing this code in the __init__.py, object-oriented priors have not yet been implemented into approxposterior.

As is, the code should be able to reproduce the following example (also included in the doc-strings of this module):

from approxposterior.priors import UniformPrior, GaussianPrior

# Number of numerical samples
N=10000

# Define prior distributions
u = UniformPrior(0.0, 100.0)
g = GaussianPrior(50.0, 10.0)

# Plot histograms
plt.hist(u.random_sample(N), bins = 100, density=True, alpha = 0.5, color="C0");
plt.hist(g.random_sample(N), bins = 100, density=True, alpha = 0.5, color="C1");

# Plot the analytic density
xmin, xmax = plt.xlim();
x = np.linspace(xmin, xmax, 1000);
plt.plot(x, u.dist.pdf(x), c="C0", lw = 3.0, label="uniform")
plt.plot(x, g.dist.pdf(x), c="C1", lw = 3.0, label="normal")

# Tweak plot style
plt.xlabel(r"$x$")
plt.ylabel(r"Prior Probability Density, $\mathcal{P}(x)$")
plt.legend(framealpha=0.0)

priors-1