== Listing 3.1: the GP posterior computed three ways ==
observed inputs: 0.02 0.04 0.27 0.61 0.64 0.73 0.81 0.91
max |Cholesky - explicit inverse| posterior mean: 4.15e-14
max |Cholesky - block conditioning| posterior mean: 1.33e-15
max |gp_posterior function - GP class| posterior mean: 0.00e+00
x      truth   post.mean  post.sd
0.15    0.809     0.677    0.153
0.50    0.000     0.021    0.181
0.90   -0.588    -0.627    0.044
noise variance 1e-10: max |mean - y| at the data = 1.24e-06
60 grid points, ls=0.3: cond(K) = 3.5e+09
relative residual |K a - y|/|y|: explicit inverse 6.5e-08, Cholesky solve 8.6e-13

== Listing 3.2: marginal likelihood and hyperparameter fitting ==
rbf       log marginal likelihood   -7.652, max |analytic - finite difference| gradient error 3.3e-09
matern52  log marginal likelihood   -8.885, max |analytic - finite difference| gradient error 5.6e-09
true hyperparameters: ls=0.3, signal var=1.5, noise var=0.01; 20 datasets per n, median (inter-quartile range)
n= 15  ls 0.285 (0.235, 0.358)   var 0.99 (0.59, 1.37)   noise 0.0098 (0.0062, 0.0119)
n= 40  ls 0.277 (0.260, 0.309)   var 1.32 (1.03, 1.83)   noise 0.0101 (0.0082, 0.0111)
n=120  ls 0.290 (0.235, 0.334)   var 1.10 (0.76, 1.80)   noise 0.0100 (0.0096, 0.0109)
profile over the length scale (signal var and noise at their true values), one dataset with n=40:
  ls=0.03  log marginal likelihood   -21.78
  ls=0.10  log marginal likelihood     4.35
  ls=0.20  log marginal likelihood    14.37
  ls=0.30  log marginal likelihood    15.02
  ls=0.50  log marginal likelihood     2.59
  ls=1.00  log marginal likelihood  -169.92
  ls=3.00  log marginal likelihood -1579.94
  best grid value at ls=0.3

== Listing 3.3: expected improvement in closed form ==
incumbent best = 0.2; 2,000,000 Monte Carlo draws per row
   mu   sigma |   EI closed   EI Monte Carlo (se) |   PI closed   PI Monte Carlo
 0.00   1.00 |    0.30689      0.30703 (0.00036) |    0.42074      0.42119
 0.30   0.10 |    0.10833      0.10844 (0.00006) |    0.84134      0.84197
 0.20   0.50 |    0.19947      0.19974 (0.00021) |    0.50000      0.50020
-0.50   0.30 |    0.00100      0.00100 (0.00001) |    0.00982      0.00990
 0.60   0.05 |    0.40000      0.39999 (0.00004) |    1.00000      1.00000
two candidates: A (mu=0.0, sd=1.0, exploratory) and B (mu=0.3, sd=0.1, exploiting), incumbent 0.2
  A: EI=0.3069  PI=0.4207  UCB(beta=4)=2.000
  B: EI=0.1083  PI=0.8413  UCB(beta=4)=0.500
EI with sd -> 0 recovers the greedy gain max(mu - best, 0):
  mu=0.9: EI(sd=1e-9) = 0.7000; mu=-0.4: EI(sd=1e-9) = 0.0e+00

== Listing 3.4: acquisition rules on 1-D problems with a decoy ==
-- Problem A (designed after B): global peak with standard deviation 0.06; 20 seeds, 3 initial points, 12 iterations; global max 1.0; success = final simple regret < 0.05
method             mean simple regret [95% bootstrap CI]    success rate
random             0.075 [0.041, 0.111]                     0.50
PI xi=0            0.125 [0.082, 0.167]                     0.40
PI xi=0.5          0.008 [0.005, 0.012]                     1.00
EI                 0.085 [0.045, 0.128]                     0.55
UCB beta=1         0.120 [0.080, 0.160]                     0.40
UCB beta=4         0.120 [0.080, 0.160]                     0.40
UCB beta=16        0.071 [0.031, 0.110]                     0.65
Thompson           0.070 [0.030, 0.110]                     0.65
EI, fixed ls=0.02  0.174 [0.115, 0.229]                     0.30
EI, fixed ls=1.0   0.142 [0.102, 0.180]                     0.30
-- Problem B (the first problem I tried, kept because it is informative): a needle, standard deviation 0.025; 20 seeds, 3 initial points, 12 iterations; global max 1.0; success = final simple regret < 0.05
method             mean simple regret [95% bootstrap CI]    success rate
random             0.140 [0.100, 0.183]                     0.30
EI                 0.170 [0.140, 0.200]                     0.15
UCB beta=4         0.170 [0.141, 0.200]                     0.15

== Listing 3.5: GP-UCB regret on a finite domain against Theorem 3.5 ==
|D|=100 grid points, RBF ls=0.15, noise variance 0.01, T=60, delta=0.1, 30 functions drawn from the prior
runs where the confidence event |f - mu| <= sqrt(beta_t) sigma failed, beta_t of Theorem 3.5: 0 of 30 (allowed share 0.1)
runs where it failed with constant beta=2: 30 of 30; violating (x,t) pairs: 27944 of 180000 (0.1552)
runs satisfying R_T <= 2 sum_t sqrt(beta_t) sigma_(t-1)(x_t): 30 of 30
runs satisfying sum_t sigma^2_(t-1)(x_t) <= c * I(y_T; f_T), c = 2/log(1+1/noise) = 0.433: 30 of 30
T    mean cumulative regret GP-UCB [95% CI]   random sampling   gamma_T upper bound   bound sqrt(4 c T beta_T gamma_T)
 10  7.76 [7.01, 8.55]                12.24          27.38                106.8
 30  8.92 [8.18, 9.68]                35.64          35.22                228.1
 60  9.62 [8.92, 10.37]               70.72          40.61                362.9
for scale: T * mean(max f - min f) = 143 at T=60 (mean range of the drawn functions 2.38); this trivial bound holds for any rule

== Listing 3.6: Thompson sampling is probability matching ==
two candidates, exact P(f_a > f_b) = 0.5096; Monte Carlo from joint posterior samples = 0.5105
candidate x:                  0.0     0.2     0.4     0.6     0.8     1.0
posterior mean:              0.20    0.64    0.89    0.62    0.07   -0.30
posterior sd:                0.10    0.31    0.10    0.49    0.51    0.10
P(x is the maximizer):      0.000   0.206   0.506   0.287   0.001   0.000
Thompson selection share:   0.000   0.200   0.513   0.286   0.002   0.000
total variation distance 0.0074; the standard error of one share is at most 0.0035

== Listing 3.7: sequential BO against random search on two synthetic benchmarks ==
Branin-like on [0,1]^2: f* = -0.0540; Hartmann-like on [0,1]^6: f* = 1.3178
local maxima of Branin-like on a 201x201 grid (x1, x2, value): (1.00, 0.12, -1.5378); (0.68, 0.17, -0.0542); (0.15, 0.96, -0.0541)
-- Branin-like: 6 initial points + 24 iterations (30 evaluations), 30 seeds; simple regret after 15 and 30 evaluations
method       regret@15  mean [95% CI]     regret@30  mean [95% CI]     paired diff vs random @end [95% CI]
random       0.309 [0.192, 0.437]       0.154 [0.110, 0.203]       -
EI           0.214 [0.136, 0.304]       0.000 [0.000, 0.001]       -0.154 [-0.201, -0.110]
PI xi=0.01   0.262 [0.141, 0.413]       0.003 [0.002, 0.004]       -0.152 [-0.199, -0.108]
UCB beta=4   0.291 [0.148, 0.471]       0.001 [0.000, 0.004]       -0.153 [-0.204, -0.109]
Thompson     0.178 [0.106, 0.261]       0.000 [0.000, 0.001]       -0.154 [-0.203, -0.108]
-- Hartmann-like (PI omitted to save time): 12 initial points + 48 iterations (60 evaluations), 16 seeds; simple regret after 30 and 60 evaluations
method       regret@30  mean [95% CI]     regret@60  mean [95% CI]     paired diff vs random @end [95% CI]
random       0.748 [0.644, 0.848]       0.667 [0.586, 0.744]       -
EI           0.478 [0.361, 0.590]       0.257 [0.177, 0.339]       -0.410 [-0.517, -0.303]
UCB beta=4   0.477 [0.367, 0.586]       0.181 [0.111, 0.250]       -0.486 [-0.592, -0.380]
Thompson     0.507 [0.397, 0.620]       0.200 [0.140, 0.258]       -0.467 [-0.567, -0.362]

== Listing 3.8: greedy batch selection and the (1 - 1/e) guarantee ==
ground sets of 22 random points in [0,1]^2, RBF ls=0.25, noise variance 0.1; optimum by exhaustive search
b   worst ratio greedy/optimal (5 ground sets)   mean ratio   mean random-set / optimal   1-1/e
2   0.9999                                  1.0000       0.9615                    0.6321
3   0.9998                                  0.9999       0.9271                    0.6321
4   0.9974                                  0.9988       0.8991                    0.6321
diminishing-returns violations in 10000 random (A subset of B, x) checks: 0

== Listing 3.9: batch acquisition (batch of 5) on the Hartmann-like function ==
12 initial points, 6 rounds of 5 evaluations (42 in total), 16 seeds, f* = 1.3178
method                      regret after 3 rounds [95% CI]   regret after 6 rounds [95% CI]
random batch                0.781 [0.694, 0.868]            0.753 [0.663, 0.846]
kriging believer            0.583 [0.476, 0.692]            0.264 [0.178, 0.358]
greedy variance             0.858 [0.761, 0.959]            0.819 [0.724, 0.896]
independent Thompson        0.616 [0.518, 0.712]            0.308 [0.226, 0.395]
sequential EI (30 rounds)   0.522 [0.412, 0.629]            0.323 [0.240, 0.417]
paired difference kriging believer - random batch at 6 rounds: -0.489 [-0.540, -0.433]
paired difference kriging believer - sequential EI at equal evaluations: -0.059 [-0.119, -0.005]

== Listing 3.10: two-fidelity Bayesian optimization ==
correlation of cheap and expensive function on random inputs: 0.985
hypothetical costs: cheap 1.0, expensive 5.0; budget 60; 20 seeds; f* = -0.0540
both start with 4 expensive points; multi-fidelity also gets 10 cheap points (cost 10 of the 60)
method              mean regret [95% CI] at cost 30   at cost 45               at cost 60              median at 30 / 45 / 60
single fidelity EI  1.642 [0.811, 2.692]      0.485 [0.247, 0.742]      0.325 [0.123, 0.556]      0.581 / 0.271 / 0.037
two-fidelity        1.031 [0.742, 1.368]      0.507 [0.302, 0.749]      0.190 [0.098, 0.290]      1.094 / 0.306 / 0.081
paired difference two-fidelity - single at cost 60: -0.135 [-0.374, 0.044]
share of two-fidelity queries (after the initial design) spent on the cheap function: 0.41; mean fitted rho at the end: 0.77

== Listing 3.11: constrained Bayesian optimization ==
constraint: inside a disk of radius 0.2 around (0.4, 0.5), 12.5% of the square
unconstrained optimum -0.0540 (infeasible); constrained optimum (grid search polished by SLSQP) -0.7499
8 initial points + 30 iterations, 30 seeds
method              runs with a feasible point   share of evaluations feasible   mean regret of best feasible (those runs) [95% CI]
random               30 of 30                    0.123                           0.512 [0.409, 0.619]
EI (ignores c)       28 of 30                    0.054                           0.624 [0.483, 0.769]
constrained EI       30 of 30                    0.596                           0.001 [0.000, 0.001]

== Listing 3.12: screening a synthetic molecule library ==
library of 1500 synthetic molecules with 8 descriptors; oracle noise sd 0.05; best true value 2.352, 15th best 1.974, median 0.287
10 random initial molecules + 40 queries (batch methods: 8 rounds of 5), 16 seeds
method              regret of best queried [95% CI]   top-15 molecules found [95% CI]   best-queried rank (median)
random                0.598 [0.407, 0.771]              0.50 [0.12, 0.94]                 28
EI                    0.032 [0.000, 0.091]              9.44 [7.69, 10.88]                1
UCB beta=4            0.000 [0.000, 0.000]              12.50 [11.19, 13.62]              1
Thompson              0.060 [0.003, 0.131]              8.56 [5.94, 11.00]                1
kriging believer b=5  0.111 [0.034, 0.210]              5.12 [3.44, 6.88]                 2

== Listing 3.13: active learning (variance reduction) versus random labelling ==
hyperparameters (standardized outputs) fitted on a separate set of 150 labelled molecules: ls = [0.61, 0.73, 0.52, 2.04, 5.38, 2.56, 2.65, 1.47], var 2.52, noise 0.0029
RMSE of the posterior mean over all 1500 molecules (true property units; sd of the property = 0.428); 16 seeds
labels   random [95% CI]            greedy variance [95% CI]   EI-chosen (from BO)   gain I(y_A;f_A) greedy / random
  10     0.343 [0.314, 0.375]       0.410 [0.404, 0.416]       0.338                 1.12
  20     0.263 [0.242, 0.284]       0.225 [0.222, 0.227]       0.283                 1.14
  40     0.194 [0.183, 0.208]       0.177 [0.175, 0.180]       0.209                 1.15
  80     0.132 [0.126, 0.137]       0.136 [0.134, 0.137]       0.137                 1.19
