In 1D the normal density is a bell: centre \mu, width \sigma. What matters is how many standard deviationsx is from \mu: (x - \mu)/\sigma.
In 2D (two features per pixel) the density is a hill over the plane.
Its contour lines (points of equal density) are ellipses around the centre \boldsymbol\mu.
Round hill: both features vary equally and independently. Stretched hill: one direction varies more. Tilted hill: the features are correlated (bright in one channel, bright in the other).
All of this is in one 2 \times 2 matrix, the covariance\Sigma = \begin{pmatrix} \sigma_1^2 & c \\ c & \sigma_2^2 \end{pmatrix}: variances on the diagonal, covariance c off it.
Notation: \Sigma for the covariance of a model component, S for a covariance computed from data.
Recall: covariance and principal axes
The PCA unit (day 1) showed that the eigenvectors of a covariance matrix are the principal axes of a point cloud, and each eigenvalue \lambda_i is the variance along its axis. The labelling day’s “equivalent ellipse” of an object is the same picture.
For a Gaussian:
the ellipse axes point along the eigenvectors\mathbf v_1, \mathbf v_2 of \Sigma;
the half-lengths of the 1σ ellipse are \sqrt{\lambda_1} and \sqrt{\lambda_2} (the 2σ ellipse is twice as large);
the off-diagonal entry tilts the ellipse; with c = 0 the axes are the coordinate axes.
The points P = (2, 1) and Q = (-1, 2) are both \sqrt5 = 2.24 from the centre. Is either of them unusual?
Mahalanobis distance
Euclidean distance ignores the shape of the cloud. The Mahalanobis distance measures in standard deviations along the direction you are going:
d_M(\mathbf x)^2 = (\mathbf x - \boldsymbol\mu)^\top\Sigma^{-1}(\mathbf x - \boldsymbol\mu).
In 1D: d_M = |x - \mu|/\sigma, the familiar “how many sigmas”.
The 1σ ellipse is exactly d_M = 1; the 2σ ellipse d_M = 2.
In the example: P lies along the long axis, d_M(P) = \sqrt{5/6} = 0.91, inside the 1σ ellipse. Q lies along the short axis, d_M(Q) = \sqrt5 = 2.24: more than 2σ out. Same Euclidean distance, very different.
Use: “is this pixel (or object) unusual for this class?” (unit 4: anomalies).
The math: the density and its ellipses
\mathcal N(\mathbf x \mid \boldsymbol\mu, \Sigma) = \frac{1}{(2\pi)^{d/2}\,|\Sigma|^{1/2}}\exp\!\left(-\tfrac12 (\mathbf x - \boldsymbol\mu)^\top\Sigma^{-1}(\mathbf x - \boldsymbol\mu)\right)
In 1D the exponent is -\tfrac12 (x - \mu)^2/\sigma^2; \Sigma^{-1} replaces 1/\sigma^2.
\Sigma is symmetric positive definite: \Sigma = Q\Lambda Q^\top with orthonormal eigenvectors in Q and \Lambda = \operatorname{diag}(\lambda_1, \dots, \lambda_d). In the rotated coordinates \mathbf y = Q^\top(\mathbf x - \boldsymbol\mu): d_M^2 = \sum_i y_i^2/\lambda_i.
So the density factorises into independent 1D normals with variances \lambda_i along the eigenvectors, and d_M = 1 is the ellipse with half-axes \sqrt{\lambda_i}. |\Sigma| = \prod_i\lambda_i.
Sampling: with \mathbf z standard normal (d independent \mathcal N(0, 1)), \mathbf x = \boldsymbol\mu + Q\Lambda^{1/2}\mathbf z has covariance Q\Lambda^{1/2}\Lambda^{1/2}Q^\top = \Sigma. (Any L with LL^\top = \Sigma, e.g. the Cholesky factor, works too.)
A soft label: “70 % component 1, 30 % component 2”. The r_k sum to 1.
The hard label is the component with the largest r_k.
1D example: \pi = (0.7, 0.3), \mu = (100, 160), \sigma = 20 for both. At x = 130 (halfway) the two densities are equal, so r_1 = 0.7: the weights decide. At x = 120: r_1 = 0.91; at x = 140: r_1 = 0.34.
The boundary (r_1 = r_2) is at 135.6, not at the midpoint 130: the more frequent class gets more room.
Bayes boundaries: straight or curved
Where two components are equally probable, \pi_1\mathcal N_1(\mathbf x) = \pi_2\mathcal N_2(\mathbf x):
same covariance (tied): the boundary is a straight line (a plane), shifted by the weights. k-means’ bisector is the special case of equal weights and round, equal components.
different covariances: a curved boundary (a conic: ellipse, parabola or hyperbola). The wider component wins far away on both sides.
1D: \mathcal N(0, 1) and \mathcal N(3, 2^2), equal weights: the narrow component wins only for -3.42 < x < 1.42. Far to the left the wide one wins again.
The math: tied covariances give straight boundaries
Take logs of \pi_1\mathcal N_1(\mathbf x) = \pi_2\mathcal N_2(\mathbf x) with \Sigma_1 = \Sigma_2 = \Sigma:
\log\pi_1 - \tfrac12(\mathbf x - \boldsymbol\mu_1)^\top\Sigma^{-1}(\mathbf x - \boldsymbol\mu_1) = \log\pi_2 - \tfrac12(\mathbf x - \boldsymbol\mu_2)^\top\Sigma^{-1}(\mathbf x - \boldsymbol\mu_2).
The normalising constants are equal and cancel; so does \mathbf x^\top\Sigma^{-1}\mathbf x:
linear in \mathbf x. With \Sigma_1 \ne \Sigma_2 the term \tfrac12\mathbf x^\top(\Sigma_2^{-1} - \Sigma_1^{-1})\mathbf x survives: a quadratic boundary. In 1D with equal variances: x = \frac{\mu_1 + \mu_2}{2} + \frac{\sigma^2}{\mu_2 - \mu_1}\log\frac{\pi_1}{\pi_2}, the thresholding day’s equal-variance threshold. Example: 130 + \frac{400}{60}\log\frac{0.7}{0.3} = 135.6.
Try it: a mixture
The likelihood of a data set
How well do parameters \theta explain the data? The likelihood: the probability density of the observed points, treated as a function of \theta:
Maximum likelihood: choose the parameters that make the observed data most probable.
For one Gaussian the answer is in closed form: \boldsymbol\mu = the sample mean, \Sigma = the sample covariance S (divided by n).
For a mixture, \log p(\mathcal D) = \sum_i \log \sum_k \pi_k\mathcal N_k(\mathbf x_i): a log of a sum, and no closed form. If we knew the hidden labels, each component would be fitted by its own mean and covariance. That chicken-and-egg problem is unit 3.
In \Sigma^{-1} =: P (using \partial\log|P|/\partial P = P^{-1} and \partial\,\mathbf a^\top P\mathbf a/\partial P = \mathbf a\mathbf a^\top): \frac n2 P^{-1} - \frac12\sum_i(\mathbf x_i - \hat{\boldsymbol\mu})(\mathbf x_i - \hat{\boldsymbol\mu})^\top = 0 \Rightarrow \hat\Sigma = \frac1n\sum_i(\mathbf x_i - \hat{\boldsymbol\mu})(\mathbf x_i - \hat{\boldsymbol\mu})^\top = S.
With known labels the mixture splits into K such problems, plus \hat\pi_k = n_k/n. With hidden labels the \log\sum_k couples everything.
Generative and discriminative
The same 2D point cloud, two ways of learning from it:
With labels (random-forests day, SVM day): learn a boundary between the classes directly. Discriminative: says nothing about where points come from.
Without labels (this day): learn a model of how the points were made, a mixture. Generative: it gives a density p(\mathbf x), can draw new points, can say “this point is unlikely under every class”, and its Bayes boundaries come for free.
Generative models need more assumptions (here: Gaussian groups). When the assumptions hold, few data suffice; when they do not, a discriminative boundary is usually more accurate.
Summary
A 2D Gaussian is a hill with elliptical contours: axes = eigenvectors of \Sigma, half-lengths \sqrt{\lambda_i} (1σ).
Mahalanobis distanced_M^2 = (\mathbf x - \boldsymbol\mu)^\top\Sigma^{-1}(\mathbf x - \boldsymbol\mu): standard deviations in the direction of \mathbf x.
Covariance types: spherical, diagonal, full, tied: more freedom, more parameters.
A mixture: pick a component with probability \pi_k, then draw from its Gaussian; the component is hidden.