PyMC

5.28.4 · active · verified Sat Apr 11

PyMC (formerly PyMC3) is a Python package for Bayesian statistical modeling and probabilistic machine learning. It focuses on advanced Markov chain Monte Carlo (MCMC) and variational inference (VI) algorithms, offering flexibility and extensibility for a wide range of problems. It is currently at version 5.28.4 and maintains a frequent release cadence, with minor updates and bug fixes released regularly.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a simple Bayesian linear regression model, perform Markov chain Monte Carlo (MCMC) sampling using `pm.sample()`, and analyze the posterior results with ArviZ.

import numpy as np
import pymc as pm
import arviz as az

# 1. Simulate some data
np.random.seed(42)
size = 100
alpha_true, beta_true = 1, [1, 2.5]
sigma_true = 1

X1 = np.random.randn(size)
X2 = np.random.randn(size) * 0.2
Y = alpha_true + beta_true[0] * X1 + beta_true[1] * X2 + np.random.normal(size=size) * sigma_true

# 2. Define the PyMC model
with pm.Model() as linear_model:
    # Priors for unknown model parameters
    alpha = pm.Normal('alpha', mu=0, sigma=10)
    beta = pm.Normal('beta', mu=0, sigma=10, shape=2)
    sigma = pm.HalfNormal('sigma', sigma=1)

    # Expected value of outcome
    mu = alpha + beta[0] * X1 + beta[1] * X2

    # Likelihood (sampling distribution) of observations
    Y_obs = pm.Normal('Y_obs', mu=mu, sigma=sigma, observed=Y)

    # 3. Perform MCMC sampling
    idata = pm.sample(draws=1000, tune=1000, cores=2, random_seed=42)

# 4. Analyze results (e.g., print summary)
print(az.summary(idata, var_names=['alpha', 'beta', 'sigma']))

# To visualize, you would typically use:
# import matplotlib.pyplot as plt
# az.plot_trace(idata)
# plt.show()

view raw JSON →