PyTensor Distributions

raw JSON →
0.1.3 verified Fri May 01 auth: no python

PyTensor-powered probability distributions for probabilistic programming and Bayesian inference. Version 0.1.3, released 2025, maintenance level with monthly patches.

pip install pytensor-distributions
error ImportError: cannot import name 'Normal' from 'pytensor_distributions'
cause Trying to import distribution class directly from top-level package.
fix
Use 'from pytensor_distributions.distributions import Normal' instead.
error TypeError: 'Normal' object is not callable
cause Attempting to instantiate distribution with constructor call like Normal(mu, sigma) instead of Normal.dist(mu, sigma).
fix
Use Normal.dist(mu, sigma) to create a distribution instance.
error NotImplementedError: Sampling not implemented
cause Calling .sample() on a distribution instance.
fix
Do not use .sample(); use external sampling libraries like PyMC for random draws.
breaking The distribution constructors use `.dist()` class method instead of direct instantiation (e.g., `Normal.dist(mu, sigma)` not `Normal(mu, sigma)`). Direct instantiation raises a TypeError.
fix Always call `.dist()` on distribution classes to create distribution instances.
gotcha Random sampling is not supported yet. Calling `dist.sample()` raises NotImplementedError. Samples must be drawn using external sampling methods (e.g., MCMC via PyMC).
fix Use PyMC or other sampling libraries; do not rely on pytensor-distributions for random draws.
deprecated Support for Python 3.10 and below is dropped. Requires Python >=3.11.
fix Upgrade to Python 3.11 or later.

Define a Normal distribution and compute log-probability.

import pytensor
import pytensor.tensor as pt
from pytensor_distributions.distributions import Normal

x = pt.dmatrix('x')
mu = pt.dvector('mu')
sigma = pt.dscalar('sigma')
dist = Normal.dist(mu, sigma)
logp = dist.log_prob(x)

# Evaluate
f = pytensor.function([x, mu, sigma], logp)
print(f([[1.0, 2.0], [3.0, 4.0]], [0.0, 1.0], 0.5))