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     1e-05       100     1e-11    *
3FGL J1109.6+3734
  24 Prefactor                 0.33         0     1e-05       100     1e-14    *
galdiff
  52 Prefactor                    1         0       0.1        10         1    *
isodiff
  55 Normalization                1         0     0.001     1e+03         1    *
>>> o = gta.fit()
2016-04-19 14:07:55 INFO     GTAnalysis.fit(): Starting fit.
2016-04-19 14:08:56 INFO     GTAnalysis.fit(): Fit returned successfully.
2016-04-19 14:08:56 INFO     GTAnalysis.fit(): Fit Quality: 3 LogLike:   -77279.869 DeltaLogLike:      501.128
>>> gta.print_params(True)
2016-04-19 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     1e-05       100     1e-11    *
3FGL J1109.6+3734
  24 Prefactor                0.342    0.0904     1e-05       100     1e-14    *
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:

fit Output Dictionary
Key Type Description
edm float Estimated distance to maximum of log-likelihood 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 positive-definite (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 log-likehood value.
loglike float Post-fit log-likehood value.
values ndarray Vector of best-fit 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

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.

optimization Output Dictionary
Key Type Description
loglike0 float Pre-optimization log-likelihood value.
loglike1 float Post-optimization log-likelihood value.
dloglike float Improvement in log-likehood value.
config dict Copy of input configuration to this method.

Reference/API