Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
PoissonLikelihoodFCN.h
Go to the documentation of this file.
1// @(#)root/mathcore:$Id$
2// Author: L. Moneta Fri Aug 17 14:29:24 2007
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2007 LCG ROOT Math Team, CERN/PH-SFT *
7 * *
8 * *
9 **********************************************************************/
10
11// Header file for class PoissonLikelihoodFCN
12
13#ifndef ROOT_Fit_PoissonLikelihoodFCN
14#define ROOT_Fit_PoissonLikelihoodFCN
15
17#include "Fit/BasicFCN.h"
18#include "Fit/BinData.h"
19#include "Fit/FitUtil.h"
20#include "Math/IParamFunction.h"
21
22#include <memory>
23#include <vector>
24
25//#define PARALLEL
26// #ifdef PARALLEL
27// #ifndef ROOT_Fit_FitUtilParallel
28// #include "Fit/FitUtilParallel.h"
29// #endif
30// #endif
31
32namespace ROOT {
33
34 namespace Fit {
35
36
37//___________________________________________________________________________________
38/**
39 class evaluating the log likelihood
40 for binned Poisson likelihood fits
41 it is template to distinguish gradient and non-gradient case
42
43 @ingroup FitMethodFunc
44*/
45template<class DerivFunType, class ModelFunType = ROOT::Math::IParamMultiFunction>
46class PoissonLikelihoodFCN : public BasicFCN<DerivFunType,ModelFunType,BinData> {
47
48public:
49 typedef typename ModelFunType::BackendType T;
51
52 typedef ::ROOT::Math::BasicFitMethodFunction<DerivFunType> BaseObjFunction;
54
55 typedef ::ROOT::Math::IParamMultiFunctionTempl<T> IModelFunction;
57
58 /**
59 Constructor from unbin data set and model function (pdf)
60 */
61 PoissonLikelihoodFCN (const std::shared_ptr<BinData> & data, const std::shared_ptr<IModelFunction> & func, int weight = 0, bool extended = true, const ::ROOT::EExecutionPolicy &executionPolicy = ::ROOT::EExecutionPolicy::kSequential ) :
62 BaseFCN( data, func),
63 fIsExtended(extended),
64 fWeight(weight),
65 fNEffPoints(0),
66 fGrad ( std::vector<double> ( func->NPar() ) ),
67 fExecutionPolicy(executionPolicy)
68 { }
69
70 /**
71 Constructor from unbin data set and model function (pdf) managed by the users
72 */
73 PoissonLikelihoodFCN (const BinData & data, const IModelFunction & func, int weight = 0, bool extended = true, const ::ROOT::EExecutionPolicy &executionPolicy = ::ROOT::EExecutionPolicy::kSequential ) :
74 BaseFCN(std::make_shared<BinData>(data), std::shared_ptr<IModelFunction>(dynamic_cast<IModelFunction*>(func.Clone() ) ) ),
75 fIsExtended(extended),
76 fWeight(weight),
77 fNEffPoints(0),
78 fGrad ( std::vector<double> ( func.NPar() ) ),
79 fExecutionPolicy(executionPolicy)
80 { }
81
82
83 /**
84 Destructor (no operations)
85 */
87
88 /**
89 Copy constructor
90 */
94 fWeight( f.fWeight ),
96 fGrad( f.fGrad),
98 { }
99
100 /**
101 Assignment operator
102 */
104 SetData(rhs.DataPtr() );
107 fGrad = rhs.fGrad;
109 fWeight = rhs.fWeight;
111 }
112
113
114 /// clone the function (need to return Base for Windows)
115 virtual BaseFunction * Clone() const { return new PoissonLikelihoodFCN(*this); }
116
117 // effective points used in the fit
118 virtual unsigned int NFitPoints() const { return fNEffPoints; }
119
120 /// i-th likelihood element and its gradient
121 virtual double DataElement(const double * x, unsigned int i, double * g, double * h, bool fullHessian) const {
122 if (i==0) this->UpdateNCalls();
124 }
125
126 /// evaluate gradient
127 virtual void Gradient(const double *x, double *g) const
128 {
129 // evaluate the Poisson gradient
132 }
133
134 /**
135 * Computes the full Hessian. Return false if Hessian is not supported
136 */
137 // virtual bool Hessian(const double * x, double * hess) const {
138 // //return full Hessian
139 // unsigned int np = NPoints();
140 // unsigned int ndim = NDim();
141 // unsigned int nh = ndim*(ndim+1)/2;
142 // for (unsigned int k = 0; k < nh; ++k) {
143 // hess[k] = 0;
144 // }
145 // std::vector<double> g(np); // gradient of the residual (y-f)/sigma
146 // std::vector<double> h(nh); // hessian of residual
147 // for (unsigned int i = 0; i < np; i++) {
148 // double f = DataElement(x,i,g,h,true);
149 // if (f == std::numeric_limits<double>::quiet_NaN() ) return false;
150 // for (unsigned int j = 0; j < np; j++) {
151 // for (unsigned int k = 0; k <=j; k++) {
152 // unsigned int index = j + k * (k + 1) / 2;
153 // h[index] += 2. * ( g[j]*g[k] + f * h[index]*h[index] );
154 // }
155 // }
156 // }
157 // return true;
158 // }
159
160 /// get type of fit method function
162
163 bool IsWeighted() const { return (fWeight != 0); }
164
165 // Use the weights in evaluating the likelihood
167 if (fWeight == 0) return; // do nothing if it was not weighted
168 fWeight = 1;
169 }
170
171 // Use sum of the weight squared in evaluating the likelihood
172 // (this is needed for calculating the errors)
173 void UseSumOfWeightSquare(bool on = true) {
174 if (fWeight == 0) return; // do nothing if it was not weighted
175 if (on) fWeight = 2;
176 else fWeight = 1;
177 }
178
179
180protected:
181
182
183private:
184
185 /**
186 Evaluation of the function (required by interface)
187 */
188 virtual double DoEval (const double * x) const {
189 this->UpdateNCalls();
192 }
193
194 // for derivatives
195 virtual double DoDerivative(const double * x, unsigned int icoord ) const {
196 Gradient(x, &fGrad[0]);
197 return fGrad[icoord];
198 }
199
200
201 //data member
202
203 bool fIsExtended; ///< flag to indicate if is extended (when false is a Multinomial likelihood), default is true
204 int fWeight; ///< flag to indicate if needs to evaluate using weight or weight squared (default weight = 0)
205
206 mutable unsigned int fNEffPoints; ///< number of effective points used in the fit
207
208 mutable std::vector<double> fGrad; ///< for derivatives
209
211};
212
213 // define useful typedef's
216
217
218 } // end namespace Fit
219
220} // end namespace ROOT
221
222
223#endif /* ROOT_Fit_PoissonLikelihoodFCN */
#define f(i)
Definition RSha256.hxx:104
#define g(i)
Definition RSha256.hxx:105
#define h(i)
Definition RSha256.hxx:106
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void on
BasicFCN class: base class for the objective functions used in the fits It has a reference to the dat...
Definition BasicFCN.h:40
void SetData(const std::shared_ptr< DataType > &data)
Set the data pointer.
Definition BasicFCN.h:98
std::shared_ptr< IModelFunction > ModelFunctionPtr() const
access to function pointer
Definition BasicFCN.h:81
void SetModelFunction(const std::shared_ptr< IModelFunction > &func)
Set the function pointer.
Definition BasicFCN.h:101
virtual const DataType & Data() const
access to const reference to the data
Definition BasicFCN.h:72
std::shared_ptr< DataType > DataPtr() const
access to data pointer
Definition BasicFCN.h:75
virtual const IModelFunction & ModelFunction() const
access to const reference to the model function
Definition BasicFCN.h:78
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition BinData.h:52
class evaluating the log likelihood for binned Poisson likelihood fits it is template to distinguish ...
virtual unsigned int NFitPoints() const
::ROOT::Math::IParamMultiFunctionTempl< T > IModelFunction
PoissonLikelihoodFCN(const BinData &data, const IModelFunction &func, int weight=0, bool extended=true, const ::ROOT::EExecutionPolicy &executionPolicy=::ROOT::EExecutionPolicy::kSequential)
Constructor from unbin data set and model function (pdf) managed by the users.
virtual double DoDerivative(const double *x, unsigned int icoord) const
::ROOT::Math::BasicFitMethodFunction< DerivFunType > BaseObjFunction
PoissonLikelihoodFCN(const PoissonLikelihoodFCN &f)
Copy constructor.
virtual BaseFunction * Clone() const
clone the function (need to return Base for Windows)
virtual ~PoissonLikelihoodFCN()
Destructor (no operations)
virtual double DoEval(const double *x) const
Evaluation of the function (required by interface)
::ROOT::EExecutionPolicy fExecutionPolicy
Execution policy.
BaseObjFunction::BaseFunction BaseFunction
virtual void Gradient(const double *x, double *g) const
evaluate gradient
bool fIsExtended
flag to indicate if is extended (when false is a Multinomial likelihood), default is true
PoissonLikelihoodFCN & operator=(const PoissonLikelihoodFCN &rhs)
Assignment operator.
PoissonLikelihoodFCN(const std::shared_ptr< BinData > &data, const std::shared_ptr< IModelFunction > &func, int weight=0, bool extended=true, const ::ROOT::EExecutionPolicy &executionPolicy=::ROOT::EExecutionPolicy::kSequential)
Constructor from unbin data set and model function (pdf)
int fWeight
flag to indicate if needs to evaluate using weight or weight squared (default weight = 0)
unsigned int fNEffPoints
number of effective points used in the fit
BasicFCN< DerivFunType, ModelFunType, BinData > BaseFCN
std::vector< double > fGrad
for derivatives
virtual double DataElement(const double *x, unsigned int i, double *g, double *h, bool fullHessian) const
i-th likelihood element and its gradient
virtual BaseObjFunction::Type_t Type() const
Computes the full Hessian.
Type_t
enumeration specifying the possible fit method types
static bool IsAGradFCN()
Static function to indicate if a function is supporting gradient.
virtual void UpdateNCalls() const
update number of calls
IParamFunction interface (abstract class) describing multi-dimensional parametric functions It is a d...
Double_t x[n]
Definition legend1.C:17
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
Definition HFitImpl.cxx:133
PoissonLikelihoodFCN< ROOT::Math::IMultiGradFunction, ROOT::Math::IParamMultiFunction > PoissonLLGradFunction
PoissonLikelihoodFCN< ROOT::Math::IMultiGenFunction, ROOT::Math::IParamMultiFunction > PoissonLLFunction
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
static double EvalPoissonBinPdf(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, unsigned int i, double *g, double *h, bool hasGrad, bool fullHessian)
evaluate the pdf (Poisson) contribution to the logl (return actually log of pdf) and its gradient
Definition FitUtil.h:1444
static void EvalPoissonLogLGradient(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, double *g, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy=::ROOT::EExecutionPolicy::kSequential, unsigned nChunks=0)
Definition FitUtil.h:1453
static double EvalPoissonLogL(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, int iWeight, bool extended, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy, unsigned nChunks=0)
Definition FitUtil.h:1417