Skip to main content

A mock project for Bayesian optimization.

Project description

Mock Project for Bayesian Optimization

Description

Bayesian optimization (BO) with Gaussian processes (GPs) is a powerful method for finding the global optimum (either maximum or minimum) of a multidimensional function using the least number of function evaluations. The algorithm suggests points to evaluate at, successively, whilst updating its inner representation of the hidden function. Values belonging to these points are noisy, and the algorithm handles this noise effectively, through the incorporation of uncertainty to the inner GP representation.

Implementation

This code is an implementation of the BO in Python 3 using NumPy and SciPy.

API

First, one should define the underlying GP model;

from src.pybopt import GaussianProcess

gp = GaussianProcess(
    length_scale=...,  # the characteristic length inside the Gaussian kernel
    n_of_features=...,  # the number of hidden function input dimensions
    kernel_scaler=...  # scales the kernels in the covariance matrix
)

The length_scale parameter can be a single float or a numpy array of floats, if every feature has a different length scale. Next, we can define the BO object;

from src.pybopt import BayesianOptimizer

bo = BayesianOptimizer(
    gaussian_process=gp,
    bounds_min=...,  # lower bounds for every feature
    bounds_max=...,  # upper bounds for every feature
    solver_n_points=...,  # number of guess points for GP optimum search
    solver_bfgs_n_iter=...  # number of BFGS iterations for GP optimum search
)

Arguments starting with solver_ refer to options setting the behavior of the maximum search of the GP. The following methods are available for the GP and BO objects;

  • gp.calculate_kernel calculates the kernel for two point collections,
  • gp.add_point adds new observations to the gaussian process,
  • gp.evaluate_at evaluates the gaussian process at certain test point values,
  • gp.calculate_flatness calculates the flatness of the target function,
  • bo.upper_confidence_bound calculates the upper confidence bound (UCB) utility function for a set of test points,
  • bo.probability_of_improvement calculates the probability of improvement (POI) utility function for a set of test points,
  • bo.expected_improvement calculates the expected improvement (EI) utility function for a set of test points,
  • bo.suggest makes a suggestion for the next point to evaluate at.

Showcases

This implementation was showcased using the following scripts:

test_1d.py

This script shows a simple 1D BO optimization procedure. The function to be optimized is (1.45 * x ** 2 - x) * math.sin(10 * x) on the [0, 1] interval, which has a maximum of y = 0.1578 at x = 0.8494. A decoy maximum can be found at x = 0.4516 with a y value of 0.1529. The function is evaluated noisily; a random number is drawn uniformly from the interval [0.05, 0.3], which will serve as a standard deviation. Then, a random number is drawn from a normal distribution with a mean of the function evaluation as the current test point and a standard deviation of the previously generated value. This new random number will be the observed, noisy value. The BO algorithm uses the upper confidence bound utility function with a lambda value of 3. It runs for 100 iterations.

test_2d.py

This is a more complicated function to be optimized in two dimensions. The function is a product of two subfunctions, each dependent only on one of two the input variables. The maximum is searched for on the interval [-6, 6] x [-6, 6]. In this box, among the relevant extrema, there are four larger and two smaller maxima, and also four deeper and two shallower minima. The script shows the exploration process by showing the real target function, the underlying mean and variance of the GP, and the utility function values (again, using upper confidence bound).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pybopt-2025.1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pybopt-2025.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file pybopt-2025.1.tar.gz.

File metadata

  • Download URL: pybopt-2025.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for pybopt-2025.1.tar.gz
Algorithm Hash digest
SHA256 f63617151d4560e87da9f717bfafc83c82a464cce8b318f96d9fcf713621ed15
MD5 6eeb250683e4cd54396b39761d04bddb
BLAKE2b-256 cf27485f4857bfcf6894237544c418ab228bd4c3c928285ed84cdf8ce7911b84

See more details on using hashes here.

File details

Details for the file pybopt-2025.1-py3-none-any.whl.

File metadata

  • Download URL: pybopt-2025.1-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for pybopt-2025.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c05e2b1b0fdcb8aeabe10ccb48c079e35e4ad8ece41ae3166bba5cbebb6c4f81
MD5 d254cc8eb79c442b112c842e46e6feb6
BLAKE2b-256 88063e6c8c5adafc15f6d632ea9013398c8bf8533ed89c0b8145e74425041fe9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page