jwspecmcmc

jwspecmcmc — MCMC emission-line fitting for JWST NIRSpec spectra.

A companion to jwspecfit that replaces bootstrap uncertainties with full Bayesian posterior sampling via emcee, nautilus, or NUTS.

By default, fit_lines() runs a narrow-only MCMC fit. Two independent BIC-based broad component selections can be opted in:

  • fit_balmer_broad=True — Balmer broad (narrow vs. intermediate / very-broad / both, on Balmer pixels).

  • fit_oiii_broad=True — [OIII] outflow broad (on OIII pixels).

Either or both can be enabled.

Example

>>> import jwspecfit, jwspecmcmc
>>> spec = jwspecfit.read_fits("spectrum.fits")
>>> result = jwspecmcmc.fit_lines(spec, z=6.0, sampler="nuts")
>>> result.selected_model          # "narrow" | "broad1" | "broad2" | "both"
>>> result.oiii_broad_selected     # bool — independent of selected_model
>>> result.lines["OIII_5007"].flux_err  # asymmetric 68% CI
>>> ratio = result.flux_ratio_posterior("OIII_5007", "HBETA")

Functions

fit_lines(spectrum, z, *[, sampler, ...])

Fit emission lines using MCMC sampling.

fit_with_broad(spectrum, z, *[, sampler, ...])

Fit emission lines with BIC-based broad selection, then MCMC.

plot_corner(*args, **kwargs)

Corner plot of posterior samples.

plot_flux_posterior(*args, **kwargs)

Flux posterior histogram for a single line.

plot_traces(*args, **kwargs)

Trace plots of MCMC chains.

class jwspecmcmc.GaussianPrior(mean, std, lo=-inf, hi=inf)[source]

Bases: Prior

Truncated Gaussian prior.

Parameters:
  • mean (float) – Mean of the Gaussian.

  • std (float) – Standard deviation.

  • lo (float) – Hard lower bound (-inf for unbounded).

  • hi (float) – Hard upper bound (+inf for unbounded).

hi: float = inf
lo: float = -inf
log_prob(x)[source]

Return the log-probability at x.

Parameters:

x (float) – Parameter value.

Returns:

Log-probability (-inf if outside support).

Return type:

float

mean: float
sample(rng, size=1)[source]

Draw random samples from the prior.

Parameters:
Returns:

Samples of shape (size,).

Return type:

np.ndarray

std: float
class jwspecmcmc.LogUniformPrior(lo, hi)[source]

Bases: Prior

Log-uniform (Jeffreys) prior on [lo, hi] with lo > 0.

Parameters:
  • lo (float) – Lower bound (must be positive).

  • hi (float) – Upper bound.

hi: float
lo: float
log_prob(x)[source]

Return the log-probability at x.

Parameters:

x (float) – Parameter value.

Returns:

Log-probability (-inf if outside support).

Return type:

float

sample(rng, size=1)[source]

Draw random samples from the prior.

Parameters:
Returns:

Samples of shape (size,).

Return type:

np.ndarray

class jwspecmcmc.MCMCBroadFitResult(mcmc_result, selected_model, bic_narrow, bic_broad1, bic_broad2, bic_both, oiii_selected='off', bic_oiii_off=nan, bic_oiii_broad1=nan, bic_oiii_broad2=nan, bic_oiii_both=nan, hei_selected='off', bic_hei_off=nan, bic_hei_broad1=nan, bic_hei_broad2=nan, bic_hei_both=nan)[source]

Bases: object

MCMC result with BIC-based broad Balmer component selection.

Wraps an MCMCResult (full MCMC posteriors on the winning model) together with BIC model-selection metadata.

Parameters:
  • mcmc_result (MCMCResult) – Full MCMC posteriors for the selected model.

  • selected_model (str) – Model name: "narrow", "broad1", "broad2", or "both".

  • bic_narrow (float) – BIC for narrow-only model.

  • bic_broad1 (float) – BIC for narrow + intermediate broad model (NaN if not attempted).

  • bic_broad2 (float) – BIC for narrow + very broad model (NaN if not attempted).

  • bic_both (float) – BIC for narrow + both broad components (NaN if not attempted).

  • oiii_selected (str)

  • bic_oiii_off (float)

  • bic_oiii_broad1 (float)

  • bic_oiii_broad2 (float)

  • bic_oiii_both (float)

  • hei_selected (str)

  • bic_hei_off (float)

  • bic_hei_broad1 (float)

  • bic_hei_broad2 (float)

  • bic_hei_both (float)

bic_both: float
bic_broad1: float
bic_broad2: float
bic_hei_both: float = nan
bic_hei_broad1: float = nan
bic_hei_broad2: float = nan
bic_hei_off: float = nan
bic_narrow: float
bic_oiii_both: float = nan
bic_oiii_broad1: float = nan
bic_oiii_broad2: float = nan
bic_oiii_off: float = nan
property chains: ndarray | None

Raw walker chains (emcee) or None (nautilus).

property constraints: ConstraintSet | None

Applied constraints.

property continuum: ndarray

Continuum estimate.

property convergence: dict[str, Any]

Convergence diagnostics.

doublet_flux_posterior(line_a, line_b)[source]

Combined flux posterior for a doublet.

Delegates to MCMCResult.doublet_flux_posterior().

Return type:

ndarray

Parameters:
doublet_snr(line_a, line_b)[source]

SNR of a combined doublet flux.

Delegates to MCMCResult.doublet_snr().

Return type:

float

Parameters:
property flat_chains: ndarray

Flattened posterior samples (full parameter space).

property flat_chains_free: ndarray

Flattened posterior samples (free parameter space).

property flat_log_prob: ndarray

Log-posterior for each sample.

flux_ratio_posterior(line_a, line_b)[source]

Compute the posterior distribution of a flux ratio.

Delegates to MCMCResult.flux_ratio_posterior().

Parameters:
  • line_a (str) – Numerator line name.

  • line_b (str) – Denominator line name.

Returns:

Posterior samples of flux(line_a) / flux(line_b).

Return type:

np.ndarray

flux_upper_limit(line_name, n_sigma=3.0)[source]

Noise-based flux upper limit for a line.

Delegates to MCMCResult.flux_upper_limit().

Return type:

float | None

Parameters:
flux_upper_limits(line_names=None, n_sigma=3.0, snr_threshold=3.0)[source]

Noise-based upper limits for low-SNR lines.

Delegates to MCMCResult.flux_upper_limits().

Return type:

dict[str, float]

Parameters:
property hei_broad_selected: bool

True if any HeI broad component was selected.

Type:

Convenience

hei_selected: str = 'off'
property line_names: list[str]

Ordered line names.

property lines: dict[str, MCMCLineResult]

Per-line posterior summaries.

mcmc_result: MCMCResult
property model_flux: ndarray

Median model flux (continuum-subtracted).

property oiii_broad_selected: bool

True if any OIII broad component was selected.

Type:

Convenience

oiii_selected: str = 'off'
property params: ndarray

Median posterior in the full parameter space.

property sampler_meta: dict[str, Any]

Additional sampler metadata.

property sampler_name: str

Name of the sampler used.

selected_model: str
property spectrum: Spectrum

Input spectrum.

to_fit_result()[source]

Convert to a jwspecfit.fitter.FitResult.

Delegates to MCMCResult.to_fit_result().

Return type:

FitResult

class jwspecmcmc.MCMCLineResult(name, rest_wave_A, amplitude, amplitude_err, centroid_A, centroid_err, sigma_A, sigma_err, flux, flux_err, flux_posterior, ew_A, snr)[source]

Bases: object

MCMC posterior summary for a single emission line.

Parameters:
  • name (str) – Line name.

  • rest_wave_A (float) – Rest-frame wavelength (Angstrom).

  • amplitude (float) – Median posterior amplitude.

  • amplitude_err (tuple of float) – (lower, upper) 68% credible interval half-widths on amplitude.

  • centroid_A (float) – Median posterior centroid (Angstrom).

  • centroid_err (tuple of float) – (lower, upper) 68% CI half-widths on centroid.

  • sigma_A (float) – Median posterior sigma (Angstrom).

  • sigma_err (tuple of float) – (lower, upper) 68% CI half-widths on sigma.

  • flux (float) – Median integrated flux (= amplitude for area-normalised Gaussians).

  • flux_err (tuple of float) – (lower, upper) 68% CI half-widths on flux.

  • flux_posterior (np.ndarray) – Full flux posterior samples.

  • ew_A (float) – Median rest-frame equivalent width (Angstrom).

  • snr (float) – Signal-to-noise ratio (flux / mean of flux_err tuple).

amplitude: float
amplitude_err: tuple[float, float]
centroid_A: float
centroid_err: tuple[float, float]
ew_A: float
flux: float
flux_err: tuple[float, float]
flux_posterior: ndarray
name: str
rest_wave_A: float
sigma_A: float
sigma_err: tuple[float, float]
snr: float
class jwspecmcmc.MCMCResult(lines, flat_chains, flat_chains_free, flat_log_prob, chains, params, model_flux, continuum, spectrum, line_names=<factory>, constraints=None, convergence=<factory>, sampler_name='', sampler_meta=<factory>, lya_params=None)[source]

Bases: object

Container for a complete MCMC fitting result.

Parameters:
  • lines (dict of MCMCLineResult) – Per-line posterior summaries, keyed by line name.

  • flat_chains (np.ndarray) – Flattened posterior samples in the full parameter space, shape (n_samples, 3 * n_lines).

  • flat_chains_free (np.ndarray) – Flattened posterior samples in the free parameter space, shape (n_samples, n_free).

  • flat_log_prob (np.ndarray) – Log-posterior for each sample.

  • chains (np.ndarray or None) – Raw chains of shape (n_walkers, n_steps, n_free) (emcee) or None (nautilus).

  • params (np.ndarray) – Median posterior in the full parameter space.

  • model_flux (np.ndarray) – Median model flux (µJy, continuum-subtracted).

  • continuum (np.ndarray) – Continuum estimate (µJy).

  • spectrum (Spectrum) – Input spectrum.

  • line_names (list of str) – Ordered line names.

  • constraints (ConstraintSet) – Applied constraints.

  • convergence (dict) – Convergence diagnostics (R-hat, ESS).

  • sampler_name (str) – Name of the sampler used ("emcee" or "nautilus").

  • sampler_meta (dict) – Additional sampler metadata (n_walkers, n_steps, etc.).

  • lya_params (ndarray | None)

chains: ndarray | None
constraints: ConstraintSet | None = None
continuum: ndarray
convergence: dict[str, Any]
doublet_flux_posterior(line_a, line_b)[source]

Compute the combined flux posterior for a doublet.

Sums the flux posteriors sample-by-sample, giving the correct combined distribution even when individual members are degenerate (e.g. blended CIII] 1907+1909).

Parameters:
  • line_a (str) – First doublet member.

  • line_b (str) – Second doublet member.

Returns:

Posterior samples of flux(line_a) + flux(line_b).

Return type:

np.ndarray

doublet_snr(line_a, line_b)[source]

Compute the SNR of a combined doublet flux.

Uses the summed flux posterior to get a single SNR that is not degraded by the amplitude degeneracy between blended members.

Parameters:
  • line_a (str) – First doublet member.

  • line_b (str) – Second doublet member.

Returns:

SNR of the combined doublet.

Return type:

float

flat_chains: ndarray
flat_chains_free: ndarray
flat_log_prob: ndarray
flux_ratio_posterior(line_a, line_b)[source]

Compute the posterior distribution of a flux ratio.

Parameters:
  • line_a (str) – Numerator line name.

  • line_b (str) – Denominator line name.

Returns:

Posterior samples of flux(line_a) / flux(line_b).

Return type:

np.ndarray

Raises:

KeyError – If either line is not in the result.

flux_upper_limit(line_name, n_sigma=3.0)[source]

Compute a noise-based flux upper limit for a line.

Uses the local RMS of the continuum-and-model-subtracted residuals near the line, multiplied by the line width and n_sigma. This is the standard approach for non-detections and is independent of the posterior (which may be prior- dominated for weak lines).

Parameters:
  • line_name (str) – Line name.

  • n_sigma (float) – Number of sigma for the upper limit (default 3).

Returns:

Integrated flux upper limit in f_lam units, or None if there are insufficient pixels near the line.

Return type:

float or None

flux_upper_limits(line_names=None, n_sigma=3.0, snr_threshold=3.0)[source]

Compute noise-based upper limits for low-SNR lines.

Parameters:
  • line_names (list of str, optional) – Lines to check. If None, checks all fitted lines.

  • n_sigma (float) – Number of sigma for the upper limit (default 3).

  • snr_threshold (float) – Only compute upper limits for lines with SNR below this (default 3.0). Lines above this threshold are detections.

Returns:

dict of {str{line_name: flux_upper_limit} for each line below the SNR threshold.

Return type:

float}

line_names: list[str]
lines: dict[str, MCMCLineResult]
lya_params: ndarray | None = None
model_flux: ndarray
params: ndarray
sampler_meta: dict[str, Any]
sampler_name: str = ''
spectrum: Spectrum
to_fit_result()[source]

Convert to a jwspecfit.fitter.FitResult.

Uses the median posterior as the best fit and the mean of the asymmetric 68% CI as the symmetric flux error.

Return type:

FitResult

class jwspecmcmc.PriorSet(priors=<factory>)[source]

Bases: object

Collection of priors indexed by free-parameter position.

Parameters:

priors (list of Prior) – One prior per free parameter.

log_prior(p_free)[source]

Evaluate the total log-prior for a free-parameter vector.

Parameters:

p_free (np.ndarray) – Free parameter values (length n_dim).

Returns:

Sum of individual log-priors (-inf if any parameter is outside its support).

Return type:

float

property n_dim: int

Number of free parameters.

priors: list[Prior]
sample(rng)[source]

Draw one sample from the joint prior.

Parameters:

rng (numpy.random.Generator) – Random number generator.

Returns:

Sample of shape (n_dim,).

Return type:

np.ndarray

class jwspecmcmc.UniformPrior(lo, hi)[source]

Bases: Prior

Uniform (flat) prior on [lo, hi].

Parameters:
  • lo (float) – Lower bound.

  • hi (float) – Upper bound.

hi: float
lo: float
log_prob(x)[source]

Return the log-probability at x.

Parameters:

x (float) – Parameter value.

Returns:

Log-probability (-inf if outside support).

Return type:

float

sample(rng, size=1)[source]

Draw random samples from the prior.

Parameters:
Returns:

Samples of shape (size,).

Return type:

np.ndarray

jwspecmcmc.fit_lines(spectrum, z, *, sampler='nuts', grating=None, R=None, lines=None, wave_range_A=None, deg=2, clip_sigma=2.5, init_from_mle=True, prior_overrides=None, n_walkers='auto', n_steps=2000, n_burn=None, n_live=2000, n_eff=10000, n_warmup=500, n_samples_nuts=2000, n_chains=6, target_accept_prob=0.8, max_tree_depth=10, progress=True, seed=42, fit_balmer_broad=False, fit_oiii_broad=False, fit_hei_broad=False, n_boot_bic=100, n_jobs=-1, snr_threshold=5.0, oiii_snr_threshold=5.0, hei_snr_threshold=5.0, bic_delta=6.0, sigma_factor=1.0, centroid_vmax=500.0, centroid_max_sigma=1.0, moving_average=False, tie_balmer_to_oiii=True, tie_uv_doublets=True, tie_uv_centroids=True, tie_uv_widths=True, sigma_overrides=None, centroid_overrides=None, niv_doublet_ratio=None, ciii_doublet_ratio=None)[source]

Fit emission lines using MCMC sampling.

Narrow-only MCMC fit by default. Three independent BIC-based broad component tests can be opted in to:

  • fit_balmer_broad=True — Balmer broad selection.

  • fit_oiii_broad=True — [OIII] outflow selection.

  • fit_hei_broad=True — He I broad selection (shared kinematics across all observable HeI lines).

Any combination can be enabled.

A grouped, tabular version of every argument below is available in the user guide: jwspecmcmc — Bayesian MCMC fitting.

Parameters:
  • spectrum (Spectrum) – Input spectrum (observed wavelength, flux, and error arrays).

  • z (float) – Source redshift. Sets each observed line position via lambda_obs = lambda_rest * (1 + z).

  • sampler (str) – Posterior-sampling backend: "nuts" (default; NumPyro Hamiltonian Monte Carlo), "emcee" (affine-invariant ensemble), or "nautilus" (importance nested sampling, which also returns the Bayesian evidence).

  • grating (str, optional) – NIRSpec grating name (e.g. "G395H"), used to set the resolution curve. If None, estimated from the pixel spacing.

  • R (float or callable, optional) – Resolving power as a scalar or a callable R(lambda_um). Overrides grating when given; if None it is taken from grating or the pixel spacing.

  • lines (list of str, optional) – Names of emission lines to fit (keys of jwspecfit.lines.REST_LINES_A). If None, the lines that fall inside the spectrum at redshift z are auto-selected.

  • wave_range_A (tuple of float, optional) – (lo, hi) observed-frame wavelength window in Angstrom to restrict the fit to. If None, the full spectrum is used.

  • deg (int) – Degree of the polynomial continuum (default 2).

  • clip_sigma (float) – Sigma-clip threshold used when fitting the continuum (default 2.5).

  • moving_average (bool or int) – Continuum model. False (default) fits a polynomial of degree deg; True uses a 75-pixel running-median filter; an int uses a running-median filter of that window size.

  • init_from_mle (bool) – If True (default), seed the sampler from a fast least-squares MLE (via jwspecfit.fit_lines()). Strongly recommended; it speeds convergence and reduces stuck chains.

  • n_warmup (int) – NUTS only. Warm-up (tuning) iterations per chain before samples are kept; adapts the step size and mass matrix (default 500).

  • n_samples_nuts (int) – NUTS only. Posterior draws kept per chain after warm-up; the total number of samples is n_chains * n_samples_nuts (default 2000).

  • n_chains (int) – NUTS only. Number of independent chains run in parallel; enables the Gelman-Rubin R-hat diagnostic (default 6).

  • target_accept_prob (float) – NUTS only. Target acceptance probability for step-size adaptation. Raise toward 0.95 if the sampler reports divergences (default 0.8).

  • max_tree_depth (int) – NUTS only. Maximum NUTS binary-tree depth, capping the leapfrog steps per iteration at 2 ** max_tree_depth (default 10).

  • n_walkers (int or str) – emcee only. Number of ensemble walkers; "auto" (default) chooses a value from the parameter count and CPU cores (must be at least 2 * n_dim).

  • n_steps (int) – emcee only. Number of steps per walker (default 2000).

  • n_burn (int or None) – emcee only. Burn-in steps discarded; if None (default) it is estimated automatically from the integrated autocorrelation time.

  • n_live (int) – nautilus only. Number of live points (default 2000).

  • n_eff (int) – nautilus only. Target effective posterior sample size (default 10000).

  • fit_balmer_broad (bool) – If True, run BIC selection for a broad Balmer component (narrow vs. intermediate vs. very-broad vs. both), gated by the Hα SNR. Default False (narrow-only).

  • fit_oiii_broad (bool) – If True, run an independent BIC test for a broad component on [OIII] 4959/5007 (an outflow signature), gated by the [OIII] 5007 SNR. Default False.

  • fit_hei_broad (bool) – If True, run an independent BIC test for a broad He I component shared (with common kinematics per tier) across all observable He I lines, gated by the best narrow He I SNR. Default False.

  • n_boot_bic (int) – Bootstrap iterations for the BIC model selection; 0 uses a single-point BIC. Only used when a broad flag is set (default 100).

  • n_jobs (int) – Number of parallel workers for the BIC bootstrap; -1 uses all cores (default -1).

  • snr_threshold (float) – Minimum Hα SNR required to attempt Balmer broad fitting (default 5.0).

  • oiii_snr_threshold (float) – Minimum [OIII] 5007 SNR required to attempt [OIII] broad fitting (default 5.0).

  • hei_snr_threshold (float) – Minimum He I SNR (of the best narrow He I line) required to attempt He I broad fitting (default 5.0).

  • bic_delta (float) – Minimum ΔBIC by which a more complex model must beat the simpler one to be selected (default 6.0).

  • tie_balmer_to_oiii (bool) – If True (default), tie the narrow Balmer (and [NII]) line widths and centroids to [OIII] 5007 in velocity space.

  • tie_uv_doublets (bool) – If True (default), tie UV doublet kinematics and fix resonance-line flux ratios. Recommended for stacked or poorly resolved spectra.

  • tie_uv_centroids (bool) – When tie_uv_doublets is on, tie each UV doublet’s secondary centroid to its primary in velocity space; if False the secondary centroids are free (default True).

  • tie_uv_widths (bool) – If True (default), tie the widths of the UV intercombination lines (CIII], NIV, NIII, SiIII) to a single shared velocity dispersion.

  • niv_doublet_ratio (float or None) – If given, fix the N IV] flux ratio F(1483) / F(1486) to this value; None (default) leaves the two amplitudes free.

  • ciii_doublet_ratio (float or None) – If given, fix the C III] flux ratio F(1909) / F(1907) to this value; None (default) leaves the two amplitudes free.

  • sigma_overrides (dict, optional) – Per-line width bounds {line_name: (sigma_lo, sigma_hi)} in Angstrom, overriding the automatic grating-based bounds.

  • centroid_overrides (dict, optional) – Per-line centroid bounds {line_name: (mu_lo, mu_hi)} in Angstrom, overriding the automatic velocity-based bounds.

  • sigma_factor (float) – Multiplier applied to the upper line-width bound; use values > 1 for stacked spectra whose lines are broadened (default 1.0).

  • centroid_vmax (float) – Maximum centroid offset, in km/s, allowed for each line (default 500.0).

  • centroid_max_sigma (float) – Extra resolution-aware cap on narrow-line centroid offsets: the margin is min(centroid_vmax-based, centroid_max_sigma * sigma_instrument) (default 1.0).

  • prior_overrides (dict, optional) – Per-parameter prior overrides keyed by parameter name, e.g. {"A_OIII_5007": GaussianPrior(1e-17, 1e-18, 0, 1e-15)}. Keys follow the A_<line> / mu_<line> / sigma_<line> convention; values are Prior instances.

  • progress (bool) – Show a progress bar during sampling (default True).

  • seed (int) – Random seed for initialisation and sampling (default 42).

Return type:

MCMCResult | MCMCBroadFitResult

Returns:

  • MCMCBroadFitResult – When at least one broad flag is True. Delegates all MCMCResult attributes via properties.

  • MCMCResult – When all broad flags are False.

jwspecmcmc.fit_with_broad(spectrum, z, *, sampler='nuts', grating=None, R=None, lines=None, wave_range_A=None, deg=2, clip_sigma=2.5, fit_balmer_broad=False, fit_oiii_broad=False, fit_hei_broad=False, n_boot_bic=100, n_jobs=-1, snr_threshold=5.0, oiii_snr_threshold=5.0, hei_snr_threshold=5.0, bic_delta=6.0, prior_overrides=None, n_walkers='auto', n_steps=2000, n_burn=None, n_live=2000, n_eff=10000, n_warmup=500, n_samples_nuts=2000, n_chains=6, target_accept_prob=0.8, max_tree_depth=10, progress=True, seed=42, sigma_factor=1.0, centroid_vmax=500.0, centroid_max_sigma=1.0, moving_average=False, tie_balmer_to_oiii=True, tie_uv_doublets=True, tie_uv_centroids=True, tie_uv_widths=True, sigma_overrides=None, centroid_overrides=None, niv_doublet_ratio=None, ciii_doublet_ratio=None)[source]

Fit emission lines with BIC-based broad selection, then MCMC.

Phase 1 uses jwspecfit.fit_with_broad() (fast least-squares) for BIC model selection. Phase 2 runs MCMC on the winning model.

Unlike fit_lines(), this entry point always performs the BIC broad-selection step, so the broad flags below are its primary controls. Every other argument (samplers, continuum, kinematic ties, doublet ratios, line-bound overrides, priors) is identical to fit_lines(); see that function’s docstring or the grouped tables in jwspecmcmc — Bayesian MCMC fitting for the full reference.

Parameters:
  • spectrum (Spectrum) – Input spectrum (observed wavelength, flux, and error arrays).

  • z (float) – Source redshift.

  • fit_balmer_broad (bool) – If True, run BIC selection for a broad Balmer component (narrow vs. intermediate vs. very-broad vs. both), gated by the Hα SNR. Default False.

  • fit_oiii_broad (bool) – If True, run an independent BIC test for a broad [OIII] 4959/5007 component (outflow signature), gated by the [OIII] 5007 SNR. Default False.

  • fit_hei_broad (bool) – If True, run an independent BIC test for a broad He I component shared across all observable He I lines, gated by the best narrow He I SNR. Default False.

  • n_boot_bic (int) – Bootstrap iterations for the BIC model selection; 0 uses a single-point BIC (default 100).

  • n_jobs (int) – Parallel workers for the BIC bootstrap; -1 uses all cores (default -1).

  • snr_threshold (float) – Minimum Hα SNR to attempt Balmer broad fitting (default 5.0).

  • oiii_snr_threshold (float) – Minimum [OIII] 5007 SNR to attempt [OIII] broad fitting (default 5.0).

  • hei_snr_threshold (float) – Minimum He I SNR to attempt He I broad fitting (default 5.0).

  • bic_delta (float) – Minimum ΔBIC by which a more complex model must beat the simpler one to be selected (default 6.0).

  • sampler (str) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • grating (str | None) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • R (Union[float, Callable, None]) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • lines (list[str] | None) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • wave_range_A (tuple[float, float] | None) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • deg (int) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • clip_sigma (float) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • moving_average (bool | int) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • n_walkers (int | str) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • n_steps (int) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • n_burn (int | None) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • n_live (int) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • n_eff (int) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • n_warmup (int) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • n_samples_nuts (int) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • n_chains (int) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • target_accept_prob (float) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • max_tree_depth (int) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • tie_balmer_to_oiii (bool) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • tie_uv_doublets (bool) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • tie_uv_centroids (bool) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • tie_uv_widths (bool) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • niv_doublet_ratio (float | None) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • ciii_doublet_ratio (float | None) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • sigma_overrides (dict[str, tuple[float, float]] | None) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • centroid_overrides (dict[str, tuple[float, float]] | None) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • sigma_factor (float) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • centroid_vmax (float) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • centroid_max_sigma (float) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • prior_overrides (dict[str, Any] | None) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • progress (bool) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

  • seed (int) – Identical to the corresponding arguments of fit_lines(); see that function or the grouped tables in jwspecmcmc — Bayesian MCMC fitting. (init_from_mle is not exposed here because Phase 2 always initialises from the Phase 1 fit.)

Return type:

MCMCBroadFitResult

jwspecmcmc.load_mcmc_result(path)[source]

Load an MCMC result from a .npz file.

Parameters:

path (str or Path) – Path to the .npz file saved by save_mcmc_result().

Returns:

Reconstructed result with full posterior chains.

Return type:

MCMCResult or MCMCBroadFitResult

jwspecmcmc.plot_corner(*args, **kwargs)[source]

Corner plot of posterior samples.

See jwspecmcmc.plotting.plot_corner() for full documentation.

jwspecmcmc.plot_flux_posterior(*args, **kwargs)[source]

Flux posterior histogram for a single line.

See jwspecmcmc.plotting.plot_flux_posterior() for full documentation.

jwspecmcmc.plot_traces(*args, **kwargs)[source]

Trace plots of MCMC chains.

See jwspecmcmc.plotting.plot_traces() for full documentation.

jwspecmcmc.priors_from_bounds(lb_free, ub_free, overrides=None)[source]

Build a PriorSet from parameter bounds.

Parameters:
  • lb_free (np.ndarray) – Lower bounds for free parameters.

  • ub_free (np.ndarray) – Upper bounds for free parameters.

  • overrides (dict mapping int to Prior, optional) – Per-index prior overrides.

Return type:

PriorSet

jwspecmcmc.save_mcmc_result(result, path)[source]

Save an MCMC result to a compressed .npz file.

Parameters:
Return type:

None

Modules

diagnostics

MCMC convergence diagnostics.

io

Save and load MCMC fitting results.

jax_likelihood

JAX-accelerated likelihood for NUTS/HMC sampling.

likelihood

Log-likelihood and log-probability for MCMC sampling.

plotting

Diagnostic plots for MCMC results.

priors

Prior distributions for MCMC sampling.

result

MCMC result containers.

samplers

MCMC sampler wrappers for emcee, nautilus, and NumPyro NUTS.