liveBook · Manning

Sampling

As explained in Causal AI

A sampling process is a procedure that generates observed outcomes by drawing values from a probability distribution.

Sampling generates an outcome by drawing from a probability distribution. “Generate” and “simulate” mean the same thing here. A computer typically uses a pseudo-random number generator: a seed starts a deterministic sequence that emulates random draws. The outcome can vary between runs even when the distribution stays fixed. section 2.2.2

When the distribution models a population, a draw can represent the selection of one individual. Repeating that selection produces a random sample. Data is independent and identically distributed (IID) when each point is selected the same way, independently of the others, from the same population distribution. Repeated observations alone do not establish those conditions. section 2.3.1

Generate outcomes in Python

A pgmpy DiscreteFactor with probabilities [0.45, 0.30, 0.25] for three states can produce one outcome with dist.sample(n=1), returned as a one-row pandas DataFrame. The same method can draw from a joint distribution or generate multiple rows. In Pyro, Categorical(probs=torch.tensor([0.45, 0.30, 0.25])).sample() draws a category; pyro.plate provides a way to express repeated draws in a model. section 2.2.2 section 2.3.1

Listing 2.10 Generating IID samples in Pyro
import pyro
from pyro.distributions import Bernoulli, Poisson, Gamma

def model():
    z = pyro.sample("z", Gamma(7.5, 1.0))
    x = pyro.sample("x", Poisson(z))
    with pyro.plate("IID", 10):     #1
        y = pyro.sample("y", Bernoulli(x / (5+x)))    #2
    return y

model()

The Pyro listing places repeated Bernoulli draws inside a plate while drawing their parameters through preceding model steps. section 2.3.1

Sampling also connects a data-generating process (DGP) to observed data. A DGP entails a full joint distribution; marginalizing latent variables yields an observational joint distribution, and sampling from it yields data and an empirical distribution. The empirical proportions need not equal the underlying probabilities: five observations cannot identify a unique observational distribution, much less a unique causal process. section 2.3.2

How Causal AI covers it

How deep it goes
Defines random draws and IID conditions, then demonstrates library calls and their place in a DGP-to-data hierarchy.
Examples
Shows categorical draws, pgmpy DataFrames, a Pyro plate, and five observations from a simulated window-breaking process.
What it assumes
Introduces random generation and IID sampling while assuming familiarity with basic Python calls and probability distributions.
What you can do after
Apply it via pgmpy or Pyro to generate outcomes and distinguish sampled data from its assumed population distribution.
How it connects
Links sampling to probability distributions, pseudo-random number generators, IID data, data-generating processes, and empirical distributions.

Also explained in

Compare how 23 books explain 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.