BayesianLeastSquares
Contents
- Purpose:
The purpose of the driver.
- Tutorials:
Tutorials demonstrating the application of this driver.
- Driver Interface:
Driver-specific methods of the Python interface.
- Configuration:
Configuration of the driver.
Purpose
The driver solves the least squares problem based on the Bayesian optimization approach. It is suited to search globally for a solution of a least-squares problem, which consists of a target vector \(\mathbf{t} \in \mathbb{R}^K\) and a vectorial function \(\mathbf{f}_{\rm bb}(\mathbf{p}_{\rm design}) \in \mathbb{R}^K\). The goal is to find design parameters \(\mathbf{p}_{\rm design}\) that minimize the chi-squared deviation
between K outputs \(y_i = f_i(\mathbf{p}_{\rm design})\) to the K targets \(t_i\) scaled by the target uncertainties \(\eta_i\).
If the target uncertainties are not independent but correlated, it is also possible to specify the covariance matrix \(G\) of the targets. In this case, the chi-squared deviation is given as
For inexpensive vectorial functions with evaluation times smaller than a seconds, and for functions with known Jacobian and moderate evaluation times, the ScipyLeastSquares driver is recommended.
Tutorials
Driver Interface
The driver instance can be obtained by Study.driver
.
- class jcmoptimizer.BayesianLeastSquares(host, study_id, session)[source]
- property active_learning_configuration: dict[str, Any]
Return a configuration for the ActiveLearning driver that can be used to reproduce the behavior of the current driver. Example:
config = driver.active_learning_configuration study_active_learning = client.create_study( driver="ActiveLearning", ... ) study_active_learning.configure(**config)
- property best_sample: dict[str, float | int | str]
Best sample with minimal chi-squared value found during the minimization. Example:
for key, value in best_sample.items(): print(f"{key} = {value}")
- describe()
Get description of all modules and their parameters that are used by the driver. Example:
description = driver.describe() print(description["members"]["surrogates"]["0"])
- Return type:
dict
[str
,Any
]
- Returns: A nested dictionary with description of submodules consisting
of a name and a descriptive text. If the entry describes a module, it has an additional
"members"
entry with dictionaries describing submodules and parameters.
- get_minima(environment_value=None, num_initial_samples=None, num_output=10, epsilon=0.1, delta=0.1, ftol=1e-09, min_dist=0.0)
Get a list of information about local minima of the objective. The width \(\sigma\) in each parameter direction is determined by a fit of the minimum to a Gaussian function that goes asymptotically to the mean value of the object. The minima are found using predictions of the surrogate models. The validity of constraints is completely ignored. Example:
import pandas as pd minima = driver.get_minima(num_output=10) print(pd.DataFrame(minima))
- Parameters:
environment_value (
Optional
[list
[float
]]) – Optional environment value for which local minima of design values are determined. If None, the local minima are also determined with respect to environment parameters.num_initial_samples (
Optional
[int
]) – Number of initial samples for searching (default: automatic determination).num_output (
int
) – Maximum number of minima that are returned (Default: 10)epsilon (
float
) – Parameter used for identifying identical minima (i.e. minima with distance < length scale * epsilon) and minima with non-vanishing gradient (e.g. minima at the boundary of the search space) (default: 0.1)delta (
float
) – step size parameter used for approximating second derivatives. (default: 0.1)ftol (
float
) – Precision goal for the minimum function value.min_dist (
float
) – In order to speed up the prediction, one can use a sparsified version of the base surrogates where sampling with a distance smaller than min_dist (in terms of the length scales of the surrogate) are neglected.
- Return type:
dict
[str
,list
[float
]]
- Returns: A dictionary with information about local minima
with lists of object values, uncertainties of the objective values, the parameter values and the width \(\sigma\) in each parameter direction (i.e. standard deviation after a fit to a Gaussian function)
- get_observed_values()
Get observed values. For noisy input data, the values are obtained on the basis of predictions of the surrogate models. Therefore, they can slightly differ from the input data. Example:
data = driver.get_observed_values()
Returns: Dictionary, with the following keys: :rtype:
dict
[str
,Any
]- samples:
The observed samples (design and possibly environment values).
- means:
Mean values of observations. For noiseless observations, this is the observed value itself.
- variance:
Variance of observed values. For noiseless observations, this is typically a negligibly small number.
- get_state(path=None)
Get state of the driver. Example:
best_sample = driver.get_state(path="best_sample")
- Parameters:
path (
Optional
[str
]) – A dot-separated path to a submodule or parameter. If none, the full state is returned.- Return type:
dict
[str
,Any
]
Returns: If path is None, a dictionary with information of driver state.
Note
A description of the meaning of each entry in the state can be retrieved by
describe()
.
- get_statistics(object_type='objective', quantiles=None, rel_precision=0.001, abs_precision=1e-09, max_time=inf, max_samples=1000000.0, min_dist=0.0)[source]
Determines statistics like the mean and variance of a the surrogate or chi-squared objective under a parameter distribution. By default the probability density of the parameters is a uniform distribution in the whole parameter domain. Other parameter distributions can be defined via study.configure(parameter_distribution = dict(…)).
Example:
study.configure(parameter_distribution = dict( distributions=[ {type="normal", parameter="param1", mean=1.0, stddev=2.0}, {type="uniform", parameter="param2", domain=[-1.0,1.0]} ] )) stats = study.driver.get_statistics(abs_precision=0.001)
- Parameters:
object_type (
Literal
['surrogate'
,'objective'
]) – The type of object for which a prediction is required. name: The name of the object for which predictions are required.quantiles (
Optional
[list
[float
]]) – A list with quantiles. If not specified, the quantiles[0.16,0.5,0.84]
are used.abs_precision (
float
) – The Monte Carlo integration is stopped when the empiric absolute uncertainty of the mean value of all outputs is smaller than abs_precision.rel_precision (
float
) – The Monte Carlo integration is stopped when the empiric relative uncertainty of the mean value of all outputs is smaller than rel_precision.max_time (
float
) – The Monte Carlo integration is stopped when the time max_time has passed.max_samples (
float
) – The Monte Carlo integration is stopped when the number of evaluated samples equals or exceeds the given value.min_dist (
float
) – In order to speed up the prediction, one can use a sparsified version of the base surrogates where sampling with a distance smaller than min_dist (in terms of the length scales of the surrogate) are neglected.
- Return type:
dict
[str
,Any
]
Returns: A dictionary with the entries
- mean:
Expectation values \(\mathbf{m}=\mathbb{E}[\mathbf{g}(\mathbf{x})]\) of the object function \(\mathbf{g}(\mathbf{x})\) under the parameter distribution
- variance:
Variance \(\mathbf{v}=\mathbb{E}[(\mathbf{g}(\mathbf{x})-\mathbf{m})^2]\) of the object function \(\mathbf{g}(\mathbf{x})\) under the parameter distribution
- quantiles:
A list of quantiles of shape (num_quantiles, num_outputs).
- num_samples:
Number of sampling points \(N\) that were used in the Monte Carlo integration. The numerical uncertainty of the computed mean value is \(\sqrt{v/N}\).
- historic_parameter_values(path)
Get the values of an internal parameter for each iteration of the study. Example:
min_objective_values = driver.historic_parameter_values( path="acquisition_function.min_objective")
- Parameters:
path (
str
) – A dot-separated path to the parameter.- Return type:
list
[Any
]
Note
A description of the meaning of each parameter can be retrieved by
describe()
.
- property min_objective: float
Minimal chi-squared value found during the minimization. Example:
min_chi_sq = study.driver.min_objective
- optimize_hyperparameters(num_samples=1, min_dist=0.0)
Optimize the hyperparameters of the driver. This is usually done automatically. Example:
driver.optimize_hyperparameters()
- Parameters:
num_samples (
int
) – Number of initial start samples for optimization (default: automatic determination)min_dist (
float
) – In order to speed up the prediction, one can use a sparsified version of the base surrogates where sampling with a distance smaller than min_dist (in terms of the length scales of the surrogate) are neglected.
- Return type:
None
- override_parameter(path, value)
Override an internal parameter of the driver that is otherwise selected automatically. Example:
driver.override_parameter( "surrogates.0.matrix.kernel.design_length_scales", [1.0, 2.0] )
- Parameters:
path (
str
) – A dot-separated path to the parameter to be overridden.value (
Any
) – The new value of the parameter.
- Return type:
None
Note
A description of the meaning of each parameter can be retrieved by
describe()
.
- predict(points, object_type='objective', output_type='mean_var', min_dist=0.0, quantiles=None, num_samples=None)[source]
Make predictions on the multi-output surrogate or the chi-squared objective. Example:
prediction = driver.predict(points=[[1,0,0],[2,0,1]])
- Parameters:
points (
list
[list
[float
]]) – Vectors of the space (design space + environment) of shape(num_points, num_dim)
object_type (
Literal
['surrogate'
,'objective'
]) – The type of object for which a prediction is required.output_type (
Literal
['mean_var'
,'quantiles'
,'samples'
]) –The type of output. Options are:
- mean_var:
Mean and variance of the posterior distribution. This describes the posterior distribution only for normally distributed posteriors. The function returns a dictionary with keywords
"mean"
and"variance"
mapping to array of lengthnum_points
. If the posterior distribution is multivariate normal, for each point a covariance matrix of shape(output_dim, output_dim)
is returned, otherwise a list of variances of shape(output_dim,)
is returned.- quantiles:
A list of quantiles of the distribution is estimated based on samples drawn from the distribution. The function returns a dict with entry
"quantiles"
and a tensor of shape(num_quantiles, num_points, output_dim)
- samples:
Random samples drawn from the posterior distribution. The function returns a dict with the entry
"samples"
and a tensor of shape(num_samples, num_points, output_dim)
min_dist (
float
) – In order to speed up the prediction, one can use a sparsified version of the base surrogates where sampling with a distance smaller than min_dist (in terms of the length scales of the surrogate) are neglected.quantiles (
Optional
[list
[float
]]) – A list with quantiles. If not specified, the quantiles [0.16,0.5,0.84] are used.num_samples (
Optional
[int
]) – Number of samples used for posteriors that have a sampling-based distribution or if output_type is “samples”. If not specified, the same number as in the acquisition is used. If the posterior is described by a fixed number of ensemble points, the minimum of num_samples and the ensemble size is used.
- Return type:
dict
[str
,list
]
- Returns: A dictionary with the entries “mean” and “variance” if
output_type = "mean_var
and “samples” ifoutput_type = "samples"
- run_mcmc(num_walkers=None, max_iter=50000, max_time=inf, rel_error=0.05, thin_chains=False, multi_modal=False, append=False, max_sigma_dist=inf, min_dist=0.0)[source]
Runs a Markov Chain Monte Carlo (MCMC) sampling of posterior probability of the parameters. By default the prior probability density of the parameters is a uniform distribution in the whole parameter domain. Other parameter distributions can be defined via
study.configure(parameter_distribution = dict(...))
. Example:study.run() study.configure(parameter_distribution = dict( distributions=[ {type="normal", parameter="param1", mean=1.0, stddev=2.0}, {type="uniform", parameter="param2", domain=[-1.0,1.0]} ] )) samples = study.driver.run_mcmc()
The estimated error of a Monte-Carlo integration of some function \(f\) is \(\delta = \sigma / \sqrt{N_{\rm ind}}\) where \(\sigma^2\) is the variance of \(f\) and \(N_{\rm ind}\) is the number of independent samples from the probability distribution. The error relative to the variance \(\sigma^2\) is therefore \(\delta_{\rm rel}=1/\sqrt{N_{\rm ind}}\). Assuming, that subsequent samples of a chain are correlated and this correlation vanishes at a correlation time \(\tau\), \(N_{\rm ind} = N/\tau\) of all \(N\) MCMC samples are independent. Note, that \(\tau\) can only be estimated and thus the relative Monte-Carlo integration error \(\delta_{\rm rel}=\tau/\sqrt{N}\) can be under or overestimated.
- Parameters:
num_walkers (
Optional
[int
]) – Number of walkers. If not specified, the value is automatically chosen.max_iter (
int
) – Maximum absolute chain length.max_time (
float
) – Maximum run time in seconds. If not specified, the runtime is not limited.rel_error (
float
) – Targeted relative Monte-Carlo integration error \(\delta_{\rm rel}\) of the samples.thin_chains (
bool
) – If true, only every \(\tau\)-th sample of all MCMC samples is returned. This is helpful if the full number of samples gets too large and a representative uncorrelated subset is required.multi_modal (
bool
) – If true, a more explorative sampling strategy is used.append (
bool
) – If true, the samples are appended to the samples of the previous MCMC run.max_sigma_dist (float) – If set, the sampling is restricted to a a distance max_sigma_dist * sigma to the maximum likelihood estimate. E.g. max_sigma_dist=3.0 means that only the 99.7% probability region of each parameter is sampled. (default: inf)
min_dist (
float
) – In order to speed up the prediction, one can use a sparsified version of the base surrogates where sampling with a distance smaller than min_dist (in terms of the length scales of the surrogate) are neglected.
- Return type:
dict
[str
,Any
]
Returns: A dictionary with the following entries:
- samples:
The drawn samples without “burn-in” samples thinned by half of the correlation time.
- medians:
The medians of all random parameters
- lower_uncertainties:
The distances between the medians and the 16% quantile of all random parameters
- upper_uncertainties:
The distance between the medians and the 84% quantile of all random parameters
- tau:
Estimated correlation time of each parameter.
- property uncertainties: dict[str, float]
Uncertainties of the continuous parameters of the best sample. Example:
for key, value in uncertainties.items(): print(f"uncertainty of {key}: {value}")
Configuration
The configuration parameters can be set by calling, e.g.
study.configure(example_parameter1 = [1,2,3], example_parameter2 = True)
This driver requires a definition of a target_vector \([t_1,\cdots, t_K]\) to find a least-squares solution that minimizes \(\chi^2 = \sum_{i=1}^K \left(t_i - y_i\right)^2\) where \(y_i = f_i(\mathbf{p}_{\rm design})\) is the \(i\)-th entry of the observed black-box function. To perform a weighted least-squares minimization one can specify an uncertainty_vector or more generally a covariance_matrix between the \(K\) target-vector entries.
max_iter (int)
Maximum number of evaluations of the studied system.
Default: Infinite number of evaluations.
max_time (float)
Maximum run time of study in seconds. The time is counted from the moment, that the parameter is set or reset.
Default:
inf
num_parallel (int)
Number of parallel evaluations of the studied system.
Default:
1
target_vector (list[float])
Vector of targets \(t_i\) for \(i=1,\dots, K\).
Default:
[0.0]
uncertainty_vector (list[float])
Vector of target uncertainties \(\eta_i\) such that \(\chi^2 = \sum_{i=1}^K \frac{(t_i - y_i)^2}{\eta_i^2}\).
Default: vector of ones.
covariance_matrix (list[list[float]])
Covariance matrix \(G\) such that \(\chi^2 = \sum_{i,j=1}^K (t_i - y_i) G_{i j}^{-1} (t_j - y_j).\).
Default: diagonal identity matrix.
parameter_distribution (dict)
Probability distribution of design and environment parameters.
Default:
{'include_study_constraints': False, 'distributions': [], 'constraints': []}
Probability distribution of design and environment parameters defined by distribution functions and constraints. The definition of the parameter distribution can have several effects:
In a call to the method
get_statistics
of the driver interface the value of interest is averaged over samples drawn from the space distribution.In a call to the method
run_mcmc
of the driver interface the space distribution acts as a prior distribution.In a call to the method
get_sobol_indices
of the driver interface the space distribution acts as a weighting factor for determining expectation values.In an ActiveLearning driver, one can access the value of the log-probability density (up to an additive constant) by the name
'log_prob'
in any expression, e.g. in Expression variable, Linear combination variable.See parameter_distribution configuration for details.
scaling (float)
Scaling parameter of the model uncertainty. For scaling \(\gg 1.0\) (e.g.
scaling=10.0
) the search is more explorative. For scaling \(\ll 1.0\) (e.g.scaling=0.1
) the search becomes more greedy (e.g. any local minimum is intensively exploited).Default:
1.0
optimization_step_min (int)
Minimum number of observations of the objective before the hyperparameters are optimized.
Default: Automatic choice according to number of dimensions.
Note
Each derivative observation is counting as an independent observation.
optimization_step_max (int)
Maximum number of observations of the objective after which no more hyperparameter optimization is performed.
Default:
100
Note
Each derivative observation is counting as an independent observation.
min_optimization_interval (int)
Minimum number of observations of the objective after which the hyperparameters are optimized.
Default:
2
Note
Each derivative observation is counting as an independent observation.
max_optimization_interval (int)
Maximum number of observations of the objective after which the hyperparameters are optimized.
Default:
20
Note
Each derivative observation is counting as an independent observation.
optimization_level (float)
Controls how often the hyper-parameters are optimized. Small values (e.g. 0.01) lead to more frequent optimizations. Large values (e.g. 1) lead to less frequent optimizations.
Default:
0.2
approximate (bool)
If true, the generalized chi-squared variable with different uncertainties of the predictions of each channel K is approximated by a chi-squared variable with averaged uncertainties. This allows to analytically compute probability densities and any acquisition function that is directly based on the variable.
Default:
True
effective_DOF (float)
Number of effective degrees of freedom (DOF) used for stochastic variable of the chi-squared distribution. This number roughly indicates how many output channels of the forward model are statistically independent.
Default: If not specified, the value us determined automatically.
Note
If
approximate
is false, this parameter has no effect.
effective_DOF_bounds (list[float])
The number of effective degrees of freedom (DOF) is determined by a maximum likelihood estimate within the given lower and upper bounds.
Default:
[20.0,50.0]
Note
If
approximate
is false or theeffective_DOF
is set manually, this parameter has no effect.
min_val (float)
The minimization of the objective is stopped when the observed objective value is below the specified minimum value.
Default:
-inf
min_uncertainty (float)
The study is stopped if the uncertainty (square root of variance) of the objective at the last 5 sampling points was below
min_uncertainty
.Default:
0.0
num_training_samples (int)
Number of pseudo-random initial samples before the samples are drawn according to the acquisition function.
Default: Automatic choice depending depending on dimensionality of design space.
min_PoI (float)
The study is stopped if the maximum probability of improvement (PoI) of the last 5 iterations is below
min_PoI
.Default:
0.0
adaptive_local_search (bool)
The maximization of the acquisition function consists of a global heuristic search followed by a local convergence of the best samples. By default, the local search effort adapts to the value of
max_num_model_evals
. In some cases far better samples are computed, if the local search is only stopped when convergence to a local minimum was obtained.Default:
False
max_num_model_evals (int)
Maximum number of evaluations of the surrogates for finding the maximum of the acquisition function.
Default:
1500
num_iterative_solver_steps (int)
Gaussian process regression requires to solve systems of linear equations. In order to improve the accuracy of direct solution methods, an iterative refinement can be performed with the given number of iterations.
Default:
5