PyMC3-like abstractions for pyro's stochastic function.
Define a model as a stochastic function in pyro.
Use pm_like wrapper to create a PyMC3-esque Model.
Random variables are exposed to user as attributes of Model.
pm-pyro provides abstractions for inference (NUTS : No-U-Turn Sampler), trace plots, posterior plot and posterior predictive plots.
Install
Install from pypi
pip install pm-pyro
Developer setup
# install requirements
pip install -r requirements-dev.txt
# run tests
python -m pytest pmpyro/tests.py
Example
Borrowed this example from a PyMC3 tutorial. Outcome variables Y is dependent on 2 features X_1 and X_2. The notebook for this example is available here
Model Specification
We design a simple Bayesian Linear Regression model.
Stochastic Function
The model specification is implemented as a stochastic function.
import pyro.distributions as dist
import pyro
import torch
def pyro_model(x1, x2, y):
alpha = pyro.sample('alpha', dist.Normal(0, 10))
beta = pyro.sample('beta',pdist.Normal(torch.zeros(2,), torch.ones(2,) * 10.))
sigma = pyro.sample('sigma', dist.HalfNormal(1.))
# Expected value of outcome
mu = alpha + beta[0] * x1 + beta[1] * x2
# Likelihood (sampling distribution) of observations
return pyro.sample('y_obs', dist.Normal(mu, sigma), obs=y)
Context-manager Syntax
The pm_like wrapper creates a PyMC3-esque Model.
We can use the context manager syntax for running inference.
pm.sample samples from the model using the NUTS sampler.
The trace is a python dictionary which contains the samples.
from pmpyro import pm_like
import pmpyro as pm
with pm_like(pyro_model, X1, X2, Y) as model:
trace = pm.sample(1000)
sample: 100%|██████████| 1300/1300 [00:16, 80.42it/s, step size=7.49e-01, acc. prob=0.911]
Traceplot
We can visualize the samples using traceplot.
Select random variables by passing them as a list via var_names = [ 'alpha' ... ] argument.
pm.traceplot(trace)
Plot Posterior
Visualize posterior of random variables using plot_posterior.
pm.plot_posterior(trace, var_names=['beta'])
Posterior Predictive Samples
We can sample from the posterior by running plot_posterior_predictive or sample_posterior_predictive with the same
function signatures as the stochastic function def pyro_model(x1, x2, y), replacing observed variable Y with None.
ppc = pm.plot_posterior_predictive(X1, X2, None,
trace=trace, model=model, samples=60,
alpha=0.08, obs={'y_obs' : Y})
Trace Summary
The summary of random variables is available as a pandas array.
pm.summary()
License
This project is licensed under the GPL v3 License - see the LICENSE.md file for details
Metadata
Release files for pm-pyro 0.3.3
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 |
|---|---|---|---|---|
| pm-pyro-0.3.3.tar.gz | 8.9 kB | Without provenance | Details for pm-pyro-0.3.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pm_pyro-0.3.3-py3-none-any.whl | Python 3 | none | any | Details for pm_pyro-0.3.3-py3-none-any.whl |
Total release size: 32.0 kB
Release files / pm-pyro-0.3.3.tar.gz
| Download URL | pm-pyro-0.3.3.tar.gz |
|---|---|
| Size | 8.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0b9841d7363c1805a7794a20ca342ce3bf67f13d56beb7747ea0ff0c245f1297
|
|
BLAKE2b-256 checksum How to use checksums |
8f8807fc239ccbbabe5cc79180ed6e4da77227c70fc9de163a661e61dee3467b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.5
|
Release files / pm_pyro-0.3.3-py3-none-any.whl
| Download URL | pm_pyro-0.3.3-py3-none-any.whl |
|---|---|
| Size | 23.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c7c6475fbc62b486c2dca5fde9c1369546ab97601433663291fd642f916226ae
|
|
BLAKE2b-256 checksum How to use checksums |
207d8c35154eeecd3c05079acdfe94ac3fea0089734ff8f56819027de0c27ad7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.5
|
Security for pm-pyro 0.3.3
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.