liveBook · Manning

Pyro

As explained in Causal AI

Pyro is an open-source probabilistic programming library for modeling and performing Bayesian and probabilistic inference with Python and PyTorch.

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

How Causal AI covers it

How deep it goes
Demonstrates sampling, Monte Carlo estimation, and variational inference, but defers a comprehensive treatment of inference algorithms.
Examples
Uses Gamma, Poisson, and Bernoulli sampling, conditional expectation estimation, and counterfactual inference about femur length and height.
What it assumes
Assumes Python, probability, statistics, and modeling fundamentals while emphasizing only the concepts needed for causal probabilistic modeling.
What you can do after
Build probabilistic models from distributions, simulate outcomes, estimate expectations, and implement variational counterfactual inference.
How it connects
Connects probabilistic graphical models, PyTorch, Monte Carlo methods, variational inference, and structural causal models.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Causal AI. The section links point to the parts of the book it draws on.