Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
FumiliGradientCalculator.cxx
Go to the documentation of this file.
1// @(#)root/minuit2:$Id$
2// Authors: M. Winkler, F. James, L. Moneta, A. Zsenei 2003-2005
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2005 LCG ROOT Math team, CERN/PH-SFT *
7 * *
8 **********************************************************************/
9
17#include "Minuit2/MnPrint.h"
19#include "Minuit2/MnStrategy.h"
20#include "Minuit2/MnUserFcn.h"
21
22namespace ROOT {
23
24namespace Minuit2 {
25
27 : AnalyticalGradientCalculator(fcn, trafo),
28 fFcn(fcn),
29 fHessian(MnAlgebraicSymMatrix(n))
30{
31}
32
33
35{
36
37 // Calculate gradient and Hessian for Fumili using the gradient and Hessian provided
38 // by the FCN Fumili function
39 // Need to apply internal to external for parameters and the external to int transformation
40 // for the return gradient and Hessian
41
42 int nvar = par.Vec().size();
43 std::vector<double> extParam = fTransformation(par.Vec());
44
45 // eval Gradient
46 FumiliFCNBase &fcn = const_cast<FumiliFCNBase &>(fFcn);
47
48 // evaluate gradient and Hessian
49 fcn.EvaluateAll(extParam);
50
51 MnAlgebraicVector v(nvar);
53
54 const std::vector<double> &fcn_gradient = fFcn.Gradient();
55 assert(fcn_gradient.size() == extParam.size());
56
57 // transform gradient and Hessian from external to internal
58 std::vector<double> deriv(nvar);
59 std::vector<unsigned int> extIndex(nvar);
60 for (int i = 0; i < nvar; ++i) {
61 extIndex[i] = fTransformation.ExtOfInt(i);
62 deriv[i] = 1;
63 if (fTransformation.Parameter(extIndex[i]).HasLimits())
64 deriv[i] = fTransformation.DInt2Ext(i, par.Vec()(i));
65
66 v(i) = fcn_gradient[extIndex[i]] * deriv[i];
67
68 for (int j = 0; j <= i; ++j) {
69 h(i, j) = deriv[i] * deriv[j] * fFcn.Hessian(extIndex[i], extIndex[j]);
70 }
71 }
72
73 MnPrint print("FumiliGradientCalculator");
74 print.Debug([&](std::ostream &os) {
75 // compare Fumili with Minuit gradient
78 FunctionGradient grd2 = gc(par);
79 os << "Fumili Gradient:" << v << "\nMinuit Gradient" << grd2.Vec();
80 os << "\nFumili Hessian: " << h << std::endl;
81 os << "Numerical g2 " << grd2.G2() << std::endl;
83 });
84
85 // store calculated Hessian
86 fHessian = h;
87 // compute also g2 from diagonal Hessian
88 MnAlgebraicVector g2(nvar);
89 G2(par,g2);
90
91 return FunctionGradient(v,g2);
92}
93
95
96{
97 // Needed for interface of base class.
98 return this->operator()(par);
99}
101{
102 unsigned int n = par.Vec().size();
103 if (fHessian.Nrow() != n || g2.size() != n) {
104 assert(false);
105 return false;
106 }
107 for (unsigned int i = 0; i < n ; i++) {
108 g2(i) = fHessian(i,i);
109 }
110 return true;
111}
112
114{
115 unsigned int n = par.Vec().size();
116 if (fHessian.Nrow() != n ) {
117 assert(false);
118 return false;
119 }
120 h = fHessian;
121 return true;
122}
123
124} // namespace Minuit2
125
126} // namespace ROOT
#define h(i)
Definition RSha256.hxx:106
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void gc
Extension of the FCNBase for the Fumili method.
virtual void EvaluateAll(const std::vector< double > &par)=0
Evaluate function Value, Gradient and Hessian using Fumili approximation, for values of parameters p ...
std::vector< double > Hessian(const std::vector< double > &) const override
Return Value of the i-th j-th element of the Hessian matrix estimated previously using the FumiliFCNB...
virtual const std::vector< double > & Gradient() const
Return cached Value of function Gradient estimated previously using the FumiliFCNBase::EvaluateAll me...
bool G2(const MinimumParameters &, MnAlgebraicVector &) const override
compute second derivatives (diagonal of Hessian)
FumiliGradientCalculator(const FumiliFCNBase &fcn, const MnUserTransformation &trafo, int n)
FunctionGradient operator()(const MinimumParameters &) const override
bool Hessian(const MinimumParameters &, MnAlgebraicSymMatrix &) const override
compute Hessian matrix
const MnAlgebraicVector & Vec() const
const MnAlgebraicVector & G2() const
Class describing a symmetric matrix of size n.
Definition LASymMatrix.h:45
unsigned int Nrow() const
unsigned int size() const
Definition LAVector.h:231
const MnAlgebraicVector & Vec() const
void Debug(const Ts &... args)
Definition MnPrint.h:147
static int GlobalLevel()
Definition MnPrint.cxx:122
static int SetGlobalLevel(int level)
Definition MnPrint.cxx:115
API class for defining four levels of strategies: low (0), medium (1), high (2), very high (>=3); act...
Definition MnStrategy.h:27
Wrapper used by Minuit of FCN interface containing a reference to the transformation object.
Definition MnUserFcn.h:25
class dealing with the transformation between user specified parameters (external) and internal param...
unsigned int ExtOfInt(unsigned int internal) const
double DInt2Ext(unsigned int, double) const
const MinuitParameter & Parameter(unsigned int) const
class performing the numerical gradient calculation
const Int_t n
Definition legend1.C:16
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.