Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
HessianGradientCalculator.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
12#include "Minuit2/MnFcn.h"
17#include "Minuit2/MnStrategy.h"
18#include "Minuit2/MnPrint.h"
19#include "Minuit2/MPIProcess.h"
20
21#include <cmath>
22#include <cassert>
23
24namespace ROOT {
25
26namespace Minuit2 {
27
29{
30 // use initial gradient as starting point
32 FunctionGradient gra = gc(par);
33
34 return (*this)(par, gra);
35}
36
38operator()(const MinimumParameters &par, const FunctionGradient &Gradient) const
39{
40 // interface of the base class. Use DeltaGradient for op.
41 std::pair<FunctionGradient, MnAlgebraicVector> mypair = DeltaGradient(par, Gradient);
42
43 return mypair.first;
44}
45
47{
48 // return the precision
50}
51
53{
54 // return number of calculation cycles (defined in strategy)
56}
57
58std::pair<FunctionGradient, MnAlgebraicVector>
60{
61 // calculate gradient for Hessian
62 assert(par.IsValid());
63
64 MnPrint print("HessianGradientCalculator");
65
66 MnAlgebraicVector x = par.Vec();
67 MnAlgebraicVector grd = Gradient.Grad();
68 const MnAlgebraicVector &g2 = Gradient.G2();
69 // const MnAlgebraicVector& gstep = Gradient.Gstep();
70 // update also gradient step sizes
71 MnAlgebraicVector gstep = Gradient.Gstep();
72
73 double fcnmin = par.Fval();
74 // std::cout<<"fval: "<<fcnmin<<std::endl;
75
76 double dfmin = 4. * Precision().Eps2() * (fabs(fcnmin) + Fcn().Up());
77
78 unsigned int n = x.size();
79 MnAlgebraicVector dgrd(n);
80
81 MPIProcess mpiproc(n, 0);
82 // initial starting values
83 unsigned int startElementIndex = mpiproc.StartElementIndex();
84 unsigned int endElementIndex = mpiproc.EndElementIndex();
85
86 for (unsigned int i = startElementIndex; i < endElementIndex; i++) {
87 double xtf = x(i);
88 double dmin = 4. * Precision().Eps2() * (xtf + Precision().Eps2());
89 double epspri = Precision().Eps2() + fabs(grd(i) * Precision().Eps2());
90 double optstp = sqrt(dfmin / (fabs(g2(i)) + epspri));
91 double d = 0.2 * fabs(gstep(i));
92 if (d > optstp)
93 d = optstp;
94 if (d < dmin)
95 d = dmin;
96 double chgold = 10000.;
97 double dgmin = 0.;
98 double grdold = 0.;
99 double grdnew = 0.;
100 for (unsigned int j = 0; j < Ncycle(); j++) {
101 x(i) = xtf + d;
102 double fs1 = Fcn()(x);
103 x(i) = xtf - d;
104 double fs2 = Fcn()(x);
105 x(i) = xtf;
106 // double sag = 0.5*(fs1+fs2-2.*fcnmin);
107 // LM: should I calculate also here second derivatives ???
108
109 grdold = grd(i);
110 grdnew = (fs1 - fs2) / (2. * d);
111 dgmin = Precision().Eps() * (std::fabs(fs1) + std::fabs(fs2)) / d;
112 // if(fabs(grdnew) < Precision().Eps()) break;
113 if (grdnew == 0)
114 break;
115 double change = fabs((grdold - grdnew) / grdnew);
116 if (change > chgold && j > 1)
117 break;
118 chgold = change;
119 grd(i) = grdnew;
120 // LM : update also the step sizes
121 gstep(i) = d;
122
123 if (change < 0.05)
124 break;
125 if (fabs(grdold - grdnew) < dgmin)
126 break;
127 if (d < dmin)
128 break;
129 d *= 0.2;
130 }
131
132 dgrd(i) = std::max(dgmin, std::fabs(grdold - grdnew));
133
134 print.Debug("HGC Param :", i, "\t new g1 =", grd(i), "gstep =", d, "dgrd =", dgrd(i));
135 }
136
137 mpiproc.SyncVector(grd);
138 mpiproc.SyncVector(gstep);
139 mpiproc.SyncVector(dgrd);
140
141 return std::pair<FunctionGradient, MnAlgebraicVector>(FunctionGradient(grd, g2, gstep), dgrd);
142}
143
144} // namespace Minuit2
145
146} // namespace ROOT
#define d(i)
Definition RSha256.hxx:102
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void gc
FunctionGradient operator()(const MinimumParameters &) const override
std::pair< FunctionGradient, MnAlgebraicVector > DeltaGradient(const MinimumParameters &, const FunctionGradient &) const
Class to calculate an initial estimate of the gradient.
bool SyncVector(ROOT::Minuit2::MnAlgebraicVector &mnvector)
unsigned int StartElementIndex() const
Definition MPIProcess.h:55
unsigned int EndElementIndex() const
Definition MPIProcess.h:61
const MnAlgebraicVector & Vec() const
double Up() const
Definition MnFcn.cxx:39
Sets the relative floating point (double) arithmetic precision.
double Eps() const
eps returns the smallest possible number so that 1.+eps > 1.
double Eps2() const
eps2 returns 2*sqrt(eps)
void Debug(const Ts &... args)
Definition MnPrint.h:147
unsigned int HessianGradientNCycles() const
Definition MnStrategy.h:47
const MnMachinePrecision & Precision() const
forwarded interface
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...