jwspecmcmc — Bayesian MCMC fitting
Tip
This is the authors’ recommended fitter for science-quality results.
The least-squares jwspecfit engine is best for quick looks and initial
guesses; for published measurements and any quantity that needs faithful
uncertainties, use jwspecmcmc.
jwspecmcmc replaces the bootstrap uncertainties of jwspecfit with
full Bayesian posterior sampling. You get asymmetric errors, parameter
correlations, flux-ratio posteriors, and (with nautilus) the Bayesian
evidence.
Default sampler: NUTS
The No-U-Turn Sampler (via NumPyro) is the default backend. It is a Hamiltonian Monte Carlo variant with adaptive tree depth that navigates high-dimensional correlated posteriors efficiently:
import jwspecmcmc
result = jwspecmcmc.fit_lines(
spec, z=6.0,
sampler="nuts",
n_warmup=500,
n_samples_nuts=2000,
n_chains=4,
target_accept_prob=0.8,
)
NUTS requires JAX + NumPyro — install via pip install -e ".[nuts]".
Alternative samplers
emcee — affine-invariant ensemble:
result = jwspecmcmc.fit_lines(spec, z=6.0, sampler="emcee", n_steps=2000)
nautilus — importance nested sampling, useful for evidence:
result = jwspecmcmc.fit_lines(spec, z=6.0, sampler="nautilus", n_live=2000)
Results
line = result.lines["OIII_5007"]
line.flux # median flux
line.flux_err # (lower, upper) 68% CI half-widths
line.flux_posterior # 1-D array of posterior samples
ratio = result.flux_ratio_posterior("OIII_5007", "HBETA") # sample-by-sample
Custom priors
from jwspecmcmc import GaussianPrior, LogUniformPrior
result = jwspecmcmc.fit_lines(
spec, z=6.0, sampler="nuts",
prior_overrides={
"A_OIII_5007": GaussianPrior(mean=8e-18, std=1e-18, lo=0, hi=1e-15),
},
)
Available prior classes: UniformPrior, GaussianPrior, LogUniformPrior.
Convergence
result.convergence # {"R_hat": {...}, "ESS": {...}}
Gelman-Rubin R̂ should sit below 1.05 and ESS above a few hundred per parameter for a trusted fit.
Plotting
jwspecmcmc.plot_corner(result, params=["A_OIII_5007", "A_HBETA"])
jwspecmcmc.plot_traces(result)
jwspecmcmc.plot_flux_posterior(result, "OIII_5007")
See the Plotting & visualisation page for full details on the corner/trace/flux-posterior diagnostics and the shared fit plotters.
HDF5 persistence
jwspecmcmc.save_mcmc_result(result, "mcmc.h5")
loaded = jwspecmcmc.load_mcmc_result("mcmc.h5")
# Convert back for jwspecfit plotting:
fit_result = result.to_fit_result()
jwspecfit.plot_fit(fit_result)
Broad Balmer + MCMC
jwspecmcmc.fit_lines delegates BIC broad-Balmer model selection to
jwspecfit.fit_with_broad, then MCMC-samples the winning model. Set
fit_balmer_broad=True (and/or fit_oiii_broad, fit_hei_broad) to
opt in; the default is narrow-only.
Full parameter reference
These tables document every argument of fit_lines. They apply equally
to fit_with_broad, which takes the same arguments but always performs
the BIC broad-selection step (and does not expose init_from_mle).
Required arguments have no default.
Core
Argument |
Default |
What it does |
|---|---|---|
|
(required) |
Input |
|
(required) |
Source redshift. Sets each observed line position via |
|
|
Posterior backend: |
Instrument & line selection
Argument |
Default |
What it does |
|---|---|---|
|
|
NIRSpec grating name (e.g. |
|
|
Resolving power as a scalar or callable |
|
|
Names of lines to fit (keys of |
|
|
|
Continuum
Argument |
Default |
What it does |
|---|---|---|
|
|
Degree of the polynomial continuum. |
|
|
Sigma-clip threshold used when fitting the continuum. |
|
|
Continuum model: |
Initialisation
Argument |
Default |
What it does |
|---|---|---|
|
|
Seed the sampler from a fast least-squares MLE ( |
NUTS controls (sampler="nuts")
Argument |
Default |
What it does |
|---|---|---|
|
|
Warm-up (tuning) iterations per chain before samples are kept; adapts step size and mass matrix. |
|
|
Posterior draws kept per chain after warm-up. Total samples = |
|
|
Number of independent chains run in parallel; enables the R-hat diagnostic. |
|
|
Target acceptance probability for step-size adaptation. Raise toward 0.95 if you see divergences. |
|
|
Maximum NUTS tree depth; caps leapfrog steps per iteration at |
emcee controls (sampler="emcee")
Argument |
Default |
What it does |
|---|---|---|
|
|
Number of ensemble walkers. |
|
|
Number of steps per walker. |
|
|
Burn-in steps discarded. If |
nautilus controls (sampler="nautilus")
Argument |
Default |
What it does |
|---|---|---|
|
|
Number of live points. |
|
|
Target effective posterior sample size. |
Broad-component selection (BIC; opt-in)
Argument |
Default |
What it does |
|---|---|---|
|
|
Run BIC selection for a broad Balmer component (narrow / intermediate / very-broad / both), gated by Hα SNR. |
|
|
Independent BIC test for a broad [OIII] 4959/5007 component (outflow signature), gated by [OIII] 5007 SNR. |
|
|
Independent BIC test for a broad He I component shared across all observable He I lines, gated by the best narrow He I SNR. |
|
|
Bootstrap iterations for BIC model selection; |
|
|
Parallel workers for the BIC bootstrap; |
|
|
Minimum Hα SNR to attempt Balmer broad fitting. |
|
|
Minimum [OIII] 5007 SNR to attempt [OIII] broad fitting. |
|
|
Minimum He I SNR (best narrow He I line) to attempt He I broad fitting. |
|
|
Minimum ΔBIC by which a more complex model must beat the simpler one to be selected. |
Kinematic ties & doublets
Argument |
Default |
What it does |
|---|---|---|
|
|
Tie narrow Balmer (and [NII]) widths and centroids to [OIII] 5007 in velocity space. |
|
|
Tie UV doublet kinematics and fix resonance-line flux ratios. Recommended for stacked / poorly resolved spectra. |
|
|
When |
|
|
Tie the widths of the UV intercombination lines (CIII], NIV, NIII, SiIII) to a single shared velocity dispersion. |
|
|
If set, fix the N IV] flux ratio |
|
|
If set, fix the C III] flux ratio |
Line-bound overrides
Argument |
Default |
What it does |
|---|---|---|
|
|
Per-line width bounds |
|
|
Per-line centroid bounds |
|
|
Multiplier on the upper line-width bound; use > 1 for stacked spectra with broadened lines. |
|
|
Maximum centroid offset, in km/s, allowed for each line. |
|
|
Extra resolution-aware cap on narrow-line centroid offsets: |
Priors & misc
Argument |
Default |
What it does |
|---|---|---|
|
|
Per-parameter prior overrides keyed by name ( |
|
|
Show a progress bar during sampling. |
|
|
Random seed for initialisation and sampling. |