File indexing completed on 2025-01-18 10:10:16
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef ROOT_Math_FitMethodFunction
0014 #define ROOT_Math_FitMethodFunction
0015
0016 #include "Math/IFunction.h"
0017 #include <vector>
0018 #include <limits>
0019
0020
0021
0022
0023
0024 namespace ROOT {
0025
0026 namespace Math {
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 template<class FunctionType>
0038 class BasicFitMethodFunction : public FunctionType {
0039
0040 public:
0041
0042
0043 typedef typename FunctionType::BaseFunc BaseFunction;
0044
0045
0046 enum Type_t { kUndefined = 0, kLeastSquare, kLogLikelihood, kPoissonLikelihood };
0047
0048
0049 BasicFitMethodFunction(int dim, int npoint) :
0050 fNDim(dim),
0051 fNPoints(npoint),
0052 fNCalls(0)
0053 {}
0054
0055
0056
0057
0058 ~BasicFitMethodFunction () override {}
0059
0060
0061
0062
0063 unsigned int NDim() const override { return fNDim; }
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073 virtual double DataElement(const double *x, unsigned int i, double *g = nullptr, double *h = nullptr, bool fullHessian = false) const = 0;
0074
0075
0076 virtual bool HasHessian() const { return false;}
0077
0078
0079
0080
0081 virtual bool Hessian(const double * x, double * hess) const {
0082
0083 unsigned int np = NPoints();
0084 unsigned int ndim = NDim();
0085 unsigned int nh = ndim*(ndim+1)/2;
0086 for (unsigned int k = 0; k < nh; ++k) {
0087 hess[k] = 0;
0088 }
0089 std::vector<double> g(np);
0090 std::vector<double> h(nh);
0091 for (unsigned int i = 0; i < np; i++) {
0092 double f = DataElement(x,i,g.data(),h.data(),true);
0093 if (f == std::numeric_limits<double>::quiet_NaN() ) return false;
0094 for (unsigned int j = 0; j < nh; j++) {
0095 hess[j] += h[j];
0096 }
0097 }
0098 return true;
0099 }
0100
0101
0102
0103
0104 virtual bool G2(const double * , double * ) const { return false; }
0105
0106
0107
0108
0109 virtual unsigned int NPoints() const { return fNPoints; }
0110
0111
0112
0113
0114 virtual Type_t Type() const { return kUndefined; }
0115
0116
0117
0118
0119 virtual unsigned int NCalls() const { return fNCalls; }
0120
0121
0122
0123
0124 virtual void UpdateNCalls() const { fNCalls++; }
0125
0126
0127
0128
0129 virtual void ResetNCalls() { fNCalls = 0; }
0130
0131
0132
0133
0134
0135 static bool IsAGradFCN() {
0136 return false;
0137 }
0138
0139 private:
0140
0141 unsigned int fNDim;
0142 unsigned int fNPoints;
0143 mutable unsigned int fNCalls;
0144
0145
0146 };
0147
0148 template<>
0149 inline bool BasicFitMethodFunction<ROOT::Math::IMultiGradFunction>::IsAGradFCN() {
0150 return true;
0151 }
0152
0153
0154 typedef BasicFitMethodFunction<ROOT::Math::IMultiGenFunction> FitMethodFunction;
0155 typedef BasicFitMethodFunction<ROOT::Math::IMultiGradFunction> FitMethodGradFunction;
0156
0157
0158
0159 }
0160
0161 }
0162
0163
0164
0165
0166
0167
0168 #endif