191 this->GApplication::clear();
230 for (
int i = 0; i <
m_obs.size(); ++i) {
231 double data =
m_obs[i]->nobserved();
241 GMatrixSparse curvature =
242 *(
const_cast<GObservations::likelihood&
>(
m_obs.function()).curvature());
248 std::vector<std::string> ts_srcs;
249 GModels models_orig =
m_obs.models();
250 for (
int i = 0; i < models_orig.size(); ++i) {
251 GModel* model = models_orig[i];
252 if (model->tscalc()) {
253 ts_srcs.push_back(model->name());
258 if (!ts_srcs.empty()) {
262 log_header1(EXPLICIT,
"Maximum likelihood optimisation results");
264 log_value(EXPLICIT,
"Maximum log likelihood", gammalib::str(
m_logL,3));
265 log_value(EXPLICIT,
"Observed events (Nobs)", gammalib::str(
m_nobs,3));
266 log_value(EXPLICIT,
"Predicted events (Npred)", gammalib::str(
m_npred,3)+
272 GModels models =
m_obs.models();
278 for (
int i = 0; i < models.size(); ++i) {
281 GModelSky* sky=
dynamic_cast<GModelSky*
>(models[i]);
285 GModelSpatial* spatial = sky->spatial();
286 for (
int j = 0; j < spatial->size(); j++) {
297 for (
int i = 0; i < ts_srcs.size(); ++i) {
300 GModels models_copy = models;
303 models_copy.remove(ts_srcs[i]);
306 m_obs.models(models_copy);
312 double ts = 2.0 * (logL_src-logL_nosrc);
315 models_orig[ts_srcs[i]]->ts(ts);
320 m_obs.models(models_orig);
325 log_header1(NORMAL,
"Maximum likelihood optimisation results");
327 log_value(NORMAL,
"Total number of iterations",
m_iter);
328 log_value(NORMAL,
"Maximum log likelihood", gammalib::str(
m_logL,3));
329 log_value(NORMAL,
"Observed events (Nobs)", gammalib::str(
m_nobs,3));
330 log_value(NORMAL,
"Predicted events (Npred)", gammalib::str(
m_npred,3)+
338 *(
const_cast<GObservations::likelihood&
>(
m_obs.function()).curvature()) =
360 log_header1(TERSE,
"Save results");
363 if ((*
this)[
"outmodel"].is_valid()) {
372 log_value(NORMAL,
"Model definition file",
m_outmodel.url());
381 log_value(NORMAL,
"Model definition file",
"NONE");
385 if ((*
this)[
"outcovmat"].is_valid()) {
391 log_value(NORMAL,
"Covariance matrix file",
m_outcovmat.url());
400 log_value(NORMAL,
"Covariance matrix file",
"NONE");
492 if (
m_obs.models().size() == 0) {
495 std::string filename = (*this)[
"inmodel"].filename();
498 m_obs.models(GModels(filename));
505 m_opt.eps((*
this)[
"like_accuracy"].real());
506 m_opt.accept_dec((*
this)[
"accept_dec"].real());
509 m_refit = (*this)[
"refit"].boolean();
513 m_chatter =
static_cast<GChatter
>((*this)[
"chatter"].integer());
517 (*this)[
"outmodel"].query();
518 (*this)[
"outcovmat"].query();
523 static_cast<GOptimizerLM*
>(&
m_opt)->logger(&log);
526 static_cast<GOptimizerLM*
>(&
m_opt)->logger(NULL);
531 int nthreads = (*this)[
"nthreads"].integer();
533 omp_set_num_threads(nthreads);
553 log_header1(TERSE,
"Maximum likelihood optimisation");
561 for (
int i = 0; i <
m_obs.models().size(); ++i) {
562 const GModel* model =
m_obs.models()[i];
563 for (
int k = 0; k < model->size(); ++k) {
564 if ((*model)[k].is_free()) {
572 log_string(TERSE,
"WARNING: All model parameters are fixed!");
573 log_string(TERSE,
" ctlike will proceed without fitting parameters.");
574 log_string(TERSE,
" All curvature matrix elements will be zero.");
588 log_header1(TERSE,
"Maximum likelihood re-optimisation");
600 log_header1(EXPLICIT,
"Curvature matrix");
602 log_string(EXPLICIT, (const_cast<GObservations::likelihood&>
603 (
m_obs.function()).curvature())->print());
628 log_header1(TERSE,
"Maximum likelihood re-optimisation");
638 m_obs.optimize(*opt);
647 m_obs.optimize(*opt);
655 double logL = -(opt->value());
659 log_header1(EXPLICIT,
"Maximum likelihood re-optimisation results");
660 log_string(EXPLICIT, opt->print(
m_chatter));
661 log_value(EXPLICIT,
"Total number of iterations", iter);
662 log_value(EXPLICIT,
"Maximum log likelihood", gammalib::str(logL,3));
663 log_value(EXPLICIT,
"Observed events (Nobs)", gammalib::str(
m_nobs,3));
664 log_value(EXPLICIT,
"Predicted events (Npred)", gammalib::str(
m_obs.npred(),3)+
665 " (Nobs - Npred = "+gammalib::str(
m_nobs-
m_obs.npred())+
")");
688 switch (
m_opt.status()) {
690 status.append(
"converged");
693 status.append(
"stalled");
696 status.append(
"singular curvature matrix encountered");
698 case G_LM_NOT_POSTIVE_DEFINITE:
699 status.append(
"curvature matrix not positive definite");
701 case G_LM_BAD_ERRORS:
702 status.append(
"errors are inaccurate");
705 status.append(
"unknown");
715 if (xml.elements(
"ctlike_results") == 0) {
716 xml.append(GXmlElement(
"ctlike_results title=\"ctlike fit results\""));
718 GXmlElement* result = xml.element(
"ctlike_results", 0);
719 result->append(GXmlElement(
"status", status));
720 result->append(GXmlElement(
"log-likelihood",
m_logL));
721 result->append(GXmlElement(
"precision",
m_opt.eps()));
722 result->append(GXmlElement(
"iterations",
m_iter));
723 result->append(GXmlElement(
"lambda",
m_opt.lambda()));
724 result->append(GXmlElement(
"total_parameters",
m_opt.npars()));
725 result->append(GXmlElement(
"fitted_parameters",
m_opt.nfree()));
726 result->append(GXmlElement(
"observed-events",
m_nobs));
727 result->append(GXmlElement(
"predicted-events",
m_npred));
728 result->append(GXmlElement(
"refit", refit));
729 result->append(GXmlElement(
"edisp", edisp));
730 result->append(GXmlElement(
"fix-spatial-for-ts", fix_spat_for_ts));
731 result->append(GXmlElement(
"elapsed-time", this->telapse()));
732 result->append(GXmlElement(
"cpu-seconds", this->celapse()));
735 m_obs.models().write(xml);
763 if (opt->status() == G_LM_STALLED) {
765 log_value(NORMAL,
"Refit requested",
"Fit has stalled");
771 log_value(NORMAL,
"Refit requested",
772 "Number of fit iterations exhausted");
781 double fraction = difference /
m_nobs;
782 if (std::abs(fraction) > 1.0e-5) {
784 log_value(NORMAL,
"Refit requested",
786 gammalib::str(difference,3)+
787 " between number of observed events "+
789 " and predicted events "+
790 gammalib::str(npred,3)+
791 " is larger than +/- 1.0e-5 times the number of"+
792 " observed events ("+
793 gammalib::str(fraction)+
").");
796 if (!refit && std::abs(difference) > 5.0) {
798 log_value(NORMAL,
"Refit requested",
800 gammalib::str(difference,3)+
801 " between number of observed events "+
803 " and predicted events "+
804 gammalib::str(npred,3)+
805 " is larger than +/- 5.");
bool m_refit_if_failed
Refitting in case of failure?
bool refit(const GOptimizer *opt)
Refit needed?
int m_max_iter
Maximum number of iterations.
bool m_fix_spat_for_ts
Fix spatial parameters for TS computation?
double m_npred
Number of predicted events.
Maximum likelihood fitting tool.
virtual ~ctlike(void)
Destructor.
ctlikelihood & operator=(const ctlikelihood &app)
Assignment operator.
ctlike & operator=(const ctlike &app)
Assignment operator.
void get_parameters(void)
Get application parameters.
void free_members(void)
Delete class members.
double reoptimize_lm(void)
Re-optimise model parameters for TS computation.
void process(void)
Process maximum likelihood analysis.
GFilename m_outmodel
Source model output XML file name.
const GOptimizer * opt(void) const
Return optimizer.
const double & npred(void) const
Return number of predicted events.
void init_members(void)
Initialise class members.
void set_obs_statistic(const std::string &statistic)
Set fit statistic for CTA observations.
const int & iter(void) const
Return number of maximum likelihood iterations.
int m_iter
Number of iterations.
Maximum likelihood fitting tool definition.
void optimize_lm(void)
Optimise model parameters.
void free_members(void)
Delete class members.
void free_members(void)
Delete class members.
void init_members(void)
Initialise class members.
void copy_members(const ctlike &app)
Copy class members.
GFilename m_outcovmat
Covariance matrix output file name.
double m_logL
Maximum log likelihood.
GChatter m_chatter
Chattiness.
double m_nobs
Number of observed events.
ctlike(void)
Void constructor.
void save(void)
Save results.
void init_members(void)
Initialise class members.
void clear(void)
Clear ctlike tool.
GObservations m_obs
Observation container.
bool m_apply_edisp
Apply energy dispersion?
const double & logL(void) const
Return maximum likelihood value.
Base class for likelihood tools.
GOptimizerLM m_opt
Optimizer.
GXml xml_result(void) const
Generate XML result.