threeML.classicMLE package

Submodules

threeML.classicMLE.goodness_of_fit module

class threeML.classicMLE.goodness_of_fit.GoodnessOfFit(joint_likelihood_instance, like_data_frame=None)[source]

Bases: object

by_mc(n_iterations=1000, continue_on_failure=False)[source]

Compute goodness of fit by generating Monte Carlo datasets and fitting the current model on them. The fraction of synthetic datasets which have a value for the likelihood larger or equal to the observed one is a measure of the goodness of fit

Parameters
  • n_iterations – number of MC iterations to perform (default: 1000)

  • continue_of_failure – whether to continue in the case a fit fails (False by default)

Returns

tuple (goodness of fit, frame with all results, frame with all likelihood values)

get_model(id)[source]
get_simulated_data(id)[source]

threeML.classicMLE.joint_likelihood module

class threeML.classicMLE.joint_likelihood.JointLikelihood(likelihood_model: astromodels.core.model.Model, data_list: threeML.data_list.DataList, verbose: bool = False, record: bool = True)[source]

Bases: object

property analysis_type
compute_TS(source_name, alt_hyp_mlike_df)[source]

Computes the Likelihood Ratio Test statistic (TS) for the provided source

Parameters
  • source_name – name for the source

  • alt_hyp_mlike_df – likelihood dataframe (it is the second output of the .fit() method)

Returns

a DataFrame containing the null hypothesis and the alternative hypothesis -log(likelihood) values and

the value for TS for the source for each loaded dataset

property correlation_matrix
Returns

correlation matrix from the last fit

property covariance_matrix
Returns

covariance matrix from the last fit

property current_minimum
Returns

current minimum of the joint likelihood (available only after the fit() method)

property data_list
Returns

data list for this analysis

fit(quiet: bool = False, compute_covariance: bool = True, n_samples: int = 5000)[source]

Perform a fit of the current likelihood model on the datasets

Parameters

quiet – If True, print the results (default), otherwise do not print anything

:param compute_covariance:If True (default), compute and display the errors and the correlation matrix. :return: a dictionary with the results on the parameters, and the values of the likelihood at the minimum

for each dataset and the total one.

property fit_trace
get_contours(param_1, param_1_minimum, param_1_maximum, param_1_n_steps, param_2=None, param_2_minimum=None, param_2_maximum=None, param_2_n_steps=None, progress=True, **options)[source]

Generate confidence contours for the given parameters by stepping for the given number of steps between the given boundaries. Call it specifying only source_1, param_1, param_1_minimum and param_1_maximum to generate the profile of the likelihood for parameter 1. Specify all parameters to obtain instead a 2d contour of param_1 vs param_2.

NOTE: if using parallel computation, param_1_n_steps must be an integer multiple of the number of running engines. If that is not the case, the code will reduce the number of steps to match that requirement, and issue a warning

Parameters
  • param_1 – fully qualified name of the first parameter or parameter instance

  • param_1_minimum – lower bound for the range for the first parameter

  • param_1_maximum – upper bound for the range for the first parameter

  • param_1_n_steps – number of steps for the first parameter

  • param_2 – fully qualified name of the second parameter or parameter instance

  • param_2_minimum – lower bound for the range for the second parameter

  • param_2_maximum – upper bound for the range for the second parameter

  • param_2_n_steps – number of steps for the second parameter

  • progress – (True or False) whether to display progress or not

  • log – by default the steps are taken linearly. With this optional parameter you can provide a tuple of booleans which specify whether the steps are to be taken logarithmically. For example, ‘log=(True,False)’ specify that the steps for the first parameter are to be taken logarithmically, while they are linear for the second parameter. If you are generating the profile for only one parameter, you can specify ‘log=(True,)’ or ‘log=(False,)’ (optional)

Returns

a tuple containing an array corresponding to the steps for the first parameter, an array corresponding to the steps for the second parameter (or None if stepping only in one direction), a matrix of size param_1_steps x param_2_steps containing the value of the function at the corresponding points in the grid. If param_2_steps is None (only one parameter), then this reduces to an array of size param_1_steps.

get_errors(quiet=False)[source]

Compute the errors on the parameters using the profile likelihood method.

Returns

a dictionary containing the asymmetric errors for each parameter.

property likelihood_model
Returns

likelihood model for this analysis

property minimizer
Returns

an instance of the minimizer used in the fit (available only after the fit() method)

property minimizer_in_use
minus_log_like_profile(*trial_values)[source]

Return the minus log likelihood for a given set of trial values

Parameters

trial_values – the trial values. Must be in the same number as the free parameters in the model

Returns

minus log likelihood

plot_all_contours(nsteps_1d, nsteps_2d=0, n_sigma=5, log_norm=True)[source]
restore_best_fit()[source]

Restore the model to its best fit

Returns

(none)

property results
set_minimizer(minimizer)[source]

Set the minimizer to be used, among those available.

Parameters

minimizer – the name of the new minimizer or an instance of a LocalMinimization or a GlobalMinimization

class. Using the latter two classes allows for more choices and a better control of the details of the minimization, like the choice of algorithms (if supported by the used minimizer) :return: (none)

exception threeML.classicMLE.joint_likelihood.NotANumberInLikelihood[source]

Bases: Warning

exception threeML.classicMLE.joint_likelihood.ReducingNumberOfSteps[source]

Bases: Warning

exception threeML.classicMLE.joint_likelihood.ReducingNumberOfThreads[source]

Bases: Warning

threeML.classicMLE.joint_likelihood_set module

class threeML.classicMLE.joint_likelihood_set.JointLikelihoodSet(data_getter, model_getter, n_iterations, iteration_name='interval', preprocessor=None)[source]

Bases: object

go(continue_on_failure=True, compute_covariance=False, verbose=False, **options_for_parallel_computation)[source]
property results

Returns a results set for each model. If there is more than one model, it will return a list of AnalysisResultsSet instances, otherwise it will return one AnalysisResultsSet instance

Returns

set_minimizer(minimizer)[source]
worker(interval)[source]
write_to(filenames, overwrite=False)[source]

Write the results to one file per model. If you need more control, get the results using the .results property then write each results set by itself.

Parameters
  • filenames – list of filenames, one per model, or filename (if there is only one model per interval)

  • overwrite – overwrite existing files

Returns

None

class threeML.classicMLE.joint_likelihood_set.JointLikelihoodSetAnalyzer(get_data, get_model, data_frame, like_data_frame)[source]

Bases: object

A class to help in offline re-analysis of the results obtained with the JointLikelihoodSet class

restore_best_fit_model(interval)[source]

threeML.classicMLE.likelihood_ratio_test module

class threeML.classicMLE.likelihood_ratio_test.LikelihoodRatioTest(joint_likelihood_instance0, joint_likelihood_instance1)[source]

Bases: object

property TS_distribution
by_mc(n_iterations=1000, continue_on_failure=False, save_pha=False)[source]

Compute the Likelihood Ratio Test by generating Monte Carlo datasets and fitting the current models on them. The fraction of synthetic datasets which have a value for the TS larger or equal to the observed one gives the null-hypothesis probability (i.e., the probability that the observed TS is obtained by chance from the null hypothesis)

Parameters
  • n_iterations – number of MC iterations to perform (default: 1000)

  • continue_of_failure – whether to continue in the case a fit fails (False by default)

  • save_pha – Saves pha files for reading into XSPEC as a cross check. Currently only supports OGIP data. This can become slow! (False by default)

Returns

tuple (null. hyp. probability, TSs, frame with all results, frame with all likelihood values)

get_models(id)[source]
get_simulated_data(id)[source]
property null_hypothesis_probability
plot_TS_distribution(show_chi2=True, scale=1.0, **hist_kwargs)[source]
Parameters
  • show_chi2

  • scale

  • hist_kwargs

Returns

property reference_TS

Module contents