Commit 0c98426af0c81c333316cd7b8deb4f98d17e4f9b
1 parent
62c2a2b6
Add GObservations::logL() method and add const attribute to GOptimizerFunction::value() method.
Showing
5 changed files
with
64 additions
and
28 deletions
include/GObservations.hpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GObservations.hpp - Observation container class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2009-2013 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2009-2014 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -124,6 +124,8 @@ public: |
| 124 | 124 | void models(const std::string& filename); |
| 125 | 125 | const GModels& models(void) const; |
| 126 | 126 | void optimize(GOptimizer& opt); |
| 127 | + void eval(void); | |
| 128 | + double logL(void) const; | |
| 127 | 129 | double npred(void) const; |
| 128 | 130 | std::string print(const GChatter& chatter = NORMAL) const; |
| 129 | 131 | |
| ... | ... | @@ -140,14 +142,14 @@ public: |
| 140 | 142 | likelihood& operator=(const likelihood& fct); |
| 141 | 143 | |
| 142 | 144 | // Implemented pure virtual base class methods |
| 143 | - double value(void); | |
| 144 | - double npred(void) const; | |
| 145 | - GVector* gradient(void); | |
| 146 | - GMatrixSparse* curvature(void); | |
| 145 | + virtual void eval(const GOptimizerPars& pars); | |
| 146 | + virtual double value(void) const; | |
| 147 | + virtual GVector* gradient(void); | |
| 148 | + virtual GMatrixSparse* curvature(void); | |
| 147 | 149 | |
| 148 | 150 | // Other methods |
| 149 | - void set(GObservations* obs); | |
| 150 | - void eval(const GOptimizerPars& pars); | |
| 151 | + void set(GObservations* obs); | |
| 152 | + double npred(void) const; | |
| 151 | 153 | |
| 152 | 154 | protected: |
| 153 | 155 | // Protected methods |
| ... | ... | @@ -283,14 +285,29 @@ const GModels& GObservations::models(void) const |
| 283 | 285 | |
| 284 | 286 | |
| 285 | 287 | /***********************************************************************//** |
| 286 | - * @brief Return total number of predicted events after model fitting | |
| 288 | + * @brief Return log-likelihood of models | |
| 287 | 289 | * |
| 288 | - * @return Total number of predicted events after model fitting. | |
| 290 | + * @return Log-likelihood of models. | |
| 289 | 291 | * |
| 290 | - * Returns the total number of events that is predicted by the models after | |
| 291 | - * they have been fitted to the data. This number if computed following a | |
| 292 | - * call of the optimize() method. If this method has never been called, the | |
| 293 | - * npred() method returns 0. | |
| 292 | + * Returns the log-likelihood of models. This value if computed following a | |
| 293 | + * call of the optimize() or eval() methods. If optimize() or eval() have | |
| 294 | + * never been called, the method returns 0. | |
| 295 | + ***************************************************************************/ | |
| 296 | +inline | |
| 297 | +double GObservations::logL(void) const | |
| 298 | +{ | |
| 299 | + return (m_fct.value()); | |
| 300 | +} | |
| 301 | + | |
| 302 | + | |
| 303 | +/***********************************************************************//** | |
| 304 | + * @brief Return total number of predicted events in models | |
| 305 | + * | |
| 306 | + * @return Total number of predicted events in models. | |
| 307 | + * | |
| 308 | + * Returns the total number of events that is predicted by the models. This | |
| 309 | + * number if computed following a call of the optimize() or eval() methods. | |
| 310 | + * If optimize() or eval() have never been called, the method returns 0. | |
| 294 | 311 | ***************************************************************************/ |
| 295 | 312 | inline |
| 296 | 313 | double GObservations::npred(void) const |
| ... | ... | @@ -314,14 +331,14 @@ const GObservations::likelihood& GObservations::function(void) const |
| 314 | 331 | |
| 315 | 332 | |
| 316 | 333 | /***********************************************************************//** |
| 317 | - * @brief Return likelihood function value | |
| 334 | + * @brief Return log-likelihood value of optimizer function | |
| 318 | 335 | * |
| 319 | - * @return Likelihood function value. | |
| 336 | + * @return Log-likelihood value of optimizer function. | |
| 320 | 337 | * |
| 321 | - * Returns the actual function value of the likelihood function. | |
| 338 | + * Returns the log-likelihood value of optimizer function. | |
| 322 | 339 | ***************************************************************************/ |
| 323 | 340 | inline |
| 324 | -double GObservations::likelihood::value(void) | |
| 341 | +double GObservations::likelihood::value(void) const | |
| 325 | 342 | { |
| 326 | 343 | return m_value; |
| 327 | 344 | } | ... | ... |
include/GOptimizerFunction.hpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GOptimizerFunction.hpp - Optimizer function abstract base class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2009-2013 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2009-2014 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -60,7 +60,7 @@ public: |
| 60 | 60 | |
| 61 | 61 | // Virtual methods |
| 62 | 62 | virtual void eval(const GOptimizerPars& pars) = 0; |
| 63 | - virtual double value(void) = 0; | |
| 63 | + virtual double value(void) const = 0; | |
| 64 | 64 | virtual GVector* gradient(void) = 0; |
| 65 | 65 | virtual GMatrixSparse* curvature(void) = 0; |
| 66 | 66 | ... | ... |
pyext/GObservations.i
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GObservations.i - Observations container class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2008-2013 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2008-2014 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -64,9 +64,10 @@ public: |
| 64 | 64 | void models(const std::string& filename); |
| 65 | 65 | const GModels& models(void); |
| 66 | 66 | void optimize(GOptimizer& opt); |
| 67 | + void eval(void); | |
| 68 | + double logL(void) const; | |
| 67 | 69 | double npred(void) const; |
| 68 | 70 | |
| 69 | - | |
| 70 | 71 | // Optimizer function access method |
| 71 | 72 | const GObservations::likelihood& function(void) const; |
| 72 | 73 | }; |
| ... | ... | @@ -81,14 +82,14 @@ public: |
| 81 | 82 | ~likelihood(void); |
| 82 | 83 | |
| 83 | 84 | // Implemented pure virtual base class methods |
| 84 | - double value(void); | |
| 85 | - double npred(void) const; | |
| 86 | - GVector* gradient(void); | |
| 87 | - GMatrixSparse* curvature(void); | |
| 85 | + virtual void eval(const GOptimizerPars& pars); | |
| 86 | + virtual double value(void) const; | |
| 87 | + virtual GVector* gradient(void); | |
| 88 | + virtual GMatrixSparse* curvature(void); | |
| 88 | 89 | |
| 89 | 90 | // Other methods |
| 90 | - void set(GObservations* obs); | |
| 91 | - void eval(const GOptimizerPars& pars); | |
| 91 | + void set(GObservations* obs); | |
| 92 | + double npred(void) const; | |
| 92 | 93 | }; |
| 93 | 94 | %nestedworkaround GObservations::likelihood; |
| 94 | 95 | %{ | ... | ... |
src/obs/GObservations.cpp
| ... | ... | @@ -722,6 +722,24 @@ void GObservations::optimize(GOptimizer& opt) |
| 722 | 722 | |
| 723 | 723 | |
| 724 | 724 | /***********************************************************************//** |
| 725 | + * @brief Evaluate function | |
| 726 | + * | |
| 727 | + * Evaluates the likelihood funtion at the actual set of parameters. | |
| 728 | + ***************************************************************************/ | |
| 729 | +void GObservations::eval(void) | |
| 730 | +{ | |
| 731 | + // Extract optimizer parameter container from model container | |
| 732 | + GOptimizerPars pars = m_models.pars(); | |
| 733 | + | |
| 734 | + // Compute log-likelihood | |
| 735 | + m_fct.eval(pars); | |
| 736 | + | |
| 737 | + // Return | |
| 738 | + return; | |
| 739 | +} | |
| 740 | + | |
| 741 | + | |
| 742 | +/***********************************************************************//** | |
| 725 | 743 | * @brief Print observation list information |
| 726 | 744 | * |
| 727 | 745 | * @param[in] chatter Chattiness (defaults to NORMAL). | ... | ... |
src/opt/GOptimizerLM.cpp
| ... | ... | @@ -611,7 +611,7 @@ void GOptimizerLM::iteration(GOptimizerFunction& fct, GOptimizerPars& pars) |
| 611 | 611 | |
| 612 | 612 | // Single loop for common exit point |
| 613 | 613 | do { |
| 614 | - | |
| 614 | + | |
| 615 | 615 | // Initialise iteration parameters |
| 616 | 616 | GVector* grad = fct.gradient(); |
| 617 | 617 | GMatrixSparse* curvature = fct.curvature(); | ... | ... |