<jupyter_start><jupyter_text>Batch Bayesian Optimization with Kriging BeliverThis notebook details the procedure for performing the batched Bayesian optimization using the [Kriging Beliver](http://www.cs.ubc.ca/labs/beta/EARG/stack/2010_CI_Ginsbourger-ParallelKriging.pdf) algorithm. In short, the Kriging Beliver algorithm selects $q$ query points by (1) selecting the best cannidiate according to the acquisition function, (2) appending the predicted mean and variance of the selected point to the training data, (3) refitting the GPR given the augmented data set, and (4) repeating steps 1-3 until $q$ points have been selected for querying.   Imports<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from active_learning.BO import BayesianOptimizer
from sklearn.gaussian_process.kernels import ConstantKernel, RBF<jupyter_output><empty_output><jupyter_text>Helper functions<jupyter_code>def f(x):
    """The function to predict."""
    return -2 * (6*x - 2)**2 * np.sin(12*x - 4)

def plot_results(X, y, dy, x, y_pred, sigma, new_p):
    plt.figure(figsize=(10,5))
    plt.plot(x, f(x), 'r:', label=r'$f(x) = x\,\sin(3x)$')
    plt.errorbar(X.ravel(), y, dy, fmt='r.', markersize=10, label='Observations')
    plt.plot(x, y_pred, 'b-', label='Prediction')
    plt.fill(np.concatenate([x, x[::-1]]),
             np.concatenate([y_pred - 1.9600 * sigma,
                            (y_pred + 1.9600 * sigma)[::-1]]),
             alpha=.5, fc='b', ec='None', label='95% confidence interval')
    plt.scatter(new_p, f(new_p), color='k', s=50, label='Query points')
    plt.xlabel('$x$')
    plt.ylabel('$f(x)$')
    plt.ylim(-40, 40)
    plt.legend(loc='upper left')<jupyter_output><empty_output><jupyter_text>Batched Bayesian Optimization Seed model N = 10 noisy observations<jupyter_code>N = 4 # Number of points to sample
X = np.random.sample(N)
X = np.atleast_2d(X).T
x = np.atleast_2d(np.linspace(0, 1, 1000)).T
# Observations and noise
y = f(X).ravel()
dy = 0.5 + 1.0 * np.random.random(y.shape)
noise = np.random.normal(0, dy)
y += noise<jupyter_output><empty_output><jupyter_text>Instatiate the BayesianOptimizer object<jupyter_code>kernel = ConstantKernel() * RBF()
BO = BayesianOptimizer(kernel=kernel)<jupyter_output><empty_output><jupyter_text>Fit the model given the observations. By default a vanilla RBF kernel is used. By default the model is fit internal through expecation maximization (i.e. maximizing the log marginal liklihood w.r.t the kernel hyperparamters).<jupyter_code>BO.fit(X, y, std=dy**2)<jupyter_output><empty_output><jupyter_text>Once the model is fit, we can predict on a pool of possible points `x`.<jupyter_code>y_pred, sigma = BO.predict(x, return_std=True)<jupyter_output><empty_output><jupyter_text>We can now call `BO.query` to query our current model state and select `q = 5` points for sampling. There points are selected using the Kriging Beliver algorithm by default. Set the `epsilon` value in the EI acquisition to `0.1` to help better encourge exploration.<jupyter_code>new_p = BO.query(x, q = 3, epsilon=0.1)
new_p<jupyter_output><empty_output><jupyter_text>We can now plot the results. The dotted red line is the ground truth function we're aiming to fit, the solid blue line is the GPR preduction with assocaited 95% confidence interval, the red points are the observations used to fit the GPR, and the black points are the points selected to be querred next.   Iteration 1<jupyter_code>X = BO.X
y = BO.y
dy = BO.sigma
plot_results(X, y, dy, x, y_pred, sigma, new_p)<jupyter_output><empty_output><jupyter_text>Notice how a diversity of points throughout the space are selected as a result of this batched selection.<jupyter_code>noise_new = 0.5 + 1.0 * np.random.random(new_p.shape)
BO.teach(new_p, (f(new_p) + noise_new).flatten(), noise_new.flatten()**2)
y_pred, sigma = BO.predict(x)
new_p = BO.query(x, q = 3, epsilon=0.1)<jupyter_output><empty_output><jupyter_text>Iteration 2<jupyter_code>X = BO.X
y = BO.y
dy = BO.sigma
plot_results(X, y, dy, x, y_pred, sigma, new_p)
noise_new = 0.5 + 1.0 * np.random.random(new_p.shape)
BO.teach(new_p, (f(new_p) + noise_new).flatten(), noise_new.flatten()**2)
y_pred, sigma = BO.predict(x)
new_p = BO.query(x, q = 3, epsilon=0.1)<jupyter_output><empty_output><jupyter_text>Iteration 3<jupyter_code>X = BO.X
y = BO.y
dy = BO.sigma
plot_results(X, y, dy, x, y_pred, sigma, new_p)
noise_new = 0.5 + 1.0 * np.random.random(new_p.shape)
BO.teach(new_p, (f(new_p) + noise_new).flatten(), noise_new.flatten()**2)
y_pred, sigma = BO.predict(x)
new_p = BO.query(x, q = 3, epsilon=0.1)<jupyter_output><empty_output><jupyter_text>Iteration 4<jupyter_code>X = BO.X
y = BO.y
dy = BO.sigma
plot_results(X, y, dy, x, y_pred, sigma, new_p)
noise_new = 0.5 + 1.0 * np.random.random(new_p.shape)
BO.teach(new_p, (f(new_p) + noise_new).flatten(), noise_new.flatten()**2)
y_pred, sigma = BO.predict(x)
new_p = BO.query(x, q = 3, epsilon=0.1)<jupyter_output><empty_output><jupyter_text>Iteration 5<jupyter_code>X = BO.X
y = BO.y
dy = BO.sigma
plot_results(X, y, dy, x, y_pred, sigma, new_p)
noise_new = 0.5 + 1.0 * np.random.random(new_p.shape)
BO.teach(new_p, (f(new_p) + noise_new).flatten(), noise_new.flatten()**2)
y_pred, sigma = BO.predict(x)
new_p = BO.query(x, q = 3, epsilon=0.1)<jupyter_output><empty_output><jupyter_text>Iteration 6<jupyter_code>X = BO.X
y = BO.y
dy = BO.sigma
plot_results(X, y, dy, x, y_pred, sigma, new_p)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup