Bayesian Optimisation: an interactive explanation

Surrogate model, acquisition function, exploration vs exploitation — with an animation and simulations to build intuition.

For recruiters (quick read)

Bayesian optimisation (BO) finds the maximum of an expensive-to-evaluate function, with no gradient and no analytic form. I use it as a modelling and decision tool for:

  • Hyperparameter tuning: exploring a deep network's space in few evaluations, where a grid would explode.
  • Uncertainty quantification: a Gaussian process gives a full predictive law, not a single point — crucial for frost alerts \(\Pr(T_c < 3°\mathrm{C})\).
  • Exploration / exploitation trade-off: cleanly formalised by the acquisition function.
  • Link to kriging and the Kalman filter: a GP's marginal likelihood is what Kalman optimises implicitly.
Gaussian processes Acquisition functions Uncertainty quantification BoTorch

Bayesian optimisation, animated

Before the theory, an animated overview: Gaussian process regression (the uncertainty band tightening onto the data), then the decision loop with Expected Improvement, and finally the exploration/exploitation trade-off via UCB.

Animation produced with Manim — the three pillars of BO in one minute.

Why Bayesian optimisation?

Picture training a large neural network on an HPC cluster. Each hyperparameter configuration requires a full retraining: two hours of compute. You have a budget of fifty evaluations. Which ones should you run?

Gradient descent assumes we know \(f\) and \(\nabla f\) — here we know neither. Grid search wastes the budget on blind coverage. BO answers a simple question: how do we intelligently decide where to evaluate next, given everything observed so far?

The central idea: replace the costly function with a probabilistic surrogate, cheap to query, that returns not only a prediction but also an uncertainty. We exploit that uncertainty to target the next evaluations. It is a one-step lookahead: each round we make the move that maximises the expected one-move gain — a myopic policy, but robust and cheap in practice.

The surrogate: Gaussian processes

The surrogate is almost always a Gaussian process (GP): a distribution over functions, fully determined by a mean \(\mu(\cdot)\) and a covariance \(k(\cdot,\cdot)\).

Definition
\[ y(\cdot) \sim \mathcal{GP}\big(\mu(\cdot),\, k(\cdot,\cdot)\big) \]

Key property: any finite subset of points follows a multivariate Gaussian. After observing noisy data \((\mathbf{x},\mathbf{y})\) with \(\varepsilon \sim \mathcal{N}(0,\sigma_n^2)\), we predict at any test point:

Predictive mean
Smoothing the observations
\[ \bar{\mathbf{f}}_* = K(\mathbf{x}_*,\mathbf{x})\big[K(\mathbf{x},\mathbf{x}) + \sigma_n^2 I\big]^{-1}\mathbf{y} \]
Predictive covariance
Remaining uncertainty
\[ \Sigma_* = K(\mathbf{x}_*,\mathbf{x}_*) - K(\mathbf{x}_*,\mathbf{x})\big[K(\mathbf{x},\mathbf{x}) + \sigma_n^2 I\big]^{-1}K(\mathbf{x},\mathbf{x}_*) \]

Adding \(\sigma_n^2\) on the diagonal makes the mean smooth the observations rather than pass through them, and the variance stays strictly positive everywhere. The GP thus provides both a prediction and its uncertainty — exactly what makes it the ideal surrogate.

Marginal likelihood: kernel hyperparameters are tuned by maximising \(\log p(\mathbf{y}\mid\mathbf{x}) = -\tfrac{1}{2}\mathbf{y}^\top(K+\sigma_n^2 I)^{-1}\mathbf{y} - \tfrac{1}{2}\log|K+\sigma_n^2 I| - \tfrac{n}{2}\log 2\pi\). The first term measures fit, the second penalises complexity — a built-in Occam's razor. It is also exactly what the Kalman filter optimises implicitly in forecast::Arima.

Kernels (covariance functions)

With the mean usually zero, it is the kernel that characterises the GP. It must be symmetric and positive semi-definite. Two families dominate.

Squared Exponential (SE)

\[ k_{\text{SE}}(r) = \sigma_f^2 \exp\!\left(-\frac{r^2}{2\ell^2}\right) \]

Infinitely differentiable → very smooth trajectories. The length-scale \(\ell\) controls the correlation range.

Matérn class

\[ k_{3/2}(r) = \sigma_f^2\left(1 + \tfrac{\sqrt{3}\,r}{\ell}\right)\exp\!\left(-\tfrac{\sqrt{3}\,r}{\ell}\right) \]

The parameter \(\nu\) sets regularity. \(\nu = 3/2\) or \(5/2\) gives less smooth, often more realistic trajectories. As \(\nu \to \infty\), we recover the SE.

The Fit – Choose – Evaluate loop

BO is a sequential process: each round it fits the surrogate, chooses the most promising point, evaluates it, then repeats.

① Fit

Refit the GP \(g \approx f\) on the data

② Choose

\(x_{n+1} = \arg\max_x \alpha_n(x)\)

③ Evaluate

Compute \(f(x_{n+1})\), add to the data

Maximising \(\alpha_n\) (the inner loop) must be far cheaper than one evaluation of \(f\).

Acquisition functions

The acquisition function \(\alpha_n(x)\) captures the benefit of evaluating at \(x\). Write \(\mu_n(x)\), \(\sigma_n(x)\) for the predictive mean and standard deviation, and \(y_n^*\) for the best observed value. Three main families exist.

① Improvement
Expected Improvement
\[ \alpha^{\text{EI}}_n = (\mu_n - y_n^*)\Phi(z) + \sigma_n\,\phi(z) \]

\(z = (\mu_n - y_n^*)/\sigma_n\). 1st term = exploitation, 2nd = exploration. Also includes PI and the knowledge gradient.

② Bandit
Upper Confidence Bound
\[ \alpha^{\text{UCB}}_n = \mu_n(x) + \beta\,\sigma_n(x) \]

\(\beta\) sets the trade-off: small → exploitation, large → exploration. Also: Thompson sampling.

③ Information
Entropy Search
\[ \alpha^{\text{ES}}_n = \mathrm{MI}(x^*, y_x \mid \mathcal{D}_n) \]

Reduces uncertainty about the optimum itself (ES, PES, MES). More costly, often more efficient.

FamilyAcquisitionsGuiding idea
ImprovementPI, EI, knowledge gradientMaximise expected gain
BanditUCB, Thompson samplingBalance exploration / exploitation
InformationES, PES, MESReduce uncertainty about the optimum

Simulation: UCB in action

Slide \(\beta\) and the number of observations. The yellow square marks the point UCB would choose next: watch how a high \(\beta\) pushes toward uncertain regions (exploration), a low \(\beta\) toward the mean's peaks (exploitation).

1D UCB — mean, 2σ band, and next point

2.0
5
± 2σ band Mean True function Observations Next point (UCB argmax)

Simulation: compare PI, EI and UCB

Same surrogate, same observations, three different acquisitions. Step through the loop and compare where each strategy would evaluate (yellow square).

Three acquisition functions, side by side

2

Yellow square = argmax of each acquisition (next point evaluated).

Applications

Hyperparameter tuning

Tuning a TCN or WaveNet on HPC, where each configuration requires a full retraining, is exactly BO's canonical use case. With fifty well-placed evaluations, you cover the space far better than a grid of hundreds of points.

Methodological argument: in my \(T_c\) forecasting benchmark, a parsimony result (XGBoost and SARIMAX beating deep learning) could be blamed on under-tuning the deep models. Tuning every model with an identical, documented BO budget rules out that objection and strengthens the conclusion.

Uncertainty quantification for frost risk

A GP gives a full predictive law. For a frost alert, the right quantity is not just \(\hat{T}_c < 3°\mathrm{C}\), but the probability \(\Pr(T_c < 3°\mathrm{C})\), read off the predictive band. The alert fires as soon as the lower bound crosses the threshold — ahead of the mean.

Link to kriging and the Kalman filter

GP regression is kriging in spatial statistics. And the GP's marginal likelihood is precisely the quantity the Kalman filter optimises implicitly — a direct bridge to my Kalman filter article.

Limitations and extensions

ChallengeExtension / solution
\(O(n^3)\) inversion (slow if \(n>1000\))Sparse GPs, variational inference (inducing points)
Unknown kernel hyperparametersMaximum likelihood, or prior + MCMC
High dimension (\(>20\))Trust-region BO, random embeddings, local BO
ConstraintsConstrained BO (GP on the constraint)
Multiple objectivesMulti-objective BO → Pareto front (hypervolume)
Very tight budgetHyperband, successive halving (early stopping)
Implementation: BoTorch (PyTorch) is the standard; GPyTorch for scalable GPs, GPJax in JAX. References: Rasmussen & Williams (2006), Frazier (2018), Shahriari et al. (2016).