Skip to content
ml-recall

Topic notes

Probabilistic & Graphical Models

Generative classifiers, Bayesian networks and Markov random fields, d-separation, exact inference by message passing (forward, Viterbi), parameter learning with priors and EM, and Gaussian processes.

4-min read AI-checked

Practise 133 questions Take a test

Key ideas

Generative vs discriminative. A generative classifier models p(x,y)=p(y) p(x∣y)p(\mathbf{x}, y) = p(y)\,p(\mathbf{x} \mid y) and classifies with Bayes' rule; a discriminative one (logistic regression) models p(y∣x)p(y \mid \mathbf{x}) directly. Naive Bayes assumes the features are independent given the class, so it only stores per-class counts: training is one pass, updates are cheap, but it cannot capture feature interactions (it fails on XOR). In a Gaussian class-conditional model, the covariance choice is the main complexity knob: a full matrix has d(d+1)/2d(d+1)/2 parameters per class, a diagonal one dd. A shared covariance gives a linear boundary, separate ones a quadratic boundary.

Bayesian networks (directed acyclic graphs) factorise the joint as p(x)=∏ip(xi∣pai)p(\mathbf{x}) = \prod_i p(x_i \mid \mathrm{pa}_i)1. A binary node with kk binary parents needs 2k2^k numbers (one per parent configuration), far fewer than a full table1.

d-separation. A trail between two variables is blocked by an evidence set ZZ if it contains1,2:

Connection Blocked when So
chain x→z→yx \to z \to y zz observed x⊥y∣zx \perp y \mid z
fork x←z→yx \leftarrow z \to y zz observed x⊥y∣zx \perp y \mid z
collider x→z←yx \to z \leftarrow y neither zz nor any descendant observed x⊥yx \perp y, but in general not x⊥y∣zx \perp y \mid z

Observing a collider (or a descendant) makes its parents dependent; typically, once the effect is seen, evidence for one cause lowers the probability of the other (explaining away). If every trail is blocked, the independence holds for every distribution on the graph; a d-connected pair is only not guaranteed independent. The Markov blanket (parents, children, children's other parents) shields a node from all the rest1.

Markov random fields (undirected) write p∝∏CϕC(xC)p \propto \prod_C \phi_C(\mathbf{x}_C) over the maximal cliques, with a partition function ZZ. Sets are independent given a separating set; a node's Markov blanket is just its neighbours2. For a Gaussian, a zero in the precision matrix Σ−1\Sigma^{-1} means conditional independence given all other variables2. Moralisation turns a DAG into an undirected I-map: marry the parents of each node, then drop the arrows; it loses the marginal independence of a collider's parents2. Two DAGs encode the same independencies (I-equivalent) exactly when they share the skeleton and the immoralities (colliders with unlinked parents)2.

Inference. Exact inference is NP-hard in general1. Variable elimination sums variables out one at a time; the cost is set by the largest intermediate factor, so the order matters: eliminate variables with few neighbours first2. On a tree-structured factor graph the sum-product algorithm gives exact marginals in two passes; a leaf variable sends the constant message 1. On graphs with loops it is only approximate ("loopy BP")3. Replacing sums by maxima (max-product, or max-sum in logs) finds the most probable joint configuration; Viterbi is the special case for chains3. A constant factor such as 1/Z1/Z does not change the arg max.

HMMs. Hidden chain with transitions aija_{ij} and emissions bj(o)=P(o∣state j)b_j(o) = P(o \mid \text{state } j). The forward recursion sums over the previous state, Viterbi maximises over it and keeps back-pointers. Both cost O(n2T)O(n^2 T) for nn states and TT steps, against O(TnT)O(T n^T) for enumerating all nTn^T paths4.

Learning. With complete data, the maximum-likelihood conditional probability table is normalised counts, undefined for a parent configuration never seen2. A Beta prior fixes this: Beta(α,β)\mathrm{Beta}(\alpha, \beta) plus ss successes and ff failures gives Beta(α+s,β+f)\mathrm{Beta}(\alpha + s, \beta + f)2. With latent variables, EM alternates an E-step (posterior of the latent variables) and an M-step (maximise the expected complete-data log-likelihood)5. Each iteration never decreases the observed-data likelihood, but EM may stop at a local optimum5. For MAP estimation only the M-step changes: the prior does not involve the latent variables, so the E-step is the same and the M-step maximises the same expectation plus log⁡p(θ)\log p(\theta)4.

Gaussian processes. The posterior mean is a weighted combination of the observed targets, but the posterior variance does not depend on their values6. With an RBF kernel, far from the data every k(x,xi)k(x, x_i) vanishes, so the posterior falls back to the prior (mean 0, variance k(x,x)k(x, x))6. That epistemic uncertainty grows away from the data and shrinks as data arrive; the observation noise σ2\sigma^2 is aleatoric and does not shrink6. The marginal likelihood used to fit ℓ\ell and σ2\sigma^2 is non-convex, and a smooth-and-noisy fit and a wiggly-and-precise fit can both be local optima6.

Formulas

Worked example

Rain RR and sprinkler SS both cause wet grass WW (R→W←SR \to W \leftarrow S). P(R=1)=0.2P(R{=}1) = 0.2, P(S=1)=0.3P(S{=}1) = 0.3, and P(W=1∣R,S)=0.05,0.8,0.9,0.99P(W{=}1 \mid R, S) = 0.05, 0.8, 0.9, 0.99 for (R,S)=(0,0),(0,1),(1,0),(1,1)(R, S) = (0,0), (0,1), (1,0), (1,1). That is 1+1+4=61 + 1 + 4 = 6 parameters instead of 23−1=72^3 - 1 = 7.

In code

import numpy as np

pi = np.array([0.6, 0.4])                          # initial distribution
A = np.array([[0.7, 0.3], [0.4, 0.6]])             # A[i, j] = P(next j | now i)
B = np.array([[0.5, 0.4, 0.1], [0.1, 0.3, 0.6]])   # B[i, o] = P(obs o | state i)
obs = [0, 1, 2]
alpha = delta = pi * B[:, obs[0]]
for o in obs[1:]:
    alpha = (alpha @ A) * B[:, o]                       # forward: sum over previous state
    delta = (delta[:, None] * A).max(axis=0) * B[:, o]  # Viterbi: max over previous state
print(alpha.sum().round(5))   # 0.03628  = P(observations)
print(delta.max().round(5))   # 0.01512  = P(best path, observations)

Common pitfalls

References

  1. Wikipedia: Bayesian network — Graphical model; Definitions and concepts (Factorization definition, Markov blanket, d-separation); Inference and learning (Inferring unobserved variables, Parameter learning); Inference complexity and approximation algorithms
  2. Pen and Paper Exercises in Machine Learning — Ch. 3.2 Canonical connections; Ch. 4.2 Factorisation and independencies for undirected graphical models; Ch. 4.6 Undirected graphical model with pairwise potentials; Ch. 5.1 I-equivalence; Ch. 5.4 Moralisation; Ch. 6.5 Choice of elimination order in factor graphs; Ch. 8.2 Posterior of the mean of a Gaussian with known variance; Ch. 8.3 Maximum likelihood estimation of probability tables; Ch. 8.5 Bayesian inference for the Bernoulli model
  3. Wikipedia: Belief propagation — Description of the sum-product algorithm; Exact algorithm for trees; Approximate algorithm for general graphs; Related algorithm and complexity issues
  4. Bishop, Pattern Recognition and Machine Learning (2006) — Sec. 1.5.5 Loss functions for regression, p. 48; Sec. 9.4 The EM Algorithm in General, eqs. (9.76)–(9.77), p. 454; Sec. 13.2.1 Maximum likelihood for the HMM, p. 615; Sec. 13.2.2 The forward-backward algorithm, p. 621; Sec. 13.2.5 The Viterbi algorithm, pp. 629–630
  5. Wikipedia: Expectation–maximization algorithm — Description — The EM algorithm; Properties; Proof of correctness
  6. Dive into Deep Learning — Sec. 18.3 Gaussian Process Inference (18.3.2 Equations for Making Predictions; 18.3.3 Interpreting Equations; 18.3.4 Worked Example from Scratch; 18.3.7 Exercises)