38 for (
unsigned int i = 0; i <
n; i++)
40 double amin = mfcn(
x);
53 state.
Trafo(), maxcalls);
68 unsigned int maxcalls)
const
89 std::unique_ptr<AnalyticalGradientCalculator> hc;
93 hc = std::make_unique<AnalyticalGradientCalculator>(fcn,trafo);
96 bool ret = hc->Hessian(st.
Parameters(), vhmat);
98 print.
Error(
"Error computing analytical Hessian. MnHesse fails and will return a null matrix");
103 for (
unsigned int i = 0; i <
n; i++)
110 print.
Debug(
"Original error matrix", vhmat);
115 print.
Debug(
"PosDef error matrix", vhmat);
117 int ifail =
Invert(vhmat);
120 print.
Warn(
"Matrix inversion fails; will return diagonal matrix");
123 for (
unsigned int j = 0; j <
n; j++) {
124 tmpsym(j,j) = 1. / g2(j);
144 print.
Debug(
"Hessian is ACCURATE. New state:",
"\n First derivative:", st.
Gradient().
Grad(),
145 "\n Covariance matrix:", vhmat,
"\n Edm:", edm);
152 unsigned int maxcalls)
const
160 double amin = mfcn(st.
Vec());
161 double aimsag = std::sqrt(prec.
Eps2()) * (std::fabs(amin) + mfcn.
Up());
167 maxcalls = 200 + 100 *
n + 5 *
n *
n;
179 print.
Info(
"Using analytical gradient but a numerical Hessian calculator - it could be not optimal");
186 print.
Warn(
"Analytical calculator ",grd,
" numerical ",tmp.Grad(),
" g2 ",g2);
192 "\n fcn :", amin,
"\n grad :", grd,
"\n step :", gst,
"\n g2 :", g2);
194 for (
unsigned int i = 0; i <
n; i++) {
197 double dmin = 8. * prec.
Eps2() * (std::fabs(xtf) + prec.
Eps2());
198 double d = std::fabs(gst(i));
202 print.
Debug(
"Derivative parameter", i,
"d =",
d,
"dmin =", dmin);
204 for (
unsigned int icyc = 0; icyc <
Ncycles(); icyc++) {
208 for (
unsigned int multpy = 0; multpy < 5; multpy++) {
214 sag = 0.5 * (fs1 + fs2 - 2. * amin);
216 print.
Debug(
"cycle", icyc,
"mul", multpy,
"\tsag =", sag,
"d =",
d);
235 print.
Warn(
"2nd derivative zero for parameter", trafo.
Name(trafo.
ExtOfInt(i)),
236 "; MnHesse fails and will return diagonal matrix");
238 for (
unsigned int j = 0; j <
n; j++) {
239 double tmp = g2(j) < prec.
Eps2() ? 1. : 1. / g2(j);
240 vhmat(j, j) = tmp < prec.
Eps2() ? 1. : tmp;
247 double g2bfor = g2(i);
248 g2(i) = 2. * sag / (
d *
d);
249 grd(i) = (fs1 - fs2) / (2. *
d);
254 d = std::sqrt(2. * aimsag / std::fabs(g2(i)));
256 d = std::min(0.5,
d);
260 print.
Debug(
"g1 =", grd(i),
"g2 =", g2(i),
"step =", gst(i),
"d =",
d,
"diffd =", std::fabs(
d - dlast) /
d,
261 "diffg2 =", std::fabs(g2(i) - g2bfor) / g2(i));
266 if (std::fabs((g2(i) - g2bfor) / g2(i)) <
TolerG2())
268 d = std::min(
d, 10. * dlast);
269 d = std::max(
d, 0.1 * dlast);
275 print.
Warn(
"Maximum number of allowed function calls exhausted; will return diagonal matrix");
277 for (
unsigned int j = 0; j <
n; j++) {
278 double tmp = g2(j) < prec.
Eps2() ? 1. : 1. / g2(j);
279 vhmat(j, j) = tmp < prec.
Eps2() ? 1. : tmp;
287 print.
Debug(
"Second derivatives", g2);
304 unsigned int endParIndexOffDiagonal = mpiprocOffDiagonal.
EndElementIndex();
306 unsigned int offsetVect = 0;
307 for (
unsigned int in = 0; in < startParIndexOffDiagonal; in++)
308 if ((in + offsetVect) % (
n - 1) == 0)
309 offsetVect += (in + offsetVect) / (
n - 1);
311 for (
unsigned int in = startParIndexOffDiagonal; in < endParIndexOffDiagonal; in++) {
313 int i = (in + offsetVect) / (
n - 1);
314 if ((in + offsetVect) % (
n - 1) == 0)
316 int j = (in + offsetVect) % (
n - 1) + 1;
318 if ((i + 1) == j || in == startParIndexOffDiagonal)
323 double fs1 = mfcn(
x);
325 double elem = (fs1 + amin - yy(i) - yy(j)) / (dirin(i) * dirin(j));
330 x(i) -= dirin(i);
x(i) -= dirin(i);
double fs3 = mfcn(
x);
331 x(j) -= dirin(j);
x(j) -= dirin(j);
double fs4 = mfcn(
x);
332 x(i) += dirin(i);
x(i) += dirin(i);
double fs2 = mfcn(
x);
334 double elem = (fs1 - fs2 - fs3 + fs4)/(4.*dirin(i)*dirin(j));
338 if (j % (
n - 1) == 0 || in == endParIndexOffDiagonal - 1)
350 print.
Debug(
"Original error matrix", vhmat);
358 print.
Debug(
"PosDef error matrix", vhmat);
360 int ifail =
Invert(vhmat);
363 print.
Warn(
"Matrix inversion fails; will return diagonal matrix");
366 for (
unsigned int j = 0; j <
n; j++) {
367 double tmp = g2(j) < prec.
Eps2() ? 1. : 1. / g2(j);
368 tmpsym(j, j) = tmp < prec.
Eps2() ? 1. : tmp;
389 print.
Debug(
"Hessian is ACCURATE. New state:",
"\n First derivative:", grd,
"\n Second derivative:", g2,
390 "\n Gradient step:", gst,
"\n Covariance matrix:", vhmat,
"\n Edm:", edm);
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void gc
Similar to the AnalyticalGradientCalculator, the ExternalInternalGradientCalculator supplies Minuit w...
Interface (abstract class) defining the function to be minimized, which has to be implemented by the ...
virtual double Up() const =0
Error definition of the function.
virtual bool HasHessian() const
virtual GradientParameterSpace gradParameterSpace() const
virtual bool HasGradient() const
bool IsAnalytical() const
const MnAlgebraicVector & Gstep() const
const MnAlgebraicVector & Grad() const
const MnAlgebraicVector & G2() const
class holding the full result of the minimization; both internal and external (MnUserParameterState) ...
void Add(const MinimumState &state, Status status=MnValid)
add latest minimization state (for example add Hesse result after Migrad)
const MnUserParameterState & UserState() const
const MinimumState & State() const
HessianGradientCalculator: class to calculate Gradient for Hessian.
Class describing a symmetric matrix of size n.
unsigned int Nrow() const
unsigned int size() const
unsigned int StartElementIndex() const
bool SyncSymMatrixOffDiagonal(ROOT::Minuit2::MnAlgebraicSymMatrix &mnmatrix)
unsigned int EndElementIndex() const
MinimumError keeps the inv.
const MnAlgebraicSymMatrix & InvHessian() const
bool IsMadePosDef() const
const MnAlgebraicVector & Vec() const
MinimumState keeps the information (position, Gradient, 2nd deriv, etc) after one minimization step (...
const MnAlgebraicVector & Vec() const
const MinimumParameters & Parameters() const
const FunctionGradient & Gradient() const
Wrapper class to FCNBase interface used internally by Minuit.
const FCNBase & Fcn() const
unsigned int NumOfCalls() const
MinimumState ComputeAnalytical(const FCNBase &, const MinimumState &, const MnUserTransformation &) const
internal function to compute the Hessian using an analytical computation or externally provided in th...
MnUserParameterState operator()(const FCNBase &, const MnUserParameterState &, unsigned int maxcalls=0) const
FCN + MnUserParameterState.
unsigned int Ncycles() const
forward interface of MnStrategy
MinimumState ComputeNumerical(const MnFcn &, const MinimumState &, const MnUserTransformation &, unsigned int maxcalls) const
internal function to compute the Hessian using numerical derivative computation
Sets the relative floating point (double) arithmetic precision.
double Eps2() const
eps2 returns 2*sqrt(eps)
Force the covariance matrix to be positive defined by adding extra terms in the diagonal.
void Debug(const Ts &... args)
void Error(const Ts &... args)
void Info(const Ts &... args)
void Warn(const Ts &... args)
unsigned int Strategy() const
unsigned int HessianCentralFDMixedDerivatives() const
unsigned int HessianForcePosDef() const
Wrapper used by Minuit of FCN interface containing a reference to the transformation object.
class which holds the external user and/or internal Minuit representation of the parameters and error...
unsigned int NFcn() const
unsigned int VariableParameters() const
const std::vector< double > & IntParameters() const
const MnUserTransformation & Trafo() const
class performing the numerical gradient calculation
double Estimate(const FunctionGradient &, const MinimumError &) const
int Invert(LASymMatrix &)
LASymMatrix MnAlgebraicSymMatrix
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...