Pangolin's goal is to be the world's friendliest probabilistic programming language and to make probabilistic inference fun. It is still something of an early-stage research project.
See INSTALL.md
See CHANGELOG.md
See justindomke.github.io/pangolin.
Simple "probabilistic calculator":
from pangolin import interface as pi
from pangolin.blackjax import E
x = pi.normal(0,2) # x ~ normal(0,2)
y = pi.normal(x,6) # y ~ normal(x,6)
print(E(x,y,-2.0)) # E[x|y=-2] (close to -0.2)Bayesian inference on the 8-schools model:
import pangolin as pg
from pangolin import interface as pi
# data for 8 schools model
num_schools = 8
observed = [28, 8, -3, 7, -1, 1, 18, 12]
stddevs = [15, 10, 16, 11, 9, 11, 10, 18]
# define model
mu = pi.normal(0,10) # μ ~ normal(0,10)
tau = pi.exp(pi.normal(5,1)) # τ ~ lognormal(5,1)
theta = [pi.normal(mu,tau) for i in range(num_schools)] # θ[i] ~ normal(μ,τ)
y = [pi.normal(theta[i],stddevs[i]) for i in range(num_schools)] # y[i] ~ normal(θ[i],stddevs[i])
# do inference / sample from p(theta | y=observed)
theta_samps = pg.blackjax.sample(theta, y, observed, niter=10000)
# plot results (no pangolin here!)
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
sns.swarmplot(np.array(theta_samps)[:,::50].T,s=2,zorder=0)
plt.xlabel('school')
plt.ylabel('treatment effect')If you're in the market for a PPL, you might want to compare the above to the same (or close) model implemented in other PPLs:
| PPL | Comment |
|---|---|
| Pyro | Requires "sample" statements, passing variable names as strings, and uses slightly mysterious plate construct. |
| NumPyro | Requires "sample" statements, passing variable names as strings, and uses slightly mysterious plate construct. |
| PyMC | Pretty good, though requires creating a "model" function and passing variables names as strings. |
| JAGS | Pretty good, both simple and explicit. We had this in 1991! Requires using a separate language. |
| Stan | Looks very simple, but uses somewhat subtle batching semantics. Could be written similarly to the JAGS model, just with mandatory declarations of all types/shapes. Requires a separate language. |
| Tensorflow probability | Legend has it that some find this a wee bit complicated. |
For more examples, take a look at the demos. Here's a recommended order:
- IR.ipynb demonstrates pangolin's internal representation of probabilistic models.
- interface.ipynb demonstrates pangolin's friendly interface and what internal representation it produces.
- 8schools.ipynb is the classic 8-schools model.
- regression.ipynb is Bayesian linear regression.
- timeseries.ipynb is a simple timeseries model.
- autoregressive.ipynb is an autoregressive Kalman-filter-esque model.
- GP-regression.ipynb is Gaussian Process regression.
(For the current Python interface)
- Gradual enhancement. Easy things should be really easy. More complex features should be easily discoverable. Steep learning curves should be avoided.
- Small API surface. The set of abstractions the user needs to learn should be as small as possible.
- Graceful interop. As much as possible, the system should feel lke a natural part of the broader Python NumPy ecosystem, rather than a "new language".
- Look like math. As much as possible, calculations should resemble mathematical notation. Exceptions are allowed when algorithmic limitations make this impossible or where standard mathematical notation is ambiguous or bad.
Long-term, Pangolin has the following goals:
- To "decouple" probabilistic models from inference algorithms. It should be possible to write a model once, and then perform inference using many inference "backends". (Among other things, this should facilitate benchmarks)
- To make it easier to experiment with novel inference algorithms that inspect the target distribution.
- To support different possible interfaces, in different languages.
- To be "unopinionated" about how users might specify models, and how inference might be done.
An earlier version of Pangolin is available and based on much the same ideas, except only supporting JAGS as a backend. It can be found with documentation, in the
pangolin-jags directory.

