Bayesian inference

With Bayesian inference we ask the question ‘how does our understanding of the inputs change given some observation of the outputs of the model?’, i.e. we perform an updating step of the prior distributions to posterior, based on some observations. Bayesvalidrox provides a dedicated class to perform this task, bayesvalidrox.bayes_inference.bayes_inference.BayesInference, which infers the posterior via rejection-sampling or MCMC. The likelihood in rejection sampling is estimated with the help of bootstrapping. MCMC-specific parameters are to be given as a dictionary called mcmc_params and can include

  • init_samples: initial samples

  • n_steps: number of steps

  • n_walkers: number of walkers

  • n_burn: length of the burn-in

  • moves: function to use for the moves, e.g. taken from emcee

  • multiprocessing: setting for multiprocessing

  • verbose: verbosity

UML diagram for classes related to Bayesian inference.

The observation should be set as Model.observations in the Engine, and an estimation of its uncertainty can be provided as a bayesvalidrox.bayes_inference.discrepancy.Discrepancy object.

Example

For this example we need to add the following imports.

>>> from bayesvalidrox import Discrepancy, BayesInference

In order to run Bayesian inference we first need to provide an observation. For this example we take an evaluation of the model on some chosen sample and add the resulting values as Model.observations. As this expects a 1D-array for each output key, we need to change the format slightly.

>>> true_sample = [[2]]
>>> observation = Model.run_model_parallel(true_sample)
>>> Model.observations = {}
>>> for key in observation:
>>>     if key == 'x_values':
>>>         Model.observations[key]=observation[key]
>>>     else:
>>>         Model.observations[key]=observation[key][0]

Next we define the uncertainty on the observation with the class bayesvalidrox.bayes_inference.discrepancy.Discrepancy. For this example we set the uncertainty to be zero-mean gaussian and dependent on the values in the observation, i.e. larger values have a larger uncertainty associated with them. The parameters contain the variance for each point in the observation.

>>> obsData = pd.DataFrame(Model.observations, columns=Model.Output.names)
>>> DiscrepancyOpts = Discrepancy('')
>>> DiscrepancyOpts.type = 'Gaussian'
>>> DiscrepancyOpts.parameters = obsData**2

Now we can initialize an object of class bayesvalidrox.bayes_inference.bayes_inference.BayesInference with all the wanted properties. This object has to be given our Engine. If it should use the surrogate during inference, set emulator to True, otherwise the model will be evaluated directly.

>>> BayesObj = BayesInference(Engine_)
>>> BayesObj.emulator = True