import numpy as np
from math import inf
from spotPython.fun.objectivefunctions import analytical
from spotPython.spot import spot
from scipy.optimize import shgo
from scipy.optimize import direct
from scipy.optimize import differential_evolution
import matplotlib.pyplot as plt
9 Handling Noise: Optimal Computational Budget Allocation in Spot
This notebook demonstrates how noisy functions can be handled with OCBA by Spot
.
9.1 Example: Spot
, OCBA, and the Noisy Sphere Function
9.1.1 The Objective Function: Noisy Sphere
The spotPython
package provides several classes of objective functions. We will use an analytical objective function with noise, i.e., a function that can be described by a (closed) formula: \[f(x) = x^2 + \epsilon\]
Since sigma
is set to 0.1
, noise is added to the function:
= analytical().fun_sphere
fun = {"sigma": 0.1,
fun_control "seed": 123}
A plot illustrates the noise:
= np.linspace(-1,1,100).reshape(-1,1)
x = fun(x, fun_control=fun_control)
y
plt.figure()"k")
plt.plot(x,y, plt.show()
Spot
is adopted as follows to cope with noisy functions:
fun_repeats
is set to a value larger than 1 (here: 2)noise
is set totrue
. Therefore, a nugget (Lambda
) term is added to the correlation matrixinit size
(of thedesign_control
dictionary) is set to a value larger than 1 (here: 2)
= spot.Spot(fun=fun,
spot_1_noisy = np.array([-1]),
lower = np.array([1]),
upper = 50,
fun_evals = 2,
fun_repeats ="ei",
infill_criterion= True,
noise =0.0,
tolerance_x= 1,
ocba_delta =123,
seed=True,
show_models= fun_control,
fun_control ={"init_size": 3,
design_control"repeats": 2},
={"noise": True}) surrogate_control
spot_1_noisy.run()
<spotPython.spot.spot.Spot at 0x1379e7bb0>
9.2 Print the Results
spot_1_noisy.print_results()
min y: -0.08106318979661208
x0: 0.1335999447536301
min mean y: -0.06294830660588041
x0: 0.1335999447536301
[['x0', 0.1335999447536301], ['x0', 0.1335999447536301]]
=False) spot_1_noisy.plot_progress(log_y
9.3 Noise and Surrogates: The Nugget Effect
9.3.1 The Noisy Sphere
9.3.1.1 The Data
We prepare some data first:
import numpy as np
import spotPython
from spotPython.fun.objectivefunctions import analytical
from spotPython.spot import spot
from spotPython.design.spacefilling import spacefilling
from spotPython.build.kriging import Kriging
import matplotlib.pyplot as plt
= spacefilling(1)
gen = np.random.RandomState(1)
rng = np.array([-10])
lower = np.array([10])
upper = analytical().fun_sphere
fun = {"sigma": 2,
fun_control "seed": 125}
= gen.scipy_lhd(10, lower=lower, upper = upper)
X = fun(X, fun_control=fun_control)
y = X.reshape(-1,1)
X_train = y y_train
A surrogate without nugget is fitted to these data:
= Kriging(name='kriging',
S =123,
seed=50,
log_level=1,
n_theta=False)
noise
S.fit(X_train, y_train)
= np.linspace(start=-13, stop=13, num=1000).reshape(-1, 1)
X_axis = S.predict(X_axis, return_val="all")
mean_prediction, std_prediction, ei
="Observations")
plt.scatter(X_train, y_train, label="mue")
plt.plot(X_axis, mean_prediction, label
plt.legend()"$x$")
plt.xlabel("$f(x)$")
plt.ylabel(= plt.title("Sphere: Gaussian process regression on noisy dataset") _
In comparison to the surrogate without nugget, we fit a surrogate with nugget to the data:
= Kriging(name='kriging',
S_nug =123,
seed=50,
log_level=1,
n_theta=True)
noise
S_nug.fit(X_train, y_train)= np.linspace(start=-13, stop=13, num=1000).reshape(-1, 1)
X_axis = S_nug.predict(X_axis, return_val="all")
mean_prediction, std_prediction, ei ="Observations")
plt.scatter(X_train, y_train, label="mue")
plt.plot(X_axis, mean_prediction, label
plt.legend()"$x$")
plt.xlabel("$f(x)$")
plt.ylabel(= plt.title("Sphere: Gaussian process regression with nugget on noisy dataset") _
The value of the nugget term can be extracted from the model as follows:
S.Lambda
S_nug.Lambda
9.088150066416743e-05
We see:
- the first model
S
has no nugget, - whereas the second model has a nugget value (
Lambda
) larger than zero.
9.4 Exercises
9.4.1 Noisy fun_cubed
Analyse the effect of noise on the fun_cubed
function with the following settings:
= analytical().fun_cubed
fun = {"sigma": 10,
fun_control "seed": 123}
= np.array([-10])
lower = np.array([10]) upper
9.4.2 fun_runge
Analyse the effect of noise on the fun_runge
function with the following settings:
= np.array([-10])
lower = np.array([10])
upper = analytical().fun_runge
fun = {"sigma": 0.25,
fun_control "seed": 123}
9.4.3 fun_forrester
Analyse the effect of noise on the fun_forrester
function with the following settings:
= np.array([0])
lower = np.array([1])
upper = analytical().fun_forrester
fun = {"sigma": 5,
fun_control "seed": 123}
9.4.4 fun_xsin
Analyse the effect of noise on the fun_xsin
function with the following settings:
= np.array([-1.])
lower = np.array([1.])
upper = analytical().fun_xsin
fun = {"sigma": 0.5,
fun_control "seed": 123}
0] spot_1_noisy.mean_y.shape[
18