liveBook · Manning

Importance sampling

As explained in Causal AI

Importance sampling estimates expectations under a target distribution using samples from another distribution and likelihood-based weights.

Importance sampling performs probabilistic inference by generating outcomes from a joint distribution, weighting them according to their probability in a target distribution, and averaging the weighted outcomes with Monte Carlo estimation. It is useful when directly conditioning would discard many generated samples. section 2.2.5

In a Pyro causal model, condition the model on transportation='train', draw 5,000 importance samples, extract the education marginal, and convert sample counts into estimated probabilities. The resulting distribution approximates P(E | T='train'), though probabilistic inference alone is not yet causal inference. section 3.1.11

Estimating education for train riders

A conditioned Pyro model samples the education variable after fixing transportation to train. An empirical marginal and histogram summarize the sampled probabilities for high-school and university education. section 3.1.11

Listing 3.9 Inference on the causal model in Pyro
import numpy as np
import pyro
from pyro.distributions import Categorical
from pyro.infer import Importance, EmpiricalMarginal  #1
import matplotlib.pyplot as plt

conditioned_model = pyro.condition(    #2
    model,    #3
    data={'transportation':torch.tensor(1.)}    #3
)

m = 5000     #4
posterior = pyro.infer.Importance(    #5
    conditioned_model,    #5
    num_samples=m
).run()     #6

E_marginal = EmpiricalMarginal(posterior, "education")     #7
E_samples = [E_marginal().item() for _ in range(m)]   #7
E_unique, E_counts = np.unique(E_samples, return_counts=True)     #8
E_probs = E_counts / m    #8

plt.bar(E_unique, E_probs, align='center', alpha=0.5)     #9
plt.xticks(E_unique, E_alias)    #9
plt.ylabel('probability')    #9
plt.xlabel('education')     #9
plt.title('P(E | T = "train") - Importance Sampling')    #9

Inspect the Pyro implementation that conditions the model, runs 5,000 importance samples, extracts an empirical marginal, and plots estimated probabilities. section 3.1.11

How Causal AI covers it

How deep it goes
Demonstrates weighted Monte Carlo inference and a Pyro workflow, but defers broader inference algorithms and causal applications.
Examples
Uses a Pyro transportation model to estimate education probabilities for train riders.
What it assumes
Assumes a specified probabilistic model and familiarity with Monte Carlo sampling, while demonstrating the Pyro workflow explicitly.
What you can do after
Apply it via Pyro to condition a model, sample an empirical marginal, and estimate target probabilities.
How it connects
Connects probabilistic inference with Monte Carlo estimation, inverse probability reweighting, probabilistic graphical models, and causal inference.

Also explained in

Compare how 3 books explain importance sampling →

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.