kinisi.fitting#
Base classes for fitting analysis with MCMC sampling and Bayesian evidence estimation.
- class kinisi.fitting.FittingBase(data, function, parameter_names, parameter_units, priors=None, coordinate_name=None)#
Bases:
objectA base class for fitting analysis with MCMC sampling and Bayesian evidence estimation.
This class provides common functionality for fitting models to data with uncertainties, including maximum likelihood estimation, MCMC sampling, and nested sampling for Bayesian evidence calculation.
- Parameters:
data – Data to fit (sc.DataArray with variances)
function (
Callable) – A callable function that describes the relationshipparameter_names (
tuple[str,...]) – A tuple of parameter names for the functionparameter_units (
tuple[Unit,...]) – A tuple of sc.Unit objects corresponding to the parameter namespriors (
None|list) – Prior probability distributions for the parameters of the function. Defaults to None, in which case a uniform distribution is defined with limits of +/- 50 percent of the max likelihood fit values.coordinate_name (
str|None) – Name of the coordinate to use as independent variable
- get_independent_variable()#
Get the independent variable values for fitting.
- log_likelihood(parameters)#
Calculate the likelihood of the model given the data.
- Parameters:
parameters (
tuple[float]) – The parameters of the model.- Return type:
float- Returns:
The likelihood of the model.
- nll(parameters)#
Calculate the negative log likelihood of the model given the data.
- Parameters:
parameters (
tuple[float]) – The parameters of the model.- Return type:
float- Returns:
The negative log likelihood of the model.
- log_prior(parameters)#
Calculate the log prior probability of the model parameters using a uniform prior.
- Parameters:
parameters (
tuple[float]) – The parameters of the model.- Return type:
float- Returns:
The log prior probability of the model parameters.
- log_posterior(parameters)#
Calculate the log posterior probability of the model parameters.
- Parameters:
parameters (
tuple[float]) – The parameters of the model.- Return type:
float- Returns:
The log posterior probability of the model parameters.
- nlp(parameters)#
Calculate the negative log posterior of the model given the data.
- Parameters:
parameters (
tuple[float]) – The parameters of the model.- Return type:
float- Returns:
The negative log posterior of the model.
- prior_transform(parameters)#
Transform the parameters from the prior space to the parameter space.
- Parameters:
parameters (
tuple[float]) – The parameters of the model in prior space.- Return type:
tuple[float]- Returns:
The parameters of the model in parameter space.
- max_likelihood()#
Find the max likelihood fit parameters for the model.
- Return type:
None
- max_aposteriori()#
Find the max aposteriori fit parameters for the model.
- Return type:
None
- mcmc(x0=None, n_samples=1000, n_walkers=32, n_burn=500, n_thin=10, progress=True)#
Perform MCMC sampling of the model parameters.
- Parameters:
x0 (
tuple[Variable] |None) – Initial starting position for MCMC sampling. Optional, defaults to max likelihood/aposteriori or mean of samples.n_samples (
int) – Number of samples to generate. Optional, defaults to 1000.n_walkers (
int) – Number of MCMC walkers. Optional, defaults to 32.n_burn (
int) – Number of burn-in samples. Optional, defaults to 500.n_thin (
int) – Thinning factor. Optional, defaults to 10.progress (
bool) – Whether to show the progress bar. Optional, defaults to True.
- Return type:
None
- nested_sampling()#
Perform nested sampling to estimate the Bayesian evidence of the model.
- Return type:
None
- property distribution: ndarray#
A distribution of samples for the relationship that can be used for easy plotting of credible intervals.