Skip to main content
© 2026 ePowerAI. All rights reserved.
CoursesContact
eAI
CoursesContact
Week 13: Graphical Models (lite)
Prob & Stats
01Week 1: Probability Spaces and Events
02Week 2: Random Variables and Distributions
03Week 3: Expectation, Variance, and Covariance
04Week 4: Conditioning and Independence
05Week 5: Bayes' Rule and Generative Classification
06Week 6: Multivariate Gaussians
07Week 7: Laws of Large Numbers and the CLT
08Week 8: Point Estimation — Maximum Likelihood
09Week 9: Bayesian Estimation and MAP
10Week 10: Bias, Variance, and Uncertainty
11Week 11: Information Theory for ML
12Week 12: Exponential Families (lite)
13Week 13: Graphical Models (lite)
14Week 14: Capstone — Probabilistic Language of ML
Week 13· Prob & Stats· ML interface22 min read

Week 13: Graphical Models (lite)

✦Learning Outcomes
  • Factor a joint with a Bayes net chain rule on a DAG
  • Contrast directed (Bayes net) vs undirected (MRF) modeling stories
  • Apply d-separation on toy graphs (chain, fork, collider) at cartoon level
  • Avoid claiming full inference algorithms (no junction tree course)
◆Prerequisites

Required: Weeks 4–5 (conditional independence, Bayes' rule, conditional probability).

Note: Graphical models are the compact language for structured probabilistic models. This week teaches graph-reading literacy — the ability to look at a factor graph or Bayes net and read off which variables are conditionally independent.

Why Graphs#

In Week 4, we factored a joint distribution using the chain rule:

P(X1,X2,…,Xn)=∏i=1nP(Xi∣X1,…,Xi−1).P(X_1, X_2, \ldots, X_n) = \prod_{i=1}^n P(X_i \mid X_1, \ldots, X_{i-1}).P(X1​,X2​,…,Xn​)=i=1∏n​P(Xi​∣X1​,…,Xi−1​).

Without any assumptions, the conditioning set for XnX_nXn​ includes n−1n-1n−1 variables. This is exponentially expensive — for binary variables, the full joint table has 2n2^n2n entries.

Structure saves us. Most real-world systems have sparse dependencies. A person's cholesterol level might depend on their diet and genetics, but not directly on the weather in a different city. Graphical models encode these sparsity assumptions explicitly through conditional independence statements, reducing the number of parameters from exponential to something manageable.

The core idea: missing edges in the graph   ⟺  \iff⟺ conditional independence in the distribution.

Directed Models (Bayes Nets)#

A Bayesian network (Bayes net) is a directed acyclic graph (DAG) where:

  • Nodes represent random variables X1,…,XnX_1, \ldots, X_nX1​,…,Xn​.
  • Edges Xi→XjX_i \to X_jXi​→Xj​ mean XiX_iXi​ directly influences XjX_jXj​.
  • Each node has a conditional probability table (CPT) P(Xi∣parents(Xi))P(X_i \mid \text{parents}(X_i))P(Xi​∣parents(Xi​)).

The joint distribution factorises as:

P(X1,…,Xn)=∏i=1nP(Xi∣parents(Xi)).P(X_1, \ldots, X_n) = \prod_{i=1}^n P(X_i \mid \text{parents}(X_i)).P(X1​,…,Xn​)=i=1∏n​P(Xi​∣parents(Xi​)).

The graph tells you the factorisation. Variables without parents have marginal (prior) distributions.

Worked example — 3-node chain. Rain →\to→ Sprinkler →\to→ Wet Grass:

  • P(R)P(R)P(R): probability of rain.
  • P(S∣R)P(S \mid R)P(S∣R): probability sprinkler is on, given rain or no rain.
  • P(W∣S)P(W \mid S)P(W∣S): probability grass is wet, given sprinkler on/off.

The joint factorises as:

P(R,S,W)=P(R)⋅P(S∣R)⋅P(W∣S).P(R, S, W) = P(R) \cdot P(S \mid R) \cdot P(W \mid S).P(R,S,W)=P(R)⋅P(S∣R)⋅P(W∣S).

Instead of 23−1=72^3 - 1 = 723−1=7 free parameters for a full joint table, this model needs 1+2+2=51 + 2 + 2 = 51+2+2=5 parameters — and the savings grow dramatically with more variables.

Causal interpretation (cautious): Directed edges can represent causal relationships, but not necessarily — they may just encode statistical dependence. In ML, treat the DAG as a factorisation device first; causal claims require stronger assumptions (do-calculus, beyond this course).

Undirected Models (Markov Random Fields)#

An undirected graphical model (Markov Random Field, or MRF) uses an undirected graph where:

  • Nodes represent random variables.
  • Edges represent direct probabilistic interaction (no directionality).
  • The joint distribution factorises over cliques (fully connected subgraphs):
P(X)=1Z∏C∈cliquesψC(XC),P(X) = \frac{1}{Z} \prod_{C \in \text{cliques}} \psi_C(X_C),P(X)=Z1​C∈cliques∏​ψC​(XC​),

where ψC≥0\psi_C \ge 0ψC​≥0 are potential functions (not probabilities — they need not sum to 1) and Z=∑X∏CψC(XC)Z = \sum_X \prod_C \psi_C(X_C)Z=∑X​∏C​ψC​(XC​) is the partition function (normaliser).

Key difference from Bayes nets:

  • In a Bayes net, each factor P(Xi∣parents)P(X_i \mid \text{parents})P(Xi​∣parents) is a proper conditional probability that sums to 1.
  • In an MRF, the potentials are arbitrary non-negative scores. The partition function ZZZ is needed to make the product a valid distribution.

The partition function problem: Computing ZZZ requires summing over all possible assignments of all variables — exponentially expensive in general. This is the central computational challenge of undirected models and appears in energy-based models, restricted Boltzmann machines, and the normalising flow literature.

When to use each:

  • Directed: When there is a natural generative story — causes produce effects. Good for models with latent variables (e.g., topic models, HMMs).
  • Undirected: When variables interact symmetrically — pixels in an image, atoms in a molecule, nodes in a relational network. No natural direction.

Reading Independencies (d-separation)#

In a DAG, we can read conditional independence statements directly from the graph using d-separation (directional separation). Three basic motifs cover the key patterns:

1. Chain: A→B→CA \to B \to CA→B→C#

AAA and CCC are marginally dependent (information flows through BBB). But given BBB, AAA and CCC are conditionally independent: A⊥ ⁣ ⁣ ⁣⊥C∣BA \perp\!\!\!\perp C \mid BA⊥⊥C∣B. Knowing the intermediate variable blocks the path.

Example: Rain →\to→ Traffic →\to→ Late. If you know there's traffic, knowing it rained doesn't give additional information about lateness.

2. Fork: A←B→CA \leftarrow B \to CA←B→C#

AAA and CCC are marginally dependent (they share a common cause BBB). Given BBB, AAA and CCC become conditionally independent: BBB "explains away" the correlation.

Example: Diet ←\leftarrow← Health-consciousness →\to→ Exercise. If you know someone is health-conscious, their diet and exercise are independent (conditionally).

3. Collider (v-structure): A→B←CA \to B \leftarrow CA→B←C#

AAA and CCC are marginally independent (no edge between them). But given BBB, they become dependent! This is "explaining away": if you know the common effect occurred, learning about one cause changes beliefs about the other.

Example: Skill →\to→ Job Offer ←\leftarrow← Luck. Skill and luck are independent in the population. But if you know someone got a job offer, and you also know they are unskilled, you infer they must have been lucky — conditioning on the collider creates dependence.

The basic d-separation rule: A path between XXX and YYY is blocked (d-separated) given a set ZZZ if:

  • There is a chain or fork with the middle node in ZZZ, OR
  • There is a collider where neither the collider nor any of its descendants are in ZZZ.

XXX and YYY are d-separated by ZZZ iff all paths between them are blocked — then X⊥ ⁣ ⁣ ⁣⊥Y∣ZX \perp\!\!\!\perp Y \mid ZX⊥⊥Y∣Z.

ML Touchpoints#

Graphical models appear throughout modern ML, often implicitly:

  • Naive Bayes (Week 5): A simple Bayes net with one class node pointing to all features. The missing edges between features encode the conditional independence assumption.
  • Hidden Markov Models (HMMs): A chain-structured Bayes net for sequences — X1→X2→X3→⋯X_1 \to X_2 \to X_3 \to \cdotsX1​→X2​→X3​→⋯ with observations YiY_iYi​ depending on XiX_iXi​. Used in speech recognition, genomics, and time-series modeling.
  • Variational Autoencoders: The generative model p(z)p(x∣z)p(z) p(x \mid z)p(z)p(x∣z) is a simple Bayes net with latent z→z \toz→ observed xxx. The inference network q(z∣x)q(z \mid x)q(z∣x) flips the edge.
  • Conditional Random Fields (CRFs): Undirected models for structured prediction (sequence labelling, image segmentation) — model P(Y∣X)P(Y \mid X)P(Y∣X) with cliques over the output variables, conditioned on the input.
  • Factor graphs: A more general representation (bipartite graph of variables and factors) used in message-passing algorithms and error-correcting codes.

This week teaches you to read these graphs, not to infer from them. Full inference algorithms (belief propagation, junction tree, variational message passing) are a separate advanced topic.

Exercise · Multiple choice

In a 3-node chain A → B → C, which independence holds?

A ⟂ C | B
A ⟂ B | C
B ⟂ C | A
A ⟂ C (marginally)

Knowledge check#

Question 1 of 4

In the collider A → B ← C, what happens to A and C when we condition on B?

They become dependent (explaining away)
They become more independent
Nothing changes
One determines the other

Browser lab#

Enumerate the joint distribution for a tiny 3-node discrete Bayes net and verify a marginal by enumeration.

python · runs in browser
import numpy as np
import matplotlib.pyplot as plt

# Bayes net: Rain (R) → Sprinkler (S) → Wet (W)
# P(R): [P(R=0), P(R=1)]
P_R = np.array([0.8, 0.2])
# P(S | R): rows=R, cols=S
P_S_given_R = np.array([[0.6, 0.4],
                         [0.1, 0.9]])
# P(W | S): rows=S, cols=W
P_W_given_S = np.array([[0.9, 0.1],
                         [0.05, 0.95]])

joint = np.zeros((2, 2, 2))
for r in range(2):
    for s in range(2):
        for w in range(2):
            joint[r, s, w] = P_R[r] * P_S_given_R[r, s] * P_W_given_S[s, w]

print("Joint P(R, S, W):")
for r in range(2):
    for s in range(2):
        for w in range(2):
            if joint[r, s, w] > 0.0001:
                print(f"  R={r}, S={s}, W={w}: {joint[r,s,w]:.4f}")

# Marginal P(W): sum over R, S
P_W_marginal = joint.sum(axis=(0, 1))
print(f"\nMarginal P(W=0) = {P_W_marginal[0]:.4f}")
print(f"Marginal P(W=1) = {P_W_marginal[1]:.4f}")

# P(W=1 | R=1) by enumeration
mask_R1 = joint[1, :, :]
P_W_given_R1 = mask_R1.sum(axis=0) / mask_R1.sum()
print(f"\nP(W=1 | R=1) = {P_W_given_R1[1]:.4f}")

fig, axes = plt.subplots(1, 2, figsize=(10, 4))
axes[0].bar(["Dry", "Wet"], P_W_marginal, color=["tab:green", "tab:blue"])
axes[0].set_title("Marginal P(W)")
axes[0].set_ylabel("Probability")

states = ["R=0,S=0", "R=0,S=1", "R=1,S=0", "R=1,S=1"]
P_W1_given = np.array([
    P_W_given_S[0, 1], P_W_given_S[1, 1],
    P_W_given_S[0, 1], P_W_given_S[1, 1]
])
axes[1].bar(states, P_W1_given, color="tab:blue", alpha=0.7)
axes[1].set_title("P(W=1 | S) — depends only on S")
axes[1].set_ylabel("Probability")
axes[1].tick_params(axis="x", rotation=45)

plt.tight_layout()
plt.show()
← Previous
Week 12: Exponential Families (lite)
Next →
Week 14: Capstone — Probabilistic Language of ML
On this page
  • Why Graphs
  • Directed Models (Bayes Nets)
  • Undirected Models (Markov Random Fields)
  • Reading Independencies (d-separation)
  • 1. Chain: A to B to C
  • 2. Fork: A leftarrow B to C
  • 3. Collider (v-structure): A to B leftarrow C
  • ML Touchpoints
  • Knowledge check
  • Browser lab