ilex.pyfit
- class PyfitLikelihood(*args: Any, **kwargs: Any)[source]
-
- property model_parameters
- property sigma
- class fit(**kwargs)[source]
General fitting class, model a function using either bayesian inference using BILBY, or least squares using scipy.curve_fit(). This class has a handful of useful quality of life features, including model plotting and statistics such as chi2.
- x
X data
- Type:
np.ndarray
- y
Y data
- Type:
np.ndarray
- xerr
X data error
- Type:
np.ndarray
- yerr
Y data error
- Type:
np.ndarray
- func
Callable python function to model/fit to
- Type:
__func__
- method
Method to fit
[bayesian] - Use bayesian inference (see https://lscsoft.docs.ligo.org/bilby/)
[least squares] - use least squares method (see https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html)
- Type:
str
- fit_keywords
Dictionary of keywords to apply to fitting method
- Type:
Dict
- prior
Priors for modelling
- Type:
Dict
- static
Priors to keep constant during modelling
- Type:
Dict
- posterior
Sampled posteriors
- Type:
Dict
- likelihood
Likelihood class used for bayesian inference
- Type:
Bilby.Likelihood
- keys
parameter names to be sampled
- Type:
List
- chi2
Chi squared statistic
- Type:
float
- chi2err
Chi squared error
- Type:
float
- rchi2
reduced Chi squared
- Type:
float
- rchi2err
reduced Chi squared error
- Type:
float
- nfitpar
number of fitted parameters
- Type:
int
- dof
degrees of freedom
- Type:
int
- p
p-value statistic
- Type:
float
- bic
bayesian information criterion
- Type:
float
- bic_err
bic error
- Type:
float
- residuals
if true, plot residuals when .plot() is called
- Type:
bool
- plotPosterior
if true, save image of posterior corner plot
- Type:
bool
- fit(redo=False, **kwargs)[source]
Fit to function
- Parameters:
redo (bool) – redo fitting (in case BILBY is being used, remove cached results of any previous fit), default is False
- get_mean_err(func=True)[source]
Get mean errors of posterior
- Parameters:
func (bool, optional) – if true, retrieve errors of posteriors defined in function
- Returns:
_errs – dictionary of errors for posteriors
- Return type:
dict
- get_model(x=None)[source]
Get model fit
- Parameters:
x (array-like) – datapoints to evaluate model, if None will use x values already given to current instance
- Returns:
x (array-like) – x data
y (array-like) – y data - evaluated model values
- get_posteriors(func=True)[source]
Get posteriors
- Parameters:
func (bool) – if true, only retrieve posteriors of parameters of function to be modelled, default is True
- Returns:
posteriors – Dictionary of posterior median/mean best values
- Return type:
dict
- get_priors(func=True)[source]
get priors and statics
- Parameters:
func (bool) – if true, only retrieve priors of parameters of function to be modelled, default is True
- plot(show=True, filename=None, **ax_kw)[source]
Plot fitted model and data
- Parameters:
show (bool, optional) – if true, display plot
filename (str) – name of output image file, default is None
**ax_kw (Dict) – keyword parameters for plotting
- Returns:
fig – figure instance
- Return type:
plt._figure_
- set_posterior(name, val, plus, minus)[source]
Set posterior
- Parameters:
name (str) – name of posterior
val (float) – bestfit value/median
plus (float) – positive std/err
minus (float) – negative std/err
- set_prior(name, pr)[source]
Set prior
- Parameters:
name (str) – name of parameter
pr (float or 2-element list) – prior value
- property sigma