Logo ROOT  
Reference Guide
FitUtil.cxx
Go to the documentation of this file.
1// @(#)root/mathcore:$Id$
2// Author: L. Moneta Tue Nov 28 10:52:47 2006
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7 * *
8 * *
9 **********************************************************************/
10
11// Implementation file for class FitUtil
12
13#include "Fit/FitUtil.h"
14
15#include "Fit/BinData.h"
16#include "Fit/UnBinData.h"
17
18#include "Math/IFunctionfwd.h"
19#include "Math/IParamFunction.h"
20#include "Math/Integrator.h"
25
26#include "Math/Error.h"
27#include "Math/Util.h" // for safe log(x)
28
29#include <limits>
30#include <cmath>
31#include <cassert>
32#include <algorithm>
33#include <numeric>
34//#include <memory>
35
36#include "TROOT.h"
37
38//#define DEBUG
39#ifdef DEBUG
40#define NSAMPLE 10
41#include <iostream>
42#endif
43
44// need to implement integral option
45
46namespace ROOT {
47
48 namespace Fit {
49
50 namespace FitUtil {
51
52 // derivative with respect of the parameter to be integrated
53 template<class GradFunc = IGradModelFunction>
55 ParamDerivFunc(const GradFunc & f) : fFunc(f), fIpar(0) {}
56 void SetDerivComponent(unsigned int ipar) { fIpar = ipar; }
57 double operator() (const double *x, const double *p) const {
58 return fFunc.ParameterDerivative( x, p, fIpar );
59 }
60 unsigned int NDim() const { return fFunc.NDim(); }
61 const GradFunc & fFunc;
62 unsigned int fIpar;
63 };
64
65// simple gradient calculator using the 2 points rule
66
68
69 public:
70 // construct from function and gradient dimension gdim
71 // gdim = npar for parameter gradient
72 // gdim = ndim for coordinate gradients
73 // construct (the param values will be passed later)
74 // one can choose between 2 points rule (1 extra evaluation) istrat=1
75 // or two point rule (2 extra evaluation)
76 // (found 2 points rule does not work correctly - minuit2FitBench fails)
77 SimpleGradientCalculator(int gdim, const IModelFunction & func,double eps = 2.E-8, int istrat = 1) :
78 fEps(eps),
79 fPrecision(1.E-8 ), // sqrt(epsilon)
80 fStrategy(istrat),
81 fN(gdim ),
82 fFunc(func),
83 fVec(std::vector<double>(gdim) ) // this can be probably optimized
84 {}
85
86 // internal method to calculate single partial derivative
87 // assume cached vector fVec is already set
88 double DoParameterDerivative(const double *x, const double *p, double f0, int k) const {
89 double p0 = p[k];
90 double h = std::max( fEps* std::abs(p0), 8.0*fPrecision*(std::abs(p0) + fPrecision) );
91 fVec[k] += h;
92 double deriv = 0;
93 // t.b.d : treat case of infinities
94 //if (fval > - std::numeric_limits<double>::max() && fval < std::numeric_limits<double>::max() )
95 double f1 = fFunc(x, &fVec.front() );
96 if (fStrategy > 1) {
97 fVec[k] = p0 - h;
98 double f2 = fFunc(x, &fVec.front() );
99 deriv = 0.5 * ( f2 - f1 )/h;
100 }
101 else
102 deriv = ( f1 - f0 )/h;
103
104 fVec[k] = p[k]; // restore original p value
105 return deriv;
106 }
107 // number of dimension in x (needed when calculating the integrals)
108 unsigned int NDim() const {
109 return fFunc.NDim();
110 }
111 // number of parameters (needed for grad ccalculation)
112 unsigned int NPar() const {
113 return fFunc.NPar();
114 }
115
116 double ParameterDerivative(const double *x, const double *p, int ipar) const {
117 // fVec are the cached parameter values
118 std::copy(p, p+fN, fVec.begin());
119 double f0 = fFunc(x, p);
120 return DoParameterDerivative(x,p,f0,ipar);
121 }
122
123 // calculate all gradient at point (x,p) knnowing already value f0 (we gain a function eval.)
124 void ParameterGradient(const double * x, const double * p, double f0, double * g) {
125 // fVec are the cached parameter values
126 std::copy(p, p+fN, fVec.begin());
127 for (unsigned int k = 0; k < fN; ++k) {
128 g[k] = DoParameterDerivative(x,p,f0,k);
129 }
130 }
131
132 // calculate gradient w.r coordinate values
133 void Gradient(const double * x, const double * p, double f0, double * g) {
134 // fVec are the cached coordinate values
135 std::copy(x, x+fN, fVec.begin());
136 for (unsigned int k = 0; k < fN; ++k) {
137 double x0 = x[k];
138 double h = std::max( fEps* std::abs(x0), 8.0*fPrecision*(std::abs(x0) + fPrecision) );
139 fVec[k] += h;
140 // t.b.d : treat case of infinities
141 //if (fval > - std::numeric_limits<double>::max() && fval < std::numeric_limits<double>::max() )
142 double f1 = fFunc( &fVec.front(), p );
143 if (fStrategy > 1) {
144 fVec[k] = x0 - h;
145 double f2 = fFunc( &fVec.front(), p );
146 g[k] = 0.5 * ( f2 - f1 )/h;
147 }
148 else
149 g[k] = ( f1 - f0 )/h;
150
151 fVec[k] = x[k]; // restore original x value
152 }
153 }
154
155 private:
156
157 double fEps;
159 int fStrategy; // strategy in calculation ( =1 use 2 point rule( 1 extra func) , = 2 use r point rule)
160 unsigned int fN; // gradient dimension
162 mutable std::vector<double> fVec; // cached coordinates (or parameter values in case of gradientpar)
163 };
164
165
166 // function to avoid infinities or nan
167 double CorrectValue(double rval) {
168 // avoid infinities or nan in rval
169 if (rval > - std::numeric_limits<double>::max() && rval < std::numeric_limits<double>::max() )
170 return rval;
171 else if (rval < 0)
172 // case -inf
173 return -std::numeric_limits<double>::max();
174 else
175 // case + inf or nan
176 return + std::numeric_limits<double>::max();
177 }
178
179 // Check if the value is a finite number. The argument rval is updated if it is infinite or NaN,
180 // setting it to the maximum finite value (preserving the sign).
181 bool CheckInfNaNValue(double &rval)
182 {
183 if (rval > - std::numeric_limits<double>::max() && rval < std::numeric_limits<double>::max() )
184 return true;
185 else if (rval < 0) {
186 // case -inf
187 rval = -std::numeric_limits<double>::max();
188 return false;
189 }
190 else {
191 // case + inf or nan
192 rval = + std::numeric_limits<double>::max();
193 return false;
194 }
195 }
196
197
198 // calculation of the integral of the gradient functions
199 // for a function providing derivative w.r.t parameters
200 // x1 and x2 defines the integration interval , p the parameters
201 template <class GFunc>
203 const double *x1, const double * x2, const double * p, double *g) {
204
205 // needs to calculate the integral for each partial derivative
206 ParamDerivFunc<GFunc> pfunc( gfunc);
207 IntegralEvaluator<ParamDerivFunc<GFunc> > igDerEval( pfunc, p, true);
208 // loop on the parameters
209 unsigned int npar = gfunc.NPar();
210 for (unsigned int k = 0; k < npar; ++k ) {
211 pfunc.SetDerivComponent(k);
212 g[k] = igDerEval( x1, x2 );
213 }
214 }
215
216
217
218 } // end namespace FitUtil
219
220
221
222//___________________________________________________________________________________________________________________________
223// for chi2 functions
224//___________________________________________________________________________________________________________________________
225
226 double FitUtil::EvaluateChi2(const IModelFunction &func, const BinData &data, const double *p, unsigned int &nPoints,
227 ::ROOT::EExecutionPolicy executionPolicy, unsigned nChunks)
228 {
229 // evaluate the chi2 given a function reference , the data and returns the value and also in nPoints
230 // the actual number of used points
231 // normal chi2 using only error on values (from fitting histogram)
232 // optionally the integral of function in the bin is used
233
234 unsigned int n = data.Size();
235
236 // set parameters of the function to cache integral value
237#ifdef USE_PARAMCACHE
238 (const_cast<IModelFunction &>(func)).SetParameters(p);
239#endif
240 // do not cache parameter values (it is not thread safe)
241 //func.SetParameters(p);
242
243
244 // get fit option and check case if using integral of bins
245 const DataOptions & fitOpt = data.Opt();
246 bool useBinIntegral = fitOpt.fIntegral && data.HasBinEdges();
247 bool useBinVolume = (fitOpt.fBinVolume && data.HasBinEdges());
248 bool useExpErrors = (fitOpt.fExpErrors);
249 bool isWeighted = data.IsWeighted();
250
251#ifdef DEBUG
252 std::cout << "\n\nFit data size = " << n << std::endl;
253 std::cout << "evaluate chi2 using function " << &func << " " << p << std::endl;
254 std::cout << "use empty bins " << fitOpt.fUseEmpty << std::endl;
255 std::cout << "use integral " << fitOpt.fIntegral << std::endl;
256 std::cout << "use all error=1 " << fitOpt.fErrors1 << std::endl;
257 if (isWeighted) std::cout << "Weighted data set - sumw = " << data.SumOfContent() << " sumw2 = " << data.SumOfError2() << std::endl;
258#endif
259
261 if (executionPolicy == ROOT::EExecutionPolicy::kMultiThread) {
262 // do not use GSL integrator which is not thread safe
264 }
265#ifdef USE_PARAMCACHE
266 IntegralEvaluator<> igEval( func, 0, useBinIntegral, igType);
267#else
268 IntegralEvaluator<> igEval( func, p, useBinIntegral, igType);
269#endif
270 double maxResValue = std::numeric_limits<double>::max() /n;
271 double wrefVolume = 1.0;
272 if (useBinVolume) {
273 if (fitOpt.fNormBinVolume) wrefVolume /= data.RefVolume();
274 }
275
276 (const_cast<IModelFunction &>(func)).SetParameters(p);
277
278 auto mapFunction = [&](const unsigned i){
279
280 double chi2{};
281 double fval{};
282
283 const auto x1 = data.GetCoordComponent(i, 0);
284 const auto y = data.Value(i);
285 auto invError = data.InvError(i);
286
287 //invError = (invError!= 0.0) ? 1.0/invError :1;
288
289 const double * x = nullptr;
290 std::vector<double> xc;
291 double binVolume = 1.0;
292 if (useBinVolume) {
293 unsigned int ndim = data.NDim();
294 xc.resize(data.NDim());
295 for (unsigned int j = 0; j < ndim; ++j) {
296 double xx = *data.GetCoordComponent(i, j);
297 double x2 = data.GetBinUpEdgeComponent(i, j);
298 binVolume *= std::abs(x2 - xx);
299 xc[j] = 0.5*(x2 + xx);
300 }
301 x = xc.data();
302 // normalize the bin volume using a reference value
303 binVolume *= wrefVolume;
304 } else if(data.NDim() > 1) {
305 // multi-dim case (no bin volume)
306 xc.resize(data.NDim());
307 xc[0] = *x1;
308 for (unsigned int j = 1; j < data.NDim(); ++j)
309 xc[j] = *data.GetCoordComponent(i, j);
310 x = xc.data();
311 } else {
312 x = x1;
313 }
314
315
316 if (!useBinIntegral) {
317#ifdef USE_PARAMCACHE
318 fval = func ( x );
319#else
320 fval = func ( x, p );
321#endif
322 }
323 else {
324 // calculate integral normalized by bin volume
325 // need to set function and parameters here in case loop is parallelized
326 std::vector<double> x2(data.NDim());
327 data.GetBinUpEdgeCoordinates(i, x2.data());
328 fval = igEval(x, x2.data());
329 }
330 // normalize result if requested according to bin volume
331 if (useBinVolume) fval *= binVolume;
332
333 // expected errors
334 if (useExpErrors) {
335 double invWeight = 1.0;
336 if (isWeighted) {
337 // we need first to check if a weight factor needs to be applied
338 // weight = sumw2/sumw = error**2/content
339 //invWeight = y * invError * invError;
340 // we use always the global weight and not the observed one in the bin
341 // for empty bins use global weight (if it is weighted data.SumError2() is not zero)
342 invWeight = data.SumOfContent()/ data.SumOfError2();
343 //if (invError > 0) invWeight = y * invError * invError;
344 }
345
346 // if (invError == 0) invWeight = (data.SumOfError2() > 0) ? data.SumOfContent()/ data.SumOfError2() : 1.0;
347 // compute expected error as f(x) / weight
348 double invError2 = (fval > 0) ? invWeight / fval : 0.0;
349 invError = std::sqrt(invError2);
350 //std::cout << "using Pearson chi2 " << x[0] << " " << 1./invError2 << " " << fval << std::endl;
351 }
352
353//#define DEBUG
354#ifdef DEBUG
355 std::cout << x[0] << " " << y << " " << 1./invError << " params : ";
356 for (unsigned int ipar = 0; ipar < func.NPar(); ++ipar)
357 std::cout << p[ipar] << "\t";
358 std::cout << "\tfval = " << fval << " bin volume " << binVolume << " ref " << wrefVolume << std::endl;
359#endif
360//#undef DEBUG
361
362 if (invError > 0) {
363
364 double tmp = ( y -fval )* invError;
365 double resval = tmp * tmp;
366
367
368 // avoid inifinity or nan in chi2 values due to wrong function values
369 if ( resval < maxResValue )
370 chi2 += resval;
371 else {
372 //nRejected++;
373 chi2 += maxResValue;
374 }
375 }
376 return chi2;
377 };
378
379#ifdef R__USE_IMT
380 auto redFunction = [](const std::vector<double> & objs){
381 return std::accumulate(objs.begin(), objs.end(), double{});
382 };
383#else
384 (void)nChunks;
385
386 // If IMT is disabled, force the execution policy to the serial case
387 if (executionPolicy == ROOT::EExecutionPolicy::kMultiThread) {
388 Warning("FitUtil::EvaluateChi2", "Multithread execution policy requires IMT, which is disabled. Changing "
389 "to ROOT::EExecutionPolicy::kSequential.");
390 executionPolicy = ROOT::EExecutionPolicy::kSequential;
391 }
392#endif
393
394 double res{};
395 if(executionPolicy == ROOT::EExecutionPolicy::kSequential){
396 for (unsigned int i=0; i<n; ++i) {
397 res += mapFunction(i);
398 }
399#ifdef R__USE_IMT
400 } else if(executionPolicy == ROOT::EExecutionPolicy::kMultiThread) {
402 auto chunks = nChunks !=0? nChunks: setAutomaticChunking(data.Size());
403 res = pool.MapReduce(mapFunction, ROOT::TSeq<unsigned>(0, n), redFunction, chunks);
404#endif
405// } else if(executionPolicy == ROOT::Fit::kMultitProcess){
406 // ROOT::TProcessExecutor pool;
407 // res = pool.MapReduce(mapFunction, ROOT::TSeq<unsigned>(0, n), redFunction);
408 } else{
409 Error("FitUtil::EvaluateChi2","Execution policy unknown. Avalaible choices:\n ROOT::EExecutionPolicy::kSequential (default)\n ROOT::EExecutionPolicy::kMultiThread (requires IMT)\n");
410 }
411
412 // reset the number of fitting data points
413 nPoints = n; // no points are rejected
414 //if (nRejected != 0) nPoints = n - nRejected;
415
416 return res;
417}
418
419
420//___________________________________________________________________________________________________________________________
421
422double FitUtil::EvaluateChi2Effective(const IModelFunction & func, const BinData & data, const double * p, unsigned int & nPoints) {
423 // evaluate the chi2 given a function reference , the data and returns the value and also in nPoints
424 // the actual number of used points
425 // method using the error in the coordinates
426 // integral of bin does not make sense in this case
427
428 unsigned int n = data.Size();
429
430#ifdef DEBUG
431 std::cout << "\n\nFit data size = " << n << std::endl;
432 std::cout << "evaluate effective chi2 using function " << &func << " " << p << std::endl;
433#endif
434
435 assert(data.HaveCoordErrors() || data.HaveAsymErrors());
436
437 double chi2 = 0;
438 //int nRejected = 0;
439
440
441 //func.SetParameters(p);
442
443 unsigned int ndim = func.NDim();
444
445 // use Richardson derivator
447
448 double maxResValue = std::numeric_limits<double>::max() /n;
449
450
451
452 for (unsigned int i = 0; i < n; ++ i) {
453
454
455 double y = 0;
456 const double * x = data.GetPoint(i,y);
457
458 double fval = func( x, p );
459
460 double delta_y_func = y - fval;
461
462
463 double ey = 0;
464 const double * ex = 0;
465 if (!data.HaveAsymErrors() )
466 ex = data.GetPointError(i, ey);
467 else {
468 double eylow, eyhigh = 0;
469 ex = data.GetPointError(i, eylow, eyhigh);
470 if ( delta_y_func < 0)
471 ey = eyhigh; // function is higher than points
472 else
473 ey = eylow;
474 }
475 double e2 = ey * ey;
476 // before calculating the gradient check that all error in x are not zero
477 unsigned int j = 0;
478 while ( j < ndim && ex[j] == 0.) { j++; }
479 // if j is less ndim some elements are not zero
480 if (j < ndim) {
481 // need an adapter from a multi-dim function to a one-dimensional
483 // select optimal step size (use 10--2 by default as was done in TF1:
484 double kEps = 0.01;
485 double kPrecision = 1.E-8;
486 for (unsigned int icoord = 0; icoord < ndim; ++icoord) {
487 // calculate derivative for each coordinate
488 if (ex[icoord] > 0) {
489 //gradCalc.Gradient(x, p, fval, &grad[0]);
490 f1D.SetCoord(icoord);
491 // optimal spep size (take ex[] as scale for the points and 1% of it
492 double x0= x[icoord];
493 double h = std::max( kEps* std::abs(ex[icoord]), 8.0*kPrecision*(std::abs(x0) + kPrecision) );
494 double deriv = derivator.Derivative1(f1D, x[icoord], h);
495 double edx = ex[icoord] * deriv;
496 e2 += edx * edx;
497#ifdef DEBUG
498 std::cout << "error for coord " << icoord << " = " << ex[icoord] << " deriv " << deriv << std::endl;
499#endif
500 }
501 }
502 }
503 double w2 = (e2 > 0) ? 1.0/e2 : 0;
504 double resval = w2 * ( y - fval ) * ( y - fval);
505
506#ifdef DEBUG
507 std::cout << x[0] << " " << y << " ex " << ex[0] << " ey " << ey << " params : ";
508 for (unsigned int ipar = 0; ipar < func.NPar(); ++ipar)
509 std::cout << p[ipar] << "\t";
510 std::cout << "\tfval = " << fval << "\tresval = " << resval << std::endl;
511#endif
512
513 // avoid (infinity and nan ) in the chi2 sum
514 // eventually add possibility of excluding some points (like singularity)
515 if ( resval < maxResValue )
516 chi2 += resval;
517 else
518 chi2 += maxResValue;
519 //nRejected++;
520
521 }
522
523 // reset the number of fitting data points
524 nPoints = n; // no points are rejected
525 //if (nRejected != 0) nPoints = n - nRejected;
526
527#ifdef DEBUG
528 std::cout << "chi2 = " << chi2 << " n = " << nPoints << std::endl;
529#endif
530
531 return chi2;
532
533}
534
535
536////////////////////////////////////////////////////////////////////////////////
537/// evaluate the chi2 contribution (residual term) only for data with no coord-errors
538/// This function is used in the specialized least square algorithms like FUMILI or L.M.
539/// if we have error on the coordinates the method is not yet implemented
540/// integral option is also not yet implemented
541/// one can use in that case normal chi2 method
542
543double FitUtil::EvaluateChi2Residual(const IModelFunction & func, const BinData & data, const double * p, unsigned int i, double * g, double * h, bool hasGrad, bool useFullHessian) {
544 if (data.GetErrorType() == BinData::kCoordError && data.Opt().fCoordErrors ) {
545 MATH_ERROR_MSG("FitUtil::EvaluateChi2Residual","Error on the coordinates are not used in calculating Chi2 residual");
546 return 0; // it will assert otherwise later in GetPoint
547 }
548
549
550 //func.SetParameters(p);
551
552 double y, invError = 0;
553
554 const DataOptions & fitOpt = data.Opt();
555 bool useBinIntegral = fitOpt.fIntegral && data.HasBinEdges();
556 bool useBinVolume = (fitOpt.fBinVolume && data.HasBinEdges());
557 bool useExpErrors = (fitOpt.fExpErrors);
558
559 const double * x1 = data.GetPoint(i,y, invError);
560
561 IntegralEvaluator<> igEval( func, p, useBinIntegral);
562 double fval = 0;
563 unsigned int ndim = data.NDim();
564 double binVolume = 1.0;
565 const double * x2 = 0;
566 if (useBinVolume || useBinIntegral) x2 = data.BinUpEdge(i);
567
568 double * xc = 0;
569
570 if (useBinVolume) {
571 xc = new double[ndim];
572 for (unsigned int j = 0; j < ndim; ++j) {
573 binVolume *= std::abs( x2[j]-x1[j] );
574 xc[j] = 0.5*(x2[j]+ x1[j]);
575 }
576 // normalize the bin volume using a reference value
577 binVolume /= data.RefVolume();
578 }
579
580 const double * x = (useBinVolume) ? xc : x1;
581
582 if (!useBinIntegral) {
583 fval = func ( x, p );
584 }
585 else {
586 // calculate integral (normalized by bin volume)
587 // need to set function and parameters here in case loop is parallelized
588 fval = igEval( x1, x2) ;
589 }
590 // normalize result if requested according to bin volume
591 if (useBinVolume) fval *= binVolume;
592
593 // expected errors
594 if (useExpErrors) {
595 // we need first to check if a weight factor needs to be applied
596 // weight = sumw2/sumw = error**2/content
597 //NOTE: assume histogram is not weighted
598 // don't know how to do with bins with weight = 0
599 //double invWeight = y * invError * invError;
600 // if (invError == 0) invWeight = (data.SumOfError2() > 0) ? data.SumOfContent()/ data.SumOfError2() : 1.0;
601 // compute expected error as f(x) / weight
602 double invError2 = (fval > 0) ? 1.0 / fval : 0.0;
603 invError = std::sqrt(invError2);
604 }
605
606
607 double resval = ( y -fval )* invError;
608
609 // avoid infinities or nan in resval
610 resval = CorrectValue(resval);
611
612 // estimate gradient
613 if (g) {
614
615 unsigned int npar = func.NPar();
616
617 // use gradient of model function only if FCN support gradient
618 const IGradModelFunction * gfunc = (hasGrad) ?
619 dynamic_cast<const IGradModelFunction *>( &func) : nullptr;
620
621 if (!h ) useFullHessian = false;
622 if (useFullHessian && (!gfunc || useBinIntegral || (gfunc && !gfunc->HasParameterHessian())))
623 return std::numeric_limits<double>::quiet_NaN();
624
625 if (gfunc) {
626 //case function provides gradient
627 if (!useBinIntegral ) {
628 gfunc->ParameterGradient(x , p, g);
629 if (useFullHessian) {
630 gfunc->ParameterHessian(x, p, h);
631 }
632 }
633 else {
634 // needs to calculate the integral for each partial derivative
635 CalculateGradientIntegral( *gfunc, x1, x2, p, g);
636 }
637 }
638 else {
639 SimpleGradientCalculator gc( npar, func);
640 if (!useBinIntegral ) {
641 gc.ParameterGradient(x, p, fval, g);
642 } else {
643 // needs to calculate the integral for each partial derivative
645 }
646 }
647 // multiply by - 1 * weight
648 for (unsigned int k = 0; k < npar; ++k) {
649 g[k] *= - invError;
650 if (useBinVolume) g[k] *= binVolume;
651 if (h) {
652 for (unsigned int l = 0; l <= k; l++) { // use lower diagonal because I modify g[k]
653 unsigned int idx = l + k * (k + 1) / 2;
654 if (useFullHessian) {
655 h[idx] *= 2.* resval * (-invError); // hessian of model function
656 if (useBinVolume) h[idx] *= binVolume;
657 }
658 else {
659 h[idx] = 0;
660 }
661 // add term depending on only gradient of model function
662 h[idx] += 2. * g[k]*g[l];
663 }
664 }
665 }
666 }
667
668 if (useBinVolume) delete [] xc;
669
670 return resval;
671
672}
673
674void FitUtil::EvaluateChi2Gradient(const IModelFunction &f, const BinData &data, const double *p, double *grad,
675 unsigned int &nPoints, ROOT::EExecutionPolicy executionPolicy, unsigned nChunks)
676{
677 // evaluate the gradient of the chi2 function
678 // this function is used when the model function knows how to calculate the derivative and we can
679 // avoid that the minimizer re-computes them
680 //
681 // case of chi2 effective (errors on coordinate) is not supported
682
683 if (data.HaveCoordErrors()) {
684 MATH_ERROR_MSG("FitUtil::EvaluateChi2Gradient",
685 "Error on the coordinates are not used in calculating Chi2 gradient");
686 return; // it will assert otherwise later in GetPoint
687 }
688
689 const IGradModelFunction *fg = dynamic_cast<const IGradModelFunction *>(&f);
690 assert(fg != nullptr); // must be called by a gradient function
691
692 const IGradModelFunction &func = *fg;
693
694#ifdef DEBUG
695 std::cout << "\n\nFit data size = " << n << std::endl;
696 std::cout << "evaluate chi2 using function gradient " << &func << " " << p << std::endl;
697#endif
698
699 const DataOptions &fitOpt = data.Opt();
700 bool useBinIntegral = fitOpt.fIntegral && data.HasBinEdges();
701 bool useBinVolume = (fitOpt.fBinVolume && data.HasBinEdges());
702
703 double wrefVolume = 1.0;
704 if (useBinVolume) {
705 if (fitOpt.fNormBinVolume) wrefVolume /= data.RefVolume();
706 }
707
709 if (executionPolicy == ROOT::EExecutionPolicy::kMultiThread) {
710 // do not use GSL integrator which is not thread safe
712 }
713 IntegralEvaluator<> igEval(func, p, useBinIntegral,igType);
714
715 unsigned int npar = func.NPar();
716 unsigned initialNPoints = data.Size();
717
718 std::vector<bool> isPointRejected(initialNPoints);
719
720 auto mapFunction = [&](const unsigned int i) {
721 // set all vector values to zero
722 std::vector<double> gradFunc(npar);
723 std::vector<double> pointContribution(npar);
724
725 const auto x1 = data.GetCoordComponent(i, 0);
726 const auto y = data.Value(i);
727 auto invError = data.Error(i);
728
729 invError = (invError != 0.0) ? 1.0 / invError : 1;
730
731 double fval = 0;
732
733 const double *x = nullptr;
734 std::vector<double> xc;
735
736 unsigned int ndim = data.NDim();
737 double binVolume = 1;
738 if (useBinVolume) {
739 xc.resize(ndim);
740 for (unsigned int j = 0; j < ndim; ++j) {
741 double x1_j = *data.GetCoordComponent(i, j);
742 double x2_j = data.GetBinUpEdgeComponent(i, j);
743 binVolume *= std::abs(x2_j - x1_j);
744 xc[j] = 0.5 * (x2_j + x1_j);
745 }
746
747 x = xc.data();
748
749 // normalize the bin volume using a reference value
750 binVolume *= wrefVolume;
751 } else if (ndim > 1) {
752 xc.resize(ndim);
753 xc[0] = *x1;
754 for (unsigned int j = 1; j < ndim; ++j)
755 xc[j] = *data.GetCoordComponent(i, j);
756 x = xc.data();
757 } else {
758 x = x1;
759 }
760
761 if (!useBinIntegral) {
762 fval = func(x, p);
763 func.ParameterGradient(x, p, &gradFunc[0]);
764 } else {
765 std::vector<double> x2(data.NDim());
766 data.GetBinUpEdgeCoordinates(i, x2.data());
767 // calculate normalized integral and gradient (divided by bin volume)
768 // need to set function and parameters here in case loop is parallelized
769 fval = igEval(x, x2.data());
770 CalculateGradientIntegral(func, x, x2.data(), p, &gradFunc[0]);
771 }
772 if (useBinVolume)
773 fval *= binVolume;
774
775#ifdef DEBUG
776 std::cout << x[0] << " " << y << " " << 1. / invError << " params : ";
777 for (unsigned int ipar = 0; ipar < npar; ++ipar)
778 std::cout << p[ipar] << "\t";
779 std::cout << "\tfval = " << fval << std::endl;
780#endif
781 if (!CheckInfNaNValue(fval)) {
782 isPointRejected[i] = true;
783 // Return a zero contribution to all partial derivatives on behalf of the current point
784 return pointContribution;
785 }
786
787 // loop on the parameters
788 unsigned int ipar = 0;
789 for (; ipar < npar; ++ipar) {
790
791 // correct gradient for bin volumes
792 if (useBinVolume)
793 gradFunc[ipar] *= binVolume;
794
795 // avoid singularity in the function (infinity and nan ) in the chi2 sum
796 // eventually add possibility of excluding some points (like singularity)
797 double dfval = gradFunc[ipar];
798 if (!CheckInfNaNValue(dfval)) {
799 break; // exit loop on parameters
800 }
801
802 // calculate derivative point contribution
803 pointContribution[ipar] = -2.0 * (y - fval) * invError * invError * gradFunc[ipar];
804 }
805
806 if (ipar < npar) {
807 // case loop was broken for an overflow in the gradient calculation
808 isPointRejected[i] = true;
809 }
810
811 return pointContribution;
812 };
813
814 // Vertically reduce the set of vectors by summing its equally-indexed components
815 auto redFunction = [&](const std::vector<std::vector<double>> &pointContributions) {
816 std::vector<double> result(npar);
817
818 for (auto const &pointContribution : pointContributions) {
819 for (unsigned int parameterIndex = 0; parameterIndex < npar; parameterIndex++)
820 result[parameterIndex] += pointContribution[parameterIndex];
821 }
822
823 return result;
824 };
825
826 std::vector<double> g(npar);
827
828#ifndef R__USE_IMT
829 // If IMT is disabled, force the execution policy to the serial case
830 if (executionPolicy == ROOT::EExecutionPolicy::kMultiThread) {
831 Warning("FitUtil::EvaluateChi2Gradient", "Multithread execution policy requires IMT, which is disabled. Changing "
832 "to ROOT::EExecutionPolicy::kSequential.");
833 executionPolicy = ROOT::EExecutionPolicy::kSequential;
834 }
835#endif
836
837 if (executionPolicy == ROOT::EExecutionPolicy::kSequential) {
838 std::vector<std::vector<double>> allGradients(initialNPoints);
839 for (unsigned int i = 0; i < initialNPoints; ++i) {
840 allGradients[i] = mapFunction(i);
841 }
842 g = redFunction(allGradients);
843 }
844#ifdef R__USE_IMT
845 else if (executionPolicy == ROOT::EExecutionPolicy::kMultiThread) {
847 auto chunks = nChunks != 0 ? nChunks : setAutomaticChunking(initialNPoints);
848 g = pool.MapReduce(mapFunction, ROOT::TSeq<unsigned>(0, initialNPoints), redFunction, chunks);
849 }
850#endif
851 // else if(executionPolicy == ROOT::Fit::kMultiprocess){
852 // ROOT::TProcessExecutor pool;
853 // g = pool.MapReduce(mapFunction, ROOT::TSeq<unsigned>(0, n), redFunction);
854 // }
855 else {
856 Error("FitUtil::EvaluateChi2Gradient",
857 "Execution policy unknown. Available choices:\n 0: Serial (default)\n 1: MultiThread (requires IMT)\n");
858 }
859
860#ifndef R__USE_IMT
861 //to fix compiler warning
862 (void)nChunks;
863#endif
864
865 // correct the number of points
866 nPoints = initialNPoints;
867
868 if (std::any_of(isPointRejected.begin(), isPointRejected.end(), [](bool point) { return point; })) {
869 unsigned nRejected = std::accumulate(isPointRejected.begin(), isPointRejected.end(), 0);
870 assert(nRejected <= initialNPoints);
871 nPoints = initialNPoints - nRejected;
872
873 if (nPoints < npar)
874 MATH_ERROR_MSG("FitUtil::EvaluateChi2Gradient",
875 "Error - too many points rejected for overflow in gradient calculation");
876 }
877
878 // copy result
879 std::copy(g.begin(), g.end(), grad);
880}
881
882//______________________________________________________________________________________________________
883//
884// Log Likelihood functions
885//_______________________________________________________________________________________________________
886
887// utility function used by the likelihoods
888
889// for LogLikelihood functions
890
891double FitUtil::EvaluatePdf(const IModelFunction & func, const UnBinData & data, const double * p, unsigned int i, double * g, double * /*h*/, bool hasGrad, bool) {
892 // evaluate the pdf contribution to the generic logl function in case of bin data
893 // return actually the log of the pdf and its derivatives
894
895
896 //func.SetParameters(p);
897
898 const double * x = data.Coords(i);
899 double fval = func ( x, p );
900 double logPdf = ROOT::Math::Util::EvalLog(fval);
901 //return
902 if (g == 0) return logPdf;
903
904 const IGradModelFunction * gfunc = (hasGrad) ?
905 dynamic_cast<const IGradModelFunction *>( &func) : nullptr;
906
907 // gradient calculation
908 if (gfunc) {
909 //case function provides gradient
910 gfunc->ParameterGradient( x , p, g );
911 }
912 else {
913 // estimate gradient numerically with simple 2 point rule
914 // should probably calculate gradient of log(pdf) is more stable numerically
915 SimpleGradientCalculator gc(func.NPar(), func);
916 gc.ParameterGradient(x, p, fval, g );
917 }
918 // divide gradient by function value since returning the logs
919 for (unsigned int ipar = 0; ipar < func.NPar(); ++ipar) {
920 g[ipar] /= fval; // this should be checked against infinities
921 }
922
923#ifdef DEBUG
924 std::cout << x[i] << "\t";
925 std::cout << "\tpar = [ " << func.NPar() << " ] = ";
926 for (unsigned int ipar = 0; ipar < func.NPar(); ++ipar)
927 std::cout << p[ipar] << "\t";
928 std::cout << "\tfval = " << fval;
929 std::cout << "\tgrad = [ ";
930 for (unsigned int ipar = 0; ipar < func.NPar(); ++ipar)
931 std::cout << g[ipar] << "\t";
932 std::cout << " ] " << std::endl;
933#endif
934
935
936 return logPdf;
937}
938
939double FitUtil::EvaluateLogL(const IModelFunction &func, const UnBinData &data, const double *p,
940 int iWeight, bool extended, unsigned int &nPoints,
941 ROOT::EExecutionPolicy executionPolicy, unsigned nChunks)
942{
943 // evaluate the LogLikelihood
944
945 unsigned int n = data.Size();
946
947 //unsigned int nRejected = 0;
948
949 bool normalizeFunc = false;
950
951 // set parameters of the function to cache integral value
952#ifdef USE_PARAMCACHE
953 (const_cast<IModelFunctionTempl<double> &>(func)).SetParameters(p);
954#endif
955
956 nPoints = data.Size(); // npoints
957
958#ifdef R__USE_IMT
959 // in case parameter needs to be propagated to user function use trick to set parameters by calling one time the function
960 // this will be done in sequential mode and parameters can be set in a thread safe manner
961 if (!normalizeFunc) {
962 if (data.NDim() == 1) {
963 const double * x = data.GetCoordComponent(0,0);
964 func( x, p);
965 }
966 else {
967 std::vector<double> x(data.NDim());
968 for (unsigned int j = 0; j < data.NDim(); ++j)
969 x[j] = *data.GetCoordComponent(0, j);
970 func( x.data(), p);
971 }
972 }
973#endif
974
975 double norm = 1.0;
976 if (normalizeFunc) {
977 // compute integral of the function
978 std::vector<double> xmin(data.NDim());
979 std::vector<double> xmax(data.NDim());
980 IntegralEvaluator<> igEval(func, p, true);
981 // compute integral in the ranges where is defined
982 if (data.Range().Size() > 0) {
983 norm = 0;
984 for (unsigned int ir = 0; ir < data.Range().Size(); ++ir) {
985 data.Range().GetRange(&xmin[0], &xmax[0], ir);
986 norm += igEval.Integral(xmin.data(), xmax.data());
987 }
988 } else {
989 // use (-inf +inf)
990 data.Range().GetRange(&xmin[0], &xmax[0]);
991 // check if funcition is zero at +- inf
992 if (func(xmin.data(), p) != 0 || func(xmax.data(), p) != 0) {
993 MATH_ERROR_MSG("FitUtil::EvaluateLogLikelihood",
994 "A range has not been set and the function is not zero at +/- inf");
995 return 0;
996 }
997 norm = igEval.Integral(&xmin[0], &xmax[0]);
998 }
999 }
1000
1001 // needed to compute effective global weight in case of extended likelihood
1002
1003 auto mapFunction = [&](const unsigned i) {
1004 double W = 0;
1005 double W2 = 0;
1006 double fval = 0;
1007
1008 if (data.NDim() > 1) {
1009 std::vector<double> x(data.NDim());
1010 for (unsigned int j = 0; j < data.NDim(); ++j)
1011 x[j] = *data.GetCoordComponent(i, j);
1012#ifdef USE_PARAMCACHE
1013 fval = func(x.data());
1014#else
1015 fval = func(x.data(), p);
1016#endif
1017
1018 // one -dim case
1019 } else {
1020 const auto x = data.GetCoordComponent(i, 0);
1021#ifdef USE_PARAMCACHE
1022 fval = func(x);
1023#else
1024 fval = func(x, p);
1025#endif
1026 }
1027
1028 if (normalizeFunc)
1029 fval = fval * (1 / norm);
1030
1031 // function EvalLog protects against negative or too small values of fval
1032 double logval = ROOT::Math::Util::EvalLog(fval);
1033 if (iWeight > 0) {
1034 double weight = data.Weight(i);
1035 logval *= weight;
1036 if (iWeight == 2) {
1037 logval *= weight; // use square of weights in likelihood
1038 if (!extended) {
1039 // needed sum of weights and sum of weight square if likelkihood is extended
1040 W = weight;
1041 W2 = weight * weight;
1042 }
1043 }
1044 }
1045 return LikelihoodAux<double>(logval, W, W2);
1046 };
1047
1048#ifdef R__USE_IMT
1049 // auto redFunction = [](const std::vector<LikelihoodAux<double>> & objs){
1050 // return std::accumulate(objs.begin(), objs.end(), LikelihoodAux<double>(0.0,0.0,0.0),
1051 // [](const LikelihoodAux<double> &l1, const LikelihoodAux<double> &l2){
1052 // return l1+l2;
1053 // });
1054 // };
1055 // do not use std::accumulate to be sure to maintain always the same order
1056 auto redFunction = [](const std::vector<LikelihoodAux<double>> & objs){
1057 auto l0 = LikelihoodAux<double>(0.0,0.0,0.0);
1058 for ( auto & l : objs ) {
1059 l0 = l0 + l;
1060 }
1061 return l0;
1062 };
1063#else
1064 (void)nChunks;
1065
1066 // If IMT is disabled, force the execution policy to the serial case
1067 if (executionPolicy == ROOT::EExecutionPolicy::kMultiThread) {
1068 Warning("FitUtil::EvaluateLogL", "Multithread execution policy requires IMT, which is disabled. Changing "
1069 "to ROOT::EExecutionPolicy::kSequential.");
1070 executionPolicy = ROOT::EExecutionPolicy::kSequential;
1071 }
1072#endif
1073
1074 double logl{};
1075 double sumW{};
1076 double sumW2{};
1077 if(executionPolicy == ROOT::EExecutionPolicy::kSequential){
1078 for (unsigned int i=0; i<n; ++i) {
1079 auto resArray = mapFunction(i);
1080 logl+=resArray.logvalue;
1081 sumW+=resArray.weight;
1082 sumW2+=resArray.weight2;
1083 }
1084#ifdef R__USE_IMT
1085 } else if(executionPolicy == ROOT::EExecutionPolicy::kMultiThread) {
1087 auto chunks = nChunks !=0? nChunks: setAutomaticChunking(data.Size());
1088 auto resArray = pool.MapReduce(mapFunction, ROOT::TSeq<unsigned>(0, n), redFunction, chunks);
1089 logl=resArray.logvalue;
1090 sumW=resArray.weight;
1091 sumW2=resArray.weight2;
1092#endif
1093// } else if(executionPolicy == ROOT::Fit::kMultiProcess){
1094 // ROOT::TProcessExecutor pool;
1095 // res = pool.MapReduce(mapFunction, ROOT::TSeq<unsigned>(0, n), redFunction);
1096 } else{
1097 Error("FitUtil::EvaluateLogL","Execution policy unknown. Avalaible choices:\n ROOT::EExecutionPolicy::kSequential (default)\n ROOT::EExecutionPolicy::kMultiThread (requires IMT)\n");
1098 }
1099
1100 if (extended) {
1101 // add Poisson extended term
1102 double extendedTerm = 0; // extended term in likelihood
1103 double nuTot = 0;
1104 // nuTot is integral of function in the range
1105 // if function has been normalized integral has been already computed
1106 if (!normalizeFunc) {
1107 IntegralEvaluator<> igEval( func, p, true);
1108 std::vector<double> xmin(data.NDim());
1109 std::vector<double> xmax(data.NDim());
1110
1111 // compute integral in the ranges where is defined
1112 if (data.Range().Size() > 0 ) {
1113 nuTot = 0;
1114 for (unsigned int ir = 0; ir < data.Range().Size(); ++ir) {
1115 data.Range().GetRange(&xmin[0],&xmax[0],ir);
1116 nuTot += igEval.Integral(xmin.data(),xmax.data());
1117 }
1118 } else {
1119 // use (-inf +inf)
1120 data.Range().GetRange(&xmin[0],&xmax[0]);
1121 // check if funcition is zero at +- inf
1122 if (func(xmin.data(), p) != 0 || func(xmax.data(), p) != 0) {
1123 MATH_ERROR_MSG("FitUtil::EvaluateLogLikelihood","A range has not been set and the function is not zero at +/- inf");
1124 return 0;
1125 }
1126 nuTot = igEval.Integral(&xmin[0],&xmax[0]);
1127 }
1128
1129 // force to be last parameter value
1130 //nutot = p[func.NDim()-1];
1131 if (iWeight != 2)
1132 extendedTerm = - nuTot; // no need to add in this case n log(nu) since is already computed before
1133 else {
1134 // case use weight square in likelihood : compute total effective weight = sw2/sw
1135 // ignore for the moment case when sumW is zero
1136 extendedTerm = - (sumW2 / sumW) * nuTot;
1137 }
1138
1139 }
1140 else {
1141 nuTot = norm;
1142 extendedTerm = - nuTot + double(n) * ROOT::Math::Util::EvalLog( nuTot);
1143 // in case of weights need to use here sum of weights (to be done)
1144 }
1145 logl += extendedTerm;
1146
1147 }
1148
1149#ifdef DEBUG
1150 std::cout << "Evaluated log L for parameters (";
1151 for (unsigned int ip = 0; ip < func.NPar(); ++ip)
1152 std::cout << " " << p[ip];
1153 std::cout << ") fval = " << -logl << std::endl;
1154#endif
1155
1156 return -logl;
1157}
1158
1159void FitUtil::EvaluateLogLGradient(const IModelFunction &f, const UnBinData &data, const double *p, double *grad,
1160 unsigned int &nPoints, ROOT::EExecutionPolicy executionPolicy, unsigned nChunks)
1161{
1162 // evaluate the gradient of the log likelihood function
1163
1164 const IGradModelFunction *fg = dynamic_cast<const IGradModelFunction *>(&f);
1165 assert(fg != nullptr); // must be called by a grad function
1166
1167 const IGradModelFunction &func = *fg;
1168
1169 unsigned int npar = func.NPar();
1170 unsigned initialNPoints = data.Size();
1171
1172 (const_cast<IGradModelFunction &>(func)).SetParameters(p);
1173
1174#ifdef DEBUG
1175 std::cout << "\n===> Evaluate Gradient for parameters ";
1176 for (unsigned int ip = 0; ip < npar; ++ip)
1177 std::cout << " " << p[ip];
1178 std::cout << "\n";
1179#endif
1180
1181 const double kdmax1 = std::sqrt(std::numeric_limits<double>::max());
1182 const double kdmax2 = std::numeric_limits<double>::max() / (4 * initialNPoints);
1183
1184 auto mapFunction = [&](const unsigned int i) {
1185 std::vector<double> gradFunc(npar);
1186 std::vector<double> pointContribution(npar);
1187
1188
1189 const double * x = nullptr;
1190 std::vector<double> xc;
1191 if (data.NDim() > 1) {
1192 xc.resize(data.NDim() );
1193 for (unsigned int j = 0; j < data.NDim(); ++j)
1194 xc[j] = *data.GetCoordComponent(i, j);
1195 x = xc.data();
1196 } else {
1197 x = data.GetCoordComponent(i, 0);
1198 }
1199
1200 double fval = func(x, p);
1201 func.ParameterGradient(x, p, &gradFunc[0]);
1202
1203#ifdef DEBUG
1204 {
1206 if (i < 5 || (i > data.Size()-5) ) {
1207 if (data.NDim() > 1) std::cout << i << " x " << x[0] << " y " << x[1] << " func " << fval
1208 << " gradient " << gradFunc[0] << " " << gradFunc[1] << " " << gradFunc[3] << std::endl;
1209 else std::cout << i << " x " << x[0] << " gradient " << gradFunc[0] << " " << gradFunc[1] << " " << gradFunc[3] << std::endl;
1210 }
1211 }
1212#endif
1213
1214 for (unsigned int kpar = 0; kpar < npar; ++kpar) {
1215 if (fval > 0)
1216 pointContribution[kpar] = -1. / fval * gradFunc[kpar];
1217 else if (gradFunc[kpar] != 0) {
1218 double gg = kdmax1 * gradFunc[kpar];
1219 if (gg > 0)
1220 gg = std::min(gg, kdmax2);
1221 else
1222 gg = std::max(gg, -kdmax2);
1223 pointContribution[kpar] = -gg;
1224 }
1225 // if func derivative is zero term is also zero so do not add in g[kpar]
1226 }
1227
1228 return pointContribution;
1229 };
1230
1231 // Vertically reduce the set of vectors by summing its equally-indexed components
1232 auto redFunction = [&](const std::vector<std::vector<double>> &pointContributions) {
1233 std::vector<double> result(npar);
1234
1235 for (auto const &pointContribution : pointContributions) {
1236 for (unsigned int parameterIndex = 0; parameterIndex < npar; parameterIndex++)
1237 result[parameterIndex] += pointContribution[parameterIndex];
1238 }
1239
1240 return result;
1241 };
1242
1243 std::vector<double> g(npar);
1244
1245#ifndef R__USE_IMT
1246 // If IMT is disabled, force the execution policy to the serial case
1247 if (executionPolicy == ROOT::EExecutionPolicy::kMultiThread) {
1248 Warning("FitUtil::EvaluateLogLGradient", "Multithread execution policy requires IMT, which is disabled. Changing "
1249 "to ROOT::EExecutionPolicy::kSequential.");
1250 executionPolicy = ROOT::EExecutionPolicy::kSequential;
1251 }
1252#endif
1253
1254 if (executionPolicy == ROOT::EExecutionPolicy::kSequential) {
1255 std::vector<std::vector<double>> allGradients(initialNPoints);
1256 for (unsigned int i = 0; i < initialNPoints; ++i) {
1257 allGradients[i] = mapFunction(i);
1258 }
1259 g = redFunction(allGradients);
1260 }
1261#ifdef R__USE_IMT
1262 else if (executionPolicy == ROOT::EExecutionPolicy::kMultiThread) {
1264 auto chunks = nChunks != 0 ? nChunks : setAutomaticChunking(initialNPoints);
1265 g = pool.MapReduce(mapFunction, ROOT::TSeq<unsigned>(0, initialNPoints), redFunction, chunks);
1266 }
1267#endif
1268 else {
1269 Error("FitUtil::EvaluateLogLGradient", "Execution policy unknown. Avalaible choices:\n "
1270 "ROOT::EExecutionPolicy::kSequential (default)\n "
1271 "ROOT::EExecutionPolicy::kMultiThread (requires IMT)\n");
1272 }
1273
1274#ifndef R__USE_IMT
1275 // to fix compiler warning
1276 (void)nChunks;
1277#endif
1278
1279 // copy result
1280 std::copy(g.begin(), g.end(), grad);
1281 nPoints = data.Size(); // npoints
1282
1283#ifdef DEBUG
1284 std::cout << "FitUtil.cxx : Final gradient ";
1285 for (unsigned int param = 0; param < npar; param++) {
1286 std::cout << " " << grad[param];
1287 }
1288 std::cout << "\n";
1289#endif
1290}
1291//_________________________________________________________________________________________________
1292// for binned log likelihood functions
1293////////////////////////////////////////////////////////////////////////////////
1294/// evaluate the pdf (Poisson) contribution to the logl (return actually log of pdf)
1295/// and its gradient (gradient of log(pdf))
1296
1297double FitUtil::EvaluatePoissonBinPdf(const IModelFunction & func, const BinData & data, const double * p, unsigned int i, double * g, double * h, bool hasGrad, bool useFullHessian) {
1298 double y = 0;
1299 const double * x1 = data.GetPoint(i,y);
1300
1301 const DataOptions & fitOpt = data.Opt();
1302 bool useBinIntegral = fitOpt.fIntegral && data.HasBinEdges();
1303 bool useBinVolume = (fitOpt.fBinVolume && data.HasBinEdges());
1304
1305 IntegralEvaluator<> igEval( func, p, useBinIntegral);
1306 double fval = 0;
1307 const double * x2 = 0;
1308 // calculate the bin volume
1309 double binVolume = 1;
1310 std::vector<double> xc;
1311 if (useBinVolume) {
1312 unsigned int ndim = data.NDim();
1313 xc.resize(ndim);
1314 for (unsigned int j = 0; j < ndim; ++j) {
1315 double x2j = data.GetBinUpEdgeComponent(i, j);
1316 binVolume *= std::abs( x2j-x1[j] );
1317 xc[j] = 0.5*(x2j+ x1[j]);
1318 }
1319 // normalize the bin volume using a reference value
1320 binVolume /= data.RefVolume();
1321 }
1322
1323 const double * x = (useBinVolume) ? &xc.front() : x1;
1324
1325 if (!useBinIntegral ) {
1326 fval = func ( x, p );
1327 }
1328 else {
1329 // calculate integral normalized (divided by bin volume)
1330 std::vector<double> vx2(data.NDim());
1331 data.GetBinUpEdgeCoordinates(i, vx2.data());
1332 fval = igEval( x1, vx2.data() ) ;
1333 }
1334 if (useBinVolume) fval *= binVolume;
1335
1336 // logPdf for Poisson: ignore constant term depending on N
1337 fval = std::max(fval, 0.0); // avoid negative or too small values
1338 double nlogPdf = fval;
1339 if (y > 0.0) {
1340 // include also constants due to saturate model (see Baker-Cousins paper)
1341 nlogPdf -= y * ROOT::Math::Util::EvalLog( fval / y) - y;
1342 }
1343
1344 if (g == nullptr) return nlogPdf;
1345
1346 unsigned int npar = func.NPar();
1347 const IGradModelFunction * gfunc = (hasGrad) ?
1348 dynamic_cast<const IGradModelFunction *>( &func) : nullptr;
1349
1350 // for full Hessian we need a gradient function and not bin intgegral computation
1351 if (useFullHessian && (!gfunc || useBinIntegral || (gfunc && !gfunc->HasParameterHessian())))
1352 return std::numeric_limits<double>::quiet_NaN();
1353
1354 // gradient calculation
1355 if (gfunc) {
1356 //case function provides gradient
1357 if (!useBinIntegral ) {
1358 gfunc->ParameterGradient( x , p, g );
1359 if (useFullHessian && h) {
1360 if (!gfunc->HasParameterHessian())
1361 return std::numeric_limits<double>::quiet_NaN();
1362 bool goodHessFunc = gfunc->ParameterHessian(x , p, h);
1363 if (!goodHessFunc) {
1364 return std::numeric_limits<double>::quiet_NaN();
1365 }
1366 }
1367 }
1368 else {
1369 // needs to calculate the integral for each partial derivative
1370 CalculateGradientIntegral( *gfunc, x1, x2, p, g);
1371 }
1372
1373 }
1374 else {
1375 SimpleGradientCalculator gc(func.NPar(), func);
1376 if (!useBinIntegral )
1377 gc.ParameterGradient(x, p, fval, g);
1378 else {
1379 // needs to calculate the integral for each partial derivative
1381 }
1382 }
1383 // correct g[] do be derivative of poisson term. We compute already derivative w.r.t. LL
1384 double coeffGrad = (fval > 0) ? (1. - y/fval) : ( (y > 0) ? std::sqrt( std::numeric_limits<double>::max() ) : 1. );
1385 double coeffHess = (fval > 0) ? y/(fval*fval) : ( (y > 0) ? std::sqrt( std::numeric_limits<double>::max() ) : 0. );
1386 if (useBinVolume) {
1387 coeffGrad *= binVolume;
1388 coeffHess *= binVolume*binVolume;
1389 }
1390 for (unsigned int k = 0; k < npar; ++k) {
1391 // compute also approximate Hessian (excluding term with second derivative of model function)
1392 if (h) {
1393 for (unsigned int l = k; l < npar; ++l) {
1394 unsigned int idx = k + l * (l + 1) / 2;
1395 if (useFullHessian) {
1396 h[idx] *= coeffGrad; // h contains first model function derivatives
1397 }
1398 else {
1399 h[idx] = 0;
1400 }
1401 // add term deoending on only gradient of model function
1402 h[idx] += coeffHess * g[k]*g[l]; // g are model function derivatives
1403 }
1404 }
1405 // compute gradient of NLL element
1406 // and apply bin volume correction if needed
1407 g[k] *= coeffGrad;
1408 if (useBinVolume)
1409 g[k] *= binVolume;
1410 }
1411
1412#ifdef DEBUG
1413 std::cout << "x = " << x[0] << " y " << y << " fval " << fval << " logPdf = " << nlogPdf << " gradient : ";
1414 for (unsigned int ipar = 0; ipar < npar; ++ipar)
1415 std::cout << g[ipar] << "\t";
1416 if (h) {
1417 std::cout << "\thessian : ";
1418 for (unsigned int ipar = 0; ipar < npar; ++ipar) {
1419 std::cout << " {";
1420 for (unsigned int jpar = 0; jpar <= ipar; ++jpar) {
1421 std::cout << h[ipar + jpar * (jpar + 1) / 2] << "\t";
1422 }
1423 std::cout << "}";
1424 }
1425 }
1426 std::cout << std::endl;
1427#endif
1428#undef DEBUG
1429
1430 return nlogPdf;
1431}
1432
1433double FitUtil::EvaluatePoissonLogL(const IModelFunction &func, const BinData &data, const double *p, int iWeight,
1434 bool extended, unsigned int &nPoints, ROOT::EExecutionPolicy executionPolicy,
1435 unsigned nChunks)
1436{
1437 // evaluate the Poisson Log Likelihood
1438 // for binned likelihood fits
1439 // this is Sum ( f(x_i) - y_i * log( f (x_i) ) )
1440 // add as well constant term for saturated model to make it like a Chi2/2
1441 // by default is etended. If extended is false the fit is not extended and
1442 // the global poisson term is removed (i.e is a binomial fit)
1443 // (remember that in this case one needs to have a function with a fixed normalization
1444 // like in a non extended unbinned fit)
1445 //
1446 // if use Weight use a weighted dataset
1447 // iWeight = 1 ==> logL = Sum( w f(x_i) )
1448 // case of iWeight==1 is actually identical to weight==0
1449 // iWeight = 2 ==> logL = Sum( w*w * f(x_i) )
1450 //
1451 // nPoints returns the points where bin content is not zero
1452
1453
1454 unsigned int n = data.Size();
1455
1456#ifdef USE_PARAMCACHE
1457 (const_cast<IModelFunction &>(func)).SetParameters(p);
1458#endif
1459
1460 nPoints = data.Size(); // npoints
1461
1462
1463 // get fit option and check case of using integral of bins
1464 const DataOptions &fitOpt = data.Opt();
1465 bool useBinIntegral = fitOpt.fIntegral && data.HasBinEdges();
1466 bool useBinVolume = (fitOpt.fBinVolume && data.HasBinEdges());
1467 bool useW2 = (iWeight == 2);
1468
1469 // normalize if needed by a reference volume value
1470 double wrefVolume = 1.0;
1471 if (useBinVolume) {
1472 if (fitOpt.fNormBinVolume) wrefVolume /= data.RefVolume();
1473 }
1474
1475//#define DEBUG
1476#ifdef DEBUG
1477 std::cout << "Evaluate PoissonLogL for params = [ ";
1478 for (unsigned int j = 0; j < func.NPar(); ++j) std::cout << p[j] << " , ";
1479 std::cout << "] - data size = " << n << " useBinIntegral " << useBinIntegral << " useBinVolume "
1480 << useBinVolume << " useW2 " << useW2 << " wrefVolume = " << wrefVolume << std::endl;
1481#endif
1482
1483
1485 if (executionPolicy == ROOT::EExecutionPolicy::kMultiThread) {
1486 // do not use GSL integrator which is not thread safe
1488 }
1489#ifdef USE_PARAMCACHE
1490 IntegralEvaluator<> igEval(func, 0, useBinIntegral, igType);
1491#else
1492 IntegralEvaluator<> igEval(func, p, useBinIntegral, igType);
1493#endif
1494
1495 auto mapFunction = [&](const unsigned i) {
1496 auto x1 = data.GetCoordComponent(i, 0);
1497 auto y = *data.ValuePtr(i);
1498
1499 const double *x = nullptr;
1500 std::vector<double> xc;
1501 double fval = 0;
1502 double binVolume = 1.0;
1503
1504 if (useBinVolume) {
1505 unsigned int ndim = data.NDim();
1506 xc.resize(data.NDim());
1507 for (unsigned int j = 0; j < ndim; ++j) {
1508 double xx = *data.GetCoordComponent(i, j);
1509 double x2 = data.GetBinUpEdgeComponent(i, j);
1510 binVolume *= std::abs(x2 - xx);
1511 xc[j] = 0.5 * (x2 + xx);
1512 }
1513 x = xc.data();
1514 // normalize the bin volume using a reference value
1515 binVolume *= wrefVolume;
1516 } else if (data.NDim() > 1) {
1517 xc.resize(data.NDim());
1518 xc[0] = *x1;
1519 for (unsigned int j = 1; j < data.NDim(); ++j) {
1520 xc[j] = *data.GetCoordComponent(i, j);
1521 }
1522 x = xc.data();
1523 } else {
1524 x = x1;
1525 }
1526
1527 if (!useBinIntegral) {
1528#ifdef USE_PARAMCACHE
1529 fval = func(x);
1530#else
1531 fval = func(x, p);
1532#endif
1533 } else {
1534 // calculate integral (normalized by bin volume)
1535 // need to set function and parameters here in case loop is parallelized
1536 std::vector<double> x2(data.NDim());
1537 data.GetBinUpEdgeCoordinates(i, x2.data());
1538 fval = igEval(x, x2.data());
1539 }
1540 if (useBinVolume) fval *= binVolume;
1541
1542
1543
1544#ifdef DEBUG
1545 int NSAMPLE = 100;
1546 if (i % NSAMPLE == 0) {
1547 std::cout << "evt " << i << " x = [ ";
1548 for (unsigned int j = 0; j < func.NDim(); ++j) std::cout << x[j] << " , ";
1549 std::cout << "] ";
1550 if (fitOpt.fIntegral) {
1551 std::cout << "x2 = [ ";
1552 for (unsigned int j = 0; j < func.NDim(); ++j) std::cout << data.GetBinUpEdgeComponent(i, j) << " , ";
1553 std::cout << "] ";
1554 }
1555 std::cout << " y = " << y << " fval = " << fval << std::endl;
1556 }
1557#endif
1558
1559
1560 // EvalLog protects against 0 values of fval but don't want to add in the -log sum
1561 // negative values of fval
1562 fval = std::max(fval, 0.0);
1563
1564 double nloglike = 0; // negative loglikelihood
1565 if (useW2) {
1566 // apply weight correction . Effective weight is error^2/ y
1567 // and expected events in bins is fval/weight
1568 // can apply correction only when y is not zero otherwise weight is undefined
1569 // (in case of weighted likelihood I don't care about the constant term due to
1570 // the saturated model)
1571
1572 // use for the empty bins the global weight
1573 double weight = 1.0;
1574 if (y != 0) {
1575 double error = data.Error(i);
1576 weight = (error * error) / y; // this is the bin effective weight
1577 nloglike -= weight * y * ( ROOT::Math::Util::EvalLog(fval/y) );
1578 }
1579 else {
1580 // for empty bin use the average weight computed from the total data weight
1581 weight = data.SumOfError2()/ data.SumOfContent();
1582 }
1583 if (extended) {
1584 nloglike += weight * ( fval - y);
1585 }
1586
1587 } else {
1588 // standard case no weights or iWeight=1
1589 // this is needed for Poisson likelihood (which are extened and not for multinomial)
1590 // the formula below include constant term due to likelihood of saturated model (f(x) = y)
1591 // (same formula as in Baker-Cousins paper, page 439 except a factor of 2
1592 if (extended) nloglike = fval - y;
1593
1594 if (y > 0) {
1595 nloglike += y * (ROOT::Math::Util::EvalLog(y) - ROOT::Math::Util::EvalLog(fval));
1596 }
1597 }
1598#ifdef DEBUG
1599 {
1601 std::cout << " nll = " << nloglike << std::endl;
1602 }
1603#endif
1604 return nloglike;
1605 };
1606
1607#ifdef R__USE_IMT
1608 auto redFunction = [](const std::vector<double> &objs) {
1609 return std::accumulate(objs.begin(), objs.end(), double{});
1610 };
1611#else
1612 (void)nChunks;
1613
1614 // If IMT is disabled, force the execution policy to the serial case
1615 if (executionPolicy == ROOT::EExecutionPolicy::kMultiThread) {
1616 Warning("FitUtil::EvaluatePoissonLogL", "Multithread execution policy requires IMT, which is disabled. Changing "
1617 "to ROOT::EExecutionPolicy::kSequential.");
1618 executionPolicy = ROOT::EExecutionPolicy::kSequential;
1619 }
1620#endif
1621
1622 double res{};
1623 if (executionPolicy == ROOT::EExecutionPolicy::kSequential) {
1624 for (unsigned int i = 0; i < n; ++i) {
1625 res += mapFunction(i);
1626 }
1627#ifdef R__USE_IMT
1628 } else if (executionPolicy == ROOT::EExecutionPolicy::kMultiThread) {
1630 auto chunks = nChunks != 0 ? nChunks : setAutomaticChunking(data.Size());
1631 res = pool.MapReduce(mapFunction, ROOT::TSeq<unsigned>(0, n), redFunction, chunks);
1632#endif
1633 // } else if(executionPolicy == ROOT::Fit::kMultitProcess){
1634 // ROOT::TProcessExecutor pool;
1635 // res = pool.MapReduce(mapFunction, ROOT::TSeq<unsigned>(0, n), redFunction);
1636 } else {
1637 Error("FitUtil::EvaluatePoissonLogL",
1638 "Execution policy unknown. Avalaible choices:\n ROOT::EExecutionPolicy::kSequential (default)\n ROOT::EExecutionPolicy::kMultiThread (requires IMT)\n");
1639 }
1640
1641#ifdef DEBUG
1642 std::cout << "Loglikelihood = " << res << std::endl;
1643#endif
1644
1645 return res;
1646}
1647
1648void FitUtil::EvaluatePoissonLogLGradient(const IModelFunction &f, const BinData &data, const double *p, double *grad,
1649 unsigned int &, ROOT::EExecutionPolicy executionPolicy, unsigned nChunks)
1650{
1651 // evaluate the gradient of the Poisson log likelihood function
1652
1653 const IGradModelFunction *fg = dynamic_cast<const IGradModelFunction *>(&f);
1654 assert(fg != nullptr); // must be called by a grad function
1655
1656 const IGradModelFunction &func = *fg;
1657
1658#ifdef USE_PARAMCACHE
1659 (const_cast<IGradModelFunction &>(func)).SetParameters(p);
1660#endif
1661
1662 const DataOptions &fitOpt = data.Opt();
1663 bool useBinIntegral = fitOpt.fIntegral && data.HasBinEdges();
1664 bool useBinVolume = (fitOpt.fBinVolume && data.HasBinEdges());
1665
1666 double wrefVolume = 1.0;
1667 if (useBinVolume && fitOpt.fNormBinVolume)
1668 wrefVolume /= data.RefVolume();
1669
1671 if (executionPolicy == ROOT::EExecutionPolicy::kMultiThread) {
1672 // do not use GSL integrator which is not thread safe
1674 }
1675
1676 IntegralEvaluator<> igEval(func, p, useBinIntegral, igType);
1677
1678 unsigned int npar = func.NPar();
1679 unsigned initialNPoints = data.Size();
1680
1681 auto mapFunction = [&](const unsigned int i) {
1682 // set all vector values to zero
1683 std::vector<double> gradFunc(npar);
1684 std::vector<double> pointContribution(npar);
1685
1686 const auto x1 = data.GetCoordComponent(i, 0);
1687 const auto y = data.Value(i);
1688 auto invError = data.Error(i);
1689
1690 invError = (invError != 0.0) ? 1.0 / invError : 1;
1691
1692 double fval = 0;
1693
1694 const double *x = nullptr;
1695 std::vector<double> xc;
1696
1697 unsigned ndim = data.NDim();
1698 double binVolume = 1.0;
1699 if (useBinVolume) {
1700
1701 xc.resize(ndim);
1702
1703 for (unsigned int j = 0; j < ndim; ++j) {
1704 double x1_j = *data.GetCoordComponent(i, j);
1705 double x2_j = data.GetBinUpEdgeComponent(i, j);
1706 binVolume *= std::abs(x2_j - x1_j);
1707 xc[j] = 0.5 * (x2_j + x1_j);
1708 }
1709
1710 x = xc.data();
1711
1712 // normalize the bin volume using a reference value
1713 binVolume *= wrefVolume;
1714 } else if (ndim > 1) {
1715 xc.resize(ndim);
1716 xc[0] = *x1;
1717 for (unsigned int j = 1; j < ndim; ++j)
1718 xc[j] = *data.GetCoordComponent(i, j);
1719 x = xc.data();
1720 } else {
1721 x = x1;
1722 }
1723
1724 if (!useBinIntegral) {
1725 fval = func(x, p);
1726 func.ParameterGradient(x, p, &gradFunc[0]);
1727 } else {
1728 // calculate integral (normalized by bin volume)
1729 // need to set function and parameters here in case loop is parallelized
1730 std::vector<double> x2(data.NDim());
1731 data.GetBinUpEdgeCoordinates(i, x2.data());
1732 fval = igEval(x, x2.data());
1733 CalculateGradientIntegral(func, x, x2.data(), p, &gradFunc[0]);
1734 }
1735 if (useBinVolume)
1736 fval *= binVolume;
1737
1738#ifdef DEBUG
1739 {
1741 if (i < 5 || (i > data.Size()-5) ) {
1742 if (data.NDim() > 1) std::cout << i << " x " << x[0] << " y " << x[1] << " func " << fval
1743 << " gradient " << gradFunc[0] << " " << gradFunc[1] << " " << gradFunc[3] << std::endl;
1744 else std::cout << i << " x " << x[0] << " gradient " << gradFunc[0] << " " << gradFunc[1] << " " << gradFunc[3] << std::endl;
1745 }
1746 }
1747#endif
1748
1749 // correct the gradient
1750 for (unsigned int ipar = 0; ipar < npar; ++ipar) {
1751
1752 // correct gradient for bin volumes
1753 if (useBinVolume)
1754 gradFunc[ipar] *= binVolume;
1755
1756 // df/dp * (1. - y/f )
1757 if (fval > 0)
1758 pointContribution[ipar] = gradFunc[ipar] * (1. - y / fval);
1759 else if (gradFunc[ipar] != 0) {
1760 const double kdmax1 = std::sqrt(std::numeric_limits<double>::max());
1761 const double kdmax2 = std::numeric_limits<double>::max() / (4 * initialNPoints);
1762 double gg = kdmax1 * gradFunc[ipar];
1763 if (gg > 0)
1764 gg = std::min(gg, kdmax2);
1765 else
1766 gg = std::max(gg, -kdmax2);
1767 pointContribution[ipar] = -gg;
1768 }
1769 }
1770
1771
1772 return pointContribution;
1773 };
1774
1775 // Vertically reduce the set of vectors by summing its equally-indexed components
1776 auto redFunction = [&](const std::vector<std::vector<double>> &pointContributions) {
1777 std::vector<double> result(npar);
1778
1779 for (auto const &pointContribution : pointContributions) {
1780 for (unsigned int parameterIndex = 0; parameterIndex < npar; parameterIndex++)
1781 result[parameterIndex] += pointContribution[parameterIndex];
1782 }
1783
1784 return result;
1785 };
1786
1787 std::vector<double> g(npar);
1788
1789#ifndef R__USE_IMT
1790 // If IMT is disabled, force the execution policy to the serial case
1791 if (executionPolicy == ROOT::EExecutionPolicy::kMultiThread) {
1792 Warning("FitUtil::EvaluatePoissonLogLGradient",
1793 "Multithread execution policy requires IMT, which is disabled. Changing "
1794 "to ROOT::EExecutionPolicy::kSequential.");
1795 executionPolicy = ROOT::EExecutionPolicy::kSequential;
1796 }
1797#endif
1798
1799 if (executionPolicy == ROOT::EExecutionPolicy::kSequential) {
1800 std::vector<std::vector<double>> allGradients(initialNPoints);
1801 for (unsigned int i = 0; i < initialNPoints; ++i) {
1802 allGradients[i] = mapFunction(i);
1803 }
1804 g = redFunction(allGradients);
1805 }
1806#ifdef R__USE_IMT
1807 else if (executionPolicy == ROOT::EExecutionPolicy::kMultiThread) {
1809 auto chunks = nChunks != 0 ? nChunks : setAutomaticChunking(initialNPoints);
1810 g = pool.MapReduce(mapFunction, ROOT::TSeq<unsigned>(0, initialNPoints), redFunction, chunks);
1811 }
1812#endif
1813
1814 // else if(executionPolicy == ROOT::Fit::kMultiprocess){
1815 // ROOT::TProcessExecutor pool;
1816 // g = pool.MapReduce(mapFunction, ROOT::TSeq<unsigned>(0, n), redFunction);
1817 // }
1818 else {
1819 Error("FitUtil::EvaluatePoissonLogLGradient",
1820 "Execution policy unknown. Avalaible choices:\n 0: Serial (default)\n 1: MultiThread (requires IMT)\n");
1821 }
1822
1823#ifndef R__USE_IMT
1824 //to fix compiler warning
1825 (void)nChunks;
1826#endif
1827
1828 // copy result
1829 std::copy(g.begin(), g.end(), grad);
1830
1831#ifdef DEBUG
1832 std::cout << "***** Final gradient : ";
1833 for (unsigned int ii = 0; ii< npar; ++ii) std::cout << grad[ii] << " ";
1834 std::cout << "\n";
1835#endif
1836
1837}
1838
1839
1840unsigned FitUtil::setAutomaticChunking(unsigned nEvents){
1841 auto ncpu = ROOT::GetThreadPoolSize();
1842 if (nEvents/ncpu < 1000) return ncpu;
1843 return nEvents/1000;
1844 //return ((nEvents/ncpu + 1) % 1000) *40 ; //arbitrary formula
1845}
1846
1847}
1848
1849} // end namespace ROOT
#define MATH_ERROR_MSG(loc, str)
Definition: Error.h:83
#define f(i)
Definition: RSha256.hxx:104
#define h(i)
Definition: RSha256.hxx:106
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:188
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition: TError.cxx:232
winID h TVirtualViewer3D TVirtualGLPainter p
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 char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
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 g
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void gc
float xmin
Definition: THbookFile.cxx:95
float xmax
Definition: THbookFile.cxx:95
R__EXTERN TVirtualMutex * gROOTMutex
Definition: TROOT.h:63
#define R__LOCKGUARD(mutex)
Definition: TF1.cxx:150
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition: BinData.h:52
SimpleGradientCalculator(int gdim, const IModelFunction &func, double eps=2.E-8, int istrat=1)
Definition: FitUtil.cxx:77
double ParameterDerivative(const double *x, const double *p, int ipar) const
Definition: FitUtil.cxx:116
void Gradient(const double *x, const double *p, double f0, double *g)
Definition: FitUtil.cxx:133
void ParameterGradient(const double *x, const double *p, double f0, double *g)
Definition: FitUtil.cxx:124
double DoParameterDerivative(const double *x, const double *p, double f0, int k) const
Definition: FitUtil.cxx:88
Class describing the un-binned data sets (just x coordinates values) of any dimensions.
Definition: UnBinData.h:46
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
virtual void SetParameters(const double *p)=0
Set the parameter values.
virtual unsigned int NPar() const =0
Return the number of Parameters.
Interface (abstract class) for parametric gradient multi-dimensional functions providing in addition ...
virtual void ParameterGradient(const T *x, const double *p, T *grad) const
Evaluate the all the derivatives (gradient vector) of the function with respect to the parameters at ...
virtual bool ParameterHessian(const T *, const double *, T *) const
Evaluate the all the Hessian (second derivatives matrix) of the function with respect to the paramete...
OneDimMultiFunctionAdapter class to wrap a multidimensional function in one dimensional one.
User class for calculating the derivatives of a function.
double Derivative1(double x)
Returns the first derivative of the function at point x, computed by Richardson's extrapolation metho...
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:67
This class provides a simple interface to execute the same task multiple times in parallel threads,...
auto MapReduce(F func, unsigned nTimes, R redfunc) -> InvokeResult_t< F >
Execute a function nTimes in parallel (Map) and accumulate the results into a single value (Reduce).
Type
enumeration specifying the integration types.
@ kGAUSS
simple Gauss integration method with fixed rule
@ kDEFAULT
default type specified in the static options
RVec< PromoteType< T > > abs(const RVec< T > &v)
Definition: RVec.hxx:1780
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
Double_t ey[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
Double_t ex[n]
Definition: legend1.C:17
TF1 * f1
Definition: legend1.C:11
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
ROOT::Math::IParamMultiGradFunction IGradModelFunction
Definition: FitUtil.h:65
ROOT::Math::IParamMultiFunction IModelFunction
Definition: FitUtil.h:64
void CalculateGradientIntegral(const GFunc &gfunc, const double *x1, const double *x2, const double *p, double *g)
Definition: FitUtil.cxx:202
double EvaluatePoissonBinPdf(const IModelFunction &func, const BinData &data, const double *x, unsigned int ipoint, double *g=nullptr, double *h=nullptr, bool hasGrad=false, bool fullHessian=false)
evaluate the pdf contribution to the Poisson LogL given a model function and the BinPoint data.
Definition: FitUtil.cxx:1297
double CorrectValue(double rval)
Definition: FitUtil.cxx:167
void EvaluatePoissonLogLGradient(const IModelFunction &func, const BinData &data, const double *p, double *grad, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy=::ROOT::EExecutionPolicy::kSequential, unsigned nChunks=0)
evaluate the Poisson LogL given a model function and the data at the point p.
double EvaluateChi2Residual(const IModelFunction &func, const BinData &data, const double *p, unsigned int ipoint, double *g=nullptr, double *h=nullptr, bool hasGrad=false, bool fullHessian=false)
evaluate the residual contribution to the Chi2 given a model function and the BinPoint data and if th...
Definition: FitUtil.cxx:543
double EvaluatePoissonLogL(const IModelFunction &func, const BinData &data, const double *p, int iWeight, bool extended, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy, unsigned nChunks=0)
evaluate the Poisson LogL given a model function and the data at the point p.
double EvaluateChi2Effective(const IModelFunction &func, const BinData &data, const double *x, unsigned int &nPoints)
evaluate the effective Chi2 given a model function and the data at the point x.
Definition: FitUtil.cxx:422
void EvaluateLogLGradient(const IModelFunction &func, const UnBinData &data, const double *p, double *grad, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy=::ROOT::EExecutionPolicy::kSequential, unsigned nChunks=0)
evaluate the LogL gradient given a model function and the data at the point p.
double EvaluatePdf(const IModelFunction &func, const UnBinData &data, const double *p, unsigned int ipoint, double *g=nullptr, double *h=nullptr, bool hasGrad=false, bool fullHessian=false)
evaluate the pdf contribution to the LogL given a model function and the BinPoint data.
Definition: FitUtil.cxx:891
bool CheckInfNaNValue(double &rval)
Definition: FitUtil.cxx:181
unsigned setAutomaticChunking(unsigned nEvents)
Definition: FitUtil.cxx:1840
double EvaluateLogL(const IModelFunction &func, const UnBinData &data, const double *p, int iWeight, bool extended, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy, unsigned nChunks=0)
evaluate the LogL given a model function and the data at the point x.
double EvaluateChi2(const IModelFunction &func, const BinData &data, const double *p, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy, unsigned nChunks=0)
Chi2 Functions.
Definition: FitUtil.cxx:226
void EvaluateChi2Gradient(const IModelFunction &func, const BinData &data, const double *p, double *grad, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy=::ROOT::EExecutionPolicy::kSequential, unsigned nChunks=0)
evaluate the Chi2 gradient given a model function and the data at the point p.
void(off) SmallVectorTemplateBase< T
T EvalLog(T x)
safe evaluation of log(x) with a protections against negative or zero argument to the log smooth line...
Definition: Util.h:64
VecExpr< UnaryOp< Sqrt< T >, VecExpr< A, T, D >, T >, T, D > sqrt(const VecExpr< A, T, D > &rhs)
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
UInt_t GetThreadPoolSize()
Returns the size of ROOT's thread pool.
Definition: TROOT.cxx:565
const char * Size
Definition: TXMLSetup.cxx:56
DataOptions : simple structure holding the options on how the data are filled.
Definition: DataOptions.h:28
bool fErrors1
use all errors equal to 1, i.e. fit without errors (default is false)
Definition: DataOptions.h:52
bool fNormBinVolume
normalize data by a normalized the bin volume (bin volume divided by a reference value)
Definition: DataOptions.h:49
bool fUseEmpty
use empty bins (default is false) with a fixed error of 1
Definition: DataOptions.h:50
bool fIntegral
use integral of bin content instead of bin center (default is false)
Definition: DataOptions.h:47
bool fExpErrors
use expected errors from the function and not from the data
Definition: DataOptions.h:53
bool fBinVolume
normalize data by the bin volume (it is used in the Poisson likelihood fits)
Definition: DataOptions.h:48
double operator()(const double *x, const double *p) const
Definition: FitUtil.cxx:57
void SetDerivComponent(unsigned int ipar)
Definition: FitUtil.cxx:56
unsigned int NDim() const
Definition: FitUtil.cxx:60
ParamDerivFunc(const GradFunc &f)
Definition: FitUtil.cxx:55
TLine l
Definition: textangle.C:4