Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 24 — Lab: Inference in Depth

Open in Colab

Lab Overview

You will build exact inference from factors, then three samplers, and check every one against enumeration on the Asia network.

ExerciseTopicPoints
1Factor product15
2Summing a variable out10
3Reducing by evidence10
4Variable elimination20
5Rejection sampling10
6A likelihood weight15
7The Gibbs conditional20
ChallengeEffective sample size+10

How it works. A factor is a pair (vars, table): a list of variable names and a NumPy array with one axis of size 2 per variable, in the same order. Complete each function where you see # TODO and run the check cell under it. Do not change the check cells.


Exercise 1: Factor Product (15 points)

product(f, g): the result’s variables are those of f, followed by those of g not in f; each entry is the product of the matching entries of f and g. (One way: loop over all assignments of the result; another: np.einsum.)


Exercise 2: Summing Out (10 points)

sum_out(f, var) removes var by adding over its two values.


Exercise 3: Reducing by Evidence (10 points)

reduce(f, var, value) keeps only the entries with var = value, and removes var. A factor without var is returned unchanged.


Exercise 4: Variable Elimination (20 points)

ve(query, evidence, order): start from the CPT factors of all variables; reduce them by the evidence; for each variable in order, multiply the factors that contain it and sum it out; finally multiply the remaining factors and normalise. Return P(query=1∣evidence)P(\text{query} = 1 \mid \text{evidence}).


Exercise 5: Rejection Sampling (10 points)

rejection(q, ev, n, rng): draw n samples in the order of VARS (each variable with rng.random() < P(1 | parents)), keep those consistent with ev, and return the share of kept samples with q = 1 (or nan if none is kept).


Exercise 6: A Likelihood Weight (15 points)

lw_sample(ev, rng): sample in the order of VARS, but set evidence variables to their values instead of sampling them, multiplying the weight by P(value∣parents)P(\text{value} \mid \text{parents}). Return (x, weight).


Exercise 7: The Gibbs Conditional (20 points)

gibbs_conditional(v, x): the probability that v = 1 given all other variables in x, computed from v’s own CPT and its children’s CPTs only:

P(v=1∣⋅)=ϕ(1)ϕ(0)+ϕ(1),ϕ(a)=P(v=a∣parents)∏c∈children(v)P(xc∣parents(c))∣v=aP(v = 1 \mid \cdot) = \frac{\phi(1)}{\phi(0) + \phi(1)}, \quad \phi(a) = P(v = a \mid \text{parents}) \prod_{c \in \text{children}(v)} P(x_c \mid \text{parents}(c))\big|_{v = a}

Challenge: Effective Sample Size (+10 points)

ess(x): n/(1+2∑k≥1ρk)n / (1 + 2\sum_{k \ge 1} \rho_k), summing the autocorrelations ρk\rho_k of the centred series while they are at least 0.05.


Lab Report

Submit this notebook with:

  • every check cell showing ✅,

  • one paragraph: with the evidence A = 1, X = 1, D = 1, about how many of 1 000 000 samples would your rejection sampler keep? Why does the likelihood weighting of Exercise 6 not waste samples, and when does it struggle?