bayesbay.BayesianInversion
- class bayesbay.BayesianInversion(parameterization, log_likelihood, n_chains=10, walkers_starting_states=None, save_dpred=True)
A high-level class for Bayesian inversion using Markov chain Monte Carlo.
This is a subclass of
BaseBayesianInversionthat combines perturbations fromparameterizationandlog_likelihoodand initializes abayesbay.MarkovChainfor each walker.- Parameters:
parameterization (bayesbay.parameterization.Parameterization) – Pre-configured parameterization describing model structure, priors, and parameter-space perturbations.
log_likelihood (bayesbay.likelihood.LogLikelihood) – Log-likelihood object used to score proposed states. Its perturbation functions (e.g., hierarchical target updates) are merged with those from
parameterization.n_chains (int, optional) – Number of Markov chains to run. Default is 10.
walkers_starting_states (List[bayesbay.State], optional) – Optional starting state for each chain. If provided, the list length must be equal to
n_chains. IfNone, each chain initializes fromparameterization.save_dpred (bool, optional) – Whether to store predicted data in saved chain states. Default is
True.
- Raises:
TypeError – If
log_likelihoodis not an instance ofbayesbay.likelihood.LogLikelihood.ValueError – If
walkers_starting_statesis provided and its length does not matchn_chains.
Reference Details
- chains
The
MarkovChaininstances 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
Stateor 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
Stateor 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
Stateor 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
Stateor 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.samplerssuch 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_likelihoodwhen 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_statefor 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