Simple Algorithm
Machine Learning · Convergence · 28 min

Bayesian Optimization

Acquisition functions, GP-UCB and optimizing expensive black boxes.

Tuning a network's hyperparameters, choosing a molecule to synthesize, calibrating a slow simulator: each evaluation costs hours or money, returns no gradient, and you can afford a few dozen. Bayesian optimization spends cheap computation to save expensive evaluations. It fits a Gaussian process to everything seen so far and asks the posterior where the next evaluation is most worth making, trading exploitation of promising regions against exploration of uncertain ones, and for GP-UCB that trade comes with a no-regret guarantee.

What you’ll learn

  • Describe the Bayesian optimization loop (surrogate, acquisition, evaluation, update) and the problems it suits.
  • Derive the probability of improvement and the closed form of expected improvement, and compare EI, UCB, Thompson sampling and information-based acquisitions.
  • State the GP-UCB regret bound in terms of the maximum information gain, prove it for finite domains, and quote the information-gain rates and lower bounds for common kernels.
  • Handle practice: hyperparameters during the search (and why estimated priors can break EI), evaluation costs, parallel queries and high dimensions.

Optimizing what you can barely afford to evaluate

The task is x⋆=arg max⁡x∈Xf(x)\mathbf x^\star=\argmax_{\mathbf x\in\mathcal X}f(\mathbf x) on a box X⊂Rd\mathcal X\subset\R^d of modest dimension, where ff is a black box: each evaluation is expensive, possibly noisy, and returns no gradient. The budget is tens to hundreds of evaluations, so any computation that saves one evaluation is worth it. The loop:

text
BAYESIAN-OPTIMIZATION(f, domain X, budget T)
  evaluate f at a few initial points (random or space-filling)
  for t = 1, 2, ..., T:
      fit a GP to all evaluations so far          # refit hyperparameters now and then
      x_t = argmax over x in X of acquisition(x)  # cheap: uses only the posterior
      y_t = f(x_t) + noise                        # the expensive step
  return the best point evaluated (or the maximizer of the posterior mean)

The acquisition function scores each candidate from the posterior mean μt−1(x)\mu_{t-1}(\mathbf x) and standard deviation σt−1(x)\sigma_{t-1}(\mathbf x). Maximizing it is itself a nonconvex problem, but a cheap one: multistart gradient ascent, or a dense grid in low dimension.

Predict before you look

The widget evaluates on a grid of 401 points, and 13 of them count as finding the maximum. If you instead queried grid points uniformly at random (with replacement), how many queries would you need on average to hit one?

Show the answer

31 queries

Each query succeeds with probability p=13/401≈3.2%p=13/401\approx3.2\%, so the number of queries is geometric with mean 1/p≈30.81/p\approx30.8 (the median is about 22, since the distribution has a long tail). Expected improvement needed 6: the surrogate turns every evaluation into information about where the maximum is not.

Acquisition functions

Let f+f^+ be the best value observed so far and μ,σ\mu,\sigma the posterior mean and standard deviation at a candidate x\mathbf x. The oldest criterion, Kushner's (1964) probability of improvement, is PI(x)=Pr⁡(f(x)≥f++ξ)=Φ((μ−f+−ξ)/σ)\mathrm{PI}(\mathbf x)=\Pr\big(f(\mathbf x)\ge f^++\xi\big)=\Phi\big((\mu-f^+-\xi)/\sigma\big). It ignores how much improvement is on offer, so with ξ=0\xi=0 it happily picks near-certain, tiny gains. Expected improvement (Močkus, 1975), the criterion of the EGO algorithm of Jones, Schonlau & Welch (1998), weighs size and probability together.

DerivationExpected improvement in closed form
  1. EI(x)=E[I]=∫f+∞(f−f+) N(f;μ,σ2) df\mathrm{EI}(\mathbf x)=\E[I]=\int_{f^+}^{\infty}(f-f^+)\,\mathcal N(f;\mu,\sigma^2)\,df

    The improvement is I=max⁡(f(x)−f+,0)I=\max(f(\mathbf x)-f^+,0), and under the posterior f(x)∼N(μ,σ2)f(\mathbf x)\sim\mathcal N(\mu,\sigma^2):

  2. EI=σ∫−Z∞(Z+u) ϕ(u) du\mathrm{EI}=\sigma\int_{-Z}^{\infty}(Z+u)\,\phi(u)\,du

    Substitute f=μ+σuf=\mu+\sigma u and write Z=(μ−f+)/σZ=(\mu-f^+)/\sigma; the lower limit becomes u=−Zu=-Z:

  3. EI(x)=(μ−f+) Φ(Z)+σ ϕ(Z),Z=μ−f+σ\mathrm{EI}(\mathbf x)=(\mu-f^+)\,\Phi(Z)+\sigma\,\phi(Z),\qquad Z=\frac{\mu-f^+}{\sigma}

    Use ∫−Z∞ϕ=Φ(Z)\int_{-Z}^\infty\phi=\Phi(Z) and ∫−Z∞u ϕ(u) du=ϕ(−Z)=ϕ(Z)\int_{-Z}^\infty u\,\phi(u)\,du=\phi(-Z)=\phi(Z), since ϕ′(u)=−u ϕ(u)\phi'(u)=-u\,\phi(u):

  4. The first term rewards a mean above the incumbent (exploitation), the second rewards uncertainty (exploration), and EI is zero only where σ=0\sigma=0 and μ≤f+\mu\le f^+. A small margin ξ\xi (the widget uses 0.01) replaces f+f^+ by f++ξf^++\xi to discourage dithering around the incumbent.

Predict before you look

At a candidate whose posterior mean equals the incumbent, μ=f+\mu=f^+, with posterior standard deviation σ=0.5\sigma=0.5, what is the expected improvement?

Show the answer

0.2

Z=0Z=0, so EI=0⋅Φ(0)+0.5 ϕ(0)=0.5/2π≈0.20\mathrm{EI}=0\cdot\Phi(0)+0.5\,\phi(0)=0.5/\sqrt{2\pi}\approx0.20. A point that is merely as good as the incumbent on average still has value, purely from its uncertainty: that is the exploration term at work.

AcquisitionRuleCharacter
Probability of improvementΦ((μ−f+−ξ)/σ)\Phi\big((\mu-f^+-\xi)/\sigma\big)greedy; needs a margin ξ\xi to explore
Expected improvement(μ−f+)Φ(Z)+σϕ(Z)(\mu-f^+)\Phi(Z)+\sigma\phi(Z)the classic default; closed form, no tuning
GP-UCBμ+βt1/2σ\mu+\beta_t^{1/2}\sigmaoptimism under uncertainty; a schedule for βt\beta_t gives regret guarantees
Thompson samplingmaximize one posterior sample f~∼p(f∣D)\tilde f\sim p(f\mid\data)randomized, so batches of different queries come naturally (Thompson, 1933, for bandits)
Entropy search familyexpected reduction in uncertainty about x⋆\mathbf x^\star or f(x⋆)f(\mathbf x^\star)information-greedy; needs approximations (Hennig & Schuler, 2012; Hernández-Lobato, Hoffman & Ghahramani, 2014; Wang & Jegelka, 2017)
Knowledge gradientexpected gain in the best posterior mean after one more evaluationvalues information for the final recommendation (Frazier, Powell & Dayanik, 2009)

No regret: the GP-UCB guarantee

Measure a strategy by its cumulative regret RT=∑t=1T(f(x⋆)−f(xt))R_T=\sum_{t=1}^T\big(f(\mathbf x^\star)-f(\mathbf x_t)\big). A strategy has no regret if RT/T→0R_T/T\to0; then the best point found also converges, because min⁡trt≤RT/T\min_tr_t\le R_T/T. The difficulty of a problem enters through the maximum information gain, the most that TT noisy evaluations can reveal about ff under the prior:

γT=max⁡A⊂X, ∣A∣=TI(yA;f)=max⁡∣A∣=T12log⁡det⁡(I+σn−2KA).\gamma_T=\max_{A\subset\mathcal X,\ \abs A=T}I(\mathbf y_A;f)=\max_{\abs A=T}\tfrac12\log\det\big(I+\sigma_n^{-2}K_A\big).
DerivationProof for a finite domain
  1. ∣f(x)−μt−1(x)∣≤βt1/2 σt−1(x)for all x∈X, t≥1\big|f(\mathbf x)-\mu_{t-1}(\mathbf x)\big|\le\beta_t^{1/2}\,\sigma_{t-1}(\mathbf x)\qquad\text{for all }\mathbf x\in\mathcal X,\ t\ge1

    Confidence. At each x\mathbf x and tt, f(x)f(\mathbf x) given the data is Gaussian, and Pr⁡(∣Z∣>c)≤e−c2/2\Pr(\abs Z>c)\le e^{-c^2/2} for a standard normal ZZ. A union bound over ∣X∣\abs{\mathcal X} points and all tt, using ∑t6/(π2t2)=1\sum_t6/(\pi^2t^2)=1, gives with probability at least 1−δ1-\delta:

  2. rt=f(x⋆)−f(xt)≤[μt−1(xt)+βt1/2σt−1(xt)]−[μt−1(xt)−βt1/2σt−1(xt)]=2βt1/2σt−1(xt)r_t=f(\mathbf x^\star)-f(\mathbf x_t)\le\big[\mu_{t-1}(\mathbf x_t)+\beta_t^{1/2}\sigma_{t-1}(\mathbf x_t)\big]-\big[\mu_{t-1}(\mathbf x_t)-\beta_t^{1/2}\sigma_{t-1}(\mathbf x_t)\big]=2\beta_t^{1/2}\sigma_{t-1}(\mathbf x_t)

    Regret per step. xt\mathbf x_t maximizes the upper bound, which at x⋆\mathbf x^\star is at least f(x⋆)f(\mathbf x^\star), while f(xt)f(\mathbf x_t) is at least the lower bound at xt\mathbf x_t:

  3. ∑t=1Trt2≤∑t=1T4βtσt−12(xt)≤4βTlog⁡(1+σn−2)∑t=1Tlog⁡(1+σn−2σt−12(xt))≤C1βTγT\sum_{t=1}^Tr_t^2\le\sum_{t=1}^T4\beta_t\sigma_{t-1}^2(\mathbf x_t)\le\frac{4\beta_T}{\log(1+\sigma_n^{-2})}\sum_{t=1}^T\log\big(1+\sigma_n^{-2}\sigma_{t-1}^2(\mathbf x_t)\big)\le C_1\beta_T\gamma_T

    Information. The information the chosen queries reveal is I(y1:T;f)=12∑tlog⁡(1+σn−2σt−12(xt))≤γTI(\mathbf y_{1:T};f)=\tfrac12\sum_t\log\big(1+\sigma_n^{-2}\sigma_{t-1}^2(\mathbf x_t)\big)\le\gamma_T. Since s=σn−2σt−12(xt)∈[0,σn−2]s=\sigma_n^{-2}\sigma_{t-1}^2(\mathbf x_t)\in[0,\sigma_n^{-2}] and s≤σn−2log⁡(1+σn−2)log⁡(1+s)s\le\frac{\sigma_n^{-2}}{\log(1+\sigma_n^{-2})}\log(1+s) on that interval, and βt\beta_t is nondecreasing:

  4. RT=∑t=1Trt≤T∑t=1Trt2≤C1TβTγTR_T=\sum_{t=1}^Tr_t\le\sqrt{T\sum_{t=1}^Tr_t^2}\le\sqrt{C_1T\beta_T\gamma_T}

    Cauchy–Schwarz finishes it. The bound is sublinear exactly when βTγT\beta_T\gamma_T grows slower than TT.

KernelMaximum information gain γT\gamma_TRegret lower bound (RKHS setting)
Linear, dd inputsO(dlog⁡T)\order{d\log T}—
Squared exponentialO((log⁡T)d+1)\order{(\log T)^{d+1}}Ω(T(log⁡T)d/2)\Omega\big(\sqrt{T(\log T)^{d/2}}\big)
Matérn-ν\nuO(Td/(2ν+d)log⁡2ν/(2ν+d)T)\order{T^{d/(2\nu+d)}\log^{2\nu/(2\nu+d)}T} (Vakili, Khezeli & Picheny, 2021)Ω(T(ν+d)/(2ν+d))\Omega\big(T^{(\nu+d)/(2\nu+d)}\big)
The information-gain rates for the linear and squared-exponential kernels are from Srinivas et al., whose original Matérn rate, O~(Td(d+1)/(2ν+d(d+1)))\tilde O\big(T^{d(d+1)/(2\nu+d(d+1))}\big), was later sharpened; the lower bounds, for ff in an RKHS ball, are from Scarlett, Bogunovic & Cevher (2017). Smooth kernels make the problem easy; rough kernels and high dimension make γT\gamma_T, and the regret, grow fast.
Check your understanding

What does a no-regret guarantee, RT/T→0R_T/T\to0, say about the optimization?

  1. The algorithm evaluates x⋆\mathbf x^\star exactly after finitely many steps
  2. The best value found so far approaches f(x⋆)f(\mathbf x^\star), since the smallest instantaneous regret is at most the average
  3. Every query after some time is within any ε\varepsilon of the optimum
  4. The posterior variance vanishes everywhere
Show the answer

B. The best value found so far approaches f(x⋆)f(\mathbf x^\star), since the smallest instantaneous regret is at most the average

min⁡t≤Trt≤1T∑trt=RT/T→0\min_{t\le T}r_t\le\frac1T\sum_tr_t=R_T/T\to0, so the best evaluated point is asymptotically optimal. It does not mean every late query is good (exploration continues), nor that x⋆\mathbf x^\star itself is hit.

Practice: hyperparameters, costs and dimension

The guarantees above assume a known kernel. In practice the hyperparameters are re-estimated as evaluations arrive, and that is where things can go wrong:

  • Estimated priors can break convergence. For noise-free evaluations, Bull (2011) gave convergence rates for expected improvement with a fixed GP prior on any function in the prior's RKHS (optimal for rough functions), but showed that when the prior parameters are re-estimated from the data by standard estimators the procedure may never find the optimum of some such functions; modified estimators keep the rates.
  • Too few points, degenerate fits. In the widget, fitting on the two starting evaluations alone gives a degenerate model (ℓ\ell at its floor of 0.1), after which EI wanders; fitting after a few queries usually works. Snoek, Larochelle & Adams (2012) sidestep point estimates by averaging the acquisition over hyperparameters drawn by MCMC, and use an ARD Matérn 5/2 kernel rather than the overly smooth RBF.

Other practicalities:

  • Cost and parallelism. Evaluations differ in cost, so Snoek et al. (2012) maximize expected improvement per second; with several machines, batch acquisitions choose many points at once (Thompson sampling parallelizes naturally). For hyperparameter tuning, multi-fidelity methods that stop bad configurations early, such as Hyperband, compete with and complement BO (Hyperparameter Optimization).
  • Dimension. A GP needs many points to model a function on a high-dimensional box, and γT\gamma_T grows with dd. Standard BO is usually applied to up to about 10–20 inputs. Beyond that, methods assume structure: a low effective dimension, exploited by searching a random low-dimensional embedding (Wang, Hutter, Zoghi, Matheson & de Freitas, 2016), an additive decomposition (Kandasamy, Schneider & Póczos, 2015), or local trust regions (Eriksson et al., 2019). Hvarfner, Hellsten & Nardi (2024) reported that simply scaling the length-scale prior with the dimension makes standard BO competitive in high dimensions; how far that observation extends is still being assessed as of the mid-2020s.

Key takeaways

  • Bayesian optimization fits a GP to all evaluations and queries the maximizer of a cheap acquisition function; it pays off when evaluations are expensive, gradient-free and few, in modest dimension.
  • EI=(μ−f+)Φ(Z)+σϕ(Z)\mathrm{EI}=(\mu-f^+)\Phi(Z)+\sigma\phi(Z) with Z=(μ−f+)/σZ=(\mu-f^+)/\sigma balances exploitation and exploration in closed form; PI is greedier, UCB optimistic, Thompson sampling randomized, entropy search information-greedy.
  • GP-UCB with βt=2log⁡(∣X∣t2π2/6δ)\beta_t=2\log(\abs{\mathcal X}t^2\pi^2/6\delta) has RT≤C1TβTγTR_T\le\sqrt{C_1T\beta_T\gamma_T} with probability at least 1−δ1-\delta; the proof chains a confidence band, rt≤2βt1/2σt−1(xt)r_t\le2\beta_t^{1/2}\sigma_{t-1}(\mathbf x_t), the information gain and Cauchy–Schwarz. γT\gamma_T is polylogarithmic for the RBF kernel and polynomial for Matérn kernels.
  • In practice: refit or average the hyperparameters (estimated priors can break EI's convergence, as Bull showed), account for costs and batches, and expect standard BO to struggle beyond roughly 10–20 dimensions unless structure or dimension-aware priors help.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

At a candidate, μ−f+=0.3\mu-f^+=0.3 and σ=0.3\sigma=0.3. Using Φ(1)≈0.841\Phi(1)\approx0.841 and ϕ(1)≈0.242\phi(1)\approx0.242, what is the expected improvement?

  1. about 0.30
  2. about 0.25
  3. about 0.07
  4. about 0.33
Show the answer

D. about 0.33

Z=1Z=1, so EI=0.3⋅0.841+0.3⋅0.242≈0.252+0.073=0.325\mathrm{EI}=0.3\cdot0.841+0.3\cdot0.242\approx0.252+0.073=0.325. EI exceeds the mean gain μ−f+=0.3\mu-f^+=0.3 because the improvement is truncated at zero: bad outcomes cost nothing, good ones count in full.

Question 2

What happens to GP-UCB if βt=0\beta_t=0?

  1. It queries the maximizer of the posterior mean, pure exploitation, and can lock onto a suboptimal region forever
  2. It becomes expected improvement
  3. It explores uniformly at random
  4. Nothing changes, because βt\beta_t only affects the regret bound
Show the answer

A. It queries the maximizer of the posterior mean, pure exploitation, and can lock onto a suboptimal region forever

With βt=0\beta_t=0 the rule maximizes μt−1\mu_{t-1} alone. Nothing rewards uncertainty, so a region where the model is pessimistic but wrong is never visited again; the confidence step of the regret proof fails. The widget's too-smooth model, stuck on the decoy hill, shows the same failure from a different cause.

Question 3

Why is the GP-UCB regret bound C1TβTγT\sqrt{C_1T\beta_T\gamma_T} sublinear for the squared-exponential kernel?

  1. Because βT\beta_T decreases with TT
  2. Because the posterior variance is zero after dd queries
  3. Because the squared-exponential kernel makes ff linear
  4. Because γT=O((log⁡T)d+1)\gamma_T=\order{(\log T)^{d+1}} grows only polylogarithmically, so TβTγT=o(T)\sqrt{T\beta_T\gamma_T}=o(T)
Show the answer

D. Because γT=O((log⁡T)d+1)\gamma_T=\order{(\log T)^{d+1}} grows only polylogarithmically, so TβTγT=o(T)\sqrt{T\beta_T\gamma_T}=o(T)

The bound is sublinear whenever βTγT=o(T)\beta_T\gamma_T=o(T). For a finite domain βT\beta_T grows like log⁡T\log T, and the smooth kernel's fast eigenvalue decay keeps γT\gamma_T polylogarithmic, so the regret is O~(T)\tilde O(\sqrt T) up to logarithmic factors.

Question 4

You plan to tune 200 continuous hyperparameters with 100 evaluations, using BO with an isotropic RBF kernel and default priors. What is the main concern?

  1. The acquisition function cannot be computed in 200 dimensions
  2. A GP cannot model a generic function of 200 inputs from 100 points, so the surrogate is nearly uninformative unless structure is assumed (low effective dimension, additivity) or the priors are adapted to the dimension
  3. Expected improvement is undefined when d>Td>T
  4. Gaussian processes only accept one-dimensional inputs
Show the answer

B. A GP cannot model a generic function of 200 inputs from 100 points, so the surrogate is nearly uninformative unless structure is assumed (low effective dimension, additivity) or the priors are adapted to the dimension

With far more inputs than evaluations, a generic smooth function is underdetermined, and the information gain grows quickly with dd. Practical high-dimensional BO bets on structure (random embeddings, additive models, trust regions) or, following Hvarfner et al. (2024), on length-scale priors scaled to the dimension.

Further reading

  • D. R. Jones, M. Schonlau & W. J. Welch, "Efficient global optimization of expensive black-box functions," Journal of Global Optimization 13(4), 455–492 (1998).
  • N. Srinivas, A. Krause, S. M. Kakade & M. W. Seeger, "Information-theoretic regret bounds for Gaussian process optimization in the bandit setting," IEEE Transactions on Information Theory 58(5), 3250–3265 (2012); conference version at ICML 2010.
  • J. Snoek, H. Larochelle & R. P. Adams, "Practical Bayesian optimization of machine learning algorithms," Advances in Neural Information Processing Systems 25 (2012).
  • P. I. Frazier, "A tutorial on Bayesian optimization," arXiv:1807.02811 (2018).
  • R. Garnett, Bayesian Optimization (Cambridge University Press, 2023).