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
20// #ifndef ROOT_Math_IParamFunctionfwd
21// #include "Math/IParamFunctionfwd.h"
22// #endif
23
24namespace ROOT {
25
26 namespace Math {
27
28//______________________________________________________________________________________
29/**
30 FitMethodFunction class
31 Interface for objective functions (like chi2 and likelihood used in the fit)
32 In addition to normal function interface provide interface for calculating each
33 data contribution to the function which is required by some algorithm (like Fumili)
34
35 @ingroup FitMethodFunc
36*/
37template<class FunctionType>
38class BasicFitMethodFunction : public FunctionType {
39
40public:
41
42
43 typedef typename FunctionType::BaseFunc BaseFunction;
44
45 /// enumeration specifying the possible fit method types
47
48
49 BasicFitMethodFunction(int dim, int npoint) :
50 fNDim(dim),
51 fNPoints(npoint),
52 fNCalls(0)
53 {}
54
55 /**
56 Virtual Destructor (no operations)
57 */
59
60 /**
61 Number of dimension (parameters) . From IGenMultiFunction interface
62 */
63 unsigned int NDim() const override { return fNDim; }
64
65 /**
66 method returning the data i-th contribution to the fit objective function
67 For example the residual for the least square functions or the pdf element for the
68 likelihood functions.
69 Estimating also the gradient of the data element if the passed pointer is not null
70 and the Hessian. The flag fullHessian is set when one needs to compute the full Hessian (not the approximated one)
71 and should be used when the full second derivatives of the model functions are available
72 */
73 virtual double DataElement(const double *x, unsigned int i, double *g = nullptr, double *h = nullptr, bool fullHessian = false) const = 0;
74
75 // flag to indicate if full Hessian computation is supported
76 virtual bool HasHessian() const { return false;}
77
78 /**
79 * Computes the full Hessian. Return false if Hessian is not supported
80 */
81 virtual bool Hessian(const double * x, double * hess) const {
82 //return full Hessian of the objective function which is Sum(F(i))
83 unsigned int np = NPoints();
84 unsigned int ndim = NDim();
85 unsigned int nh = ndim*(ndim+1)/2;
86 for (unsigned int k = 0; k < nh; ++k) {
87 hess[k] = 0;
88 }
89 std::vector<double> g(np); // gradient of the F(i)
90 std::vector<double> h(nh); // hessian of F(i)
91 for (unsigned int i = 0; i < np; i++) {
92 double f = DataElement(x,i,g.data(),h.data(),true);
93 if (f == std::numeric_limits<double>::quiet_NaN() ) return false;
94 for (unsigned int j = 0; j < nh; j++) {
95 hess[j] += h[j];
96 }
97 }
98 return true;
99 }
100
101 /**
102 * Computes the Second derivatives. Return false if this is not supported
103 */
104 virtual bool G2(const double * , double * ) const { return false; }
105
106 /**
107 return the number of data points used in evaluating the function
108 */
109 virtual unsigned int NPoints() const { return fNPoints; }
110
111 /**
112 return the type of method, override if needed
113 */
114 virtual Type_t Type() const { return kUndefined; }
115
116 /**
117 return the total number of function calls (override if needed)
118 */
119 virtual unsigned int NCalls() const { return fNCalls; }
120
121 /**
122 update number of calls
123 */
124 virtual void UpdateNCalls() const { fNCalls++; }
125
126 /**
127 reset number of function calls
128 */
129 virtual void ResetNCalls() { fNCalls = 0; }
130
131
132 /**
133 Static function to indicate if a function is supporting gradient
134 */
135 static bool IsAGradFCN() {
136 return false;
137 }
138
139private:
140
141 unsigned int fNDim; // function dimension
142 unsigned int fNPoints; // size of the data
143 mutable unsigned int fNCalls; // number of function calls
144
145
146};
147
148template<>
150 return true;
151}
152
153// define the normal and gradient function
156
157
158
159} // end namespace Math
160
161} // end namespace ROOT
162
163
164
165
166
167
168#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:43
BasicFitMethodFunction< ROOT::Math::IMultiGradFunction > FitMethodGradFunction
Definition Fitter.h:44
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.