Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
FitMethodFunction.h
Go to the documentation of this file.
1// @(#)root/mathcore:$Id$
2// Author: L. Moneta Thu Aug 16 15:40:28 2007
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2007 LCG ROOT Math Team, CERN/PH-SFT *
7 * *
8 * *
9 **********************************************************************/
10
11// Header file for class FitMethodFunction
12
13#ifndef ROOT_Math_FitMethodFunction
14#define ROOT_Math_FitMethodFunction
15
16#include "Math/IFunction.h"
17#include <vector>
18#include <limits>
19#include <cmath>
20
21// #ifndef ROOT_Math_IParamFunctionfwd
22// #include "Math/IParamFunctionfwd.h"
23// #endif
24
25namespace ROOT {
26
27 namespace Math {
28
29//______________________________________________________________________________________
30/**
31 FitMethodFunction class
32 Interface for objective functions (like chi2 and likelihood used in the fit)
33 In addition to normal function interface provide interface for calculating each
34 data contribution to the function which is required by some algorithm (like Fumili)
35
36 @ingroup FitMethodFunc
37*/
38template<class FunctionType>
40
41public:
42
43
44 typedef typename FunctionType::BaseFunc BaseFunction;
45
46 /// enumeration specifying the possible fit method types
48
49
50 BasicFitMethodFunction(int dim, int npoint) :
51 fNDim(dim),
52 fNPoints(npoint),
53 fNCalls(0)
54 {}
55
56 /**
57 Virtual Destructor (no operations)
58 */
60
61 /**
62 Number of dimension (parameters) . From IGenMultiFunction interface
63 */
64 unsigned int NDim() const override { return fNDim; }
65
66 /**
67 method returning the data i-th contribution to the fit objective function
68 For example the residual for the least square functions or the pdf element for the
69 likelihood functions.
70 Estimating also the gradient of the data element if the passed pointer is not null
71 and the Hessian. The flag fullHessian is set when one needs to compute the full Hessian (not the approximated one)
72 and should be used when the full second derivatives of the model functions are available
73 */
74 virtual double DataElement(const double *x, unsigned int i, double *g = nullptr, double *h = nullptr, bool fullHessian = false) const = 0;
75
76 // flag to indicate if full Hessian computation is supported
77 virtual bool HasHessian() const { return false;}
78
79 /**
80 * Computes the full Hessian. Return false if Hessian is not supported
81 */
82 virtual bool Hessian(const double * x, double * hess) const {
83 //return full Hessian of the objective function which is Sum(F(i))
84 unsigned int np = NPoints();
85 unsigned int ndim = NDim();
86 unsigned int nh = ndim*(ndim+1)/2;
87 for (unsigned int k = 0; k < nh; ++k) {
88 hess[k] = 0;
89 }
90 std::vector<double> g(np); // gradient of the F(i)
91 std::vector<double> h(nh); // hessian of F(i)
92 for (unsigned int i = 0; i < np; i++) {
93 double f = DataElement(x,i,g.data(),h.data(),true);
94 if (std::isnan(f))
95 return false;
96 for (unsigned int j = 0; j < nh; j++) {
97 hess[j] += h[j];
98 }
99 }
100 return true;
101 }
102
103 /**
104 * Computes the Second derivatives. Return false if this is not supported
105 */
106 virtual bool G2(const double * , double * ) const { return false; }
107
108 /**
109 return the number of data points used in evaluating the function
110 */
111 virtual unsigned int NPoints() const { return fNPoints; }
112
113 /**
114 return the type of method, override if needed
115 */
116 virtual Type_t Type() const { return kUndefined; }
117
118 /**
119 return the total number of function calls (override if needed)
120 */
121 virtual unsigned int NCalls() const { return fNCalls; }
122
123 /**
124 update number of calls
125 */
126 virtual void UpdateNCalls() const { fNCalls++; }
127
128 /**
129 reset number of function calls
130 */
131 virtual void ResetNCalls() { fNCalls = 0; }
132
133
134 /**
135 Static function to indicate if a function is supporting gradient
136 */
137 static bool IsAGradFCN() {
138 return false;
139 }
140
141private:
142
143 unsigned int fNDim; // function dimension
144 unsigned int fNPoints; // size of the data
145 mutable unsigned int fNCalls; // number of function calls
146
147
148};
149
150template<>
154
155// define the normal and gradient function
158
159
160
161} // end namespace Math
162
163} // end namespace ROOT
164
165
166
167
168
169
170#endif /* ROOT_Math_FitMethodFunction */
#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 char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t np
FitMethodFunction class Interface for objective functions (like chi2 and likelihood used in the fit) ...
virtual Type_t Type() const
return the type of method, override if needed
Type_t
enumeration specifying the possible fit method types
BasicFitMethodFunction(int dim, int npoint)
virtual bool Hessian(const double *x, double *hess) const
Computes the full Hessian.
virtual double DataElement(const double *x, unsigned int i, double *g=nullptr, double *h=nullptr, bool fullHessian=false) const =0
method returning the data i-th contribution to the fit objective function For example the residual fo...
virtual bool G2(const double *, double *) const
Computes the Second derivatives.
virtual unsigned int NPoints() const
return the number of data points used in evaluating the function
~BasicFitMethodFunction() override
Virtual Destructor (no operations)
unsigned int NDim() const override
Number of dimension (parameters) .
static bool IsAGradFCN()
Static function to indicate if a function is supporting gradient.
virtual void ResetNCalls()
reset number of function calls
virtual unsigned int NCalls() const
return the total number of function calls (override if needed)
virtual void UpdateNCalls() const
update number of calls
Double_t x[n]
Definition legend1.C:17
Namespace for new Math classes and functions.
BasicFitMethodFunction< ROOT::Math::IMultiGenFunction > FitMethodFunction
Definition Fitter.h:44
BasicFitMethodFunction< ROOT::Math::IMultiGradFunction > FitMethodGradFunction
Definition Fitter.h:45