McmcSampler#

class jetset.mcmc.McmcSampler(model_minimizer, build_mcmc_parameters=True)[source]#

Bases: object

Run and manage emcee sampling for a fitted JetSeT model.

Notes

Wraps an input ModelMinimizer, clones its model state, stores sampler outputs, and provides serialization-safe state handling for chain analysis and plotting.

Attributes Summary

Methods Summary

corner_plot([comp_name, quantiles, levels, ...])

Corner plot.

get_par(par_name_or_idx[, comp_name, get_index])

Return par.

get_par_quantiles(par_name[, comp_name, ...])

Return par quantiles.

get_sample(par_name[, comp_name])

Return sample.

get_trace(par_name[, comp_name])

Return trace.

load(file_name)

Load object state from disk.

plot_chain([par_name, comp_name, log_plot])

Plot chain.

plot_model([sed_data, fit_range, size, ...])

Plot posterior model envelope and a best-fit reference curve.

plot_par(par_name[, comp_name, nbins, ...])

Plot par.

reset_to_mcmc_best_fit([verbose])

Reset to mcmc best fit.

reset_to_minimizer_best_fit()

Reset sampled parameter values to minimizer best-fit values.

run_sampler([nwalkers, steps, pos, burnin, ...])

Run sampler.

save(name)

Save object state to disk.

set_bounds([par_name, comp_name, bound, ...])

Set bounds.

set_plot_label(par_name, plot_label, comp_name)

Set the display label used for a sampled parameter in plots.

tune_burnin([tau_coeff])

Tune burn-in length from the sampler autocorrelation time.

Attributes Documentation

best_fit_par_table#
sampler_parameters#

sampler table.

Returns:

Requested value.

Return type:

object

Methods Documentation

corner_plot(comp_name=None, quantiles=(0.16, 0.5, 0.84), levels=None, title_kwargs={}, per_component=True, **kwargs)[source]#

Corner plot.

Parameters:
  • comp_name (str, optional) – Model-component name.

  • quantiles (tuple, optional) – Quantiles to evaluate/report.

  • levels (object, optional) – Contour levels for corner plots.

  • title_kwargs (dict, optional) – Keyword arguments forwarded to plot-title rendering.

  • **kwargs (dict) – Additional keyword arguments.

Returns:

Computed value.

Return type:

object

get_par(par_name_or_idx, comp_name=None, get_index=False)[source]#

Return par.

Parameters:
  • par_name_or_idx (object) – Parameter identifier by name or index.

  • comp_name (object, optional) – Model-component name.

  • get_index (bool, optional) – If True, also return the index of the selected item.

Returns:

Requested value.

Return type:

object

get_par_quantiles(par_name, comp_name=None, quantiles=(0.16, 0.5, 0.84))[source]#

Return par quantiles.

Parameters:
  • par_name (str) – Parameter name.

  • comp_name (object, optional) – Model-component name.

  • quantiles (tuple, optional) – Quantiles to evaluate/report.

Returns:

Requested value.

Return type:

object

get_sample(par_name, comp_name=None)[source]#

Return sample.

Parameters:
  • par_name (str) – Parameter name.

  • comp_name (object, optional) – Model-component name.

Returns:

Requested value.

Return type:

object

get_trace(par_name, comp_name=None)[source]#

Return trace.

Parameters:
  • par_name (str) – Parameter name.

  • comp_name (object, optional) – Model-component name.

Returns:

Requested value.

Return type:

object

classmethod load(file_name)[source]#

Load object state from disk.

Parameters:

file_name (object) – Input/output file path.

Returns:

Loaded object.

Return type:

object

plot_chain(par_name=None, comp_name=None, log_plot=False)[source]#

Plot chain.

Parameters:
  • par_name (str, optional) – Parameter name.

  • comp_name (object, optional) – Model-component name.

  • log_plot (bool, optional) – If True, use logarithmic plot scaling where applicable.

Returns:

Plot object or generated visualization.

Return type:

object

plot_model(sed_data=None, fit_range=None, size=100, frame='obs', density=False, quantiles=None, get_model=False, plot_mcmc_best_fit_model=True, rnd_seed=0, plot_components=False)[source]#

Plot posterior model envelope and a best-fit reference curve.

Parameters:
  • sed_data (ObsData, optional) – Observational SED data used for plotting and residuals. If None, self.sed_data is used.

  • fit_range (sequence of float, optional) – Two-element fit interval [nu_min, nu_max] used for model and residual overlays. If None, [self.model.nu_min_fit, self.model.nu_max_fit] is used.

  • size (int, optional) – Number of posterior samples used to build the shaded model envelope.

  • frame ({'obs', 'src'}, optional) – Frame used to evaluate and display model SED values.

  • density (bool, optional) – If True, convert sampled nuFnu curves to Fnu by dividing by frequency before computing the envelope.

  • quantiles (tuple of float, optional) – Lower/upper quantiles for the shaded envelope (for example (0.16, 0.84)). If None, use the full min/max range.

  • get_model (bool, optional) – If True, also return the sampled envelope arrays [x, y_min, y_max] after flux-limit masking.

  • plot_mcmc_best_fit_model (bool, optional) – If True, overlay the MCMC best-fit model. If False, overlay the minimizer best-fit model.

  • rnd_seed (int, optional) – Seed used to draw posterior samples reproducibly.

  • plot_components (bool, optional) – If True, plot component/sub-component curves before the final reference curve.

Returns:

Plot object. If get_model is True, returns (plot_obj, [x, y_min, y_max]).

Return type:

PlotSED or tuple

plot_par(par_name, comp_name=None, nbins=20, log_plot=False, quantiles=(0.16, 0.5, 0.84), figsize=None)[source]#

Plot par.

Parameters:
  • par_name (str) – Parameter name.

  • comp_name (object, optional) – Model-component name.

  • nbins (int, optional) – Number of bins for histogram estimates.

  • log_plot (bool, optional) – If True, use logarithmic plot scaling where applicable.

  • quantiles (tuple, optional) – Quantiles to evaluate/report.

  • figsize (object, optional) – Matplotlib figure size.

Returns:

Plot object or generated visualization.

Return type:

object

reset_to_mcmc_best_fit(verbose=True)[source]#

Reset to mcmc best fit.

Parameters:

verbose (bool, optional) – If True, print additional information.

reset_to_minimizer_best_fit()[source]#

Reset sampled parameter values to minimizer best-fit values.

Notes

This updates only the internal parameter dictionary used by the sampler helper; it does not run a new minimization.

run_sampler(nwalkers=None, steps=100, pos=None, burnin=50, use_UL=False, threads=None, walker_start_bound=0.005, loglog=False, progress='notebook')[source]#

Run sampler.

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

  • steps (int, optional) – Number of MCMC steps.

  • pos (object, optional) – Initial walker positions.

  • burnin (int, optional) – Number of burn-in steps to discard.

  • use_UL (bool, optional) – If True, enable ul.

  • threads (object, optional) – Number of worker threads/processes.

  • walker_start_bound (float, optional) – Initial spread factor for walker starting points.

  • loglog (bool, optional) – If True, operate in log10 space.

  • progress (str, optional) – If True, display sampling progress.

save(name)[source]#

Save object state to disk.

Parameters:

name (object) – Name identifier.

set_bounds(par_name=None, comp_name=None, bound=0.2, bound_rel=False, preserve_fit_range=True, par_bounds=None, zero_abs_tol=1e-200)[source]#

Set bounds.

Parameters:
  • bound (float, optional) – Absolute parameter-bound span.

  • bound_rel (bool, optional) – Relative parameter-bound span.

  • preserve_fit_range (bool, optional) – Range for preserve fit.

set_plot_label(par_name, plot_label, comp_name)[source]#

Set the display label used for a sampled parameter in plots.

Parameters:
  • par_name (str) – Name of the parameter whose label must be updated.

  • plot_label (str) – New text label used in corner and chain plots.

  • comp_name (str) – Name of the model component that owns par_name.

Raises:

RuntimeError – If the requested parameter/component pair is not found.

tune_burnin(tau_coeff=None)[source]#

Tune burn-in length from the sampler autocorrelation time.

Parameters:

tau_coeff (float, optional) – Multiplicative factor applied to the maximum integrated autocorrelation time. The burn-in is set to int(tau_coeff * max(tau)). If None, an internal value is computed to keep burn-in below the total chain length.

Notes

This method uses emcee integrated autocorrelation times computed through sampler.get_autocorr_time(tol=0) and then updates self.burnin and all post-run cached products (samples, log-prob, best-fit and quantiles) via _set_samples_post_run().

See emcee documentation for details: