optimpv.BayesInfEmcee package

Submodules

optimpv.BayesInfEmcee.EmceeOptimizer module

class optimpv.BayesInfEmcee.EmceeOptimizer.EmceeOptimizer(params=None, agents=None, nwalkers=20, nsteps=1000, burn_in=100, progress=True, name='emcee_opti', **kwargs)[source]

Bases: BaseAgent

Optimizer using the emcee library for MCMC Bayesian inference. Inherits from BaseAgent and interacts with Agent objects.

create_metrics_list()[source]

Create a list of all metrics from all agents.

Returns:

List of metric names

Return type:

list

create_search_space(params)[source]

Create search space details (initial vector, bounds, mapping) from FitParam list.

Parameters:

params (list of FitParam) – List of FitParam objects defining the parameters to optimize.

Returns:

x0array

Initial parameter vector for optimization (potentially log-transformed).

boundslist of tuples

List of (lower, upper) bound tuples for optimization vector.

param_mappinglist

List of parameter names corresponding to x0 elements.

log_params_indiceslist

Indices of parameters optimized in log10 space.

Return type:

tuple

Raises:

ValueError – If a parameter type is unsupported (not ‘float’).

get_best_params(method='max_likelihood')[source]

Return the ‘best’ parameters based on the MCMC samples. This method allows the user to specify how to determine the ‘best’ parameters

Parameters:

method (str) – How to determine ‘best’ params (‘median’, ‘mean’, ‘max_likelihood’). ‘median’ - median of the samples ‘mean’ - mean of the samples ‘max_likelihood’ - maximum likelihood estimate (MLE) based on the log-probability samples ‘max_likelihood’ is the default method.

Returns:

Dictionary of best parameter values in original space.

Return type:

dict

Raises:

ValueError – If the method is not one of ‘median’, ‘mean’, or ‘max_likelihood’.

get_chain(**kwargs)[source]

Return the MCMC chain. kwargs passed to sampler.get_chain()

get_flat_samples()[source]

Return the flattened samples after burn-in and thinning.

initialize_walkers()[source]
optimize()[source]

Run the MCMC optimization using emcee.

plot_corner(**kwargs)[source]

Generate a corner plot of the posterior distribution.

plot_traces(**kwargs)[source]

Plot the MCMC traces for each parameter.

reconstruct_params(x_opt)[source]

Reconstruct a full parameter dictionary from an optimization vector x_opt.

Parameters:

x_opt (array-like) – Parameter vector from the optimizer (potentially log-transformed).

Returns:

Dictionary mapping full parameter names to their values.

Return type:

dict

Raises:

ValueError – If a parameter type is unsupported (not ‘float’).

update_params_with_best_balance(method='max_likelihood', return_best_balance=False)[source]

Update the parameters with the best balance based on MCMC results. This method updates the parameters in self.params with the best values determined by the specified method. It can also return the best parameters dictionary if requested.

Parameters:
  • method (str, optional) – method to determine ‘best’ params (‘median’, ‘mean’, ‘max_likelihood’), by default ‘max_likelihood’

  • return_best_balance (bool, optional) – If True, return the best parameters dictionary, by default False

Module contents