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 — Implementation: Learning Graphical Models in Practice

Open in Colab

This notebook applies the chapter’s methods to four tasks:

  1. From records to answers: learn a network’s graph and tables from records, then use it for diagnosis, and compare its answers with the true network’s.

  2. A classifier with structure: naive Bayes against a tree-augmented naive Bayes (TAN) whose tree is learned by the Chow–Liu algorithm, on real medical data.

  3. A hidden variable: a mixture model of handwritten digits learned by EM, where the hidden variable is the cluster.

  4. Filling holes: the same mixture model imputes missing pixels.

1. From Records to Answers

The learning code of the chapter, in compact form: sampling, counting with pseudo-counts, the BIC of one variable’s family, and hill climbing with random restarts.

We learn a network from 20 000 records, the structure first and then the tables with α=0.5\alpha = 0.5, and ask it the questions a doctor would ask.

learned in 0.3 s; edges: ['A->T', 'B->D', 'E->D', 'E->X', 'L->E', 'L->S', 'S->B', 'T->E']

question                                true network   learned
lung cancer | X-ray, dyspnoea                 0.6213    0.6201
tuberculosis | X-ray, dyspnoea, Asia          0.3917    0.4571
bronchitis | dyspnoea, smoker                 0.8802    0.8789
lung cancer | smoker                          0.1000    0.0994
tuberculosis | Asia                           0.0500    0.0637

records with a visit to Asia: 211, of which with tuberculosis: 13

The learned graph is the true one with one edge reversed (L → S), an equivalent graph. On the questions about lung cancer and bronchitis the learned network answers within 0.002 of the true one. On tuberculosis it is off: 0.457 instead of 0.392 given the symptoms and a visit to Asia, 0.064 instead of 0.05 given the visit alone. Those answers depend on P(T∣A=1)P(T \mid A = 1), which is estimated from only the records with a visit, and 13 tuberculosis cases among 211 visits give 0.064 (with α=0.5\alpha = 0.5). A learned model is as good as the data on the question asked, and a rare condition needs many more records than the network’s size suggests.

2. A Classifier with Structure: Naive Bayes and TAN

Naive Bayes is a network with one edge from the class to each feature and no other edge: it assumes the features independent given the class. On real data they rarely are, and naive Bayes counts correlated evidence several times. TAN (tree-augmented naive Bayes, Friedman, Geiger and Goldszmidt, 1997) keeps the class edges and adds a tree among the features, learned by the Chow–Liu algorithm: weight each pair of features by their conditional mutual information given the class, and keep the maximum spanning tree. Among all trees, this one maximises the likelihood (Chow and Liu, 1968).

The data: 569 breast tumours, 30 measurements each, malignant or benign. Each measurement is made binary at its median on the training folds.

569 tumours, 30 measurements; benign: 63%

5-fold cross-validation   accuracy  log-loss  mean confidence
naive Bayes                  0.919     0.868            0.992
TAN                          0.937     0.211            0.962

TAN is more accurate (0.937 against 0.919), but the larger difference is in the probabilities. Naive Bayes’s mean confidence in its answer is 0.992 while it is right 91.9% of the time: because radius, perimeter and area say nearly the same thing, it multiplies the same evidence in three times and becomes almost certain. Its log-loss, which punishes confident mistakes, is 0.868; TAN’s is 0.211, with a mean confidence of 0.962 for 93.7% right. When the probabilities are used (to rank patients, to set a threshold, to combine with costs), this matters more than accuracy.

Source
<Figure size 1200x700 with 1 Axes>

The tree links measurements of the same kind: radius, perimeter and area of a tumour are nearly the same information, and the tree makes each depend on another so that the classifier does not count it three times.

3. A Hidden Variable: Clustering Digits with EM

A mixture of Bernoullis is a network with one hidden parent ZZ (the cluster, KK values) and one child per pixel, each pixel independent given ZZ. The cluster is never observed, so the tables are learned by EM: the E-step computes each image’s posterior over clusters (its responsibilities), the M-step counts pixels weighted by them. The data: 1 797 handwritten digits, 8 × 8 pixels, made black and white.

start 0: final log-likelihood   -35147.3, purity (share of images in their cluster's majority digit) 0.722
start 1: final log-likelihood   -34811.4, purity (share of images in their cluster's majority digit) 0.710
start 2: final log-likelihood   -34776.7, purity (share of images in their cluster's majority digit) 0.728
start 3: final log-likelihood   -34834.6, purity (share of images in their cluster's majority digit) 0.770
start 4: final log-likelihood   -35515.2, purity (share of images in their cluster's majority digit) 0.663

best start by likelihood: 2
cluster 0: 173 images; digits 0-9: [172   0   0   0   0   0   1   0   0   0]
cluster 1: 164 images; digits 0-9: [  0  23 135   2   0   0   0   0   4   0]
cluster 2: 351 images; digits 0-9: [  1   0   1 144   0  52   1   0  11 141]
cluster 3: 140 images; digits 0-9: [ 1  1 38 21  0  0  0  1 71  7]
cluster 4: 208 images; digits 0-9: [  0 103   3   9   4   2   2   3  81   1]
cluster 5: 182 images; digits 0-9: [  1   4   0   0   0   1 176   0   0   0]
cluster 6: 192 images; digits 0-9: [  0   0   0   7   9   1   0 168   0   7]
cluster 7: 133 images; digits 0-9: [  1   1   0   0   0 124   0   0   4   3]
cluster 8:  84 images; digits 0-9: [ 0 50  0  0  3  0  0  7  3 21]
cluster 9: 170 images; digits 0-9: [  2   0   0   0 165   2   1   0   0   0]

The five starts end at five different log-likelihoods, from −35 515-35\,515 to −34 777-34\,777: each is a local maximum, and EM from one start would have been a gamble. The best likelihood (start 2) is not the best agreement with the digit labels (start 3, purity 0.770 against 0.728): the likelihood measures how well the model describes the pixels, and the labels were never part of the data.

Source
<Figure size 1200x180 with 10 Axes>

The clusters are not the ten digits. Zeros, fours, sixes, sevens and fives each get a clean cluster; ones are split in two, the largest cluster mixes threes, nines and some fives (144, 141 and 52 of them), and no cluster is mostly nines. Nothing told EM that there are ten digits or what a digit is: it found ten groups of images whose pixels are similar, and a 3 and a 9 written in 8 × 8 pixels share most of theirs. Clustering finds structure in the data, not the structure we have in mind.

4. Filling Holes

The mixture handles missing pixels naturally: the E-step uses only the pixels that were seen, and a missing pixel is then predicted by averaging the clusters’ tables with the image’s responsibilities. We hide 20% of the pixels at random and compare with the simplest imputation, each pixel’s most frequent value.

hidden pixels: 23225
filled by the mixture: 86.9% correct;  by each pixel's most frequent value: 79.2% correct
Source
<Figure size 1000x400 with 24 Axes>

Using the image’s own seen pixels, the mixture fills 86.9% of the holes correctly, against 79.2% for each pixel’s most frequent value. The model has learned what digits look like, and the 80% of each image that is left says which kind of digit it is. This is EM’s E-step put to work: the same posterior that gave expected counts during learning now predicts the missing values.

Summary

  • A network learned from records (structure and tables) answers diagnostic queries close to the true network where the data are plentiful, and drifts where a configuration is rare.

  • Structure learning helps classifiers: a Chow–Liu tree among the features turns naive Bayes, which double-counts correlated measurements, into TAN, with better calibrated probabilities.

  • A hidden variable makes the likelihood multimodal; EM from several starts, keeping the best likelihood, is standard, and the best likelihood is not always the most useful clustering.

  • The same EM that learns with a hidden variable learns with missing values, and the learned model fills the holes.