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:
dataData or sequence of Data

The observation data.

modelModel or sequence of Model

The model used to fit the data.

stat{‘chi2’, ‘cstat’, ‘pstat’, ‘pgstat’, ‘wstat’} or sequence, optional

The likelihood option for the data and model. Available likelihood options are:

  • 'chi2': Gaussian data

  • 'cstat': Poisson data

  • 'pstat': Poisson data with known background

  • 'pgstat': Poisson data with Gaussian background

  • 'wstat': Poisson data with Poisson background

The default is None, which means automatically choosing the suitable likelihood options for the datasets and models.

seedint, optional

Seed of random number generator used for fit. The default is 42.

Methods

summary([file])

Print the summary of fitting setup.

summary(file=None) None[source]#

Print the summary of fitting setup.

Parameters:
filefile-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 | 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:
initdict, optional

Initial guess for the maximum likelihood estimation.

method{‘minuit’, ‘lm’, ‘ns’}, optional

Optimization algorithm used to find the MLE. Available options are:

  • 'minuit': Migrad algorithm of iminuit.

  • 'lm': Levenberg-Marquardt algorithm of optimistix.

  • 'ns': Nested sampling of jaxns. This option first search MLE globally, then polish it with local minimization.

The default is ‘minuit’.

Returns:
MLEResult

The MLE result.

Other Parameters:
max_stepsint, optional

The maximum number of steps the solver can take. The default is 131072.

throwbool, optional

Whether to report any failures of the solver. Defaults to True.

verboseint or bool, optional

Whether to print fit progress information. The default is False.

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:
warmupint, optional

Number of warmup steps. The default is 2000.

stepsint, optional

Number of steps to run for each chain. The default is 5000.

chainsint, optional

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, 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.

initdict, optional

Initial parameter for sampler to start from.

chain_methodstr, optional

The chain method passed to numpyro.infer.MCMC.

progressbool, optional

Whether to show progress bars during sampling. The default is True.

post_warmup_stateHMCState, optional

The state before the sampling phase. The sampling will start from the given state if provided.

**kwargsdict

Extra parameters passed to numpyro.infer.NUTS. The default for dense_mass is True.

Returns:
PosteriorResult

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:
warmupint, optional

Number of warmup steps. The default is 5000.

stepsint, optional

Number of steps to run for each chain. The default is 10000.

chainsint, optional

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, 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.

initdict, optional

Initial parameter for sampler to start from.

chain_methodstr, optional

The chain method passed to numpyro.infer.MCMC.

progressbool, optional

Whether to show progress bars during sampling. The default is True.

post_warmup_stateBarkerMHState, optional

The state before the sampling phase. The sampling will start from the given state if provided.

**kwargsdict

Extra parameters passed to numpyro.infer.BarkerMH. The default for dense_mass is True.

Returns:
PosteriorResult

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:
warmupint, optional

Number of warmup steps. The default is 70000.

stepsint, optional

Number of steps to run. The default is 5000.

chainsint, optional

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, 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.

initdict, optional

Initial parameter for sampler to start from.

chain_methodstr, optional

The chain method passed to numpyro.infer.MCMC.

progressbool, optional

Whether to show progress bars during sampling. The default is True.

post_warmup_stateSAState, optional

The state before the sampling phase. The sampling will start from the given state if provided.

**kwargsdict

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:
PosteriorResult

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:
warmupint, optional

Number of warmup steps. The default is 2000.

stepsint, optional

Number of steps to run for each chain. The default is 5000.

chainsint, optional

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, 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.

initdict, optional

Initial parameter for sampler to start from.

chain_methodstr, optional

The chain method passed to numpyro.infer.MCMC.

progressbool, optional

Whether to show progress bars during sampling. The default is True.

post_warmup_stateHMCState, optional

The state before the sampling phase. The sampling will start from the given state if provided.

**kwargsdict

Extra parameters passed to BlackJAXNUTS. The default for dense_mass is True.

Returns:
PosteriorResult

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:
warmupint, optional

Number of warmup steps. The default is 5000.

stepsint, optional

Number of steps to run for each chain. The default is 5000.

chainsint, optional

Number of MCMC chains to run. Defaults to 4 * D, where D is the dimension of model parameters.

thinning: 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.

initdict, optional

Initial parameter for sampler to start from.

chain_methodstr, optional

The chain method passed to numpyro.infer.MCMC.

n_parallelint, optional

Number of parallel samplers to run. The default is jax.local_device_count().

progressbool, optional

Whether to show progress bars during sampling. The default is True.

post_warmup_stateEnsembleSamplerState, optional

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.

**kwargsdict

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

Returns:
PosteriorResult

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:
warmupint, optional

Number of warmup steps. The default is 5000.

stepsint, optional

Number of steps to run for each chain. The default is 5000.

chainsint, optional

Number of MCMC chains to run. Defaults to 4 * D, where D is the dimension of model parameters.

thinning: 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.

initdict, optional

Initial parameter for sampler to start from.

chain_methodstr, optional

The chain method passed to numpyro.infer.MCMC.

n_parallelint, optional

Number of parallel samplers to run. The default is jax.local_device_count().

progressbool, optional

Whether to show progress bars during sampling. The default is True.

post_warmup_stateEnsembleSamplerState, optional

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.

**kwargsdict

Extra parameters passed to numpyro.infer.ESS.

Returns:
PosteriorResult

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:
warmupint, optional

Number of warmup steps. The default is 5000.

stepsint, optional

Number of steps to run for each chain. The default is 5000.

chainsint, optional

Number of MCMC chains to run. Defaults to 4 * D, where D is the dimension of model parameters.

thinning: 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.

initdict, optional

Initial parameter for sampler to start from.

n_parallelint, optional

Number of parallel samplers to run. The default is jax.local_device_count().

progressbool, optional

Whether to show progress bars during sampling. The default is True.

post_warmup_statesequence, optional

The state before the sampling phase. The sampling will start from the given state if provided.

tunebool, optional

If True, the parameters of some moves will be automatically tuned.

ignore_nanbool, optional

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

Warning

Setting ignore_nan=True may fail to spot potential issues with model computation.

warmup_kwargs: dict, optional

Extra parameters passed to emcee.EnsembleSampler for warm-up phase.

sampling_kwargs: dict | None = None,

Extra parameters passed to emcee.EnsembleSampler for sampling phase.

Returns:
PosteriorResult

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:
warmupint, optional

Number of warmup steps. The default is 5000.

stepsint, optional

Number of steps to run for each chain. The default is 5000.

chainsint, optional

Number of MCMC chains to run. Defaults to 4 * D, where D is the dimension of model parameters.

thinning: 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.

initdict, optional

Initial parameter for sampler to start from.

n_parallelint, optional

Number of parallel samplers to run. The default is jax.local_device_count().

progressbool, optional

Whether to show progress bars during sampling. The default is True.

post_warmup_statesequence, optional

The state before the sampling phase. The sampling will start from the given state if provided.

tunebool, optional

If True, the parameters of some moves will be automatically tuned.

ignore_nanbool, optional

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

Warning

Setting ignore_nan=True may fail to spot potential issues with model computation.

warmup_kwargs: dict, optional

Extra parameters passed to zeus.EnsembleSampler for warm-up phase.

sampling_kwargs: dict | None = None,

Extra parameters passed to zeus.EnsembleSampler for sampling phase.

Returns:
PosteriorResult

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_samplesint, optional

Maximum number of posterior samples. The default is 131072.

num_live_pointsint, optional

Approximate number of live points. The default is c * (k + 1).

sint, optional

Number of slices per dimension. The default is 5.

kint, optional

Number of phantom samples. The default is 0.

cint, optional

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.

deviceslist, optional

Devices to use. Defaults to all available devices.

difficult_modelbool, optional

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_estimationbool, optional

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

verbosebool, optional

Print progress information. The default is False.

term_conddict, optional

Termination conditions for the sampling. The default is as in jaxns.TermCondition.

**kwargsdict

Extra parameters passes to jaxns.DefaultNestedSampler.

Returns:
PosteriorResult

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:
essint, optional

The desired effective sample size.

ignore_nanbool, optional

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

Warning

Setting ignore_nan=True may fail to spot potential issues with model computation.

constructor_kwargsdict, optional

Extra parameters passed to nautilus.Sampler.

termination_kwargsdict, optional

Extra parameters passed to nautilus.Sampler.run().

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:
essint, optional

The desired effective sample size.

ignore_nanbool, optional

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

Warning

Setting ignore_nan=True may fail to spot potential issues with model computation.

viz_paramslist, optional

Parameters to visualize during sampling. The default is all.

print_resultbool, optional

Whether to print sampling result. The default is True.

constructor_kwargsdict, optional

Extra parameters passed to ultranest.ReactiveNestedSampler.

termination_kwargsdict, optional

Extra parameters passed to ultranest.ReactiveNestedSampler.run().

read_file_configdict, optional

Read the log file from a previous run. The dictionary should contain the log directory and other optional parameters. It should be noted that when providing this keyword argument, the sampler will not run, but read the log file instead and make sure the data and model settings are the same as the previous run.