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

Open in Colab

All models are wrong but some are useful.

George E. P. Box (1979)

25.1 What Is Learned

Chapters 22–24 took the network as given: its graph drawn by an expert, its tables filled in by hand. In practice the tables, and often the graph itself, are learned from records. Two questions, two kinds of data:

Complete dataMissing values or hidden variables
Parameters (the tables)counting: maximum likelihood, Bayesian estimation (25.2–25.3)the EM algorithm (25.4)
Structure (the graph)score and search, independence tests (25.5–25.6)structural EM (beyond this chapter)

We learn from samples of the Asia network of Chapter 23, so the true answer is always known and every learned model can be checked against it. A test set of 20 000 fresh records measures how well a learned model predicts new data: the average log-likelihood per record, higher is better.

test set: 20000 records of 8 variables
the true network on the test set: -2.2337 per record (the best any learned model can expect)

25.2 Maximum Likelihood: Counting

With complete data and a known graph, the likelihood of the records factorises into one term per variable and parent configuration. Each term is maximised separately, and the answer is the obvious one:

θ^x∣u=N(x,u)N(u)\hat\theta_{x \mid u} = \frac{N(x, u)}{N(u)}

the fraction of records with parents uu in which the variable takes the value xx. Learning the tables is counting.

 records  visits  P(T|A=1)  P(B|S=1)  P(D|E,B)  test log-lik
     100       0    0.5000    0.6364    1.0000          -inf
    1000      11    0.0909    0.6360    0.9057       -2.2453
   10000      93    0.0538    0.6011    0.8904       -2.2339
  100000    1024    0.0625    0.6019    0.8960       -2.2339
    true            0.0500    0.6000    0.9000

The estimates approach the truth as the records grow, but at very different speeds. P(bronchitis∣smoker)P(\text{bronchitis} \mid \text{smoker}) is estimated from half of the records and is close at 1 000. P(tuberculosis∣visit to Asia)P(\text{tuberculosis} \mid \text{visit to Asia}) is estimated only from the 1% of records with a visit: none of the first 100 records has one, and the “estimate” 0.5 is the default for an empty count. Even 100 000 records give 1 024 visits and 0.0625 for a true 0.05.

The 100-record model fails on the test set: its log-likelihood is −∞-\infty. Somewhere in its tables is an entry estimated as exactly 0 (a configuration never seen), and a test record with that configuration has probability 0. Maximum likelihood says that what was not seen cannot happen, and a single such test record makes the model’s whole test likelihood zero.

25.3 Bayesian Estimation: Pseudo-Counts

The Bayesian answer puts a prior on each table entry. For a binary variable the convenient prior is the Beta distribution (the Dirichlet for more values), because the posterior stays in the same family: a prior Beta(α,α)\text{Beta}(\alpha, \alpha) and counts N0,N1N_0, N_1 give the posterior Beta(α+N1,α+N0)\text{Beta}(\alpha + N_1, \alpha + N_0), whose mean is

θ^=N1+αN0+N1+2α.\hat\theta = \frac{N_1 + \alpha}{N_0 + N_1 + 2\alpha}.

The prior acts as α\alpha imaginary records of each value, called pseudo-counts. α=1\alpha = 1 is Laplace smoothing; α=0.5\alpha = 0.5 is the Jeffreys prior. No entry is ever 0, and with many records the counts swamp the prior. The cell below learns from 20 samples of each size and averages the test log-likelihood.

 records  alpha=0 finite    alpha=0  alpha=0.5    alpha=1    alpha=5
      50             0%         nan    -2.3608    -2.3867    -2.6935
     100             0%         nan    -2.3011    -2.3157    -2.4828
     200             0%         nan    -2.2740    -2.2808    -2.3642
     500             0%         nan    -2.2486    -2.2520    -2.2871
    1000            10%     -2.2401    -2.2413    -2.2428    -2.2593
    2000            40%     -2.2362    -2.2376    -2.2383    -2.2466
    5000            85%     -2.2350    -2.2355    -2.2358    -2.2389
Source
<Figure size 1100x360 with 2 Axes>

Three things to read in the plot:

  • Without a prior (α=0\alpha = 0) the test likelihood is −∞-\infty in every run up to 500 records, in 90% of runs at 1 000 and still in 15% at 5 000. On the runs that survive it is about as good as the others, but a model that sometimes gives a real case probability zero cannot be used.

  • α=0.5\alpha = 0.5 is best at every size here; α=1\alpha = 1 is close behind.

  • A strong prior (α=5\alpha = 5) pulls every entry towards 0.5 and costs a great deal with little data: at 50 records it is the worst. With 5 000 records all priors give nearly the same model, and all approach the true network’s -2.234.

The strength of the prior is a hyperparameter (Chapter 9): it can be chosen on a validation set. A prior can also encode knowledge: an expert who believes a test has 98% sensitivity can start the count at 98 positives and 2 negatives.

25.4 Missing Data and the EM Algorithm

Real records have holes: a test not ordered, a question not answered. Why a value is missing matters more than how many are missing:

  • MCAR (missing completely at random): the hole does not depend on anything, as a lost form.

  • MAR (missing at random): it depends only on values that are observed, as a test ordered only for smokers.

  • MNAR (missing not at random): it depends on the missing value itself, as patients with bronchitis skipping the question about it.

Below, each of T, L, E and B is hidden in 30% of 2 000 records, completely at random.

missing values: 14.3% of all cells; complete records: 517 of 2000

Deleting the incomplete records (complete-case analysis) keeps only 517 of 2 000: three quarters of the information is thrown away, including all the observed values in the incomplete records. Under MCAR it is unbiased but wasteful; under MAR it can be biased.

The EM algorithm (Dempster, Laird and Rubin, 1977) uses every observed value. Starting from any tables, it repeats two steps:

  1. E-step (expectation). For each record, compute the posterior of its missing values given its observed ones under the current tables (an inference, Chapter 24). A record with B missing becomes two weighted records: one with B = 1, weight P(B=1∣observed)P(B = 1 \mid \text{observed}), and one with B = 0.

  2. M-step (maximisation). Re-estimate the tables by counting, as in 25.3, with these expected counts.

Each iteration never decreases the likelihood of the observed data, so EM climbs to a local maximum.

40 EM iterations in 1.4 s; log-likelihood never decreased: True

                               test log-lik  P(B|S=1)  P(D|E,B)
all 2 000 records (no holes)        -2.2368    0.6038    0.9333
complete cases only (517)           -2.2472    0.5741    0.9583
EM on the records with holes        -2.2379    0.6024    0.9485
true network                        -2.2337    0.6000    0.9000
Source
<Figure size 700x300 with 1 Axes>

The log-likelihood jumps in the first iterations, from random tables, and then creeps up; it never goes down. EM’s model, learned with 30% of four variables missing, is almost as good on the test set (-2.2379) as the model learned from the records without any holes (-2.2368), and clearly better than complete-case analysis (-2.2472), whose estimate of P(bronchitis∣smoker)P(\text{bronchitis} \mid \text{smoker}) is 0.574 against EM’s 0.602 and a true 0.60.

EM cannot repair MNAR. Below, bronchitis is hidden in 80% of the records where it is present and 10% of those where it is absent. EM fills each hole from the other variables, as if the hole said nothing, and the holes are mostly the records with bronchitis:

MCAR: hidden 45% of the time     EM: P(B|S=1) = 0.621, P(B|S=0) = 0.293
MNAR: hidden more when present   EM: P(B|S=1) = 0.424, P(B|S=0) = 0.151
true                                 P(B|S=1) = 0.600, P(B|S=0) = 0.300

With about 40% of bronchitis values hidden either way, the MCAR holes give 0.621 and 0.293, near the truth; the MNAR holes give 0.424 and 0.151, far too low. No method fixes this from the records alone: the reason values go missing has to be modelled (a variable “B was recorded”, with B as a parent) or known from outside.

Hidden variables are the extreme case: a variable never observed in any record, such as a disease nobody tested for, or a cluster label. EM still applies, but the likelihood now has many local maxima (and symmetric ones: the two values of a hidden binary variable can swap names), so EM is run from several random starts and the best is kept. The implementation notebook does this for a mixture model, which is a network with one hidden parent.

When the graph is not known, it has to be learned too. A first idea, choosing the graph with the highest likelihood, fails: adding an edge never lowers the maximum likelihood (a table with more parents can always reproduce one with fewer), so the complete graph always wins. We need a score that pays for complexity. The most used is the BIC (Bayesian information criterion, Schwarz, 1978):

BIC(G)=log⁡P(data∣θ^G,G)−log⁡N2 dim⁡(G)\text{BIC}(G) = \log P(\text{data} \mid \hat\theta_G, G) - \frac{\log N}{2}\, \dim(G)

where dim⁡(G)\dim(G) counts the free parameters: one per parent configuration of each binary variable. Each parameter must earn 12log⁡N\frac{1}{2}\log N of likelihood. Like the likelihood, the BIC decomposes into one term per variable and its parents, so changing one edge changes only one or two terms.

true graph               log-likelihood   -11178.4   BIC   -11255.0
true graph + A -> X      log-likelihood   -11175.4   BIC   -11260.5
true graph + S -> D      log-likelihood   -11178.0   BIC   -11271.6

Each useless edge raises the likelihood slightly (the table fits noise in the 5 000 records) but lowers the BIC: the penalty is larger than the gain.

Searching. The number of graphs on nn variables grows faster than exponentially (29 000 for 5 variables, 7.8×10117.8 \times 10^{11} for 8), so they cannot all be scored. Hill climbing starts from the empty graph and repeatedly applies the single change that improves the score most: add an edge, delete one, or reverse one, never creating a cycle. It stops when no change helps.

likelihood only, 5 000 records: 17 edges: 3 right, 5 reversed, 9 extra, missed none; same v-structures: False
BIC,             5 000 records: 8 edges: 4 right, 3 reversed, 1 extra, missed ['A-T']; same v-structures: False
BIC of the learned graph -11254.8, of the true graph -11255.0

Without the penalty, the search keeps adding edges until it hits the limit of three parents: 17 edges for the true 8. With the BIC it stops at 8 edges, and its graph scores slightly above the true graph (−11 254.8-11\,254.8 against −11 255.0-11\,255.0) although it is not the true graph: it lacks Asia → tuberculosis and has an edge T → L that the truth does not. The score is right to prefer it; with 5 000 records the data cannot tell the two apart (the notes after the restarts below say why).

A greedy search can also stop in a local optimum, where every single change makes the score worse although a better graph exists several changes away. Moves between equivalent graphs tie exactly, so which one the search takes depends on rounding, and so does where it ends. The standard remedies are random restarts (climb again from random graphs, keep the best), tabu lists, and simulated annealing.

  1000 records: true graph    -2244.6, greedy    -2241.2, best of 20 restarts    -2241.2 (0.2 s)
        6 right, 1 reversed, 0 extra, missed ['A-T']; same v-structures: True
  5000 records: true graph   -11255.0, greedy   -11254.8, best of 20 restarts   -11254.8 (0.3 s)
        4 right, 3 reversed, 1 extra, missed ['A-T']; same v-structures: False
 20000 records: true graph   -44826.0, greedy   -44830.8, best of 20 restarts   -44826.0 (1.0 s)
        7 right, 1 reversed, 0 extra, missed none; same v-structures: True
Source
<Figure size 1400x340 with 4 Axes>

What the restarts show:

  • Restarts matter. At 20 000 records the greedy search from the empty graph is stuck at −44 830.8-44\,830.8, below the true graph’s −44 826.0-44\,826.0; the best of 20 restarts reaches the true score exactly. At 1 000 and 5 000 records no restart beat the first climb.

  • Reversed edges are not errors. At 20 000 records the best graph has the true skeleton and the true v-structures but one reversed edge (L → S): it is Markov equivalent to the truth (Chapter 23), so it scores the same, and no amount of observational data can choose between them. A learned structure is really an equivalence class.

  • Weak edges need data. The edge Asia → tuberculosis concerns 1% of the records and changes a probability from 0.01 to 0.05; with 1 000 or 5 000 records the BIC judges it not worth its parameter, and it is found only at 20 000.

  • Determinism confuses the score. At 5 000 records the best graph has an extra edge between T and L and scores slightly above the truth: given E = “T or L”, T and L are strongly dependent in the data, and in 5 000 records a different graph can explain that as well as the true one.

25.6 Structure Learning by Independence Tests

The other family of methods reads the graph from the independences in the data. The PC algorithm (Spirtes, Glymour and Scheines) starts from the complete undirected graph and removes the edge X−YX - Y as soon as some set ZZ of neighbours makes them independent, testing sets of size 0, then 1, then 2. The set that separated them is remembered: for a pair X−Z−YX - Z - Y with X,YX, Y not adjacent, if ZZ was not in their separating set, the triple is a v-structure X→Z←YX \to Z \leftarrow Y.

Independence is tested with the GG-test: G=2∑Olog⁡(O/E)G = 2 \sum O \log(O / E) over each stratum of the conditioning set, compared with a χ2\chi^2 distribution.

edges found:   ['A-T', 'B-D', 'B-S', 'E-L', 'E-T', 'L-S']
missed:        ['D-E', 'E-X']
extra:         []
  D-E removed because the test found independence given ['L', 'T']
  E-X removed because the test found independence given ['L', 'T']
v-structures:  [('L', 'E', 'T')]

With 20 000 records the tests recover six of the eight edges with no extra one, and the v-structure T → E ← L. They lose E – X and E – D, and for a reason that no amount of data fixes: given T and L, the value of E is determined (E = “T or L”), so conditioned on {T, L} the variable E is a constant and is independent of everything. The test correctly reports independence, and PC wrongly concludes there is no edge. This is a failure of faithfulness, the assumption that every independence in the data comes from the graph’s structure: Chapter 23 met the same deterministic E producing three independences that d-separation does not predict. Score-based search kept these edges, because removing them costs likelihood.

Score-based and constraint-based methods fail differently: a score averages over the whole graph and is robust to one bad test, but its search can get stuck; tests are fast and local, but one wrong test removes an edge for good and can mis-orient its neighbours. Hybrid methods (such as MMHC) use tests to restrict the candidate parents and a score to choose among them.

Learning structure is not learning causes. Both methods find a Markov equivalence class at best, and only under assumptions: no hidden common causes, faithful independences, and records drawn from one population. Orienting the remaining edges needs interventions (Chapter 23’s do-operator) or knowledge, such as time order.

25.7 Summary

  • With complete data and a known graph, learning the tables is counting; maximum likelihood gives zero to what was not seen.

  • Dirichlet (Beta) priors add pseudo-counts; their strength is a hyperparameter, and a moderate one (α≈0.5\alpha \approx 0.5–1) helps most with little data.

  • The EM algorithm learns from incomplete records by alternating inference (expected counts) and counting; it never decreases the likelihood, finds a local maximum, and cannot repair values missing not at random.

  • Structure is learned by a score that penalises complexity (BIC) and a search (hill climbing with restarts), or by independence tests (PC). Both recover an equivalence class at best; weak edges need much data, and deterministic relations mislead them.

Exercises

  1. In 400 records, 120 are smokers and 30 of these have lung cancer. Give the maximum-likelihood estimate of P(L∣S=1)P(L \mid S = 1) and the estimate with α=1\alpha = 1.

  2. Why does a single unseen configuration make the test log-likelihood −∞-\infty? Which tables of the Asia network are most at risk?

  3. A record has S = 1 and B missing; the current tables give P(B=1∣S=1,D=1)=0.8P(B = 1 \mid S = 1, D = 1) = 0.8 for its other values. What does it contribute to the expected counts of B’s table?

  4. How many free parameters does the Asia network have? How many does the complete graph on its 8 variables have (no limit on parents)?

  5. Two graphs differ only in the direction of the edge S – L. Why do they have the same BIC?

  6. Give an example, in your own field, of values missing not at random. What would you model to handle it?

Further Reading

  • D. Koller, N. Friedman, Probabilistic Graphical Models, MIT Press, 2009, Chapters 17–19.

  • D. Heckerman, “A tutorial on learning with Bayesian networks”, Microsoft Research MSR-TR-95-06, 1995.

  • A. Dempster, N. Laird, D. Rubin, “Maximum likelihood from incomplete data via the EM algorithm”, Journal of the Royal Statistical Society B, 1977.

  • G. Schwarz, “Estimating the dimension of a model”, Annals of Statistics, 1978.

  • P. Spirtes, C. Glymour, R. Scheines, Causation, Prediction, and Search, 2nd ed., MIT Press, 2000.

  • R. Little, D. Rubin, Statistical Analysis with Missing Data, 3rd ed., Wiley, 2019.