Key ideas
Generative vs discriminative. A generative classifier models and classifies with Bayes' rule; a discriminative one (logistic regression) models 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 parameters per class, a diagonal one . A shared covariance gives a linear boundary, separate ones a quadratic boundary.
Bayesian networks (directed acyclic graphs) factorise the joint as 1. A binary node with binary parents needs numbers (one per parent configuration), far fewer than a full table1.
d-separation. A trail between two variables is blocked by an evidence set if it contains1,2:
| Connection | Blocked when | So |
|---|---|---|
| chain | observed | |
| fork | observed | |
| collider | neither nor any descendant observed | , but in general not |
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 over the maximal cliques, with a partition function . 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 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 does not change the arg max.
HMMs. Hidden chain with transitions and emissions . The forward recursion sums over the previous state, Viterbi maximises over it and keeps back-pointers. Both cost for states and steps, against for enumerating all 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: plus successes and failures gives 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 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 vanishes, so the posterior falls back to the prior (mean 0, variance )6. That epistemic uncertainty grows away from the data and shrinks as data arrive; the observation noise is aleatoric and does not shrink6. The marginal likelihood used to fit and is non-convex, and a smooth-and-noisy fit and a wiggly-and-precise fit can both be local optima6.
Formulas
- Bayes: .
- Parameters of a discrete BN: .
- for some non-negative .
- Forward: , . Viterbi: .
- Beta posterior mean 2; Laplace (add-one) smoothing is the uniform prior : .
- Point estimates: squared loss → posterior mean, absolute loss → median, 0–1 loss → mode (MAP)4.
- Gaussian prior times Gaussian likelihood: posterior precision = sum of the precisions, posterior mean = precision-weighted average of the two means2.
- GP regression6: , ; a new noisy has variance .
Worked example
Rain and sprinkler both cause wet grass (). , , and for . That is parameters instead of .
- : , , , ; sum .
- , up from the prior 0.2.
- Now learn : . The sprinkler explains the wet grass away.
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
- Colliders work the other way round: their parents are independent until the collider or one of its descendants is observed.
- "EM always increases the likelihood" is false: it never decreases it, and it can converge to a local optimum5.
- The most likely path (Viterbi) is not the sequence of individually most likely states taken from the forward or forward–backward values; that sequence can even have probability 04.
- Emission probabilities are , not the probability of a state given what was observed.
- A conditional independence that holds in one distribution need not be visible as d-separation in its graph; d-separation is a sufficient, graph-level guarantee.
- With the kernel hyperparameters fixed, more data never increases a GP's posterior variance, whatever value is observed6.
- Without smoothing, an unseen feature value gives naive Bayes a zero probability for that class.
References
- 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
- 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
- Wikipedia: Belief propagation — Description of the sum-product algorithm; Exact algorithm for trees; Approximate algorithm for general graphs; Related algorithm and complexity issues
- 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
- Wikipedia: Expectation–maximization algorithm — Description — The EM algorithm; Properties; Proof of correctness
- 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)