Uncertainty Quantification (labtoolbox.uncertainty)#
Uncertainty quantification utilities for LabToolbox.
This package exposes methods for uncertainty propagation and several uncertainty modeling strategies.
- labtoolbox.uncertainty.montecarlo(func, values, errs, N=10000, seed=None)[source]#
Estimate the propagated uncertainty on a function of N variables using Monte Carlo simulation.
- Parameters:
func (callable) – The function to evaluate. Must accept the same number of arguments as the length of values.
values (array-like) – Central values of the input variables. Must be of the same length as errs.
errs (array-like) – Standard deviations (1-sigma uncertainties) of the input variables.
N (int, optional) – Number of Monte Carlo samples to generate. Default is 1e4.
seed (int or None, optional) – Seed for the random number generator, for reproducibility. Default is None.
- Returns:
mean (float) – Mean value of the function evaluated over the sampled inputs.
std (float) – Standard deviation (uncertainty) of the function output.
Notes
The input variables are sampled as independent normal distributions with given means and standard deviations.
Correlations between input variables are not taken into account.
Example
>>> def f(x, y): return x * y >>> montecarlo(f, [2.0, 3.0], [0.1, 0.2]) (6.00..., 0.42...)
- labtoolbox.uncertainty.numerical(f, x_val, x_err, params=())[source]#
Uncertainty propagation via numerical derivatives.
- Parameters:
f (callable) – Function f(x1, …, xn; a1, …, am) that returns an array of shape (N,).
x_val (list of np.ndarray) – List of input arrays x1,…, xn, each with shape (N,).
x_err (list of np.ndarray) – List of uncertainty arrays corresponding to each x_i, shape (N,).
params (tuple, optional) – Tuple of constant parameters (a1, …, am) to be passed to the function.
- Returns:
f_val (np.ndarray) – Central values of the function, shape (N,).
f_err (np.ndarray) – Propagated uncertainty, shape (N,).
- labtoolbox.uncertainty.propagate(func, x_val, x_err, params=None, method='Monte_Carlo', MC_sample_size=10000)[source]#
Propagates uncertainty from i_nput arrays to a generic function using the uncertainty_class library.
- Parameters:
func (callable) – The base function, in the form f(x, a) where: - x is a vector of variables. - a is a vector of parameters (optional).
x_val (list of numpy.array) – List containing the i_nput variable arrays [x1, x2, …, xn]. Each entry can be: - a scalar, - a numpy array of values (same length across all xi).
x_err (list or numpy.array) – List of uncertainties for each variable (scalars or arrays), or a full covariance matrix.
params (list or numpy.array, optional) – List or array of constant parameters [a1, a2, …, am].
method (str, optional) – The uncertainty propagation method (‘Delta’ or ‘Monte_Carlo’).
MC_sample_size (int, optional) – Sample size for the Monte Carlo method.
- Returns:
f_values (numpy.ndarray) – Values of the function calculated at each point j.
f_err (numpy.ndarray) – Propagated uncertainties on the output function for each point j.
confidence_bands (tuple of numpy.ndarray) – Lower and upper confidence bands for each point j.