Skip to content

1.4 Probability and statistics

ready≈ 10.5 hoursNeeds: 0.2Unlocks: 1.5, 2.1, 2.6, 6.4
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Distribution playground, Covariance ellipse)
  4. 4 Do
  5. 5 Check

Models in this course output distributions, and losses are built from their probabilities, so you need to compute with them fluently.

A random variable has a probability mass function (discrete) or a density (continuous). The expectation Ex∼p[f(x)]\mathbb{E}_{x\sim p}[f(x)]expectationThe average of f(x) when x is drawn from p: ∑ p(x) f(x), or ∫ p(x) f(x) dx.Introduced in 1.4 Probability and statistics. is a probability-weighted average, and the variance is the expected squared distance from the mean μ\boldsymbol{\mu}meanThe mean (expected value) of a distribution.Introduced in 1.4 Probability and statistics. (MML §6.4). For vectors, the covariance matrix Σ\boldsymbol{\Sigma}covariance matrixΣ_{ij} = Cov(x_i, x_j). Symmetric and positive semi-definite.Introduced in 1.4 Probability and statistics. Not to be confused with the summation sign ∑, or with the diagonal matrix of singular values in the SVD A = UΣVᵀ, which shares the symbol. MML §10 writes the data covariance as S. collects every pair’s covariance. It is symmetric and positive semi-definite.

The Gaussian N(μ,σ2)\mathcal{N}(\mu, \sigma^2)GaussianNormal distribution with mean μ and variance σ² (the second argument is a variance, not a standard deviation).Introduced in 1.4 Probability and statistics. Some code libraries (numpy.random.normal) take the standard deviation instead. is the default: a sum of many small independent effects tends towards one (the central limit theorem, which you can watch below), and it has closed forms for almost everything (MML §6.5).

Sampling from uniforms. Computers give you uniform random numbers, and everything else is a transform of them. The inverse transform pushes u∼Uniform(0,1)u\sim\mathrm{Uniform}(0,1) through the inverse CDF: for an exponential, x=−ln⁡(1−u)/λx = -\ln(1-u)/\lambda (MML §6.7). The Gaussian CDF has no closed-form inverse, so Box–Muller turns two uniforms into two independent standard normals with a polar trick (Box and Muller, 1958). For a multivariate Gaussian, factor Σ=LL⊤\boldsymbol{\Sigma} = \mathbf{L}\mathbf{L}^\top (Cholesky, MML §4.3) and set x=μ+Lz\mathbf{x} = \boldsymbol{\mu} + \mathbf{L}\mathbf{z}. Then Cov(x)=L Cov(z) L⊤=Σ\mathrm{Cov}(\mathbf{x}) = \mathbf{L}\,\mathrm{Cov}(\mathbf{z})\,\mathbf{L}^\top = \boldsymbol{\Sigma}. The covariance widget shows exactly this.

Maximum likelihood picks the parameters under which the observed data is most probable. Maximise the log likelihood: products become sums and the optimum stays put (MML §8.3.1). For a Gaussian, the answer is the sample mean and the variance that divides by NN. That MLE is slightly biased. The unbiased estimate divides by N−1N-1, and the tests check which one you used. Module 2.4 derives the standard loss functions this way.

Gaussian maximum likelihoodstep 1 of 4
  1. log⁡p(x1..N∣μ,σ2)=∑n=1N[−12log⁡(2πσ2)−(xn−μ)22σ2]\log p(x_{1..N}\mid\mu,\sigma^2) = \sum_{n=1}^N \Big[-\tfrac12\log(2\pi\sigma^2) - \frac{(x_n-\mu)^2}{2\sigma^2}\Big]

Distribution playground

Common distributions, law of large numbers, CLT

Static view on small screens. Open this page on a laptop to interact.

μ = 1.000.06.0
n = 0 sample mean — (true 1.000) sample variance — (true 1.000)

Draw samples and watch the histogram fill in the curve, and the running mean settle on μ: the law of large numbers.

Choose a distribution and its parameters, then draw samples to fill a histogram against the true density, with a running-mean plot (law of large numbers). In CLT mode, each draw is an average of n samples, compared against the normal approximation. Keyboard: Tab through the controls; buttons draw samples.

Covariance ellipse

Correlation and 2D Gaussians

Static view on small screens. Open this page on a laptop to interact.

correlation ρ = 0.57 (|Σ₁₂| must stay below 1.41) det Σ = 1.360 eigenvalues 2.44, 0.56 sample Σ̂ = [[1.91, 0.77], [0.77, 1.03]]

Ellipses at 1σ and 2σ. Their axes point along the eigenvectors of Σ, with half-lengths √λ. The samples are the same standard-normal points z each time, mapped through x = Lz with Σ = LLᵀ (Cholesky), which is exactly what sample_mvn does in this module's task. Push Σ₁₂ past √(Σ₁₁Σ₂₂) and the matrix stops being a valid covariance.

Edit the three entries of a 2×2 covariance matrix and see the 1σ and 2σ ellipses, the eigen-axes, and samples drawn as x = Lz. The plot reports when the matrix stops being a valid covariance. Keyboard: Tab through the sliders; arrow keys adjust.

Implement Box–Muller, inverse-CDF exponential sampling, the Gaussian MLE and log likelihood, the multivariate MLE, and Cholesky sampling. Some tests are statistical: they use fixed seeds and enough samples to make random failures essentially impossible.

Sampling and Gaussian MLE stubs/m1_4_probability.py

Loading starter file…

Prefer your own editor? Edit exercises/stubs/m1_4_probability.py and run mlc check 1.4. The tests are identical; import the results on the progress page.

Hints · sample_mvn 0/3 revealed

Locally: mlc check 1.4.

1. Which estimate of the variance does maximum likelihood give for a Gaussian?
2. Σ = LLᵀ and z ~ N(0, I). What is the covariance of x = μ + Lz?
3. To sample from a distribution with CDF F using a uniform u, you compute…
4. Why maximise the log likelihood instead of the likelihood?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.