Functional Posterior Plotter
These packages allows one to compute a predictive posterior of a function, dependent on sampled parameters. We assume one has a Bayesian posterior Post(theta|D,M) described by a set of posterior samples {theta_i}~Post. If there is a function parameterised by theta f(x;theta), then this script will produce a contour plot of the conditional posterior P(f|x,D,M) in the (x,f) plane.
The driving routine is fgivenx.compute_contours, and example usage can be found by running help(fgivenx).
import numpy
import matplotlib.pyplot
import fgivenx
import fgivenx.plot
# Model definitions
# =================
# Define a simple straight line function, parameters theta=(m,c)
def f(x, theta):
m, c = theta
return m * x + c
numpy.random.seed(1)
# Posterior samples
nsamples = 1000
ms = numpy.random.normal(loc=-5,scale=1,size=nsamples)
cs = numpy.random.normal(loc=2,scale=1,size=nsamples)
samples = numpy.array([(m,c) for m,c in zip(ms,cs)]).copy()
# Prior samples
ms = numpy.random.normal(loc=0,scale=5,size=nsamples)
cs = numpy.random.normal(loc=0,scale=5,size=nsamples)
prior_samples = numpy.array([(m,c) for m,c in zip(ms,cs)]).copy()
# Computation
# ===========
# Examine the function over a range of x's
xmin, xmax = -2, 2
nx = 100
x = numpy.linspace(xmin, xmax, nx)
# Set the cache
cache = 'cache/test'
# Compute the dkls
x, dkls = fgivenx.compute_kullback_liebler(f, x, samples, prior_samples, cache=cache)
# Compute the contours
x, y, z = fgivenx.compute_contours(f, x, samples, cache=cache)
_, y_prior, z_prior = fgivenx.compute_contours(f, x, prior_samples, cache=cache+'_prior')
x, fsamps = fgivenx.compute_samples(f, x, samples, cache=cache)
x, prior_fsamps = fgivenx.compute_samples(f, x, prior_samples, cache=cache+'_prior')
# Plotting
# ========
fig, axes = matplotlib.pyplot.subplots(2,2)
# Sample plot
ax = axes[0,0]
ax.plot(prior_samples.T[0],prior_samples.T[1],'b.')
ax.plot(samples.T[0],samples.T[1],'r.')
ax.set_xlabel('$m$')
ax.set_ylabel('$c$')
# Line plot
ax = axes[0,1]
ax.set_xticklabels([])
ax.set_ylabel(r'$y = m x + c$')
fgivenx.plot.plot_lines(x, prior_fsamps, ax, color='b')
fgivenx.plot.plot_lines(x, fsamps, ax, color='r')
# Predictive posterior plot
ax = axes[1,1]
cbar = fgivenx.plot.plot(x, y_prior, z_prior, ax, colors=matplotlib.pyplot.cm.Blues_r,linewidths=0)
cbar = fgivenx.plot.plot(x, y, z, ax)
ax.set_ylabel(r'$P(y|x)$')
ax.set_xlabel('$x$')
# DKL plot
ax = axes[1,0]
ax.plot(x, dkls)
ax.set_ylim(bottom=0)
ax.set_xlabel('$x$')
ax.set_ylabel('$D_{KL}$')
axes[0,0].get_shared_x_axes().join(axes[0,0], axes[1,0], axes[1,1])
fig.tight_layout()
fig.savefig('plot.pdf')
Metadata
Release files for fgivenx 1.0.15
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fgivenx-1.0.15.tar.gz | 9.5 kB | Without provenance | Details for fgivenx-1.0.15.tar.gz |
Release files / fgivenx-1.0.15.tar.gz
| Download URL | fgivenx-1.0.15.tar.gz |
|---|---|
| Size | 9.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7e7e23223c8d7a5485ea6d629c67900cc074dc12418acb3be761a2656812eb84
|
|
BLAKE2b-256 checksum How to use checksums |
140e54063591ab3568b4ddc32664b40c2596badd333a93fbc9a900e28d775f71
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for fgivenx 1.0.15
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.