bayesvalidrox.bayes_inference.bayes_inference.BayesInference

class bayesvalidrox.bayes_inference.bayes_inference.BayesInference(engine, discrepancy=None, emulator=True, name='Calib', bootstrap=False, req_outputs=None, selected_indices=None, prior_samples=None, n_prior_samples=100000, measured_data=None, inference_method='rejection', mcmc_params=None, bayes_loocv=False, n_bootstrap_itrs=1, perturbed_data=[], bootstrap_noise=0.05, just_analysis=False, valid_metrics=['BME'], plot_post_pred=True, plot_map_pred=False, max_a_posteriori='mean', corner_title_fmt='.2e', out_dir='', bmc=False)

Bases: object

A class to perform Bayesian Analysis.

Attributes

MetaModelobj

Meta model object.

discrepancyobj

The discrepancy object for the sigma2s, i.e. the diagonal entries of the variance matrix for a multivariate normal likelihood.

namestr, optional

The type of analysis, either calibration (Calib) or validation (Valid). The default is ‘Calib’. # TODO: what is going on here for validation?

emulatorbool, optional

Analysis with emulator (MetaModel). The default is True.

bootstrapbool, optional

Bootstrap the analysis. The default is False.

req_outputslist, optional

The list of requested output to be used for the analysis. The default is None. If None, all the defined outputs for the model object is used.

selected_indicesdict, optional

A dictionary with the selected indices of each model output. The default is None. If None, all measurement points are used in the analysis.

prior_samplesarray of shape (n_samples, n_params), optional

The samples to be used in the analysis. The default is None. If None the samples are drawn from the probablistic input parameter object of the MetaModel object.

n_prior_samplesint, optional

Number of samples to be used in the analysis. The default is 500000. If samples is not None, this argument will be assigned based on the number of samples given.

measured_datadict, optional

A dictionary containing the observation data. The default is None. if None, the observation defined in the Model object of the MetaModel is used.

inference_methodstr, optional

A method for approximating the posterior distribution in the Bayesian inference step. The default is ‘rejection’, which stands for rejection sampling. A Markov Chain Monte Carlo sampler can be simply selected by passing ‘MCMC’.

mcmc_paramsdict, optional

A dictionary with args required for the Bayesian inference with MCMC. The default is None.

Pass the mcmc_params like the following:

>>> mcmc_params:{
    'init_samples': None,  # initial samples
    'n_walkers': 100,  # number of walkers (chain)
    'n_steps': 100000,  # number of maximum steps
    'n_burn': 200,  # number of burn-in steps
    'moves': None,  # Moves for the emcee sampler
    'multiprocessing': False,  # multiprocessing
    'verbose': False # verbosity
    }

The items shown above are the default values. If any parmeter is not defined, the default value will be assigned to it.

bayes_loocvbool, optional

Bayesian Leave-one-out Cross Validation. The default is False. If True, the LOOCV procedure is used to estimate the bayesian Model Evidence (BME).

n_bootstrap_itrsint, optional

Number of bootstrap iteration. The default is 1. If bayes_loocv is True, this is qualt to the total length of the observation data set.

perturbed_dataarray of shape (n_bootstrap_itrs, n_obs), optional

User defined perturbed data. The default is [].

bootstrap_noisefloat, optional

A noise level to perturb the data set. The default is 0.05.

just_analysisbool, optional

Justifiability analysis. The default is False.

valid_metricslist, optional

List of the validation metrics. The following metrics are supported:

  1. log_BME : logarithm of the Bayesian model evidence

  2. KLD : Kullback-Leibler Divergence

3. inf_entropy: Information entropy The default is [‘log_BME’].

plot_post_predbool, optional

Plot posterior predictive plots. The default is True.

plot_map_predbool, optional

Plot the model outputs vs the metamodel predictions for the maximum a posteriori (defined as max_a_posteriori) parameter set. The default is False.

max_a_posterioristr, optional

Maximum a posteriori. ‘mean’ and ‘mode’ are available. The default is ‘mean’.

corner_title_fmtstr, optional

Title format for the posterior distribution plot with python package corner. The default is ‘.2e’.

__init__(engine, discrepancy=None, emulator=True, name='Calib', bootstrap=False, req_outputs=None, selected_indices=None, prior_samples=None, n_prior_samples=100000, measured_data=None, inference_method='rejection', mcmc_params=None, bayes_loocv=False, n_bootstrap_itrs=1, perturbed_data=[], bootstrap_noise=0.05, just_analysis=False, valid_metrics=['BME'], plot_post_pred=True, plot_map_pred=False, max_a_posteriori='mean', corner_title_fmt='.2e', out_dir='', bmc=False)

Methods

__init__(engine[, discrepancy, emulator, ...])

create_error_model([type_, opt_sigma, sampler])

Creates an error model in the engine.MetaModel based on input dist samples of the chosen type

create_inference()

Starts the inference.

normpdf(outputs, obs_data, total_sigma2s[, ...])

Calculates the likelihood of simulation outputs compared with observation data.

perform_bootstrap(total_sigma2)

Perform bootstrap to get TOM (??)

plot_log_BME()

Plots the log_BME if bootstrap is active.

plot_post_params(opt_sigma)

Plots the multivar.

setup_inference()

This function sets up the inference by checking the inputs and getting needed data.

create_error_model(type_='posterior', opt_sigma='B', sampler=None)

Creates an error model in the engine.MetaModel based on input dist samples of the chosen type

Parameters

opt_sigmastring, optional

Type of uncertainty description, only used if type_==’posterior’. The default is ‘B’

type_string

Type of parameter samples to use, either ‘prior’ or ‘posterior’. The default is ‘posterior’.

samplerMCMC, optional

Should be an MCMC object if type==’posterior’ and MCMC is used in the inference.In al other cases this parameter is not needed.

Returns

None.

create_inference()

Starts the inference.

Returns

BayesInferenceobj

The Bayes inference object.

# TODO: should this function really return the class?

normpdf(outputs, obs_data, total_sigma2s, sigma2=None, std=None)

Calculates the likelihood of simulation outputs compared with observation data.

Parameters

outputsdict

A dictionary containing the simulation outputs as array of shape (n_samples, n_measurement) for each model output.

obs_datadict

A dictionary/dataframe containing the observation data.

total_sigma2sdict

A dictionary with known values of the covariance diagonal entries, a.k.a. sigma^2.

sigma2array, optional

An array of the sigma^2 samples, when the covariance diagonal entries are unknown and are being jointly inferred. The default is None.

stddict, optional

A dictionary containing the root mean squared error as array of shape (n_samples, n_measurement) for each model output. The default is None.

Returns

logLikarray of shape (n_samples)

Likelihoods.

perform_bootstrap(total_sigma2)

Perform bootstrap to get TOM (??)

Parameters

total_sigma2dict

Dictionary containing the sigma2 for the training(?) data

Returns

None.

plot_log_BME()

Plots the log_BME if bootstrap is active.

Returns

None.

plot_post_params(opt_sigma)

Plots the multivar. posterior parameter distribution.

Parameters

opt_sigmastring

Type of uncertainty description available.

Returns

None.

setup_inference()

This function sets up the inference by checking the inputs and getting needed data.