Pyro is a flexible Python library for probabilistic machine learning that uses PyTorch’s gradient-based learning techniques. It builds random-process models from canonical distributions, supporting uncertainty modeling, simulation, and inference. section 2.0 section 2.5
A Pyro model can represent a joint probability distribution by sampling variables such as z, x, and y. Monte Carlo inference repeatedly generates outcomes and estimates expectations from the resulting samples. Conditioning can filter those outcomes, although accept/reject inference becomes inefficient when the target event is rare. section 2.2.5
Pyro’s role in probabilistic workflows includes:
- Representing counts, waiting times, and other phenomena with canonical distributions. section 2.5
- Estimating expectations with generated samples and Monte Carlo averages. section 2.2.5
- Supporting probability weighting, MCMC, and variational inference as alternative inference approaches. section 2.5
- Implementing flexible structural causal models when explicit inference code is acceptable. section 9.1.5
Counterfactual height inference
A Pyro model can represent a forensic structural causal model in which femur length causes height. Variational inference handles abduction, allowing comparison between a conditional hypothetical—height if femur length were 46 cm—and a counterfactual that additionally conditions on an individual’s observed femur length of 44 cm and height of 165 cm. The same approach extends from this small linear example toward high-dimensional and nonlinear settings. section 9.3 section 9.1.5
Listing 2.9 Monte Carlo estimation in Pyro
import torch
import pyro
from pyro.distributions import Bernoulli, Gamma, Poisson
def random_process():
z = pyro.sample("z", Gamma(7.5, 1.0))
x = pyro.sample("x", Poisson(z))
y = torch.tensor(0.0)
for i in range(int(x)):
y += pyro.sample(f"{i}", Bernoulli(.5))
return z, y #1
generated_samples = [random_process() for _ in range(1000)] #2
z_mean = torch.stack([z for z, _ in generated_samples]).mean() #3
print(z_mean)
The listing makes Pyro’s implementation pattern concrete: declare sampled variables with distributions, generate repeated outcomes, and aggregate tensors for Monte Carlo estimates. section 2.2.5
