astrocook.fitting.bayesian_fitter module#

class astrocook.fitting.bayesian_fitter.BayesianVoigtFitter(spec: Any, systs: Any, z_window_kms: float = 20.0)[source]#

Bases: object

Bayesian engine for Voigt profile fitting.

This class wraps a VoigtFitterV2 instance to perform Markov Chain Monte Carlo (MCMC) or Nested Sampling using the exact same physical model, constraints, and data preparation.

Parameters:
  • spec (SpectrumV2) – The spectral data to fit.

  • systs (SystemListV2) – The system list containing the components to model.

  • z_window_kms (float, optional) – Velocity window (km/s) around component centers to include in the fit mask. Defaults to 20.0.

__init__(spec: Any, systs: Any, z_window_kms: float = 20.0)[source]#
ln_likelihood(p_free: ndarray) float[source]#

Compute the log-likelihood of a parameter vector.

Evaluates the standard Gaussian log-likelihood for the active fit region, incorporating any penalty terms (e.g., Doppler parameter runaways) inherited from the frequentist fitter.

Parameters:

p_free (np.ndarray) – The vector of free parameters to evaluate.

Returns:

The computed log-likelihood, or -np.inf if evaluation fails.

Return type:

float

ln_prior(p_free: ndarray) float[source]#

Compute the log-prior probability of a parameter vector.

Applies a uniform flat prior defined by the physical boundaries of the free parameters (e.g., column density between 10.0 and 23.0).

Parameters:

p_free (np.ndarray) – The vector of free parameters.

Returns:

0.0 if the parameters are within physical bounds, -np.inf otherwise.

Return type:

float

ln_posterior(p_free: ndarray) float[source]#

Compute the total log-posterior probability.

Calculates the sum of the log-prior and log-likelihood.

Parameters:

p_free (np.ndarray) – The vector of free parameters.

Returns:

The log-posterior probability, or -np.inf if outside prior bounds.

Return type:

float

run_mcmc(nwalkers: int = 32, nsteps: int = 1000, burn_in: int = 200, progress: bool = True) Dict[str, Any][source]#

Execute Markov Chain Monte Carlo (MCMC) sampling using emcee.

Initializes walkers around the initial parameter guess and samples the posterior distribution.

Parameters:
  • nwalkers (int, optional) – Number of MCMC walkers. Defaults to 32.

  • nsteps (int, optional) – Number of sampling steps per walker. Defaults to 1000.

  • burn_in (int, optional) – Number of initial steps to discard as burn-in. Defaults to 200.

  • progress (bool, optional) – If True, pipes progress percentage to the logger. Defaults to True.

Returns:

A dictionary containing: - 'samples': The flattened, burn-in discarded chain (np.ndarray). - 'sampler': The raw emcee.EnsembleSampler object. - 'medians': The median values for each parameter (np.ndarray). - 'std': The standard deviations for each parameter (np.ndarray).

Return type:

dict

Raises:

ImportError – If the emcee package is not installed.

run_nested(nlive: int = 250, progress: bool = True) Dict[str, Any][source]#

Execute Nested Sampling using dynesty.

Samples the parameter space to estimate both the posterior distribution and the Bayesian evidence (log-Z).

Parameters:
  • nlive (int, optional) – Number of live points for the nested sampler. Defaults to 250.

  • progress (bool, optional) – If True, pipes progress (dLogZ) to the logger. Defaults to True.

Returns:

A dictionary containing: - 'results': The raw dynesty results object. - 'logz': The estimated log-evidence. - 'logzerr': The error on the log-evidence. - 'samples': The posterior samples (np.ndarray). - 'medians': The median values for each parameter (np.ndarray). - 'std': The standard deviations for each parameter (np.ndarray).

Return type:

dict

Raises:

ImportError – If the dynesty package is not installed.

apply_results(res: Dict[str, Any]) Any[source]#

Apply Bayesian medians and uncertainties back to the system list.

Extracts the posterior medians and standard deviations, maps them back to the full parameter vector, and updates the corresponding components in the system list. Fixed or linked parameters maintain their prior state.

Parameters:

res (dict) – The results dictionary returned by run_mcmc() or run_nested().

Returns:

A new system list containing the updated parameters and Bayesian errors.

Return type:

SystemListV2