Logo ROOT  
Reference Guide
GSLNLSMinimizer.h
Go to the documentation of this file.
1// @(#)root/mathmore:$Id$
2// Author: L. Moneta Wed Dec 20 17:16:32 2006
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU General Public License *
10 * as published by the Free Software Foundation; either version 2 *
11 * of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this library (see file COPYING); if not, write *
20 * to the Free Software Foundation, Inc., 59 Temple Place, Suite *
21 * 330, Boston, MA 02111-1307 USA, or contact the author. *
22 * *
23 **********************************************************************/
24
25// Header file for class GSLNLSMinimizer
26
27#ifndef ROOT_Math_GSLNLSMinimizer
28#define ROOT_Math_GSLNLSMinimizer
29
30
31
32#include "Math/BasicMinimizer.h"
33
34#include "Math/IFunctionfwd.h"
35
37
39
41
42#include <vector>
43#include <string>
44
45namespace ROOT {
46
47 namespace Math {
48
49 class GSLMultiFit;
50
51
52//________________________________________________________________________________
53/**
54 LSResidualFunc class description.
55 Internal class used for accessing the residuals of the Least Square function
56 and their derivates which are estimated numerically using GSL numerical derivation.
57 The class contains a pointer to the fit method function and an index specifying
58 the i-th residual and wraps it in a multi-dim gradient function interface
59 ROOT::Math::IGradientFunctionMultiDim.
60 The class is used by ROOT::Math::GSLNLSMinimizer (GSL non linear least square fitter)
61
62 @ingroup MultiMin
63*/
65public:
66
67 //default ctor (required by CINT)
69 {}
70
71
72 LSResidualFunc(const ROOT::Math::FitMethodFunction & func, unsigned int i) :
73 fIndex(i),
74 fChi2(&func),
75 fX2(std::vector<double>(func.NDim() ) )
76 {}
77
78
79 // copy ctor
83 {
84 operator=(rhs);
85 }
86
87 // assignment
89 {
90 fIndex = rhs.fIndex;
91 fChi2 = rhs.fChi2;
92 fX2 = rhs.fX2;
93 return *this;
94 }
95
97 return new LSResidualFunc(*fChi2,fIndex);
98 }
99
100 unsigned int NDim() const { return fChi2->NDim(); }
101
102 void Gradient( const double * x, double * g) const {
103 double f0 = 0;
104 FdF(x,f0,g);
105 }
106
107 void FdF (const double * x, double & f, double * g) const {
108 unsigned int n = NDim();
109 std::copy(x,x+n,fX2.begin());
110 const double kEps = 1.0E-4;
111 f = DoEval(x);
112 for (unsigned int i = 0; i < n; ++i) {
113 fX2[i] += kEps;
114 g[i] = ( DoEval(&fX2.front()) - f )/kEps;
115 fX2[i] = x[i];
116 }
117 }
118
119
120private:
121
122 double DoEval (const double * x) const {
123 return fChi2->DataElement(x, fIndex);
124 }
125
126 double DoDerivative(const double * x, unsigned int icoord) const {
127 //return ROOT::Math::Derivator::Eval(*this, x, icoord, 1E-8);
128 std::copy(x,x+NDim(),fX2.begin());
129 const double kEps = 1.0E-4;
130 fX2[icoord] += kEps;
131 return ( DoEval(&fX2.front()) - DoEval(x) )/kEps;
132 }
133
134 unsigned int fIndex;
136 mutable std::vector<double> fX2; // cached vector
137};
138
139
140//_____________________________________________________________________________________________________
141/**
142 GSLNLSMinimizer class for Non Linear Least Square fitting
143 It Uses the Levemberg-Marquardt algorithm from
144 <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Nonlinear-Least_002dSquares-Fitting.html">
145 GSL Non Linear Least Square fitting</A>.
146
147 @ingroup MultiMin
148*/
150
151public:
152
153 /**
154 Default constructor
155 */
156 GSLNLSMinimizer (int type = 0);
157
158 /**
159 Destructor (no operations)
160 */
162
163private:
164 // usually copying is non trivial, so we make this unaccessible
165
166 /**
167 Copy constructor
168 */
170
171 /**
172 Assignment operator
173 */
175 if (this == &rhs) return *this; // time saving self-test
176 return *this;
177 }
178
179public:
180
181 /// set the function to minimize
182 virtual void SetFunction(const ROOT::Math::IMultiGenFunction & func);
183
184 /// set gradient the function to minimize
185 virtual void SetFunction(const ROOT::Math::IMultiGradFunction & func);
186
187
188 /// method to perform the minimization
189 virtual bool Minimize();
190
191
192 /// return expected distance reached from the minimum
193 virtual double Edm() const { return fEdm; } // not impl. }
194
195
196 /// return pointer to gradient values at the minimum
197 virtual const double * MinGradient() const;
198
199 /// number of function calls to reach the minimum
200 virtual unsigned int NCalls() const { return (fChi2Func) ? fChi2Func->NCalls() : 0; }
201
202 /// number of free variables (real dimension of the problem)
203 /// this is <= Function().NDim() which is the total
204// virtual unsigned int NFree() const { return fNFree; }
205
206 /// minimizer provides error and error matrix
207 virtual bool ProvidesError() const { return true; }
208
209 /// return errors at the minimum
210 virtual const double * Errors() const { return (fErrors.size() > 0) ? &fErrors.front() : 0; }
211// {
212// static std::vector<double> err;
213// err.resize(fDim);
214// return &err.front();
215// }
216
217 /** return covariance matrices elements
218 if the variable is fixed the matrix is zero
219 The ordering of the variables is the same as in errors
220 */
221 virtual double CovMatrix(unsigned int , unsigned int ) const;
222
223 /// return covariance matrix status
224 virtual int CovMatrixStatus() const;
225
226protected:
227
228
229private:
230
231 unsigned int fNFree; // dimension of the internal function to be minimized
232 unsigned int fSize; // number of fit points (residuals)
233
234 ROOT::Math::GSLMultiFit * fGSLMultiFit; // pointer to GSL multi fit solver
235 const ROOT::Math::FitMethodFunction * fChi2Func; // pointer to Least square function
236
237 double fEdm; // edm value
238 double fLSTolerance; // Line Search Tolerance
239 std::vector<double> fErrors;
240 std::vector<double> fCovMatrix; // cov matrix (stored as cov[ i * dim + j]
241 std::vector<LSResidualFunc> fResiduals; //! transient Vector of the residual functions
242
243
244
245};
246
247 } // end namespace Math
248
249} // end namespace ROOT
250
251
252#endif /* ROOT_Math_GSLNLSMinimizer */
double
Definition: Converters.cxx:921
#define f(i)
Definition: RSha256.hxx:104
#define g(i)
Definition: RSha256.hxx:105
int type
Definition: TGX11.cxx:120
FitMethodFunction class Interface for objective functions (like chi2 and likelihood used in the fit) ...
virtual double DataElement(const double *x, unsigned int i, double *g=0) const =0
method returning the data i-th contribution to the fit objective function For example the residual fo...
virtual unsigned int NCalls() const
return the total number of function calls (overrided if needed)
virtual unsigned int NDim() const
Number of dimension (parameters) .
Base Minimizer class, which defines the basic funcionality of various minimizer implementations (apar...
GSLMultiFit, internal class for implementing GSL non linear least square GSL fitting.
Definition: GSLMultiFit.h:52
GSLNLSMinimizer class for Non Linear Least Square fitting It Uses the Levemberg-Marquardt algorithm f...
const ROOT::Math::FitMethodFunction * fChi2Func
virtual bool Minimize()
method to perform the minimization
virtual bool ProvidesError() const
number of free variables (real dimension of the problem) this is <= Function().NDim() which is the to...
std::vector< double > fErrors
std::vector< LSResidualFunc > fResiduals
virtual double CovMatrix(unsigned int, unsigned int) const
return covariance matrices elements if the variable is fixed the matrix is zero The ordering of the v...
std::vector< double > fCovMatrix
virtual double Edm() const
return expected distance reached from the minimum
virtual int CovMatrixStatus() const
return covariance matrix status
virtual const double * Errors() const
return errors at the minimum
virtual unsigned int NCalls() const
number of function calls to reach the minimum
virtual const double * MinGradient() const
return pointer to gradient values at the minimum
GSLNLSMinimizer & operator=(const GSLNLSMinimizer &rhs)
Assignment operator.
GSLNLSMinimizer(int type=0)
Default constructor.
GSLNLSMinimizer(const GSLNLSMinimizer &)
Copy constructor.
ROOT::Math::GSLMultiFit * fGSLMultiFit
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)
set the function to minimize
~GSLNLSMinimizer()
Destructor (no operations)
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:62
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Definition: IFunction.h:327
LSResidualFunc class description.
double DoEval(const double *x) const
LSResidualFunc & operator=(const LSResidualFunc &rhs)
LSResidualFunc(const LSResidualFunc &rhs)
LSResidualFunc(const ROOT::Math::FitMethodFunction &func, unsigned int i)
unsigned int NDim() const
Retrieve the dimension of the function.
std::vector< double > fX2
void Gradient(const double *x, double *g) const
IMultiGenFunction * Clone() const
Clone a function.
const ROOT::Math::FitMethodFunction * fChi2
void FdF(const double *x, double &f, double *g) const
double DoDerivative(const double *x, unsigned int icoord) const
Double_t x[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
Namespace for new Math classes and functions.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: StringConv.hxx:21