Topic 62 of 62 · Wider ML toolkit
Gaussian process regression
HardWhat is a Gaussian process, how do you get predictions and uncertainty from it, and why does it not scale?
1 · Start with the intuition
A Gaussian process puts a prior directly over functions. The kernel says how similar function values at two inputs should be. After seeing data, predictions near observed points are pulled toward them with low uncertainty; far away, the prediction falls back to the prior mean with the prior’s full uncertainty.
2 · How the mechanism works
Mechanism step 1 of 3 · no question to answer
Choose a kernel
For example RBF: k(x, x′) = σ_f² exp(−(x − x′)² / 2ℓ²).
Next, we will look at: Condition on data. Move on when this operation makes sense.
Visualizing the behavior
Posterior after one observation (RBF, ℓ = 1, σ_n² = 0.1)
Derived- mean + 2σ
- posterior mean
- mean − 2σ
3 · The governing equation
μ* = k*ᵀ (K + σ_n² I)⁻¹ y σ*² = k(x*, x*) − k*ᵀ (K + σ_n² I)⁻¹ k* log p(y) = −½ yᵀK_y⁻¹y − ½ log|K_y| − (n/2) log 2π
Notation: K is the n × n kernel matrix on training inputs, k* the kernels between x* and the training inputs, K_y = K + σ_n²I. A zero prior mean is assumed.
The marginal likelihood balances data fit (the quadratic term) against complexity (the log-determinant), so it can choose ℓ without a validation set. Exact inference needs a Cholesky factorization of K_y: O(n³) time and O(n²) memory, which becomes impractical beyond tens of thousands of points. Sparse methods use m ≪ n inducing points for O(nm²). GPs are widely used for Bayesian optimization of expensive functions such as hyperparameter searches, and infinitely wide neural networks converge to GPs.
4 · Put concrete numbers on it
Worked numbers (from stated assumptions)5 · Common misconception to avoid
“The posterior variance tells you how surprising the observed y values were.” For fixed hyperparameters, σ*² depends only on the input locations, not on y. The observations affect uncertainty only through the fitted hyperparameters.
In practice · implementation & engineering rule of thumb
Never invert (K + σ²I) with torch.linalg.inv; use a Cholesky factorization L = torch.linalg.cholesky(K_y) and cholesky_solve for both numerical stability and a 2–3× speedup.
# Numerically stable GP posterior mean & variance via Cholesky: K_y = K_train + (sigma_n ** 2) * torch.eye(len(y_train)) L = torch.linalg.cholesky(K_y) alpha = torch.cholesky_solve(y_train.unsqueeze(-1), L) mu_star = (k_star.T @ alpha).squeeze(-1) v = torch.linalg.solve_triangular(L, k_star, upper=False) var_star = k_star_star - (v ** 2).sum(dim=0)
Go one level deeper
Follow-ups that often come after a correct first answer.
- Bayesian optimization loop
- Fit a GP to the evaluated configurations, then choose the next point by maximizing an acquisition function such as expected improvement or an upper confidence bound (μ + κσ). It trades exploiting a high mean against exploring high uncertainty, which suits expensive evaluations like training runs.
- The kernel encodes the inductive bias
- RBF assumes very smooth functions; Matérn kernels allow rougher ones; periodic kernels encode repetition; sums and products combine structure. Kernel choice is the GP analogue of choosing an architecture.
6 · Self-check & interview synthesis
Explain it in your own words first
Before revealing the reference answer, try answering the opening question aloud in four beats: what it is → how the math works → what trade-off it makes → a concrete number or failure mode.
Follow-up question interviewers often ask next
MediumHow would you run GP regression on one million points?
Show follow-up reasoning
Use approximations: inducing-point methods such as sparse variational GPs trained with minibatches, structured kernel interpolation, or random Fourier features that turn the GP into Bayesian linear regression. Each trades exactness for O(n)-ish cost.