bayesbay.BayesianInversion
- class bayesbay.BayesianInversion(parameterization, log_likelihood, n_chains=10, walkers_starting_states=None, save_dpred=True)
A high-level class for performing Bayesian inversion using Markov Chain Monte Carlo (McMC) methods.
This is a subclass of
BaseBayesianInversion
.This class provides the basic structure for setting up and running McMC sampling, given user-configured parameterization settings, data targets and corresponding forward functions.
- Parameters:
parameterization (bayesbay.parameterization.Parameterization) – pre-configured parameterization. This includes information about the dimension, parameterization bounds and properties of unknown parameterizations
targets (List[bayesbay.Target]) – a list of data targets
fwd_functions (Callable[[bayesbay.State], np.ndarray]) – a lsit of forward functions corresponding to each data targets provided above. Each function takes in a state and returns a numpy array of data predictions.
walkers_starting_states (List[State]) – a list of starting states for each chain. The states should be an instance of a
bayesbay.State
and their structure should be consistent with the specifiedparameterization
. The length of this list must be equal to the number of chains, i.e.n_chains
n_chains (int, 10 by default) – the number of chains in the McMC sampling
Reference Details
- chains
The
MarkovChain
instances of the current Bayesian inversion
- get_results(keys=None, concatenate_chains=True)
To get the saved states from current inversion
- Parameters:
keys (Union[str, List[str]]) – key(s) to retrieve from the saved states. This will be ignored when states are not of type
State
or dictconcatenate_chains (bool, optional) – whether to aggregate samples from all the Markov chains or to keep them seperate, by default True
- Returns:
a dictionary from name of the attribute stored to the values, or a list of saved states (if the base level API is used, and states are not of type
State
or dict)- Return type:
Union[Dict[str, list], list]
- static get_results_from_chains(chains, keys=None, concatenate_chains=True)
To get the saved states from a list of given Markov chains
- Parameters:
chains (Union[BaseMarkovChain, List[BaseMarkovChain]]) – Markov chain(s) that the results are going to be extracted from
keys (Union[str, List[str]]) – key(s) to retrieve from the saved states. This will be ignored when states are not of type
State
or dictconcatenate_chains (bool, optional) – whether to aggregate samples from all the Markov chains or to keep them seperate, by default True
- Returns:
a dictionary from name of the attribute stored to the values, or a list of saved states (if the base level API is used, and states are not of type
State
or dict)- Return type:
Union[Dict[str, list], list]
- run(sampler=None, n_iterations=1000, burnin_iterations=0, save_every=100, verbose=True, print_every=100, parallel_config=None)
To run the inversion
- Parameters:
sampler (bayesbay.samplers.Sampler, optional) – a sampler instance describing how chains intereact or modify their properties during sampling. This could be a sampler from the module
bayesbay.samplers
such asbayesbay.samplers.VanillaSampler
(default),bayesbay.samplers.ParallelTempering
, orbayesbay.samplers.SimulatedAnnealing
, or a customised sampler instance.n_iterations (int, optional) – total number of iterations to run, by default 1000
burnin_iterations (int, optional) – the iteration number from which we start to save samples, by default 0
save_every (int, optional) – the frequency with which we save the samples. By default a state is saved every after 100 iterations after the burn-in phase
verbose (bool, optional) – whether to print the progress during sampling or not, by default True
print_every (int, optional) – the frequency with which we print the progress and information during the sampling, by default 100 iterations
parallel_config (dict, optional) – keyword arguments passed to
joblib.Parallel
. Ignored whenlen(self.chains)
is 1
- set_perturbation_funcs(perturbation_funcs, perturbation_weights=None)
- update_log_likelihood_targets(targets)
function to be called by
self.log_likelihood
when there are more target(s) added to the inversion. This method updates the perturbation function list for the current inversion and its chains, and initializes thecurrent_state
for each chain when there are hierarchical targets added.This method is called because we register
self
(i.e. the currentBayesianInversion
) as an observer to its log likelihood instance.- Parameters:
targets (List[Target]) – the list of targets added to
self.log_likelihood