elisa.infer.fit#

Model fit in a maximum likelihood or Bayesian way.

class Fit(data: ObservationData | Sequence[ObservationData], model: Model | Sequence[Model], stat: Literal['chi2', 'cstat', 'pstat', 'pgstat', 'wstat'] | Sequence[Literal['chi2', 'cstat', 'pstat', 'pgstat', 'wstat']] | None = None, seed: int = 42)[source]#

Bases: ABC

Abstract base class for model fitting.

Parameters:

Methods

summary([file])

Print the summary of fitting setup.

summary(file=None) None[source]#

Print the summary of fitting setup.

Parameters:

file (file-like) – An object with a write(string) method. This is passed to print().

class MaxLikeFit(data: ObservationData | Sequence[ObservationData], model: Model | Sequence[Model], stat: Literal['chi2', 'cstat', 'pstat', 'pgstat', 'wstat'] | Sequence[Literal['chi2', 'cstat', 'pstat', 'pgstat', 'wstat']] | None = None, seed: int = 42)[source]#

Bases: Fit

Methods

mle([init, method, max_steps, throw, verbose])

Search Maximum Likelihood Estimation (MLE) for the model.

summary([file])

Print the summary of fitting setup.

mle(init: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray | dict | None = None, method: Literal['minuit', 'lm', 'ns'] = 'minuit', max_steps: int = None, throw: bool = True, verbose: int | bool = False) MLEResult[source]#

Search Maximum Likelihood Estimation (MLE) for the model.

Parameters:
  • init (Union[Array, ndarray, bool, number, bool, int, float, complex, TypedNdArray, dict, None]) – Initial guess for the maximum likelihood estimation.

  • method (Literal['minuit', 'lm', 'ns']) – Optimization algorithm used to find the MLE. Available options are:

  • max_steps (int) – The maximum number of steps the solver can take. The default is 131072.

  • throw (bool) – Whether to report any failures of the solver. Defaults to True.

  • verbose (int | bool) – Whether to print fit progress information. The default is False.

Returns:

The MLE result.

class BayesFit(data: ObservationData | Sequence[ObservationData], model: Model | Sequence[Model], stat: Literal['chi2', 'cstat', 'pstat', 'pgstat', 'wstat'] | Sequence[Literal['chi2', 'cstat', 'pstat', 'pgstat', 'wstat']] | None = None, seed: int = 42)[source]#

Bases: Fit

Methods

aies([warmup, steps, chains, thinning, ...])

Run numpyro's Affine-Invariant Ensemble Sampling (AIES).

barkermh([warmup, steps, chains, thinning, ...])

Run numpyro's implementation of BarkerMH sampler.

blackjax_nuts([warmup, steps, chains, ...])

Run blackjax's implementation of No-U-Turn Sampler (NUTS).

emcee([warmup, steps, chains, thinning, ...])

Run emcee's affine-invariant ensemble sampling.

ess([warmup, steps, chains, thinning, init, ...])

Run numpyro's Ensemble Slice Sampling (ESS).

jaxns([max_samples, num_live_points, s, k, ...])

Run jaxns's implementation of nested sampling.

nautilus([ess, ignore_nan, ...])

Run nautilus's implementation of nested sampling.

nuts([warmup, steps, chains, thinning, ...])

Run numpyro's implementation of No-U-Turn Sampler (NUTS).

sa([warmup, steps, chains, thinning, init, ...])

Run numpyro's implementation of Sample Adaptive (SA) MCMC.

summary([file])

Print the summary of fitting setup.

ultranest([ess, ignore_nan, viz_params, ...])

Run ultranest's implementation of nested sampling.

zeus([warmup, steps, chains, thinning, ...])

Run zeus' ensemble slice sampling.

nuts(warmup: int = 2000, steps: int = 5000, chains: int | None = None, thinning: int = 1, init: dict[str, float] | None = None, chain_method: str = 'parallel', progress: bool = True, post_warmup_state: HMCState | None = None, **kwargs: dict) PosteriorResult[source]#

Run numpyro’s implementation of No-U-Turn Sampler (NUTS).

Note

If the chains are not converged well, see ref [2] for more information on how to fine-tune NUTS.

Parameters:
  • warmup (int) – Number of warmup steps. The default is 2000.

  • steps (int) – Number of steps to run for each chain. The default is 5000.

  • chains (int | None) – Number of MCMC chains to run. If there are not enough devices available, chains will run in sequence. Defaults to the number of jax.local_device_count().

  • thinning (int) – int, optional: For each chain, every thinning step is retained, and the other steps are discarded. The total steps for each chain are steps * thinning. The default is 1.

  • init (dict[str, float] | None) – Initial parameter for sampler to start from.

  • chain_method (str) – The chain method passed to numpyro.infer.MCMC.

  • progress (bool) – Whether to show progress bars during sampling. The default is True.

  • post_warmup_state (HMCState | None) – The state before the sampling phase. The sampling will start from the given state if provided.

  • **kwargs (dict) – Extra parameters passed to numpyro.infer.NUTS. The default for dense_mass is True.

Returns:

The posterior sampling result.

References

[1]

The No-U-Turn Sampler: Adaptively Setting Path Lengths in Hamiltonian Monte Carlo (https://www.jmlr.org/papers/volume15/hoffman14a/hoffman14a.pdf)

barkermh(warmup: int = 5000, steps: int = 5000, chains: int | None = None, thinning: int = 1, init: dict[str, float] | None = None, chain_method: str = 'parallel', progress: bool = True, post_warmup_state: BarkerMHState | None = None, **kwargs: dict) PosteriorResult[source]#

Run numpyro’s implementation of BarkerMH sampler.

Note

This is a gradient-based MCMC algorithm of Metropolis-Hastings type that uses a skew-symmetric proposal distribution that depends on the gradient of the potential (the Barker proposal [1]). In particular the proposal distribution is skewed in the direction of the gradient at the current sample. This algorithm is expected to be particularly effective for low to moderate dimensional models, where it may be competitive with HMC and NUTS.

Parameters:
  • warmup (int) – Number of warmup steps. The default is 5000.

  • steps (int) – Number of steps to run for each chain. The default is 10000.

  • chains (int | None) – Number of MCMC chains to run. If there are not enough devices available, chains will run in sequence. Defaults to the number of jax.local_device_count().

  • thinning (int) – int, optional: For each chain, every thinning step is retained, and the other steps are discarded. The total steps for each chain are steps * thinning. The default is 1.

  • init (dict[str, float] | None) – Initial parameter for sampler to start from.

  • chain_method (str) – The chain method passed to numpyro.infer.MCMC.

  • progress (bool) – Whether to show progress bars during sampling. The default is True.

  • post_warmup_state (BarkerMHState | None) – The state before the sampling phase. The sampling will start from the given state if provided.

  • **kwargs (dict) – Extra parameters passed to numpyro.infer.BarkerMH. The default for dense_mass is True.

Returns:

The posterior sampling result.

References

[1]

The Barker proposal: combining robustness and efficiency in gradient-based MCMC (https://doi.org/10.1111/rssb.12482), Samuel Livingstone and Giacomo Zanella.

sa(warmup: int = 70000, steps: int = 5000, chains: int | None = None, thinning: int = 2, init: dict[str, float] | None = None, chain_method: str = 'parallel', progress: bool = True, post_warmup_state: SAState | None = None, **kwargs: dict) PosteriorResult[source]#

Run numpyro’s implementation of Sample Adaptive (SA) MCMC.

Note

This is a gradient-free sampler. It is fast in terms of n_eff / s, but requires many warmup (burn-in) steps.

If the result does not converge satisfactorily, consider increasing the values of warmup and/or adapt_state_size, or providing better initial parameter estimates via the init argument.

Parameters:
  • warmup (int) – Number of warmup steps. The default is 70000.

  • steps (int) – Number of steps to run. The default is 5000.

  • chains (int | None) – Number of MCMC chains to run. If there are not enough devices available, chains will run in sequence. Defaults to the number of jax.local_device_count().

  • thinning (int) – int, optional: For each chain, every thinning step is retained, and the other steps are discarded. The total steps for each chain are steps * thinning. The default is 2.

  • init (dict[str, float] | None) – Initial parameter for sampler to start from.

  • chain_method (str) – The chain method passed to numpyro.infer.MCMC.

  • progress (bool) – Whether to show progress bars during sampling. The default is True.

  • post_warmup_state (SAState | None) – The state before the sampling phase. The sampling will start from the given state if provided.

  • **kwargs (dict) – Extra parameters passed to numpyro.infer.SA. The default for adapt_state_size is 5 * D, where D is the dimension of model parameters.

Returns:

The posterior sampling result.

References

[1]

Sample Adaptive MCMC (https://papers.nips.cc/paper/9107-sample-adaptive-mcmc), Michael Zhu

blackjax_nuts(warmup: int = 2000, steps: int = 5000, chains: int | None = None, thinning: int = 1, init: dict[str, float] | None = None, chain_method: str = 'parallel', progress: bool = True, post_warmup_state: HMCState | None = None, **kwargs: dict) PosteriorResult[source]#

Run blackjax’s implementation of No-U-Turn Sampler (NUTS).

Note

If the chains are not converged well, see ref [2] for more information on how to fine-tune NUTS.

Parameters:
  • warmup (int) – Number of warmup steps. The default is 2000.

  • steps (int) – Number of steps to run for each chain. The default is 5000.

  • chains (int | None) – Number of MCMC chains to run. If there are not enough devices available, chains will run in sequence. Defaults to the number of jax.local_device_count().

  • thinning (int) – int, optional: For each chain, every thinning step is retained, and the other steps are discarded. The total steps for each chain are steps * thinning. The default is 1.

  • init (dict[str, float] | None) – Initial parameter for sampler to start from.

  • chain_method (str) – The chain method passed to numpyro.infer.MCMC.

  • progress (bool) – Whether to show progress bars during sampling. The default is True.

  • post_warmup_state (HMCState | None) – The state before the sampling phase. The sampling will start from the given state if provided.

  • **kwargs (dict) – Extra parameters passed to BlackJAXNUTS. The default for dense_mass is True.

Returns:

The posterior sampling result.

References

[1]

The No-U-Turn Sampler: Adaptively Setting Path Lengths in Hamiltonian Monte Carlo (https://www.jmlr.org/papers/volume15/hoffman14a/hoffman14a.pdf)

aies(warmup: int = 5000, steps: int = 5000, chains: int | None = None, thinning: int = 1, init: dict[str, float] | None = None, chain_method: str = 'parallel', n_parallel: int | None = None, progress: bool = True, post_warmup_state: EnsembleSamplerState | None = None, **kwargs: dict) PosteriorResult[source]#

Run numpyro’s Affine-Invariant Ensemble Sampling (AIES).

Affine-invariant ensemble sampling [1] is a gradient-free method that informs Metropolis-Hastings proposals by sharing information between chains. Suitable for low to moderate dimensional models. Generally, chains should be at least twice the dimensionality of the model.

Note

This sampler must be used with even number chains > 1.

Parameters:
  • warmup (int) – Number of warmup steps. The default is 5000.

  • steps (int) – Number of steps to run for each chain. The default is 5000.

  • chains (int | None) – Number of MCMC chains to run. Defaults to 4 * D, where D is the dimension of model parameters.

  • thinning (int) – int, optional: For each chain, every thinning step is retained, and the other steps are discarded. The total steps for each chain are steps * thinning. The default is 1.

  • init (dict[str, float] | None) – Initial parameter for sampler to start from.

  • chain_method (str) – The chain method passed to numpyro.infer.MCMC.

  • n_parallel (int | None) – Number of parallel samplers to run. The default is jax.local_device_count().

  • progress (bool) – Whether to show progress bars during sampling. The default is True.

  • post_warmup_state (EnsembleSamplerState | None) – The state before the sampling phase. The sampling will start from the given state if provided. This does not take effect when n_parallel>=2.

  • **kwargs (dict) – Extra parameters passed to numpyro.infer.AIES. The default for moves is {AIES.StretchMove(): 1.0}.

Returns:

The posterior sampling result.

References

[1]

emcee: The MCMC Hammer (https://iopscience.iop.org/article/10.1086/670067), Daniel Foreman-Mackey, David W. Hogg, Dustin Lang, and Jonathan Goodman.

ess(warmup: int = 5000, steps: int = 5000, chains: int | None = None, thinning: int = 1, init: dict[str, float] | None = None, chain_method: str = 'parallel', n_parallel: int | None = None, progress: bool = True, post_warmup_state: EnsembleSamplerState | None = None, **kwargs: dict) PosteriorResult[source]#

Run numpyro’s Ensemble Slice Sampling (ESS).

Ensemble slice sampling [1] is a gradient free method that finds better slice sampling directions by sharing information between chains. Suitable for low to moderate dimensional models. Generally, chains should be at least twice the dimensionality of the model.

Note

This sampler must be used with even number chains > 1.

Parameters:
  • warmup (int) – Number of warmup steps. The default is 5000.

  • steps (int) – Number of steps to run for each chain. The default is 5000.

  • chains (int | None) – Number of MCMC chains to run. Defaults to 4 * D, where D is the dimension of model parameters.

  • thinning (int) – int, optional: For each chain, every thinning step is retained, and the other steps are discarded. The total steps for each chain are steps * thinning. The default is 1.

  • init (dict[str, float] | None) – Initial parameter for sampler to start from.

  • chain_method (str) – The chain method passed to numpyro.infer.MCMC.

  • n_parallel (int | None) – Number of parallel samplers to run. The default is jax.local_device_count().

  • progress (bool) – Whether to show progress bars during sampling. The default is True.

  • post_warmup_state (EnsembleSamplerState | None) – The state before the sampling phase. The sampling will start from the given state if provided. This does not take effect when n_parallel>=2.

  • **kwargs (dict) – Extra parameters passed to numpyro.infer.ESS.

Returns:

The posterior sampling result.

References

[1]

zeus: a PYTHON implementation of ensemble slice sampling for efficient Bayesian parameter inference (https://academic.oup.com/mnras/article/508/3/3589/6381726), Minas Karamanis, Florian Beutler, and John A. Peacock.

[2]

Ensemble slice sampling (https://link.springer.com/article/10.1007/s11222-021-10038-2), Minas Karamanis, Florian Beutler.

emcee(warmup: int = 5000, steps: int = 5000, chains: int | None = None, thinning: int = 1, init: dict[str, float] | None = None, n_parallel: int | None = None, progress: bool = True, post_warmup_state: Sequence | None = None, tune: bool = False, ignore_nan: bool = False, warmup_kwargs: dict | None = None, sampling_kwargs: dict | None = None) PosteriorResult[source]#

Run emcee’s affine-invariant ensemble sampling.

Affine-invariant ensemble sampling [1] is a gradient-free method that informs Metropolis-Hastings proposals by sharing information between chains. Suitable for low to moderate dimensional models. Generally, chains should be at least twice the dimensionality of the model.

Note

This sampler must be used with even chains > 1.

Parameters:
  • warmup (int) – Number of warmup steps. The default is 5000.

  • steps (int) – Number of steps to run for each chain. The default is 5000.

  • chains (int | None) – Number of MCMC chains to run. Defaults to 4 * D, where D is the dimension of model parameters.

  • thinning (int) – int, optional: For each chain, every thinning step is retained, and the other steps are discarded. The total steps for each chain are steps * thinning. The default is 1.

  • init (dict[str, float] | None) – Initial parameter for sampler to start from.

  • n_parallel (int | None) – Number of parallel samplers to run. The default is jax.local_device_count().

  • progress (bool) – Whether to show progress bars during sampling. The default is True.

  • post_warmup_state (Sequence | None) – The state before the sampling phase. The sampling will start from the given state if provided.

  • tune (bool) – If True, the parameters of some moves will be automatically tuned.

  • ignore_nan – Whether to transform a NaN log probability to a large negative number (-1e300). The default is False.

Returns:

The posterior sampling result.

References

[1]

emcee: The MCMC Hammer (https://iopscience.iop.org/article/10.1086/670067), Daniel Foreman-Mackey, David W. Hogg, Dustin Lang, and Jonathan Goodman.

zeus(warmup: int = 3000, steps: int = 5000, chains: int | None = None, thinning: int = 1, init: dict[str, float] | None = None, n_parallel: int | None = None, progress: bool = True, post_warmup_state: Sequence | None = None, tune: bool = True, ignore_nan: bool = False, warmup_kwargs: dict | None = None, sampling_kwargs: dict | None = None) PosteriorResult[source]#

Run zeus’ ensemble slice sampling.

Ensemble slice sampling [1] is a gradient free method that finds better slice sampling directions by sharing information between chains. Suitable for low to moderate dimensional models. Generally, chains should be at least twice the dimensionality of the model.

Note

This sampler must be used with even number chains > 1.

Parameters:
  • warmup (int) – Number of warmup steps. The default is 5000.

  • steps (int) – Number of steps to run for each chain. The default is 5000.

  • chains (int | None) – Number of MCMC chains to run. Defaults to 4 * D, where D is the dimension of model parameters.

  • thinning (int) – int, optional: For each chain, every thinning step is retained, and the other steps are discarded. The total steps for each chain are steps * thinning. The default is 1.

  • init (dict[str, float] | None) – Initial parameter for sampler to start from.

  • n_parallel (int | None) – Number of parallel samplers to run. The default is jax.local_device_count().

  • progress (bool) – Whether to show progress bars during sampling. The default is True.

  • post_warmup_state (Sequence | None) – The state before the sampling phase. The sampling will start from the given state if provided.

  • tune (bool) – If True, the parameters of some moves will be automatically tuned.

  • ignore_nan – Whether to transform a NaN log probability to a large negative number (-1e300). The default is False.

Returns:

The posterior sampling result.

References

[1]

zeus: a PYTHON implementation of ensemble slice sampling for efficient Bayesian parameter inference (https://academic.oup.com/mnras/article/508/3/3589/6381726), Minas Karamanis, Florian Beutler, and John A. Peacock.

[2]

Ensemble slice sampling (https://link.springer.com/article/10.1007/s11222-021-10038-2), Minas Karamanis, Florian Beutler.

jaxns(max_samples: int = 131072, num_live_points: int | None = None, s: int | None = None, k: int | None = None, c: int | None = None, devices: list[Device] | None = None, difficult_model: bool = False, parameter_estimation: bool = False, verbose: bool = False, term_cond: dict | None = None, **kwargs: dict) PosteriorResult[source]#

Run jaxns’s implementation of nested sampling.

Note

Parameters s, k, and c are defined in the paper [1]. For more information of the sampler parameters, see ref [1] [2].

Parameters:
  • max_samples (int) – Maximum number of posterior samples. The default is 131072.

  • num_live_points (int | None) – Approximate number of live points. The default is c * (k + 1).

  • s (int | None) – Number of slices per dimension. The default is 5.

  • k (int | None) – Number of phantom samples. The default is 0.

  • c (int | None) – Number of parallel Markov chains. The default is 30 * D, where D is the dimension of model parameters. It takes effect only for num_live_points=None.

  • devices (list[Device] | None) – Devices to use. Defaults to all available devices.

  • difficult_model (bool) – If True, uses more robust default settings (s = 10 and c = 50 * D). It takes effect only for num_live_points = None, s = None or c = None. Defaults to False.

  • parameter_estimation (bool) – If True, uses more robust default settings for parameter estimation (k = D). It takes effect only for k = None. Defaults to False.

  • verbose (bool) – Print progress information. The default is False.

  • term_cond (dict | None) – Termination conditions for the sampling. The default is as in jaxns.TermCondition.

  • **kwargs (dict) – Extra parameters passes to jaxns.DefaultNestedSampler.

Returns:

The posterior sampling result.

References

nautilus(ess: int = 3000, ignore_nan: bool = False, *, constructor_kwargs: dict | None = None, termination_kwargs: dict | None = None) PosteriorResult[source]#

Run nautilus’s implementation of nested sampling.

Parameters:
  • ess (int) – The desired effective sample size.

  • ignore_nan – Whether to transform a NaN log probability to a large negative number (-1e300). The default is False.

ultranest(ess: int = 3000, ignore_nan: bool = False, viz_params: list[str] | None = None, print_result: bool = True, *, constructor_kwargs: dict | None = None, termination_kwargs: dict | None = None, read_file_config: dict | None = None) PosteriorResult[source]#

Run ultranest’s implementation of nested sampling.

Parameters:
  • ess (int) – The desired effective sample size.

  • ignore_nan – Whether to transform a NaN log probability to a large negative number (-1e300). The default is False.