asgardpy.stats.stats module¶
Module for performing some statistic functions.
- asgardpy.stats.stats.check_model_preference_aic(list_stat, list_dof)[source]¶
Akaike Information Criterion (AIC) preference over a list of stat and DoF (degree of freedom) to get relative likelihood of a given list of best-fit models.
- asgardpy.stats.stats.check_model_preference_lrt(test_stat_1, test_stat_2, ndof_1, ndof_2)[source]¶
Log-likelihood ratio test. Checking the preference of a “nested” spectral model2 (observed), over a primary model1.
- Parameters:
test_stat_1 (float) – The test statistic (-2 ln L) of the Fit result of the primary spectral model.
test_stat_2 (float) – The test statistic (-2 ln L) of the Fit result of the nested spectral model.
ndof_1 (int) – Number of degrees of freedom for the primary model
ndof_2 (int) – Number of degrees of freedom for the nested model
- Returns:
p_value (float) – p-value for the ratio of the likelihoods
gaussian_sigmas (float) – significance (Chi2) of the ratio of the likelihoods estimated in Gaussian distribution.
n_dof (int) – number of degrees of freedom or free parameters between primary and nested model.
- asgardpy.stats.stats.fetch_pivot_energy(analysis)[source]¶
Using an ‘AsgardpyAnalysis’ object to get the pivot energy for a given dataset and fit model, using the pivot_energy function.
- Returns:
pivot energy – The energy at which the statistical error in the computed flux is smallest. If no minimum is found, NaN will be returned.
- Return type:
~astropy.units.Quantity
- asgardpy.stats.stats.get_chi2_sig_pval(test_stat, ndof)[source]¶
Using the log-likelihood value for a model fitting to data, along with the total degrees of freedom, evaluate the significance value in terms of gaussian distribution along with one-tailed p-value for the fitting statistics.
In Gammapy, for 3D analysis, cash statistics is used, while for 1D analysis, wstat statistics is used. Check the documentation for more details https://docs.gammapy.org/1.3/user-guide/stats/index.html
- asgardpy.stats.stats.get_goodness_of_fit_stats(datasets, instrument_spectral_info)[source]¶
Evaluating the Goodness of Fit statistics of the fitting of the model to the dataset.
We first use the get_ts_target function to get the total test statistic for the (observed) best fit of the model to the data, and the (expected) perfect fit of model and data (model = data), for the given target source region/pixel.
We then evaluate the total number of Degrees of Freedom for the Fit as the difference between the number of relevant energy bins used in the evaluation and the number of free model parameters.
The fit statistics difference is used as the test statistic value for get_chi2_sig_pval function along with the total number of degrees of freedom to get the final statistics for the goodness of fit.
The fit statistics information is updated in the dict object provided and a logging message is passed.
Parameter¶
- datasets: gammapy.datasets.Datasets
List of Datasets object, which can contain 3D and/or 1D datasets
- instrument_spectral_info: dict
Dict of information for storing relevant fit stats
- returns:
instrument_spectral_info (dict) – Filled Dict of information with relevant fit statistics
stat_message (str) – String for logging the fit statistics
- asgardpy.stats.stats.get_ts_target(datasets)[source]¶
From a given list of DL4 datasets, with assumed associated models, estimate the total test statistic values, in the given target source region/pixel, for the (observed) best fit of the model to the data, and the (expected) perfect fit of model and data (model = data).
For consistency in the evaluation of the statistic values, we will use the basic Fit Statistic functions in Gammapy for Poisson Data:
For the different type of Statistics used in Gammapy for 3D/1D datasets, and for our use case of getting the best fit and perfect fit, we will pass the appropriate values, by adapting to the following methods,
Best Fit (Observed):
Perfect Fit (Expected):
Parameter¶
- datasets: gammapy.datasets.Datasets
List of Datasets object, which can contain 3D and/or 1D datasets
- returns:
stat_best_fit (float) – Total sum of test statistic of the best fit of model to data, summed over all energy bins.
stat_max_fit (float) – Test statistic difference of the perfect fit of model to data summed over all energy bins.