ROI Optimization and Fitting¶
Source fitting with fermipy is generally performed with the
optimize
and
fit
methods.
Fitting¶
fit
is a wrapper on the pyLikelihood
fit method and performs a likelihood fit of all free parameters of the
model. This method can be used to manually optimize of the model by
calling it after freeing one or more source parameters. The following
example demonstrates the commands that would be used to fit the
normalizations of all sources within 3 deg of the ROI center:
>>> gta.free_sources(distance=2.0,pars='norm')
>>> gta.print_params(True)
idx parname value error min max scale free

3FGL J1104.4+3812
18 Prefactor 1.77 0 1e05 100 1e11 *
3FGL J1109.6+3734
24 Prefactor 0.33 0 1e05 100 1e14 *
galdiff
52 Prefactor 1 0 0.1 10 1 *
isodiff
55 Normalization 1 0 0.001 1e+03 1 *
>>> o = gta.fit()
20160419 14:07:55 INFO GTAnalysis.fit(): Starting fit.
20160419 14:08:56 INFO GTAnalysis.fit(): Fit returned successfully.
20160419 14:08:56 INFO GTAnalysis.fit(): Fit Quality: 3 LogLike: 77279.869 DeltaLogLike: 501.128
>>> gta.print_params(True)
20160419 14:10:02 INFO GTAnalysis.print_params():
idx parname value error min max scale free

3FGL J1104.4+3812
18 Prefactor 2.13 0.0161 1e05 100 1e11 *
3FGL J1109.6+3734
24 Prefactor 0.342 0.0904 1e05 100 1e14 *
galdiff
52 Prefactor 0.897 0.0231 0.1 10 1 *
isodiff
55 Normalization 1.15 0.016 0.001 1e+03 1 *
By default fit
will repeat the fit
until a fit quality of 3 is obtained. After the fit returns all
sources with free parameters will have their properties (flux, TS,
NPred, etc.) updated in the ROIModel
instance.
The return value of the method is a dictionary containing the
following diagnostic information about the fit:
Key  Type  Description 

edm 
float 
Estimated distance to maximum of loglikelihood function. 
fit_status 
int  Optimizer return code (0 = ok). 
fit_quality 
int  Fit quality parameter for MINUIT and NEWMINUIT optimizers (3  Full accurate covariance matrix, 2  Full matrix, but forced positivedefinite (i.e. not accurate), 1  Diagonal approximation only, not accurate, 0  Error matrix not calculated at all) 
covariance 
ndarray 
Covariance matrix between free parameters of the fit. 
correlation 
ndarray 
Correlation matrix between free parameters of the fit. 
dloglike 
float 
Improvement in loglikehood value. 
loglike 
float 
Postfit loglikehood value. 
values 
ndarray 
Vector of bestfit parameter values (unscaled). 
errors 
ndarray 
Vector of parameter errors (unscaled). 
config 
dict 
Copy of input configuration to this method. 
The fit
also accepts keyword
arguments which can be used to configure its behavior at runtime:
>>> o = gta.fit(min_fit_quality=2,optimizer='NEWMINUIT',reoptimize=True)
Reference/API¶

GTAnalysis.
fit
(update=True, **kwargs)[source] Run the likelihood optimization. This will execute a fit of all parameters that are currently free in the model and update the charateristics of the corresponding model components (TS, npred, etc.). The fit will be repeated N times (set with the
retries
parameter) until a fit quality greater than or equal tomin_fit_quality
and a fit status code of 0 is obtained. If the fit does not succeed after N retries then all parameter values will be reverted to their state prior to the execution of the fit.Parameters:  update (bool) – Update the model dictionary for all sources with free parameters.
 tol (float) – Set the optimizer tolerance.
 verbosity (int) – Set the optimizer output level.
 optimizer (str) – Set the likelihood optimizer (e.g. MINUIT or NEWMINUIT).
 retries (int) – Set the number of times to rerun the fit when the fit quality is < 3.
 min_fit_quality (int) – Set the minimum fit quality. If the fit quality is smaller than this value then all model parameters will be restored to their values prior to the fit.
 reoptimize (bool) – Refit background sources when updating source properties (TS and likelihood profiles).
Returns: fit – Dictionary containing diagnostic information from the fit (fit quality, parameter covariances, etc.).
Return type:
ROI Optimization¶
The optimize
method performs an
automatic optimization of the ROI by fitting all sources with an
iterative strategy.
>>> o = gta.optimize()
It is generally good practice to run this method once at the start of your analysis to ensure that all parameters are close to their global likelihood maxima.
Key  Type  Description 

loglike0 
float 
Preoptimization loglikelihood value. 
loglike1 
float 
Postoptimization loglikelihood value. 
dloglike 
float 
Improvement in loglikehood value. 
config 
dict 
Copy of input configuration to this method. 
Reference/API¶

GTAnalysis.
optimize
(**kwargs)[source] Iteratively optimize the ROI model. The optimization is performed in three sequential steps:
 Free the normalization of the N largest components (as
determined from NPred) that contain a fraction
npred_frac
of the total predicted counts in the model and perform a simultaneous fit of the normalization parameters of these components.  Individually fit the normalizations of all sources that were
not included in the first step in order of their npred
values. Skip any sources that have NPred <
npred_threshold
.  Individually fit the shape and normalization parameters of
all sources with TS >
shape_ts_threshold
where TS is determined from the first two steps of the ROI optimization.
To ensure that the model is fully optimized this method can be run multiple times.
Parameters:  npred_frac (float) – Threshold on the fractional number of counts in the N largest components in the ROI. This parameter determines the set of sources that are fit in the first optimization step.
 npred_threshold (float) – Threshold on the minimum number of counts of individual sources. This parameter determines the sources that are fit in the second optimization step.
 shape_ts_threshold (float) – Threshold on source TS used for determining the sources that will be fit in the third optimization step.
 max_free_sources (int) – Maximum number of sources that will be fit simultaneously in the first optimization step.
 skip (list) – List of str source names to skip while optimizing.
 optimizer (dict) – Dictionary that overrides the default optimizer settings.
 Free the normalization of the N largest components (as
determined from NPred) that contain a fraction