bayeso.bo

It defines a class for Bayesian optimization.

class bayeso.bo.BO(range_X: numpy.ndarray, str_cov: str = 'matern52', str_acq: str = 'ei', normalize_Y: bool = True, use_ard: bool = True, prior_mu: Optional[callable] = None, str_optimizer_method_gp: str = 'BFGS', str_optimizer_method_bo: str = 'L-BFGS-B', str_modelselection_method: str = 'ml', debug: bool = False)

Bases: object

It is a Bayesian optimization class.

Parameters:
  • range_X (numpy.ndarray) – a search space. Shape: (d, 2).
  • str_cov (str., optional) – the name of covariance function.
  • str_acq (str., optional) – the name of acquisition function.
  • normalize_Y (bool., optional) – flag for normalizing outputs.
  • use_ard (bool., optional) – flag for automatic relevance determination.
  • prior_mu (NoneType, or function, optional) – None, or prior mean function.
  • str_optimizer_method_gp (str., optional) – the name of optimization method for Gaussian process regression.
  • str_optimizer_method_bo (str., optional) – the name of optimization method for Bayesian optimization.
  • str_modelselection_method (str., optional) – the name of model selection method for Gaussian process regression.
  • debug (bool., optional) – flag for printing log messages.
_get_bounds() → list

It returns list of range tuples, obtained from self.range_X.

Returns:list of range tuples.
Return type:list
_get_samples_grid(num_grids: int = 50) → numpy.ndarray

It returns grids of self.range_X.

Parameters:num_grids (int., optional) – the number of grids.
Returns:grids of self.range_X. Shape: (num_grids\(^{\text{d}}\), d).
Return type:numpy.ndarray
Raises:AssertionError
_get_samples_latin(num_samples: int) → numpy.ndarray

It returns num_samples examples sampled from Latin hypercube.

Parameters:num_samples (int.) – the number of samples.
Returns:examples sampled from Latin hypercube. Shape: (num_samples, d).
Return type:numpy.ndarray
Raises:AssertionError
_get_samples_sobol(num_samples: int, seed: Optional[int] = None) → numpy.ndarray

It returns num_samples examples sampled from Sobol sequence.

Parameters:
  • num_samples (int.) – the number of samples.
  • seed (NoneType or int., optional) – None, or random seed.
Returns:

examples sampled from Sobol sequence. Shape: (num_samples, d).

Return type:

numpy.ndarray

Raises:

AssertionError

_get_samples_uniform(num_samples: int, seed: Optional[int] = None) → numpy.ndarray

It returns num_samples examples uniformly sampled.

Parameters:
  • num_samples (int.) – the number of samples.
  • seed (NoneType or int., optional) – None, or random seed.
Returns:

random examples. Shape: (num_samples, d).

Return type:

numpy.ndarray

Raises:

AssertionError

_optimize(fun_negative_acquisition: callable, str_sampling_method: str, num_samples: int) → Tuple[numpy.ndarray, numpy.ndarray]

It optimizes fun_negative_function with self.str_optimizer_method_bo. num_samples examples are determined by str_sampling_method, to start acquisition function optimization.

Parameters:
  • fun_objective (function) – negative acquisition function.
  • str_sampling_method (str.) – the name of sampling method.
  • num_samples (int.) – the number of samples.
Returns:

tuple of next point to evaluate and all candidates determined by acquisition function optimization. Shape: ((d, ), (num_samples, d)).

Return type:

(numpy.ndarray, numpy.ndarray)

_optimize_objective(fun_acquisition: callable, X_train: numpy.ndarray, Y_train: numpy.ndarray, X_test: numpy.ndarray, cov_X_X: numpy.ndarray, inv_cov_X_X: numpy.ndarray, hyps: dict) → numpy.ndarray

It returns acquisition function values over X_test.

Parameters:
  • fun_acquisition (function) – acquisition function.
  • X_train (numpy.ndarray) – inputs. Shape: (n, d) or (n, m, d).
  • Y_train (numpy.ndarray) – outputs. Shape: (n, 1).
  • X_test (numpy.ndarray) – inputs. Shape: (l, d) or (l, m, d).
  • cov_X_X (numpy.ndarray) – kernel matrix over X_train. Shape: (n, n).
  • inv_cov_X_X (numpy.ndarray) – kernel matrix inverse over X_train. Shape: (n, n).
  • hyps (dict.) – dictionary of hyperparameters for Gaussian process.
Returns:

acquisition function values over X_test. Shape: (l, ).

Return type:

numpy.ndarray

get_initials(str_initial_method: str, num_initials: int, seed: Optional[int] = None) → numpy.ndarray

It returns num_initials examples, sampled by a sampling method str_initial_method.

Parameters:
  • str_initial_method (str.) – the name of sampling method.
  • num_initials (int., optional) – the number of samples.
  • seed (NoneType or int., optional) – None, or random seed.
Returns:

sampled examples. Shape: (num_samples, d).

Return type:

numpy.ndarray

Raises:

AssertionError

get_samples(str_sampling_method: str, fun_objective: Optional[callable] = None, num_samples: int = 100, seed: Optional[int] = None) → numpy.ndarray

It returns num_samples examples, sampled by a sampling method str_sampling_method.

Parameters:
  • str_sampling_method (str.) – the name of sampling method.
  • fun_objective (NoneType or function, optional) – None, or objective function.
  • num_samples (int., optional) – the number of samples.
  • seed (NoneType or int., optional) – None, or random seed.
Returns:

sampled examples. Shape: (num_samples, d).

Return type:

numpy.ndarray

Raises:

AssertionError

optimize(X_train: numpy.ndarray, Y_train: numpy.ndarray, str_sampling_method: str = 'uniform', num_samples: int = 100, str_mlm_method: str = 'regular') → Tuple[numpy.ndarray, dict]

It computes acquired example, candidates of acquired examples, acquisition function values for the candidates, covariance matrix, inverse matrix of the covariance matrix, hyperparameters optimized, and execution times.

Parameters:
  • X_train (numpy.ndarray) – inputs. Shape: (n, d) or (n, m, d).
  • Y_train (numpy.ndarray) – outputs. Shape: (n, 1).
  • str_sampling_method_method – the name of sampling method for acquisition function optimization.
  • num_samples (int., optional) – the number of samples.
  • str_mlm_method (str., optional) – the name of marginal likelihood maximization method for Gaussian process regression.
Returns:

acquired example and dictionary of information. Shape: ((d, ), dict.).

Return type:

(numpy.ndarray, dict.)

Raises:

AssertionError