Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
FumiliStandardMaximumLikelihoodFCN.cxx
Go to the documentation of this file.
1// @(#)root/minuit2:$Id$
2// Authors: M. Winkler, F. James, L. Moneta, A. Zsenei 2003-2005
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2005 LCG ROOT Math team, CERN/PH-SFT *
7 * *
8 **********************************************************************/
9
11
12#include <vector>
13#include <cmath>
14#include <limits>
15
16namespace ROOT {
17
18namespace Minuit2 {
19
20std::vector<double> FumiliStandardMaximumLikelihoodFCN::Elements(std::vector<double> const &par) const
21{
22
23 // calculate likelihood element f(i) = pdf(x(i))
24 std::vector<double> result;
25 double tmp1 = 0.0;
26 unsigned int fPositionsSize = fPositions.size();
27
28 for (unsigned int i = 0; i < fPositionsSize; i++) {
29
30 const std::vector<double> &currentPosition = fPositions[i];
31
32 // The commented line is the object-oriented way to do it
33 // but it is faster to do a single function call...
34 //(*(this->getModelFunction())).SetParameters(par);
35 tmp1 = (*(this->ModelFunction()))(par, currentPosition);
36
37 // std::cout << " i = " << i << " " << currentPosition[0] << " " << tmp1 << std::endl;
38
39 result.push_back(tmp1);
40 }
41
42 return result;
43}
44
45const std::vector<double> &FumiliStandardMaximumLikelihoodFCN::GetMeasurement(int index) const
46{
47 // Return x(i).
48 return fPositions[index];
49}
50
52{
53 // return size of positions (coordinates).
54 return fPositions.size();
55}
56
57void FumiliStandardMaximumLikelihoodFCN::EvaluateAll(std::vector<double> const &par)
58{
59 // Evaluate in one loop likelihood value, gradient and hessian
60
61 const double minDouble = 8.0 * std::numeric_limits<double>::min();
62 const double minDouble2 = std::sqrt(minDouble);
63 const double maxDouble2 = 1.0 / minDouble2;
64 // loop on the measurements
65
67 std::vector<double> &grad = Gradient();
68 std::vector<double> &h = Hessian();
69 int npar = par.size();
70 double logLikelihood = 0;
71 grad.assign(npar, 0.0);
72 h.assign(static_cast<unsigned int>(0.5 * npar * (npar + 1)), 0.0);
73
75
76 for (int i = 0; i < nmeas; ++i) {
77
78 // work for one-dimensional points
79 const std::vector<double> &currentPosition = fPositions[i];
80 modelFunc.SetParameters(currentPosition);
81 double fval = modelFunc(par);
82 if (fval < minDouble)
83 fval = minDouble; // to avoid getting infinity and nan's
84 logLikelihood -= std::log(fval);
85 double invFval = 1.0 / fval;
86 // this method should return a reference
87 std::vector<double> mfg = modelFunc.GetGradient(par);
88
89 // calc derivatives
90
91 for (int j = 0; j < npar; ++j) {
92 if (std::fabs(mfg[j]) < minDouble) {
93 // std::cout << "SMALL values: grad = " << mfg[j] << " " << minDouble << " f(x) = " << fval
94 // << " params " << j << " p0 = " << par[0] << " p1 = " << par[1] << std::endl;
95 if (mfg[j] < 0)
96 mfg[j] = -minDouble;
97 else
98 mfg[j] = minDouble;
99 }
100
101 double dfj = invFval * mfg[j];
102 // to avoid summing infinite and nan later when calculating the Hessian
103 if (std::fabs(dfj) > maxDouble2) {
104 if (dfj > 0)
105 dfj = maxDouble2;
106 else
107 dfj = -maxDouble2;
108 }
109
110 grad[j] -= dfj;
111 // if ( ! ( dfj > 0) && ! ( dfj <= 0 ) )
112 // std::cout << " nan : dfj = " << dfj << " fval = " << fval << " invF = " << invFval << " grad = " << mfg[j]
113 // << " par[j] = " << par[j] << std::endl;
114
115 // std::cout << " x = " << currentPosition[0] << " par[j] = " << par[j] << " : dfj = " << dfj << " fval = "
116 // << fval << " invF = " << invFval << " grad = " << mfg[j] << " deriv = " << grad[j] << std::endl;
117
118 // in second derivative use Fumili approximation neglecting the term containing the
119 // second derivatives of the model function
120 for (int k = j; k < npar; ++k) {
121 int idx = j + k * (k + 1) / 2;
122 if (std::fabs(mfg[k]) < minDouble) {
123 if (mfg[k] < 0)
124 mfg[k] = -minDouble;
125 else
126 mfg[k] = minDouble;
127 }
128
129 double dfk = invFval * mfg[k];
130 // avoid that dfk*dfj are one small and one huge so I get a nan
131 // to avoid summing infinite and nan later when calculating the Hessian
132 if (std::fabs(dfk) > maxDouble2) {
133 if (dfk > 0)
134 dfk = maxDouble2;
135 else
136 dfk = -maxDouble2;
137 }
138
139 h[idx] += dfj * dfk;
140 // if ( ( ! ( h[idx] > 0) && ! ( h[idx] <= 0 ) ) )
141 // std::cout << " nan : dfj = " << dfj << " fval = " << fval << " invF = " << invFval << " gradj = " <<
142 // mfg[j]
143 // << " dfk = " << dfk << " gradk = "<< mfg[k] << " hess_jk = " << h[idx] << " par[k] = " << par[k] <<
144 // std::endl;
145 }
146
147 } // end param loop
148
149 } // end points loop
150
151 // std::cout <<"\nEVALUATED GRADIENT and HESSIAN " << std::endl;
152 // for (int j = 0; j < npar; ++j) {
153 // std::cout << " j = " << j << " grad = " << grad[j] << std::endl;
154 // for (int k = j; k < npar; ++k) {
155 // std::cout << " k = " << k << " hess = " << Hessian(j,k) << " " << h[ j + k*(k+1)/2] << std::endl;
156 // }
157 // }
158
159 // set Value in base class
161}
162
163} // namespace Minuit2
164
165} // namespace ROOT
#define h(i)
Definition RSha256.hxx:106
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
std::vector< double > & Hessian()
virtual const std::vector< double > & Gradient() const
Return cached Value of function Gradient estimated previously using the FumiliFCNBase::EvaluateAll me...
void SetFCNValue(double value)
const ParametricFunction * ModelFunction() const
Returns the model function used for the data.
int GetNumberOfMeasurements() const override
Accessor to the number of measurements used for calculating the maximum likelihood.
void EvaluateAll(std::vector< double > const &par) override
Evaluate function Value, Gradient and Hessian using Fumili approximation, for values of parameters p ...
const std::vector< double > & GetMeasurement(int Index) const override
Accessor to the position of the measurement (x coordinate).
std::vector< double > Elements(std::vector< double > const &par) const override
Evaluates the model function for the different measurement points and the Parameter values supplied.
Function which has parameters.