jesterTOV.inference.samplers.eos_reweighting.EOSReweightingSampler#

class EOSReweightingSampler(
likelihood,
prior=None,
sample_transforms=None,
likelihood_transforms=None,
config=None,
seed=0,
)[source]#

Bases: JesterSampler

Sampler that reweights a discrete EOS set by jester likelihoods.

Unlike parametric samplers implemented in jester, this class does not sample a prior. Instead, it receives a fixed set of tabulated EOS curves and evaluates the combined likelihood on each one. The result is a discrete posterior over the EOS set together with the Bayesian evidence \(\log Z\).

Parameters:
  • likelihood (LikelihoodBase) – Combined likelihood (GW, NICER, radio, …) created by create_combined_likelihood().

  • prior (object, optional) – Accepted for API compatibility with JesterSampler; however, ignored for the reweighting.

  • sample_transforms (list, optional) – Accepted for API compatibility; however, ignored for the reweighting.

  • likelihood_transforms (list, optional) – Accepted for API compatibility; however, ignored for the reweighting.

  • config (EOSReweightingConfig) – Sampler configuration including EOS file paths and grid settings.

  • seed (int, optional) – Accepted for API compatibility; however, ignored for the reweighting.

__init__(
likelihood,
prior=None,
sample_transforms=None,
likelihood_transforms=None,
config=None,
seed=0,
)[source]#

Methods

__init__(likelihood[, prior, ...])

add_name(x)

Turn an array into a dictionary.

compute_evidence(log_likelihoods)

Compute Bayesian evidence and effective sample size from log-likelihoods.

evaluate_batch(f, all_masses, all_lambdas, ...)

Evaluate f on all N EOS curves using jax.lax.map().

get_log_prob()

get_n_samples()

get_sampler_output()

get_samples()

load_and_grid(paths, n_grid, m_min, m_max)

Load EOS files and resample all curves onto two common mass grids.

make_eos_fn()

Build a single-EOS log-likelihood callable for use with jax.lax.map().

posterior(params, data)

Evaluate posterior log probability from flat array.

posterior_from_dict(named_params, data)

Evaluate posterior log probability from parameter dict.

print_summary([transform])

Print summary of sampling run.

sample(key)

Evaluate all EOS curves and return evidence + posterior samples.

Attributes

DEFAULT_M_MAX_CAP

Absolute cap (\(M_\odot\)) applied to the common mass grid upper bound in load_and_grid() when m_max is not given explicitly.

likelihood

prior

sample_transforms

likelihood_transforms

parameter_names

sampler

DEFAULT_M_MAX_CAP: float = 4.0#

Absolute cap (\(M_\odot\)) applied to the common mass grid upper bound in load_and_grid() when m_max is not given explicitly.

compute_evidence(log_likelihoods)[source]#

Compute Bayesian evidence and effective sample size from log-likelihoods.

This is inspired by the lwp code: https://git.ligo.org/reed.essick/lwp/

Same computation as lwp.utils.utils.estimate_evidence (with its default uniform prior), just carried out in log space for numerical stability. Writing \(w^{(a)} = \exp(\text{log\_likelihoods}_a)\) for the raw per-EOS likelihood and \(p^{(a)} = w^{(a)} / \sum_b w^{(b)}\) for the normalised posterior weight, lwp computes

\[Z = \frac{1}{N}\sum_{a=1}^{N} w^{(a)} \,, \qquad Z_2 = \frac{1}{N}\sum_{a=1}^{N} \left(w^{(a)}\right)^2 \,, \qquad \delta Z = \sqrt{\frac{Z_2 - Z^2}{N}}\,,\]

where \(\delta Z\) is the standard error of the mean over the N weights, and reports the relative uncertainty \(\delta Z / Z\) after propagating it to log space via the delta method (\(\sigma_{\log Z} \approx \delta Z / Z\)). Using \(N_\mathrm{eff} = Z^2/Z_2\cdot N = 1/\sum_a (p^{(a)})^2\) (the Kish effective sample size, lwp.stats.stats.nkde) this simplifies to

\[\log Z = \mathrm{logsumexp}(\text{log\_likelihoods}) - \log N \,, \qquad \sigma_{\log Z} = \sqrt{\frac{1}{N_\mathrm{eff}} - \frac{1}{N}}\,.\]
Parameters:

log_likelihoods (Float[Array, 'N']) – Per-EOS log-likelihoods of shape [N].

Return type:

dict[str, Any]

Returns:

  • dict with keys log_Z, log_Z_std, N_eff,

  • N_eff_fraction, posterior_weights.

evaluate_batch(f, all_masses, all_lambdas, all_radii)[source]#

Evaluate f on all N EOS curves using jax.lax.map().

The curves are processed in Python-level batches of config.batch_size so that progress and ETA can be logged. Each batch is evaluated with a jitted jax.lax.map(). This double batching is somewhat awkward, but it is needed for the logging and the runtime is about the same as a single lax.map call.

Parameters:
  • f (Callable[[tuple[Array, Array, Array]], Array]) – Single-EOS evaluator returned by make_eos_fn().

  • all_masses (Float[Array, 'N L']) – Stacked JAX arrays of shape [N, L].

  • all_lambdas (Float[Array, 'N L']) – Stacked JAX arrays of shape [N, L].

  • all_radii (Float[Array, 'N L']) – Stacked JAX arrays of shape [N, L].

Return type:

Float[Array, 'N']

Returns:

Float[Array, “ N”] – Log-likelihoods per EOS.

get_log_prob()[source]#
Return type:

Array

get_n_samples()[source]#
Return type:

int

get_sampler_output()[source]#
Return type:

SamplerOutput

get_samples()[source]#
Return type:

dict[str, Array]

load_and_grid(paths, n_grid, m_min, m_max)[source]#

Load EOS files and resample all curves onto two common mass grids.

Two grids are built from the same underlying curves: a likelihood grid (bounded by m_min/m_max, as configured) used to evaluate the likelihoods, and a full grid that always spans the EOS set’s own natural range — from \(\min(M)\) across all curves up to \(\max(M_\mathrm{TOV})\) — regardless of m_min/m_max. Those two bounds only restrict what jester’s likelihoods see; the full-range curves are what gets resampled into the posterior (sample()) for downstream plotting/output, so posterior M-R/M-Lambda curves and the saved result are never truncated by the likelihood grid’s bounds.

Parameters:
  • paths (list[str]) – Paths to NPZ files. Each file must contain keys masses, lambdas, and radii (1-D or 2-D arrays). For a file with N curves the arrays must be shaped [N, n_points], with matching shapes across the three keys — ragged/heterogeneous curves within a single file are not supported and raise ValueError. Curves may differ in length between files, since each curve is resampled onto the common mass grid independently.

  • n_grid (int) – Number of mass grid points (used for both grids).

  • m_min (float) – Lower bound of the likelihood grid in \(M_\odot\).

  • m_max (float | None) – Upper bound of the likelihood grid in \(M_\odot\). If None, then use \(\max(M_\mathrm{TOV})\) across all curves in paths, capped at DEFAULT_M_MAX_CAP \(M_\odot\) (a warning is logged if the cap is applied, since likelihoods will not be evaluated above it).

Return type:

tuple[tuple[Float[Array, 'N L'], Float[Array, 'N L'], Float[Array, 'N L']], tuple[Float[Array, 'N L'], Float[Array, 'N L'], Float[Array, 'N L']]]

Returns:

  • likelihood_grid (tuple of Float[Array, “N L”]) – (masses, lambdas, radii) on the [m_min, m_max]-bounded grid used for likelihood evaluation.

  • full_grid (tuple of Float[Array, “N L”]) – (masses, lambdas, radii) on the EOS set’s natural [min(M), max(M_TOV)] grid, used for posterior resampling — unaffected by m_min/m_max.

make_eos_fn()[source]#

Build a single-EOS log-likelihood callable for use with jax.lax.map().

Return type:

Callable[[tuple[Array, Array, Array]], Array]

Returns:

Callable – f((masses, lambdas, radii)) → scalar log-likelihood

sample(key)[source]#

Evaluate all EOS curves and return evidence + posterior samples.

After computing the per-EOS evidence and posterior weights, this also draws n_resample (default: \(N_\mathrm{eff}\)) equal-weight posterior samples via resample_eos_posterior(), so that downstream postprocessing (e.g. generate_eos_reweighting_plots()) can plot mass-radius/mass-Lambda curves directly without having to deal with the unequal per-EOS weights itself. The resampled curves are drawn from the full, uncapped mass grid (see load_and_grid()), so plots always show each EOS out to its true \(M_\mathrm{TOV}\) even if the likelihood grid was capped.

Parameters:

key (Union[Key[Array, ''], UInt32[Array, '2'], UInt32[Array, '4']]) – JAX random key. Only its bits are used (to seed the NumPy RNG for resampling); the likelihood evaluation itself is deterministic.

Return type:

SamplerOutput

Returns:

SamplerOutput –

  • samples["eos_index"] : integer index per input EOS (length N)

  • samples["log_likelihood"] : log-likelihood per input EOS (length N)

  • samples["posterior_weight"] : normalised posterior weight per input EOS (length N)

  • samples["masses_EOS"], samples["Lambdas_EOS"], samples["radii_EOS"] : resampled equal-weight posterior curves (length N_resampled)

  • samples["resampled_eos_index"] : index into the input EOS set for each resampled draw

  • samples["resampled_log_likelihood"] : log-likelihood of each resampled draw

  • log_prob : same as samples["log_likelihood"] (length N)

  • metadata["evidence"] : evidence dict (log_Z, log_Z_std, N_eff, …)

  • metadata["N_eos"] : total number of input EOS curves

  • metadata["N_resampled"] : number of resampled posterior draws