Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
FCNBase.h
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
10#ifndef ROOT_Minuit2_FCNBase
11#define ROOT_Minuit2_FCNBase
12
13#include "Minuit2/MnConfig.h"
14
15#include <ROOT/RSpan.hxx>
16
17#include <vector>
18
19namespace ROOT::Minuit2 {
20
21/// \defgroup Minuit Minuit2 Minimization Library
22///
23/// New Object-oriented implementation of the MINUIT minimization package.
24/// More information is available at the home page of the \ref Minuit2Page "Minuit2" minimization package".
25///
26/// \ingroup Math
27
31};
32
33/// Interface (abstract class) defining the function to be minimized, which has to be implemented by the user.
34///
35/// \ingroup Minuit
36
37class FCNBase {
38
39public:
40 virtual ~FCNBase() = default;
41
42 /// The meaning of the vector of parameters is of course defined by the user,
43 /// who uses the values of those parameters to calculate their function Value.
44 /// The order and the position of these parameters is strictly the one specified
45 /// by the user when supplying the starting values for minimization. The starting
46 /// values must be specified by the user, either via an std::vector<double> or the
47 /// MnUserParameters supplied as input to the MINUIT minimizers such as
48 /// VariableMetricMinimizer or MnMigrad. Later values are determined by MINUIT
49 /// as it searches for the Minimum or performs whatever analysis is requested by
50 /// the user.
51 ///
52 /// @param v function parameters as defined by the user.
53 ///
54 /// @return the Value of the function.
55 ///
56 /// @see MnUserParameters
57 /// @see VariableMetricMinimizer
58 /// @see MnMigrad
59
60 virtual double operator()(std::vector<double> const &v) const = 0;
61
62 /// Error definition of the function. MINUIT defines Parameter errors as the
63 /// change in Parameter Value required to change the function Value by up. Normally,
64 /// for chisquared fits it is 1, and for negative log likelihood, its Value is 0.5.
65 /// If the user wants instead the 2-sigma errors for chisquared fits, it becomes 4,
66 /// as Chi2(x+n*sigma) = Chi2(x) + n*n.
67 ///
68 /// Comment a little bit better with links!!!!!!!!!!!!!!!!!
69
70 virtual double ErrorDef() const { return Up(); }
71
72 /// Error definition of the function. MINUIT defines Parameter errors as the
73 /// change in Parameter Value required to change the function Value by up. Normally,
74 /// for chisquared fits it is 1, and for negative log likelihood, its Value is 0.5.
75 /// If the user wants instead the 2-sigma errors for chisquared fits, it becomes 4,
76 /// as Chi2(x+n*sigma) = Chi2(x) + n*n.
77 ///
78 /// \todo Comment a little bit better with links!!!!!!!!!!!!!!!!! Idem for ErrorDef()
79
80 virtual double Up() const = 0;
81
82 /// add interface to set dynamically a new error definition
83 /// Re-implement this function if needed.
84 virtual void SetErrorDef(double) {};
85
86 virtual bool HasGradient() const { return false; }
87
88 /// Return the gradient vector of the function at the given parameter point.
89 ///
90 /// By default, returns an empty vector (no analytic gradient provided).
91 /// Override this method if an analytic gradient is available.
92 ///
93 /// @param v Parameter vector.
94 /// @return Gradient vector with respect to the parameters.
95 virtual std::vector<double> Gradient(std::vector<double> const &) const { return {}; }
96
97 /// \warning Not meant to be overridden! This is a requirement for an
98 /// internal optimization in RooFit that might go away with any refactoring.
99 virtual std::vector<double> GradientWithPrevResult(std::vector<double> const &parameters, double * /*previous_grad*/,
100 double * /*previous_g2*/, double * /*previous_gstep*/) const
101 {
102 return Gradient(parameters);
103 };
104
105 /// Variant of GradientWithPrevResult() that additionally receives the
106 /// already-known function value at \p parameters (e.g. from the line search
107 /// that preceded the gradient request), so implementations can avoid
108 /// re-evaluating the function at the central point.
109 ///
110 /// \warning Not meant to be overridden! This is a requirement for an
111 /// internal optimization in RooFit that might go away with any refactoring.
112 virtual std::vector<double> GradientWithPrevResult(std::vector<double> const &parameters, double *previous_grad,
113 double *previous_g2, double *previous_gstep,
114 double /*fValAtParameters*/) const
115 {
117 };
118
119 /// \warning Not meant to be overridden! This is a requirement for an
120 /// internal optimization in RooFit that might go away with any refactoring.
122
123 /// Return the diagonal elements of the Hessian (second derivatives).
124 ///
125 /// By default, returns an empty vector. Override this method if analytic second derivatives
126 /// (per-parameter curvature) are available.
127 ///
128 /// @param v Parameter vector.
129 /// @return Vector of second derivatives with respect to each parameter.
130 virtual std::vector<double> G2(std::vector<double> const &) const { return {}; }
131
132 /// Return the full Hessian matrix of the function.
133 ///
134 /// By default, returns an empty vector. Override this method if the full analytic Hessian
135 /// (matrix of second derivatives) is available.
136 ///
137 /// @param v Parameter vector.
138 /// @return Flattened Hessian matrix.
139 virtual std::vector<double> Hessian(std::vector<double> const &) const { return {}; }
140
141 virtual bool HasHessian() const { return false; }
142
143 virtual bool HasG2() const { return false; }
144
145 /// Indicate whether the mixed second order derivative with respect to
146 /// parameters i and j is identically zero, i.e. zero for *all* parameter
147 /// values. This can help to avoid expensive function calls in numerical
148 /// Hessian evaluations (see MnHesse).
149 ///
150 /// The contract for implementations:
151 ///
152 /// * The indices refer to the FCN's own full (external) parameter
153 /// space, including any parameters that are fixed in the minimizer.
154 /// Callers that work with internal indices, like MnHesse, must
155 /// translate them via MnUserTransformation::ExtOfInt() before calling
156 /// this function.
157 /// * The result must be symmetric in i and j.
158 /// * Only return `true` if the second derivative vanishes for *any*
159 /// value of the parameters: the result may be cached and is used at
160 /// arbitrary points in parameter space.
161 /// * The diagonal (i == j) is never queried by Minuit2, so its return
162 /// value has no effect.
163 virtual bool SecondDerivativeAlwaysVanishes(unsigned int /*i*/, unsigned int /*j*/) const { return false; }
164};
165
166} // namespace ROOT::Minuit2
167
168#endif // ROOT_Minuit2_FCNBase
Interface (abstract class) defining the function to be minimized, which has to be implemented by the ...
Definition FCNBase.h:37
virtual std::vector< double > GradientWithPrevResult(std::vector< double > const &parameters, double *previous_grad, double *previous_g2, double *previous_gstep, double) const
Variant of GradientWithPrevResult() that additionally receives the already-known function value at pa...
Definition FCNBase.h:112
virtual double Up() const =0
Error definition of the function.
virtual ~FCNBase()=default
virtual bool HasHessian() const
Definition FCNBase.h:141
virtual double operator()(std::vector< double > const &v) const =0
The meaning of the vector of parameters is of course defined by the user, who uses the values of thos...
virtual std::vector< double > GradientWithPrevResult(std::vector< double > const &parameters, double *, double *, double *) const
Definition FCNBase.h:99
virtual std::vector< double > G2(std::vector< double > const &) const
Return the diagonal elements of the Hessian (second derivatives).
Definition FCNBase.h:130
virtual bool SecondDerivativeAlwaysVanishes(unsigned int, unsigned int) const
Indicate whether the mixed second order derivative with respect to parameters i and j is identically ...
Definition FCNBase.h:163
virtual std::vector< double > Hessian(std::vector< double > const &) const
Return the full Hessian matrix of the function.
Definition FCNBase.h:139
virtual void SetErrorDef(double)
add interface to set dynamically a new error definition Re-implement this function if needed.
Definition FCNBase.h:84
virtual GradientParameterSpace gradParameterSpace() const
Definition FCNBase.h:121
virtual double ErrorDef() const
Error definition of the function.
Definition FCNBase.h:70
virtual std::vector< double > Gradient(std::vector< double > const &) const
Return the gradient vector of the function at the given parameter point.
Definition FCNBase.h:95
virtual bool HasG2() const
Definition FCNBase.h:143
virtual bool HasGradient() const
Definition FCNBase.h:86
GradientParameterSpace
Definition FCNBase.h:28