bayesvalidrox.surrogate_models.surrogate_models.MetaModel

class bayesvalidrox.surrogate_models.surrogate_models.MetaModel(input_obj, meta_model_type='PCE', pce_reg_method='OLS', bootstrap_method='fast', n_bootstrap_itrs=1, pce_deg=1, pce_q_norm=1.0, dim_red_method='no', apply_constraints=False, verbose=False)

Bases: object

Meta (surrogate) model

This class trains a surrogate model. It accepts an input object (input_obj) containing the specification of the distributions for uncertain parameters and a model object with instructions on how to run the computational model.

Attributes

input_objobj

Input object with the information on the model input parameters.

meta_model_typestr

Surrogate model types. Three surrogate model types are supported: polynomial chaos expansion (PCE), arbitrary PCE (aPCE) and Gaussian process regression (GPE). Default is PCE.

pce_reg_methodstr

PCE regression method to compute the coefficients. The following regression methods are available:

  1. OLS: Ordinary Least Square method

  2. BRR: Bayesian Ridge Regression

  3. LARS: Least angle regression

  4. ARD: Bayesian ARD Regression

  5. FastARD: Fast Bayesian ARD Regression

  6. VBL: Variational Bayesian Learning

7. EBL: Emperical Bayesian Learning Default is OLS.

bootstrap_methodstr

Bootstraping method. Options are ‘normal’ and ‘fast’. The default is ‘fast’. It means that in each iteration except the first one, only the coefficent are recalculated with the ordinary least square method.

n_bootstrap_itrsint

Number of iterations for the bootstrap sampling. The default is 1.

pce_degint or list of int

Polynomial degree(s). If a list is given, an adaptive algorithm is used to find the best degree with the lowest Leave-One-Out cross-validation (LOO) error (or the highest score=1-LOO). Default is 1.

pce_q_normfloat

Hyperbolic (or q-norm) truncation for multi-indices of multivariate polynomials. Default is 1.0.

dim_red_methodstr

Dimensionality reduction method for the output space. The available method is based on principal component analysis (PCA). The Default is ‘no’. There are two ways to select number of components: use percentage of the explainable variance threshold (between 0 and 100) (Option A) or direct prescription of components’ number (Option B):

>>> MetaModelOpts = MetaModel()
>>> MetaModelOpts.dim_red_method = 'PCA'
>>> MetaModelOpts.var_pca_threshold = 99.999  # Option A
>>> MetaModelOpts.n_pca_components = 12 # Option B
apply_constraintsbool

If set to true constraints will be applied during training. In this case the training uses OLS. In this version the constraints need to be set explicitly in this class.

verbosebool

Prints summary of the regression results. Default is False.

Note

To define the sampling methods and the training set, an experimental design instance shall be defined. This can be done by:

>>> MetaModelOpts.add_InputSpace()

Two experimental design schemes are supported: one-shot (normal) and adaptive sequential (sequential) designs. For experimental design refer to InputSpace.

__init__(input_obj, meta_model_type='PCE', pce_reg_method='OLS', bootstrap_method='fast', n_bootstrap_itrs=1, pce_deg=1, pce_q_norm=1.0, dim_red_method='no', apply_constraints=False, verbose=False)

Methods

__init__(input_obj[, meta_model_type, ...])

adaptive_regression(ED_Y, varIdx[, verbose])

Adaptively fits the PCE model by comparing the scores of different degrees and q-norm.

add_InputSpace()

Instanciates experimental design object.

build_metamodel([n_init_samples])

Builds the parts for the metamodel (polynomes,...) that are neede before fitting.

copy_meta_model_opts()

This method is a convinient function to copy the metamodel options.

create_model_error(X, y, MeasuredData)

Fits a GPE-based model error.

eval_metamodel(samples)

Evaluates metamodel at the requested samples.

eval_model_error(X, y_pred)

Evaluates the error model.

fit(X, y[, parallel, verbose])

Fits the surrogate to the given data (samples X, outputs y).

generate_polynomials([max_deg])

Generates (univariate) polynomials.

pca_transformation(target)

Transforms the targets (outputs) via Principal Component Analysis.

regression(X, y, basis_indices[, ...])

Fit regression using the regression method provided.

univ_basis_vals(samples[, n_max])

Evaluates univariate regressors along input directions.

update_pce_coeffs(X, y[, out_dict])

Updates the PCE coefficents using only the ordinary least square method for the fast version of the bootstrapping.

adaptive_regression(ED_Y, varIdx, verbose=False)

Adaptively fits the PCE model by comparing the scores of different degrees and q-norm.

Parameters

ED_Yarray of shape (n_samples,)

Target values, i.e. simulation results for the Experimental design.

varIdxint

Index of the output.

verbosebool, optional

Print out summary. The default is False.

Returns

returnVarsDict

Fitted estimator, best degree, best q-norm, LOOCVScore and coefficients.

add_InputSpace()

Instanciates experimental design object.

Returns

None.

class auto_vivification

Bases: dict

Implementation of perl’s AutoVivification feature.

Source: https://stackoverflow.com/a/651879/18082457

clear() None.  Remove all items from D.
copy() a shallow copy of D
fromkeys(value=None, /)

Create a new dictionary with keys from iterable and values set to value.

get(key, default=None, /)

Return the value for key if key is in the dictionary, else default.

items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
pop(k[, d]) v, remove specified key and return the corresponding value.

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem()

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

setdefault(key, default=None, /)

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

update([E, ]**F) None.  Update D from dict/iterable E and F.

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() an object providing a view on D's values
build_metamodel(n_init_samples=None) None

Builds the parts for the metamodel (polynomes,…) that are neede before fitting.

Returns

None

DESCRIPTION.

copy_meta_model_opts()

This method is a convinient function to copy the metamodel options.

Returns

new_MetaModelOptsobject

The copied object.

create_model_error(X, y, MeasuredData)

Fits a GPE-based model error.

Parameters

Xarray of shape (n_outputs, n_inputs)
Input array. It can contain any forcing inputs or coordinates of

extracted data.

yarray of shape (n_outputs,)

The model response for the MAP parameter set.

MeasuredData :

Returns

self: object

Self object.

eval_metamodel(samples)

Evaluates metamodel at the requested samples. One can also generate nsamples.

Parameters

samplesarray of shape (n_samples, n_params), optional

Samples to evaluate metamodel at. The default is None.

Returns

mean_preddict

Mean of the predictions.

std_preddict

Standard deviatioon of the predictions.

eval_model_error(X, y_pred)

Evaluates the error model.

Parameters

Xarray

Inputs.

y_preddict

Predictions.

Returns

mean_preddict

Mean predition of the GPE-based error model.

std_preddict

standard deviation of the GPE-based error model.

fit(X: array, y: dict, parallel=False, verbose=False)

Fits the surrogate to the given data (samples X, outputs y). Note here that the samples X should be the transformed samples provided by the experimental design if the transformation is used there.

Parameters

X2D list or np.array of shape (#samples, #dim)

The parameter value combinations that the model was evaluated at.

ydict of 2D lists or arrays of shape (#samples, #timesteps)

The respective model evaluations.

parallelbool

Set to True to run the training in parallel for various keys. The default is False.

verbosebool

Set to True to obtain more information during runtime. The default is False.

Returns

None.

generate_polynomials(max_deg=None)

Generates (univariate) polynomials.

Parameters

max_degint

Maximum polynomial degree.

Returns

None

pca_transformation(target)

Transforms the targets (outputs) via Principal Component Analysis. The number of features is set by self.n_pca_components. If this is not given, self.var_pca_threshold is used as a threshold.

Parameters

targetarray of shape (n_samples,)

Target values.

Returns

pcaobj

Fitted sklearnPCA object.

OutputMatrixarray of shape (n_samples,)

Transformed target values.

n_pca_componentsint

Number of selected principal components.

regression(X, y, basis_indices, reg_method=None, sparsity=True)

Fit regression using the regression method provided.

Parameters

Xarray of shape (n_samples, n_features)

Training vector, where n_samples is the number of samples and n_features is the number of features.

yarray of shape (n_samples,)

Target values.

basis_indicesarray of shape (n_terms, n_params)

Multi-indices of multivariate polynomials.

reg_methodstr, optional

DESCRIPTION. The default is None.

sparsitybool

Use with sparsity-inducing training methods. The default is True

Returns

return_out_dictDict

Fitted estimator, spareMulti-Index, sparseX and coefficients.

univ_basis_vals(samples, n_max=None)

Evaluates univariate regressors along input directions.

Parameters

samplesarray of shape (n_samples, n_params)

Samples.

n_maxint, optional

Maximum polynomial degree. The default is None.

Returns

univ_basis: array of shape (n_samples, n_params, n_max+1)

All univariate regressors up to n_max.

update_pce_coeffs(X, y, out_dict=None)

Updates the PCE coefficents using only the ordinary least square method for the fast version of the bootstrapping.

Parameters

Xarray of shape (n_samples, n_params)

Training set. These samples should be already transformed.

yarray of shape (n_samples, n_outs)

The (transformed) model responses.

out_dictdict

The training output dictionary of the first iteration, i.e. the surrogate model for the original experimental design.

Returns

final_out_dictdict

The updated training output dictionary.