bingo.local_optimizers package#
Submodules#
bingo.local_optimizers.local_opt_fitness module#
Fitness evaluation with local optimization
This module contains the implementation of a fitness function wrapper that will perform local optimization of a Chromosome as necessary using a LocalOptimizer before evaluating it.
- class bingo.local_optimizers.local_opt_fitness.LocalOptFitnessFunction(fitness_function, optimizer)#
Bases:
FitnessFunction
Fitness function wrapper for individuals that want local optimization
A class for fitness evaluation of individuals that may or may not need local optimization before evaluation.
- Parameters:
fitness_function (FitnessFunction) – A FitnessFunction for evaluating the fitness of a Chromosome.
optimizer (LocalOptimizer) – An optimizer that will perform local optimization on a Chromosome before evaluation as needed.
- eval_count#
the number of evaluations that have been performed by the wrapped fitness function
- Type:
int
- training_data#
data that can be used in the wrapped fitness function
- Type:
TrainingData
- property eval_count#
the number of evaluations that have been performed
- Type:
int
- property training_data#
data that can be used in fitness evaluations
- Type:
bingo.local_optimizers.local_optimizer module#
This module contains the abstract definition of an optimizer that can be used for local optimization of a Chromosome.
bingo.local_optimizers.normalized_marginal_likelihood module#
Normalized marginal likelihood calculation using SMCPy
This module contains the implementation of a fitness function wrapper that will perform probabilistic local optimization of a Chromosome using SMCPy. The normaized marginal likelihood from the SMC optimization is returned.
- class bingo.local_optimizers.normalized_marginal_likelihood.NormalizedMarginalLikelihood(fitness_function, deterministic_optimizer, log_scale=True, **kwargs)#
Bases:
FitnessFunction
Normalized marginal likelihood calculation using SMCPy
A class for fitness evaluation of individuals that have local optimization parameters
- Parameters:
fitness_function (FitnessFunction) – A FitnessFunction for evaluating the fitness of a Chromosome.
deterministic_optimizer (LocalOptimizer) – An optimizer that will perform deterministic local optimization on a Chromosome. Used in proposals of SmcpyOptimizer
**kwargs – other keyword arguments are passed to the SmcpyOptimizer initialization
- eval_count#
the number of evaluations that have been performed by the wrapped fitness function
- Type:
int
- training_data#
data that can be used in the wrapped fitness function
- Type:
TrainingData
- property eval_count#
the number of evaluations that have been performed
- Type:
int
- property training_data#
data that can be used in fitness evaluations
- Type:
bingo.local_optimizers.scipy_optimizer module#
Local optimization using scipy
Specifies ScipyOptimizer which is a class for local optimization of `Chromosome`s using scipy’s minimize or root methods. Also specifies ROOT_SET, a set of methods that will use scipy’s root method; MINIMIZE_SET, a set of methods that will use scipy’s minimize method; and JACOBIAN_SET, a set of methods that will use jacobian information.
- class bingo.local_optimizers.scipy_optimizer.ScipyOptimizer(objective_fn, **options)#
Bases:
LocalOptimizer
An optimizer that uses scipy.minimize or scipy.root for local optimization
A class for optimizing the parameters of a Chromosome using either scipy.minimize or scipy.root depending on the method specified.
- Parameters:
objective_fn – A function to minimize which can be evaluated by passing in a Chromosome.
options –
Additional arguments for optimization. e.g. (…, tol=1e-8, options={“maxiter”: 1000})
- e.g. param_init_bounds: iterable
[low, high) bounds that are used to initialize params, formatted as an iterable defaults to [-10000, 10000)
- e.g. method: string
method to use for optimization (e.g. BFGS, lm, etc.) defaults to BFGS
- e.g. tol: float
tolerance used for method defaults to 1e-6
- e.g. optionsdict
method-specific options (e.g. maxiter, ftol, etc.)
- objective_fn#
A function to minimize which can be evaluated by passing in a Chromosome
- options#
Additional arguments for clo options
- Type:
dict
- Raises:
KeyError – method must be a method supported by scipy
TypeError – objective_function must suit the specified method
- property objective_fn#
function to minimize, must take a Chromosome as input and return a number
- property options#
optimizer options (e.g. param_init_bounds, method, tol, options, etc.)
- Type:
dict
bingo.local_optimizers.smcpy_optimizer module#
A module for probabilistic calibration of parameters.
Probabilistic calibration of model parameters can be useful in cases where data is sparse and/or noisy. Using a calibration of this type can allow for a better estimate of true fitness while being a bit more robust to overfitting.
- class bingo.local_optimizers.smcpy_optimizer.MixtureDist(*args)#
Bases:
object
A mixture distribution class that combines multiple probability distributions.
This class represents a mixture model where samples are drawn uniformly at random from one of the component distributions. Each component distribution has equal weight (1/n where n is the number of components).
- Parameters:
*args (tuple of distribution objects) – Variable number of probability distribution objects. Each distribution should have rvs() and pdf() methods compatible with scipy.stats distributions.
- _dists#
Tuple of component probability distributions.
- Type:
tuple
Examples
>>> from scipy.stats import norm, uniform >>> mixture = MixtureDist(norm(0, 1), uniform(-2, 4)) >>> samples = mixture.rvs(1000, random_state=42) >>> log_probs = mixture.logpdf(samples)
- logpdf(x)#
Compute the log probability density function of the mixture distribution.
The PDF of a mixture distribution is the average of the PDFs of the component distributions: pdf(x) = (1/n) * sum(pdf_i(x)) where n is the number of components.
- Parameters:
x (ndarray) – Input samples of shape (num_samples, dim) where dim matches the dimensionality of component distributions.
- Returns:
Log probability densities of shape (num_samples, 1).
- Return type:
ndarray
Examples
>>> import numpy as np >>> mixture = MixtureDist(norm(0, 1), norm(5, 2)) >>> x = np.array([[0.5], [2.0], [4.5]]) >>> log_probs = mixture.logpdf(x)
- rvs(num_samples, random_state=None)#
Generate random samples from the mixture distribution.
For each sample, randomly selects one of the component distributions with equal probability and draws a sample from it.
- Parameters:
num_samples (int) – Number of samples to generate.
random_state (int, optional) – Random seed for reproducible results. If None, uses a random seed.
- Returns:
Array of shape (num_samples, dim) where dim is the dimensionality of the component distributions. For 1D distributions, returns shape (num_samples, 1).
- Return type:
ndarray
Examples
>>> mixture = MixtureDist(norm(0, 1), norm(5, 2)) >>> samples = mixture.rvs(100, random_state=42) >>> samples.shape (100, 1)
- class bingo.local_optimizers.smcpy_optimizer.SmcpyOptimizer(objective_fn, deterministic_optimizer, num_particles=150, mcmc_steps=12, ess_threshold=0.75, std=None, num_multistarts=1, reuse_starting_point=True)#
Bases:
LocalOptimizer
An optimizer that uses SMCPy for probabilistic parameter calibration
A class for probabilistic parameter calibration for the parameters of a Chromosome using SMCPy
- Parameters:
objective_fn (VectorBasedFunction, VectorGradientMixin) – A VectorBasedFunction with VectorGradientMixin (e.g., ExplicitRegression). It should produce a vector where the target value is 0.
deterministic_optimizer (LocalOptimizer) – A deterministic local optimizer e.g., ScipyOptimizer
num_particles (int) – The number of particles to use in the SMC approximation
mcmc_steps (int) – The number of MCMC steps to perform with each SMC update
ess_threshold (float (0-1)) – The effective sample size (ratio) below which SMC particles will be resampled
std (float) – (Optional) The fixed noise level, if it is known
num_multistarts (int) – (Optional) The number of deterministic optimizations performed when developing the SMC proposal
- objective_fn#
A function to minimize which can be evaluated by passing in a Chromosome
- options#
Additional arguments for clo options
- Type:
dict
- property eval_count#
the number of evaluations that have been performed
- Type:
int
- evaluate_model(params, individual)#
Evaluate a model with given parameters and return fitness vector.
This method sets the local optimization parameters for an individual, evaluates its fitness using the objective function, and reshapes the result to ensure consistent dimensionality for further processing.
- Parameters:
params (ndarray) – Model parameters to evaluate. Expected shape is (n_params,) or (n_params, n_models). Will be transposed before setting on individual.
individual (object) – Individual model object that implements set_local_optimization_params method. Represents the model structure or configuration to evaluate.
- Returns:
Fitness evaluation results with shape (n_models, n_training_samples) where n_training_samples is the length of the training data. If the original result is 1D, it will be reshaped to ensure 2D output.
- Return type:
ndarray
Examples
>>> # Assuming self is an instance with _objective_fn and training data >>> params = np.array([[1.0, 2.0], [3.0, 4.0]]) # 2 parameters, 2 models >>> result = self.evaluate_model(params, individual) >>> result.shape (2, 100) # 2 models evaluated on 100 training samples
- property objective_fn#
A VectorBasedFunction with VectorGradientMixin (e.g., ExplicitRegression). It should produce a vector where the target value is 0.
- property options#
optimizer’s options
- Type:
dict
- property training_data#
Training data used in objective function
- class bingo.local_optimizers.smcpy_optimizer.SqrtInvGamma(shape, scale)#
Bases:
object
Square root of inverse gamma distribution.
This class represents a distribution where if X ~ InvGamma(shape, scale), then Y = sqrt(X) follows this distribution. This is useful when you need the square root transformation of an inverse gamma random variable.
- Parameters:
shape (float) – Shape parameter of the underlying inverse gamma distribution. Must be positive.
scale (float) – Scale parameter of the underlying inverse gamma distribution. Must be positive.
Examples
>>> sqrt_inv_gamma = SqrtInvGamma(shape=2.0, scale=1.0) >>> samples = sqrt_inv_gamma.rvs(1000, random_state=42) >>> probabilities = sqrt_inv_gamma.pdf(samples)
- pdf(x, *args, **kwargs)#
Compute the probability density function of the square root inverse gamma distribution.
Uses the transformation formula: if Y = sqrt(X) where X ~ InvGamma(shape, scale), then pdf_Y(y) = pdf_X(y^2) where pdf_X is the inverse gamma PDF.
- Parameters:
x (ndarray or float) – Points at which to evaluate the PDF. Must be non-negative.
*args (tuple) – Additional positional arguments passed to the underlying inverse gamma PDF method.
**kwargs (dict) – Additional keyword arguments passed to the underlying inverse gamma PDF method.
- Returns:
Probability density values at the input points.
- Return type:
ndarray or float
Examples
>>> dist = SqrtInvGamma(shape=2.0, scale=1.0) >>> x = np.linspace(0.1, 3.0, 100) >>> pdf_values = dist.pdf(x)
- rvs(*args, **kwargs)#
Generate random samples from the square root inverse gamma distribution.
- Parameters:
*args (tuple) – Positional arguments passed to the underlying inverse gamma distribution’s rvs method (e.g., size, random_state).
**kwargs (dict) – Keyword arguments passed to the underlying inverse gamma distribution’s rvs method.
- Returns:
Square root of inverse gamma random samples. Shape depends on the size parameter passed.
- Return type:
ndarray or float
Examples
>>> dist = SqrtInvGamma(shape=2.0, scale=1.0) >>> samples = dist.rvs(size=100, random_state=42)