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 */
89 fWeight( f.fWeight ),
91 fGrad( f.fGrad),
93 { }
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 virtual BaseFunction * Clone() const { 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 virtual double DataElement(const double * x, unsigned int i, double * g, double * h = nullptr, bool fullHessian = false) const {
122 if (i==0) this->UpdateNCalls();
124 }
125
126 // need to be virtual to be instantiated
127 virtual void Gradient(const double *x, double *g) const {
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 virtual double DoEval (const double * x) const {
156 this->UpdateNCalls();
158 }
159
160 // for derivatives
161 virtual double DoDerivative(const double * x, unsigned int icoord ) const {
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
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
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.
virtual BaseObjFunction::Type_t Type() const
get type of fit method function
LogLikelihoodFCN & operator=(const LogLikelihoodFCN &rhs)
Assignment operator.
virtual double DoDerivative(const double *x, unsigned int icoord) const
::ROOT::Math::IParamMultiFunctionTempl< T > IModelFunction
int fWeight
flag to indicate if needs to evaluate using weight or weight squared (default weight = 0)
LogLikelihoodFCN(const LogLikelihoodFCN &f)
Copy constructor.
BasicFCN< DerivFunType, ModelFunType, UnBinData > BaseFCN
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)
virtual void Gradient(const double *x, double *g) const
::ROOT::Math::BasicFitMethodFunction< DerivFunType > BaseObjFunction
virtual BaseFunction * Clone() const
clone the function (need to return Base for Windows)
virtual ~LogLikelihoodFCN()
Destructor (no operations)
virtual unsigned int NFitPoints() const
std::vector< double > fGrad
for derivatives
BaseObjFunction::Type_t Type_t
bool fIsExtended
flag for indicating if likelihood is extended
::ROOT::EExecutionPolicy fExecutionPolicy
Execution policy.
unsigned int fNEffPoints
number of effective points used in the fit
virtual double DataElement(const double *x, unsigned int i, double *g, double *h=nullptr, bool fullHessian=false) const
i-th likelihood contribution and its gradient
virtual double DoEval(const double *x) const
Evaluation of the function (required by interface)
Class describing the un-binned data sets (just x coordinates values) of any dimensions.
Definition UnBinData.h:46
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
LogLikelihoodFCN< ROOT::Math::IMultiGenFunction, ROOT::Math::IParamMultiFunction > LogLikelihoodFunction
LogLikelihoodFCN< ROOT::Math::IMultiGradFunction, ROOT::Math::IParamMultiFunction > LogLikelihoodGradFunction
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
static double EvalLogL(const IModelFunctionTempl< double > &func, const UnBinData &data, const double *p, int iWeight, bool extended, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy, unsigned nChunks=0)
Definition FitUtil.h:1413
static void EvalLogLGradient(const IModelFunctionTempl< double > &func, const UnBinData &data, const double *p, double *g, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy=::ROOT::EExecutionPolicy::kSequential, unsigned nChunks=0)
Definition FitUtil.h:1464
static double EvalPdf(const IModelFunctionTempl< double > &func, const UnBinData &data, const double *p, unsigned int i, double *g, double *h, bool hasGrad, bool fullHessian)
Definition FitUtil.h:1451