Guide to AI and Data Courses · Lecture Notes
Chapter 12: Probabilistic Graphical Models
A physician is weighing the chance of cancer in a patient who coughs. If the patient smokes the chance goes up; if the cough has another explanation it goes down. In reasoning like this, dozens of uncertain variables depend on one another, and keeping their joint probability in a single table is impossible. Probabilistic graphical models split that table into small pieces with a graph and update the probabilities consistently as evidence arrives. These notes introduce the course and work through its three core ideas with small examples you can compute by hand.
Learning outcomes
- Factorization
- Compute the joint probability of an assignment from the conditional probability tables of a Bayesian network, and count how many numbers the network saves over the full table.
- Inference
- Compute the posterior probability of a variable given observed evidence by hand, using Bayes' rule and summation.
- Independence
- Read conditional independence off a graph in chains, forks and colliders; show explaining away with numbers.
- Sequence models
- Use the forward algorithm to find the probability of an observation sequence in a hidden Markov model, and Viterbi to find the most likely state sequence.
- Place of the course
- State what separates probabilistic graphical models from probability and statistics, machine learning, generative models and expert systems.
12.1What question does the course ask?
How do we model and query many interdependent, uncertain variables?
Probabilistic graphical models represent the joint distribution of many uncertain variables with a graph: nodes are variables and edges are direct dependencies. The conditional independences encoded by the graph break a huge distribution into small pieces, making it possible both to model and to query it.
The joint distribution of three binary (true/false) variables takes eight probabilities; that of n variables takes 2n. Twenty variables already mean over a million numbers, and thirty mean over a billion. Nor could such numbers be estimated reliably from data. The course teaches the way around this wall: most variables do not affect one another directly, so the distribution can be written as a product of small factors that hold only the direct relations.
The question of the course has two parts. The first is modeling: how do we put our knowledge into a graph and a small probability table for each node? The second is inference: when some variables are observed, what are the probabilities of the unobserved ones? Machine learning usually asks a one-directional question, "given the input, predict the output". Here, once the model is built, any variable can be queried: we can infer the disease from the symptom and the symptom from the disease.
12.1.1Three basic representations
A Bayesian network is a directed acyclic graph; every node has a probability table conditioned on its parents, and an arrow is often read as "influences". A Markov network is undirected; it measures the agreement between neighboring variables with potential functions and suits relations with no direction, such as pixels. A hidden Markov model consists of an unobserved chain of states moving through time and an observation emitted by each state; it is the basis of speech recognition and sequence labeling. The three core sections below take these representations in turn; the details of inference and learning (EM) come later in the course.
12.1.2At a glance
| Criterion | Probabilistic Graphical Models |
|---|---|
| Approach | probability and graphs combined |
| Input | variables, dependencies, data |
| Output | conditional probability, best explanation |
| Success measure | likelihood, inference accuracy |
| Math load | high: probability |
| Data needs | moderate |
12.2A short history
| Year | Milestone |
|---|---|
| 1906 | Markov studies chains in which the next state depends only on the current one. |
| 1966 | Baum and Petrie publish the statistical basis of the hidden Markov model (HMM). |
| 1977 | Dempster, Laird and Rubin publish the EM algorithm for learning with missing data. |
| 1988 | Pearl's "Probabilistic Reasoning in Intelligent Systems" brings Bayesian networks into AI. |
| 1989 | Rabiner's HMM tutorial becomes the standard reference in speech recognition. |
| 2001 | Lafferty, McCallum and Pereira propose conditional random fields (CRFs) for sequence labeling. |
12.3Core concepts
The rest of the course is built on the concepts below. The following sections work through the most important ones with examples.
| Concept | Meaning |
|---|---|
| Joint distribution | Probability of every combination of values of all variables. |
| Conditional independence | Once one variable is known, the others no longer affect each other. |
| Bayesian network | Directed acyclic graph; a conditional probability table at each node. |
| Markov network | Undirected graph; relations given by potential functions. |
| Hidden Markov model | An unobserved chain of states, each emitting an observation. |
| Exact inference | Variable elimination, junction tree. |
| Approximate inference | Sampling (MCMC), loopy belief propagation, variational. |
| Learning | Parameters (EM) and structure: finding the graph itself from data. |
12.4Factorization and Bayesian networks
The chain rule of probability turns every joint distribution into a product of conditional probabilities: P(A, B, C) = P(A) · P(B | A) · P(C | A, B). This is always true and saves nothing by itself. The saving comes from knowing that some of the conditions are unnecessary. A Bayesian network states this knowledge with arrows: every variable is conditioned only on its parents.
P(X1, …, Xn) = Π P(Xi | Parents(Xi)). The conditional probability table (CPT) of a node holds a probability distribution for every combination of its parents' values; each row sums to 1.
A chain-shaped network: Smoking (S) → Cancer (C) → Cough (K), all binary. CPT values (the values are illustrative): P(S) = 0.3; P(C | S) = 0.10, P(C | ¬S) = 0.02; P(K | C) = 0.8, P(K | ¬C) = 0.1.
- Joint probability. Someone who smokes, has cancer and coughs: P(S, C, K) = 0.3 · 0.10 · 0.8 = 0.024.
- Table saving. The full joint table needs 23 − 1 = 7 independent numbers; the network needs only 1 + 2 + 2 = 5. For a chain of 20 variables the gap is 220 − 1 = 1,048,575 numbers against 1 + 2 · 19 = 39.
- Probability of cancer. P(C) = 0.3 · 0.10 + 0.7 · 0.02 = 0.044.
- Probability of cough. P(K) = 0.044 · 0.8 + 0.956 · 0.1 = 0.0352 + 0.0956 = 0.1308.
- Evidence: the patient coughs. By Bayes' rule P(C | K) = P(C) · P(K | C) / P(K) = 0.0352 / 0.1308 ≈ 0.269.
- One more piece of evidence: the patient smokes. Once cancer is known, cough is independent of smoking, so P(C | S, K) = 0.10 · 0.8 / (0.10 · 0.8 + 0.90 · 0.1) = 0.08 / 0.17 ≈ 0.471.
Observing the cough raised the probability of cancer from 0.044 to 0.269, and learning about the smoking raised it to 0.471. This computation is called inference: using the network's factors to find a probability conditioned on evidence.
What we did here is sum the joint distribution over the unobserved variables (marginalization). With four or five variables we can do it by brute force. With thirty variables the sum has over a billion terms; methods such as variable elimination push the sums inside the factors and get the same result with far less work. Exact inference on general graphs is still NP-hard, which is why sampling and variational methods appear in the middle of the course.
Filling in a CPT whose rows do not sum to 1. In the example, once we write P(C | S) = 0.10, P(¬C | S) = 0.90 is fixed automatically; writing the two as independent pieces of information makes the table inconsistent. Also, every combination of parent values needs its own row: a node with two binary parents has 4 rows in its table.
12.5Conditional independence and d-separation
The real power of the graph is that it lets us read off independences. X and Y are independent given Z means that if Z is known, learning Y tells us nothing new about X. In a Bayesian network we determine this without any arithmetic, by looking at the paths, with the d-separation rule. A path between two nodes is built from three patterns:
| Structure | Graph | Z unknown | Z known |
|---|---|---|---|
| Chain | X → Z → Y | path open, X and Y dependent | path blocked, X and Y independent |
| Fork | X ← Z → Y | path open, X and Y dependent | path blocked, X and Y independent |
| Collider | X → Z ← Y | path blocked, X and Y independent | path opens (if Z or one of its descendants is known), X and Y dependent |
In the chain of the previous section, once Cancer is known, Smoking and Cough are independent: when the cancer status is settled, the path from smoking to cough is blocked. The surprising one is the collider: two causes know nothing about each other until their common effect is observed, but once the effect is observed they become linked.
A collider network: Rain (R) → Wet grass (W) ← Sprinkler (S). CPT values (the values are illustrative): P(R) = 0.2, P(S) = 0.1; P(W | R, S) = 0.99, P(W | R, ¬S) = 0.90, P(W | ¬R, S) = 0.80, P(W | ¬R, ¬S) = 0.05.
- Without evidence. R and S are independent: P(R | S) = P(R) = 0.2.
- Four joint probabilities (W true). P(R, S, W) = 0.2 · 0.1 · 0.99 = 0.0198; P(R, ¬S, W) = 0.2 · 0.9 · 0.90 = 0.162; P(¬R, S, W) = 0.8 · 0.1 · 0.80 = 0.064; P(¬R, ¬S, W) = 0.8 · 0.9 · 0.05 = 0.036. Their sum is P(W) = 0.2818.
- The grass is wet. P(R | W) = (0.0198 + 0.162) / 0.2818 ≈ 0.645. The probability of rain rose from 0.2 to 0.645.
- We also learn that the sprinkler is on. P(R | W, S) = 0.0198 / (0.0198 + 0.064) = 0.0198 / 0.0838 ≈ 0.236. The probability of rain fell from 0.645 to 0.236; the sprinkler explains the wet grass.
- Proof of dependence. With W known, P(R, S | W) = 0.0198 / 0.2818 ≈ 0.070, but P(R | W) · P(S | W) ≈ 0.645 · 0.297 ≈ 0.192. They are not equal, so R and S are no longer independent.
This effect is called explaining away: when one of two causes of the same effect is confirmed, the other becomes less likely.
The practical value is large. An inference algorithm can read from the graph which variables are irrelevant to a query and drop them from the computation. For the modeler, independence is an assumption: leaving two nodes unconnected says "not directly dependent", and if that is wrong the model gives faulty results.
Believing that "unconnected variables are always independent", or that "independent variables can never become dependent". In the example, R and S are independent with no evidence and become dependent once W is observed. Independence changes with what is observed; this is why one should say "independent given Z" rather than just "independent".
12.6The hidden Markov model: forward algorithm and Viterbi
In many problems the process we care about is not observed directly; only signals coming from it are. In speech recognition the spoken word is hidden and the sound is observed; the weather is hidden and whether people outside carry umbrellas is observed. The hidden Markov model (HMM) rests on two assumptions: the hidden state chain is Markov, meaning the next state depends only on the current one, and each observation is generated only from the hidden state at that moment.
The initial distribution π (of the first state), the transition probabilities Aij = P(state j | previous state i) and the emission probabilities Bj(o) = P(observation o | state j). The probability of a sequence of states and observations is the product of these factors.
Two hidden states: Rainy (R), Sunny (S). Observation: a person carries an umbrella (U) or does not (¬U). The values are illustrative. π = (0.5, 0.5); transitions: P(R→R) = 0.7, P(R→S) = 0.3, P(S→R) = 0.4, P(S→S) = 0.6; emissions: P(U | R) = 0.9, P(U | S) = 0.2. The sequence observed over three days: U, U, ¬U (umbrella, umbrella, no umbrella).
- Forward algorithm, day 1. α1(R) = 0.5 · 0.9 = 0.45; α1(S) = 0.5 · 0.2 = 0.10.
- Day 2. α2(j) = [Σi α1(i) Aij] · Bj(U). α2(R) = (0.45 · 0.7 + 0.10 · 0.4) · 0.9 = 0.355 · 0.9 = 0.3195; α2(S) = (0.45 · 0.3 + 0.10 · 0.6) · 0.2 = 0.195 · 0.2 = 0.039.
- Day 3 (no umbrella). α3(R) = (0.3195 · 0.7 + 0.039 · 0.4) · 0.1 = 0.23925 · 0.1 = 0.023925; α3(S) = (0.3195 · 0.3 + 0.039 · 0.6) · 0.8 = 0.11925 · 0.8 = 0.0954.
- Probability of the sequence. P(U, U, ¬U) = 0.023925 + 0.0954 = 0.119325. Posterior of the last day: P(S | observations) = 0.0954 / 0.119325 ≈ 0.80.
- Viterbi. Take the maximum instead of the sum. δ1 = (0.45, 0.10). δ2(R) = max(0.45 · 0.7, 0.10 · 0.4) · 0.9 = 0.315 · 0.9 = 0.2835 (previous R); δ2(S) = max(0.45 · 0.3, 0.10 · 0.6) · 0.2 = 0.135 · 0.2 = 0.027 (previous R). δ3(R) = max(0.2835 · 0.7, 0.027 · 0.4) · 0.1 = 0.019845; δ3(S) = max(0.2835 · 0.3, 0.027 · 0.6) · 0.8 = 0.08505 · 0.8 = 0.06804 (previous R).
- Backtracking. The largest value is δ3(S) = 0.06804; the back-pointers give R, R. The most likely sequence is Rainy, Rainy, Sunny. Check: 0.5 · 0.9 · 0.7 · 0.9 · 0.3 · 0.8 = 0.06804.
With three days and two states there are 23 = 8 possible state sequences, easy to list one by one. With 100 days there would be 2100. The forward algorithm and Viterbi do only N2 work per day, so they finish in T · N2 operations: dynamic programming makes the computation tractable.
The forward algorithm gives the probability of the observation sequence and is used to choose between models (which word's HMM makes this sound more likely?). Viterbi finds the most likely hidden sequence (which word sequence was spoken?). When the parameters are unknown, the EM algorithm known as Baum-Welch estimates them from observation sequences. Conditional random fields (CRFs), the discriminative counterpart of the HMM, use a similar structure for sequence labeling.
Taking the Viterbi path to be the sequence of the individually most likely state at each step. Computing each day's posterior separately and choosing the largest may not give the sequence that is most likely as a whole, because transitions also count toward the consistency of the path. This is why Viterbi carries the maximum instead of the sum and builds the path backwards with pointers.
12.7How is the course taught?
12.7.1A typical workflow
A diagnostic network has nodes for smoking, cancer and cough. A physician fills the tables from the literature. If a patient coughs and smokes, these two pieces of evidence are entered; the network recomputes the probability of cancer and shows which test would reduce uncertainty the most.
12.7.2Topics over the semester
- Probability review; joint distributions, factorization
- Bayesian networks and d-separation
- Markov networks and factor graphs
- Exact inference: variable elimination
- Belief propagation and the junction tree
- Inference by sampling: Gibbs, MCMC
- Variational inference
- Hidden Markov models: forward-backward, Viterbi
- Conditional random fields and sequence labeling
- Parameter learning: maximum likelihood, Bayesian
- Learning with missing data: the EM algorithm
- Structure learning and a first look at causality
This is a typical semester; the order and coverage vary between universities.
12.7.3Skills you will gain
- building Bayesian networks with pgmpy
- sequence modeling with hmmlearn
- Bayesian models in PyMC or Stan
- reading conditional independence off a graph
- interpreting inference results
12.7.4Try it yourself
The Try It: Why Is the Grass Wet? slide in the presentation lets you solve a typical problem of this course yourself: The grass is wet in the morning: did it rain, or did the sprinkler run? Click a node in the four-variable Bayesian network to set evidence (yes / no / unknown); the other nodes are updated by exact probability calculation.
12.8Are you ready for this course?
12.8.1Prerequisites
| Course | Why it is needed |
|---|---|
| Probability and Statistics | conditional independence, joint distribution |
| Algorithms | graph traversal, dynamic programming (Viterbi, forward-backward) |
| Data Structures | graph representation, trees and priority queues |
12.8.2Ask yourself
For every "no", the right-hand column shows where to start. If you can say "yes" to most of them, you are ready.
| Question | If not, start with |
|---|---|
| Can you compute a conditional probability with Bayes' rule? | Probability and Statistics |
| Can you store a graph as an adjacency list and traverse it? | Graph representations from a data structures course |
| Can you solve a problem by splitting it into subproblems with dynamic programming? | Dynamic programming from an algorithms course |
| Can you explain maximum likelihood estimation with an example? | Machine Learning |
12.9Neighboring courses
12.9.1Where it overlaps
| Relation | Course | Shared ground |
|---|---|---|
| builds on | This course builds on Probability and Statistics | conditional independence, joint distribution |
| shares methods | Shares methods with Machine Learning | probabilistic classifiers, EM algorithm |
| shares methods | Shares methods with Natural Language Processing and Text Mining | hidden Markov model, conditional random field |
| shares methods | Shares methods with Expert Systems and Knowledge Representation | inference under uncertainty |
| shares methods | Shares methods with Generative Models | latent variable models, variational inference |
| shares methods | Shares methods with Bioinformatics | hidden Markov models for sequence alignment |
12.9.2Where it differs
- Probability and Statistics: Probability and statistics studies distributions of a few variables; this course teaches modeling hundreds of variables by factoring them with a graph.
- Machine Learning: ML usually predicts a single target; a PGM builds the joint distribution of all variables and lets you query any of them.
- Generative Models: Generative models use large neural networks to produce samples; this course focuses on explicit, interpretable structure and exact inference.
- Expert Systems and Knowledge Representation: Expert systems write knowledge as crisp rules; a PGM writes the same knowledge with probabilities and combines evidence consistently.
12.10In practice
12.10.1Applications
| Area | What is done |
|---|---|
| Medical diagnosis | Disease probability from symptoms and tests. |
| Speech recognition | Word sequences from audio with HMMs. |
| Sequence labeling | Part-of-speech and named entity tagging (CRF). |
| Bioinformatics | Gene finding and sequence alignment. |
| Fault diagnosis | Most likely fault from observed symptoms. |
| Image segmentation | Label agreement of neighboring pixels (MRF). |
12.10.2Risks and ethics
| Risk | Explanation |
|---|---|
| Tables filled with guesses | Without data, probability tables are filled with expert guesses; a precise-looking 83 percent hides how uncertain those guesses are. |
| False independence assumptions | Treating two dependent pieces of evidence as independent counts the same information twice and makes the model overconfident. |
| Probability errors in court | Mistaking the chance of the evidence for the chance of guilt (the prosecutor's fallacy) and ignoring base rates can convict the innocent. |
| Unconverged sampling | Stop an MCMC chain too early and the posterior is wrong; results cannot be trusted without reported convergence diagnostics. |
These issues are discussed in depth in Explainable AI and AI Ethics.
12.10.3Tools and datasets
Tools: pgmpy, PyMC, Stan, hmmlearn, Pyro, GeNIe and SMILE, bnlearn (R).
Classic networks and datasets: Asia (chest clinic) network, ALARM network, Rain-sprinkler network, Insurance network, CoNLL-2003, TIMIT.
12.11Common misconceptions
An edge means cause and effect.
In a Bayesian network an edge is only a probabilistic dependency; causality needs extra assumptions or experiments.
Unconnected variables are independent.
Independence is conditional; two nodes can become dependent once a common child is observed.
Inference is always easy to compute.
Exact inference on general graphs is NP-hard; large models need approximate methods.
Deep learning made this field obsolete.
PGMs remain strong where data is scarce and explainability or uncertainty estimates matter; the two are also combined.
12.12Summary
- Probabilistic graphical models split a joint distribution into small factors with a graph; in a Bayesian network every node is conditioned only on its parents, and far fewer numbers than 2n − 1 are usually needed for n binary variables.
- Inference means finding the conditional probability of unobserved variables once evidence is observed, by summing the joint distribution and applying Bayes' rule; exact inference on general graphs is NP-hard.
- Independence is read off the graph: in chains and forks the middle node blocks the path when it is known, while in a collider knowing it opens the path and gives rise to explaining away.
- A Bayesian network is directed and a Markov network undirected; Markov networks represent relations without a direction, such as neighboring pixels, with potential functions.
- In a hidden Markov model the forward algorithm gives the probability of the observation sequence and Viterbi the most likely hidden sequence, both in T · N2 operations.
12.13Test Yourself
In the network A → C ← B, C → D all variables are binary. How many independent numbers does the full joint table need, and how many do the network's CPTs need?
Answer
The full table needs 24 − 1 = 15 numbers. In the network, A needs 1, B needs 1, C needs 1 per parent combination, that is 4, and D needs 2: 1 + 1 + 4 + 2 = 8.
In the smoking-cancer-cough chain (P(S) = 0.3, P(C | S) = 0.10, P(C | ¬S) = 0.02, P(K | C) = 0.8, P(K | ¬C) = 0.1), what is the joint probability of someone who does not smoke, does not have cancer and coughs?
Answer
P(¬S, ¬C, K) = P(¬S) · P(¬C | ¬S) · P(K | ¬C) = 0.7 · 0.98 · 0.1 = 0.0686.
In the same chain we know the patient coughs. What is the probability of smoking, P(S | K)? Hint: find P(K | S) first.
Answer
P(K | S) = 0.10 · 0.8 + 0.90 · 0.1 = 0.17. P(S, K) = 0.3 · 0.17 = 0.051, P(K) = 0.1308; P(S | K) = 0.051 / 0.1308 ≈ 0.39. The cough raises the probability of smoking from 0.3 to 0.39.
In the rain-sprinkler network the grass is wet. What is the probability that neither rain nor the sprinkler is the reason, P(¬R, ¬S | W)? What does this number show?
Answer
P(¬R, ¬S, W) = 0.8 · 0.9 · 0.05 = 0.036 and P(W) = 0.2818; the ratio is 0.036 / 0.2818 ≈ 0.13. About 87% of the wetness is explained by rain or the sprinkler; the rest is the 0.05 leak that the model leaves for "other causes".
In the same HMM (π = (0.5, 0.5), transitions and emissions as in the example) find the probability of the two-day sequence U, ¬U (umbrella, then no umbrella) with the forward algorithm.
Answer
α1 = (0.45, 0.10). α2(R) = (0.45 · 0.7 + 0.10 · 0.4) · 0.1 = 0.355 · 0.1 = 0.0355; α2(S) = (0.45 · 0.3 + 0.10 · 0.6) · 0.8 = 0.195 · 0.8 = 0.156. The total is 0.0355 + 0.156 = 0.1915.
To model the label agreement of neighboring pixels in an image, is a Bayesian network or a Markov network more suitable? Why?
Answer
A Markov network. The relation between neighboring pixels has no natural direction (saying "A influences B" feels artificial); an undirected graph with potential functions that measure agreement describes it more truthfully. The MRF used in image segmentation is of this kind.
12.14After this course
Probabilistic graphical models bring probability and structure together. Natural next steps are generative models with latent variables, natural language processing and bioinformatics with sequence models, or causal inference.
Related careers: Machine learning researcher, Data scientist, Biostatistician, Speech and language technology engineer.
12.15Further reading
- D. Koller and N. Friedman, Probabilistic Graphical Models: Principles and Techniques (2009): the comprehensive textbook of the field; covers Bayesian networks, d-separation, inference and learning in detail.
- J. Pearl, Probabilistic Reasoning in Intelligent Systems (1988): the classic that brought Bayesian networks into AI; the original account of belief propagation.
- L. R. Rabiner, "A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition" (Proceedings of the IEEE, 1989): a step-by-step tutorial on the forward algorithm, Viterbi and Baum-Welch.
12.16References
- Koller and Friedman, Probabilistic Graphical Models: Principles and Techniques, MIT Press, 2009.
- Pearl, Probabilistic Reasoning in Intelligent Systems, Morgan Kaufmann, 1988.
- Murphy, Machine Learning: A Probabilistic Perspective, MIT Press, 2012.
- Darwiche, Modeling and Reasoning with Bayesian Networks, Cambridge University Press, 2009.
- Rabiner, "A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition", Proceedings of the IEEE 77(2), 1989.
Related pages: Slides · Slides (PDF) · Course page