MapLines.tools.mcmc¶
MCMC routines used by MapLines.
MapLines.tools.mcmc¶
MCMC utilities for spectral fitting in MapLines.
This module provides helper routines to run Markov Chain Monte Carlo sampling and to evaluate posterior model realizations from the resulting chains.
The main functionality includes:
execution of the
emceeensemble sampleroptional multiprocessing support
burn-in and production runs
extraction of representative model realizations from posterior samples
These routines are used by the fitting functions in
MapLines.tools.line_fit and rely on the spectral models defined in
MapLines.tools.models.
Notes
The sampler is designed for flexible use with the posterior probability
functions defined in MapLines.tools.priors and supports both serial
and multiprocessing execution modes.
- MapLines.tools.mcmc.mcmc(p0, nwalkers, niter, ndim, lnprob, data, verbose=False, multi=True, tim=False, ncpu=10)[source]¶
Run an MCMC sampler using the
emceeensemble sampler.This function performs parameter estimation for spectral models using a Markov Chain Monte Carlo (MCMC) approach. It supports both serial and multiprocessing execution.
- Parameters:
p0 (array_like) – Initial positions of the walkers in parameter space.
nwalkers (int) – Number of walkers used by the sampler.
niter (int) – Number of iterations in the production run.
ndim (int) – Number of model parameters.
lnprob (callable) – Log-probability function used by the sampler.
data (tuple) – Additional arguments passed to
lnprob.verbose (bool, optional) – Print progress messages.
multi (bool, optional) – Enable multiprocessing.
tim (bool, optional) – Measure execution time.
ncpu (int, optional) – Number of CPUs used when multiprocessing.
- Returns:
sampler (emcee.EnsembleSampler) – MCMC sampler object.
pos (ndarray) – Final walker positions.
prob (ndarray) – Log-probability values.
state (object) – Internal sampler state.
Notes
This function performs a burn-in phase before the production run.
- MapLines.tools.mcmc.sample_walkers(nsamples, flattened_chain, waves0, fac0, facN0, velfac0, velfacN0, fwhfac0, fwhfacN0, names0, n_lines, vals, x=0, skew=False, lorentz=False, outflow=False, powlaw=False, feii=False, data=None)[source]¶
Generate model realizations from the posterior MCMC samples.
This function randomly samples walkers from the flattened MCMC chain and computes the corresponding spectral models.
- Parameters:
nsamples (int) – Number of models to draw from the chain.
flattened_chain (ndarray) – Flattened MCMC chain.
waves0 (array_like) – Rest wavelengths of spectral lines.
fac0 (array_like) – Flux normalization factors.
facN0 (array_like) – Flux normalization factors.
velfac0 (array_like) – Velocity scaling factors.
velfacN0 (array_like) – Velocity scaling factors.
fwhfac0 (array_like) – Line-width scaling factors.
fwhfacN0 (array_like) – Line-width scaling factors.
names0 (list) – Names of the spectral components.
n_lines (int) – Number of emission lines.
vals (dict) – Additional model parameters.
x (array_like, optional) – Spectral axis.
skew (bool) – Flags controlling model components.
lorentz (bool) – Flags controlling model components.
outflow (bool) – Flags controlling model components.
powlaw (bool) – Flags controlling model components.
feii (bool) – Flags controlling model components.
data (optional) – Additional data passed to the model.
- Returns:
med_model (ndarray) – Median model spectrum.
spread (ndarray) – Standard deviation of the sampled models.
Notes
The function uses the spectral model defined in
MapLines.tools.models. :contentReference[oaicite:0]{index=0}