Top

# skopt.acquisition module

import numpy as np
import warnings

from scipy.stats import norm

def gaussian_acquisition_1D(X, model, y_opt=None, acq_func="LCB",
"""
A wrapper around the acquisition function that is called by fmin_l_bfgs_b.

This is because lbfgs allows only 1-D input.
"""
return _gaussian_acquisition(np.expand_dims(X, axis=0),
model, y_opt, acq_func=acq_func,
acq_func_kwargs=acq_func_kwargs,

def _gaussian_acquisition(X, model, y_opt=None, acq_func="LCB",
"""
Wrapper so that the output of this function can be
directly passed to a minimizer.
"""
# Check inputs
X = np.asarray(X)
if X.ndim != 2:
raise ValueError("X is {}-dimensional, however,"
" it must be 2-dimensional.".format(X.ndim))

if acq_func_kwargs is None:
acq_func_kwargs = dict()
xi = acq_func_kwargs.get("xi", 0.01)
kappa = acq_func_kwargs.get("kappa", 1.96)

# Evaluate acquisition function
per_second = acq_func.endswith("ps")
if per_second:
model, time_model = model.estimators_

if acq_func == "LCB":
else:

elif acq_func in ["EI", "PI", "EIps", "PIps"]:
if acq_func in ["EI", "EIps"]:
else:

else:

if acq_func in ["EIps", "PIps"]:

else:
mu, std = time_model.predict(X, return_std=True)

# acq = acq / E(t)
inv_t = np.exp(-mu + 0.5*std**2)
acq_vals *= inv_t

# grad = d(acq_func) * inv_t + (acq_vals *d(inv_t))
# inv_t = exp(g)
# d(inv_t) = inv_t * grad(g)

else:
raise ValueError("Acquisition function not implemented.")

return acq_vals

"""
Use the lower confidence bound to estimate the acquisition
values.

The trade-off between exploitation and exploration is left to
be controlled by the user through the parameter kappa.

Parameters
----------
* X [array-like, shape=(n_samples, n_features)]:
Values where the acquisition function should be computed.

* model [sklearn estimator that implements predict with return_std]:
The fit estimator that approximates the function through the
method predict.
It should have a return_std parameter that returns the standard
deviation.

* kappa: [float, default 1.96 or 'inf']:
Controls how much of the variance in the predicted values should be
taken into account. If set to be very high, then we are favouring
exploration over exploitation and vice versa.
If set to 'inf', the acquisition function will only use the variance
which is useful in a pure exploration setting.
Useless if method is set to "LCB".

* return_grad: [boolean, optional]:
Whether or not to return the grad. Implemented only for the case where
X is a single sample.

Returns
-------
* values: [array-like, shape=(X.shape[0],)]:
Acquisition function values computed at X.

* grad: [array-like, shape=(n_samples, n_features)]:
"""
# Compute posterior.
with warnings.catch_warnings():
warnings.simplefilter("ignore")

if kappa == "inf":

else:
mu, std = model.predict(X, return_std=True)
if kappa == "inf":
return -std
return mu - kappa * std

def gaussian_pi(X, model, y_opt=0.0, xi=0.01, return_grad=False):
"""
Use the probability of improvement to calculate the acquisition values.

The conditional probability P(y=f(x) | x)form a gaussian with a
certain mean and standard deviation approximated by the model.

The PI condition is derived by computing E[u(f(x))]
where u(f(x)) = 1, if f(x) < y_opt and u(f(x)) = 0,
iff(x) > y_opt.

This means that the PI condition does not care about how "better" the
predictions are than the previous values, since it gives an equal reward
to all of them.

Note that the value returned by this function should be maximized to
obtain the X with maximum improvement.

Parameters
----------
* X [array-like, shape=(n_samples, n_features)]:
Values where the acquisition function should be computed.

* model [sklearn estimator that implements predict with return_std]:
The fit estimator that approximates the function through the
method predict.
It should have a return_std parameter that returns the standard
deviation.

* y_opt [float, default 0]:
Previous minimum value which we would like to improve upon.

* xi: [float, default=0.01]:
Controls how much improvement one wants over the previous best
values. Useful only when method is set to "EI"

* return_grad: [boolean, optional]:
Whether or not to return the grad. Implemented only for the case where
X is a single sample.

Returns
-------
* values: [array-like, shape=(X.shape[0],)]:
Acquisition function values computed at X.
"""
with warnings.catch_warnings():
warnings.simplefilter("ignore")

else:
mu, std = model.predict(X, return_std=True)

values = np.zeros_like(mu)
improve = y_opt - xi - mu[mask]

# Substitute (y_opt - xi - mu) / sigma = t and apply chain rule.

return values

def gaussian_ei(X, model, y_opt=0.0, xi=0.01, return_grad=False):
"""
Use the expected improvement to calculate the acquisition values.

The conditional probability P(y=f(x) | x)form a gaussian with a certain
mean and standard deviation approximated by the model.

The EI condition is derived by computing E[u(f(x))]
where u(f(x)) = 0, if f(x) > y_opt and u(f(x)) = y_opt - f(x),
iff(x) < y_opt.

This solves one of the issues of the PI condition by giving a reward
proportional to the amount of improvement got.

Note that the value returned by this function should be maximized to
obtain the X with maximum improvement.

Parameters
----------
* X [array-like, shape=(n_samples, n_features)]:
Values where the acquisition function should be computed.

* model [sklearn estimator that implements predict with return_std]:
The fit estimator that approximates the function through the
method predict.
It should have a return_std parameter that returns the standard
deviation.

* y_opt [float, default 0]:
Previous minimum value which we would like to improve upon.

* xi: [float, default=0.01]:
Controls how much improvement one wants over the previous best
values. Useful only when method is set to "EI"

* return_grad: [boolean, optional]:
Whether or not to return the grad. Implemented only for the case where
X is a single sample.

Returns
-------
* values: [array-like, shape=(X.shape[0],)]:
Acquisition function values computed at X.
"""
with warnings.catch_warnings():
warnings.simplefilter("ignore")

else:
mu, std = model.predict(X, return_std=True)

values = np.zeros_like(mu)
improve = y_opt - xi - mu[mask]
cdf = norm.cdf(scaled)
pdf = norm.pdf(scaled)
exploit = improve * cdf

# Substitute (y_opt - xi - mu) / sigma = t and apply chain rule.

return values


## Functions

def gaussian_acquisition_1D(

X, model, y_opt=None, acq_func='LCB', acq_func_kwargs=None, return_grad=True)

A wrapper around the acquisition function that is called by fmin_l_bfgs_b.

This is because lbfgs allows only 1-D input.

def gaussian_acquisition_1D(X, model, y_opt=None, acq_func="LCB",
"""
A wrapper around the acquisition function that is called by fmin_l_bfgs_b.

This is because lbfgs allows only 1-D input.
"""
return _gaussian_acquisition(np.expand_dims(X, axis=0),
model, y_opt, acq_func=acq_func,
acq_func_kwargs=acq_func_kwargs,


def gaussian_ei(

Use the expected improvement to calculate the acquisition values.

The conditional probability P(y=f(x) | x)form a gaussian with a certain mean and standard deviation approximated by the model.

The EI condition is derived by computing E[u(f(x))] where u(f(x)) = 0, if f(x) > y_opt and u(f(x)) = y_opt - f(x), iff(x) < y_opt.

This solves one of the issues of the PI condition by giving a reward proportional to the amount of improvement got.

Note that the value returned by this function should be maximized to obtain the X with maximum improvement.

## Parameters

• X [array-like, shape=(n_samples, n_features)]: Values where the acquisition function should be computed.

• model [sklearn estimator that implements predict with return_std]: The fit estimator that approximates the function through the method predict. It should have a return_std parameter that returns the standard deviation.

• y_opt [float, default 0]: Previous minimum value which we would like to improve upon.

• xi: [float, default=0.01]: Controls how much improvement one wants over the previous best values. Useful only when method is set to "EI"

• return_grad: [boolean, optional]: Whether or not to return the grad. Implemented only for the case where X is a single sample.

## Returns

• values: [array-like, shape=(X.shape[0],)]: Acquisition function values computed at X.
def gaussian_ei(X, model, y_opt=0.0, xi=0.01, return_grad=False):
"""
Use the expected improvement to calculate the acquisition values.

The conditional probability P(y=f(x) | x)form a gaussian with a certain
mean and standard deviation approximated by the model.

The EI condition is derived by computing E[u(f(x))]
where u(f(x)) = 0, if f(x) > y_opt and u(f(x)) = y_opt - f(x),
iff(x) < y_opt.

This solves one of the issues of the PI condition by giving a reward
proportional to the amount of improvement got.

Note that the value returned by this function should be maximized to
obtain the X with maximum improvement.

Parameters
----------
* X [array-like, shape=(n_samples, n_features)]:
Values where the acquisition function should be computed.

* model [sklearn estimator that implements predict with return_std]:
The fit estimator that approximates the function through the
method predict.
It should have a return_std parameter that returns the standard
deviation.

* y_opt [float, default 0]:
Previous minimum value which we would like to improve upon.

* xi: [float, default=0.01]:
Controls how much improvement one wants over the previous best
values. Useful only when method is set to "EI"

* return_grad: [boolean, optional]:
Whether or not to return the grad. Implemented only for the case where
X is a single sample.

Returns
-------
* values: [array-like, shape=(X.shape[0],)]:
Acquisition function values computed at X.
"""
with warnings.catch_warnings():
warnings.simplefilter("ignore")

else:
mu, std = model.predict(X, return_std=True)

values = np.zeros_like(mu)
improve = y_opt - xi - mu[mask]
cdf = norm.cdf(scaled)
pdf = norm.pdf(scaled)
exploit = improve * cdf

# Substitute (y_opt - xi - mu) / sigma = t and apply chain rule.

return values


def gaussian_lcb(

Use the lower confidence bound to estimate the acquisition values.

The trade-off between exploitation and exploration is left to be controlled by the user through the parameter kappa.

## Parameters

• X [array-like, shape=(n_samples, n_features)]: Values where the acquisition function should be computed.

• model [sklearn estimator that implements predict with return_std]: The fit estimator that approximates the function through the method predict. It should have a return_std parameter that returns the standard deviation.

• kappa: [float, default 1.96 or 'inf']: Controls how much of the variance in the predicted values should be taken into account. If set to be very high, then we are favouring exploration over exploitation and vice versa. If set to 'inf', the acquisition function will only use the variance which is useful in a pure exploration setting. Useless if method is set to "LCB".

• return_grad: [boolean, optional]: Whether or not to return the grad. Implemented only for the case where X is a single sample.

## Returns

• values: [array-like, shape=(X.shape[0],)]: Acquisition function values computed at X.

• grad: [array-like, shape=(n_samples, n_features)]: Gradient at X.

def gaussian_lcb(X, model, kappa=1.96, return_grad=False):
"""
Use the lower confidence bound to estimate the acquisition
values.

The trade-off between exploitation and exploration is left to
be controlled by the user through the parameter kappa.

Parameters
----------
* X [array-like, shape=(n_samples, n_features)]:
Values where the acquisition function should be computed.

* model [sklearn estimator that implements predict with return_std]:
The fit estimator that approximates the function through the
method predict.
It should have a return_std parameter that returns the standard
deviation.

* kappa: [float, default 1.96 or 'inf']:
Controls how much of the variance in the predicted values should be
taken into account. If set to be very high, then we are favouring
exploration over exploitation and vice versa.
If set to 'inf', the acquisition function will only use the variance
which is useful in a pure exploration setting.
Useless if method is set to "LCB".

* return_grad: [boolean, optional]:
Whether or not to return the grad. Implemented only for the case where
X is a single sample.

Returns
-------
* values: [array-like, shape=(X.shape[0],)]:
Acquisition function values computed at X.

* grad: [array-like, shape=(n_samples, n_features)]:
"""
# Compute posterior.
with warnings.catch_warnings():
warnings.simplefilter("ignore")

if kappa == "inf":

else:
mu, std = model.predict(X, return_std=True)
if kappa == "inf":
return -std
return mu - kappa * std


def gaussian_pi(

Use the probability of improvement to calculate the acquisition values.

The conditional probability P(y=f(x) | x)form a gaussian with a certain mean and standard deviation approximated by the model.

The PI condition is derived by computing E[u(f(x))] where u(f(x)) = 1, if f(x) < y_opt and u(f(x)) = 0, iff(x) > y_opt.

This means that the PI condition does not care about how "better" the predictions are than the previous values, since it gives an equal reward to all of them.

Note that the value returned by this function should be maximized to obtain the X with maximum improvement.

## Parameters

• X [array-like, shape=(n_samples, n_features)]: Values where the acquisition function should be computed.

• model [sklearn estimator that implements predict with return_std]: The fit estimator that approximates the function through the method predict. It should have a return_std parameter that returns the standard deviation.

• y_opt [float, default 0]: Previous minimum value which we would like to improve upon.

• xi: [float, default=0.01]: Controls how much improvement one wants over the previous best values. Useful only when method is set to "EI"

• return_grad: [boolean, optional]: Whether or not to return the grad. Implemented only for the case where X is a single sample.

## Returns

• values: [array-like, shape=(X.shape[0],)]: Acquisition function values computed at X.
def gaussian_pi(X, model, y_opt=0.0, xi=0.01, return_grad=False):
"""
Use the probability of improvement to calculate the acquisition values.

The conditional probability P(y=f(x) | x)form a gaussian with a
certain mean and standard deviation approximated by the model.

The PI condition is derived by computing E[u(f(x))]
where u(f(x)) = 1, if f(x) < y_opt and u(f(x)) = 0,
iff(x) > y_opt.

This means that the PI condition does not care about how "better" the
predictions are than the previous values, since it gives an equal reward
to all of them.

Note that the value returned by this function should be maximized to
obtain the X with maximum improvement.

Parameters
----------
* X [array-like, shape=(n_samples, n_features)]:
Values where the acquisition function should be computed.

* model [sklearn estimator that implements predict with return_std]:
The fit estimator that approximates the function through the
method predict.
It should have a return_std parameter that returns the standard
deviation.

* y_opt [float, default 0]:
Previous minimum value which we would like to improve upon.

* xi: [float, default=0.01]:
Controls how much improvement one wants over the previous best
values. Useful only when method is set to "EI"

* return_grad: [boolean, optional]:
Whether or not to return the grad. Implemented only for the case where
X is a single sample.

Returns
-------
* values: [array-like, shape=(X.shape[0],)]:
Acquisition function values computed at X.
"""
with warnings.catch_warnings():
warnings.simplefilter("ignore")

else:
mu, std = model.predict(X, return_std=True)

values = np.zeros_like(mu)
improve = y_opt - xi - mu[mask]