Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
LogLikelihoodFCN.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 LogLikelihoodFCN
12
13#ifndef ROOT_Fit_LogLikelihoodFCN
14#define ROOT_Fit_LogLikelihoodFCN
15
17#include "Fit/BasicFCN.h"
18#include "Fit/FitUtil.h"
19#include "Fit/UnBinData.h"
20#include "Math/IParamFunction.h"
21
22#include <memory>
23#include <vector>
24
25namespace ROOT {
26
27 namespace Fit {
28
29
30//___________________________________________________________________________________
31/**
32 LogLikelihoodFCN class
33 for likelihood fits
34
35 it is template to distinguish gradient and non-gradient case
36
37 @ingroup FitMethodFunc
38*/
39template<class DerivFunType,class ModelFunType = ROOT::Math::IParamMultiFunction>
40class LogLikelihoodFCN : public BasicFCN<DerivFunType,ModelFunType,UnBinData> {
41
42public:
43
44 typedef typename ModelFunType::BackendType T;
46
47 typedef ::ROOT::Math::BasicFitMethodFunction<DerivFunType> BaseObjFunction;
49
50 typedef ::ROOT::Math::IParamMultiFunctionTempl<T> IModelFunction;
52
53
54 /**
55 Constructor from unbin data set and model function (pdf)
56 */
57 LogLikelihoodFCN (const std::shared_ptr<UnBinData> & data, const std::shared_ptr<IModelFunction> & func, int weight = 0, bool extended = false, const ::ROOT::EExecutionPolicy &executionPolicy = ::ROOT::EExecutionPolicy::kSequential) :
58 BaseFCN( data, func),
59 fIsExtended(extended),
60 fWeight(weight),
61 fNEffPoints(0),
62 fGrad ( std::vector<double> ( func->NPar() ) ),
63 fExecutionPolicy(executionPolicy)
64 {}
65
66 /**
67 Constructor from unbin data set and model function (pdf) for object managed by users
68 */
69 LogLikelihoodFCN (const UnBinData & data, const IModelFunction & func, int weight = 0, bool extended = false, const ::ROOT::EExecutionPolicy &executionPolicy = ::ROOT::EExecutionPolicy::kSequential) :
70 BaseFCN(std::make_shared<UnBinData>(data), std::shared_ptr<IModelFunction>(dynamic_cast<IModelFunction*>(func.Clone() ) ) ),
71 fIsExtended(extended),
72 fWeight(weight),
73 fNEffPoints(0),
74 fGrad ( std::vector<double> ( func.NPar() ) ),
75 fExecutionPolicy(executionPolicy)
76 {}
77
78 /**
79 Destructor (no operations)
80 */
81 virtual ~LogLikelihoodFCN () {}
82
83 /**
84 Copy constructor
85 */
94
95
96 /**
97 Assignment operator
98 */
100 SetData(rhs.DataPtr() );
103 fGrad = rhs.fGrad;
105 fWeight = rhs.fWeight;
107 return *this;
108 }
109
110
111 /// clone the function (need to return Base for Windows)
112 BaseFunction * Clone() const override { return new LogLikelihoodFCN(*this); }
113
114
115 //using BaseObjFunction::operator();
116
117 // effective points used in the fit
118 virtual unsigned int NFitPoints() const { return fNEffPoints; }
119
120 /// i-th likelihood contribution and its gradient
121 double DataElement(const double * x, unsigned int i, double * g, double * h = nullptr, bool fullHessian = false) const override {
122 if (i==0) this->UpdateNCalls();
124 }
125
126 // need to be virtual to be instantiated
127 void Gradient(const double *x, double *g) const override {
128 // evaluate the chi2 gradient
131 }
132
133 /// get type of fit method function
135
136
137 // Use sum of the weight squared in evaluating the likelihood
138 // (this is needed for calculating the errors)
139 void UseSumOfWeightSquare(bool on = true) {
140 if (fWeight == 0) return; // do nothing if it was not weighted
141 if (on) fWeight = 2;
142 else fWeight = 1;
143 }
144
145
146
147protected:
148
149
150private:
151
152 /**
153 Evaluation of the function (required by interface)
154 */
155 double DoEval (const double * x) const override {
156 this->UpdateNCalls();
158 }
159
160 // for derivatives
161 double DoDerivative(const double * x, unsigned int icoord) const override {
162 Gradient(x, &fGrad[0]);
163 return fGrad[icoord];
164 }
165
166
167 //data member
168 bool fIsExtended; ///< flag for indicating if likelihood is extended
169 int fWeight; ///< flag to indicate if needs to evaluate using weight or weight squared (default weight = 0)
170
171
172 mutable unsigned int fNEffPoints; ///< number of effective points used in the fit
173
174 mutable std::vector<double> fGrad; ///< for derivatives
175
177};
178 // define useful typedef's
179 // using LogLikelihoodFunction_v = LogLikelihoodFCN<ROOT::Math::IMultiGenFunction, ROOT::Math::IParametricFunctionMultiDimTempl<T>>;
182
183 } // end namespace Fit
184
185} // end namespace ROOT
186
187
188#endif /* ROOT_Fit_LogLikelihoodFCN */
#define f(i)
Definition RSha256.hxx:104
#define g(i)
Definition RSha256.hxx:105
#define h(i)
Definition RSha256.hxx:106
void SetModelFunction(const std::shared_ptr< IModelFunction > &func)
Definition BasicFCN.h:101
virtual const DataType & Data() const
access to const reference to the data
Definition BasicFCN.h:72
BasicFCN(const std::shared_ptr< UnBinData > &data, const std::shared_ptr< IModelFunction > &func)
Definition BasicFCN.h:55
virtual const IModelFunction & ModelFunction() const
access to const reference to the model function
Definition BasicFCN.h:78
LogLikelihoodFCN class for likelihood fits.
BaseObjFunction::BaseFunction BaseFunction
LogLikelihoodFCN(const UnBinData &data, const IModelFunction &func, int weight=0, bool extended=false, const ::ROOT::EExecutionPolicy &executionPolicy=::ROOT::EExecutionPolicy::kSequential)
Constructor from unbin data set and model function (pdf) for object managed by users.
LogLikelihoodFCN & operator=(const LogLikelihoodFCN &rhs)
Assignment operator.
::ROOT::Math::IParamMultiFunctionTempl< T > IModelFunction
LogLikelihoodFCN(const LogLikelihoodFCN &f)
Copy constructor.
double DoEval(const double *x) const override
Evaluation of the function (required by interface).
BasicFCN< DerivFunType, ModelFunType, UnBinData > BaseFCN
double DataElement(const double *x, unsigned int i, double *g, double *h=nullptr, bool fullHessian=false) const override
i-th likelihood contribution and its gradient
ModelFunType::BackendType T
LogLikelihoodFCN(const std::shared_ptr< UnBinData > &data, const std::shared_ptr< IModelFunction > &func, int weight=0, bool extended=false, const ::ROOT::EExecutionPolicy &executionPolicy=::ROOT::EExecutionPolicy::kSequential)
Constructor from unbin data set and model function (pdf).
void UseSumOfWeightSquare(bool on=true)
BaseObjFunction::Type_t Type() const override
get type of fit method function
::ROOT::Math::BasicFitMethodFunction< DerivFunType > BaseObjFunction
virtual ~LogLikelihoodFCN()
Destructor (no operations).
virtual unsigned int NFitPoints() const
double DoDerivative(const double *x, unsigned int icoord) const override
void Gradient(const double *x, double *g) const override
BaseObjFunction::Type_t Type_t
Class describing the un-binned data sets (just x coordinates values) of any dimensions.
Definition UnBinData.h:46
STL class.
Double_t x[n]
Definition legend1.C:17
Namespace for the fitting classes.
LogLikelihoodFCN< ROOT::Math::IMultiGenFunction, ROOT::Math::IParamMultiFunction > LogLikelihoodFunction
LogLikelihoodFCN< ROOT::Math::IMultiGradFunction, ROOT::Math::IParamMultiFunction > LogLikelihoodGradFunction