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 25 — Lab: Learning Graphical Models

Open in Colab

Lab Overview

You will learn the tables of the Asia network from records, score structures, and run EM on records with holes, checking each step against values computed independently.

ExerciseTopicPoints
1Counting a table15
2Pseudo-counts10
3Log-likelihood of records15
4Counting parameters10
5The BIC of a family15
6The E-step for one record20
7One EM iteration15
ChallengeThe Chow–Liu tree+10

How it works. Records are a NumPy array with one row per record and one column per variable, in the order of VARS; a missing value is -1. A table is a dict from a tuple of parent values (in the order of the parents’ list) to P(variable=1∣parents)P(\text{variable} = 1 \mid \text{parents}). Complete each function where you see # TODO and run the check cell under it. Do not change the check cells.


Exercise 1: Counting a Table (15 points)

mle(data, v, pa) returns the maximum-likelihood table of v given the parents pa: for each tuple of parent values uu (all 2∣pa∣2^{|pa|} of them, in itertools.product([0, 1], repeat=len(pa)) order), N(v=1,u)/N(u)N(v = 1, u) / N(u). A configuration never seen gets 0.5.


Exercise 2: Pseudo-Counts (10 points)

smoothed(data, v, pa, alpha) is the same table with a Beta(α\alpha, α\alpha) prior: (N(v=1,u)+α)/(N(u)+2α)(N(v = 1, u) + \alpha) / (N(u) + 2\alpha). With alpha = 0 it must agree with mle (including 0.5 for an unseen configuration).


Exercise 3: Log-Likelihood of Records (15 points)

loglik(data, parents, cpt) returns ∑records∑vlog⁡P(v∣parents of v)\sum_{\text{records}} \sum_v \log P(v \mid \text{parents of } v) under the tables cpt. A probability of 0 gives -inf (that is expected; silence NumPy’s warning with np.errstate(divide='ignore')).


Exercise 4: Counting Parameters (10 points)

n_params(parents) is the number of free parameters of a network of binary variables: one per parent configuration of each variable.


Exercise 5: The BIC of a Family (15 points)

family_bic(data, v, pa) is one variable’s term of the BIC: its maximum log-likelihood given the parents, ∑u∑xN(x,u)log⁡N(x,u)N(u)\sum_u \sum_x N(x, u) \log \frac{N(x, u)}{N(u)} (terms with N(x,u)=0N(x, u) = 0 are 0), minus log⁡N2⋅2∣pa∣\frac{\log N}{2} \cdot 2^{|pa|}, where NN is the number of records.


Exercise 6: The E-Step for One Record (20 points)

completions(row, cpt) takes one record with -1 for its missing values and returns a list of pairs (completed_row, weight): one per way of filling the holes, with weight P(completion∣observed values)P(\text{completion} \mid \text{observed values}) under the tables cpt of the Asia graph. The weights sum to 1. (Compute the joint probability of each completion, then normalise.)


Exercise 7: One EM Iteration (15 points)

em_step(data, cpt, alpha=1.0) performs one iteration: replace every record by its completions (Exercise 6), then re-estimate every table from the weighted counts with pseudo-counts α\alpha: (∑w⋅[v=1,u]+α)/(∑w⋅[u]+2α)(\sum w \cdot [v = 1, u] + \alpha) / (\sum w \cdot [u] + 2\alpha). It returns the new tables.


Challenge: The Chow–Liu Tree (+10 points)

chow_liu(data) returns the edges (as a set of frozensets of two names) of the maximum spanning tree of the 8 variables, each pair weighted by its empirical mutual information I(X;Y)=∑x,yP^(x,y)log⁡P^(x,y)P^(x)P^(y)I(X; Y) = \sum_{x,y} \hat P(x, y) \log \frac{\hat P(x, y)}{\hat P(x) \hat P(y)} (terms with P^(x,y)=0\hat P(x, y) = 0 are 0). nx.maximum_spanning_tree does the second half.


Lab Report

Submit this notebook with:

  • every check cell showing ✅,

  • one paragraph: why does Exercise 3’s last check hold for the maximum-likelihood tables, and why does that make the likelihood alone a bad score for choosing a structure?