Logo ROOT   6.08/07
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 #ifndef ROOT_Math_BasicMinimizer
33 #include "Math/BasicMinimizer.h"
34 #endif
35 
36 
37 #ifndef ROOT_Math_IFunctionfwd
38 #include "Math/IFunctionfwd.h"
39 #endif
40 
41 #ifndef ROOT_Math_IParamFunctionfwd
42 #include "Math/IParamFunctionfwd.h"
43 #endif
44 
45 #ifndef ROOT_Math_FitMethodFunction
46 #include "Math/FitMethodFunction.h"
47 #endif
48 
49 #ifndef ROOT_Math_MinimTransformVariable
51 #endif
52 
53 
54 #include <vector>
55 #include <map>
56 #include <string>
57 
58 namespace ROOT {
59 
60  namespace Math {
61 
62  class GSLMultiFit;
63 
64 
65 //________________________________________________________________________________
66 /**
67  LSResidualFunc class description.
68  Internal class used for accessing the residuals of the Least Square function
69  and their derivates which are estimated numerically using GSL numerical derivation.
70  The class contains a pointer to the fit method function and an index specifying
71  the i-th residual and wraps it in a multi-dim gradient function interface
72  ROOT::Math::IGradientFunctionMultiDim.
73  The class is used by ROOT::Math::GSLNLSMinimizer (GSL non linear least square fitter)
74 
75  @ingroup MultiMin
76 */
78 public:
79 
80  //default ctor (required by CINT)
82  {}
83 
84 
86  fIndex(i),
87  fChi2(&func),
88  fX2(std::vector<double>(func.NDim() ) )
89  {}
90 
91 
92  // copy ctor
96  {
97  operator=(rhs);
98  }
99 
100  // assignment
102  {
103  fIndex = rhs.fIndex;
104  fChi2 = rhs.fChi2;
105  fX2 = rhs.fX2;
106  return *this;
107  }
108 
110  return new LSResidualFunc(*fChi2,fIndex);
111  }
112 
113  unsigned int NDim() const { return fChi2->NDim(); }
114 
115  void Gradient( const double * x, double * g) const {
116  double f0 = 0;
117  FdF(x,f0,g);
118  }
119 
120  void FdF (const double * x, double & f, double * g) const {
121  unsigned int n = NDim();
122  std::copy(x,x+n,fX2.begin());
123  const double kEps = 1.0E-4;
124  f = DoEval(x);
125  for (unsigned int i = 0; i < n; ++i) {
126  fX2[i] += kEps;
127  g[i] = ( DoEval(&fX2.front()) - f )/kEps;
128  fX2[i] = x[i];
129  }
130  }
131 
132 
133 private:
134 
135  double DoEval (const double * x) const {
136  return fChi2->DataElement(x, fIndex);
137  }
138 
139  double DoDerivative(const double * x, unsigned int icoord) const {
140  //return ROOT::Math::Derivator::Eval(*this, x, icoord, 1E-8);
141  std::copy(x,x+NDim(),fX2.begin());
142  const double kEps = 1.0E-4;
143  fX2[icoord] += kEps;
144  return ( DoEval(&fX2.front()) - DoEval(x) )/kEps;
145  }
146 
147  unsigned int fIndex;
149  mutable std::vector<double> fX2; // cached vector
150 };
151 
152 
153 //_____________________________________________________________________________________________________
154 /**
155  GSLNLSMinimizer class for Non Linear Least Square fitting
156  It Uses the Levemberg-Marquardt algorithm from
157  <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Nonlinear-Least_002dSquares-Fitting.html">
158  GSL Non Linear Least Square fitting</A>.
159 
160  @ingroup MultiMin
161 */
163 
164 public:
165 
166  /**
167  Default constructor
168  */
169  GSLNLSMinimizer (int type = 0);
170 
171  /**
172  Destructor (no operations)
173  */
174  ~GSLNLSMinimizer ();
175 
176 private:
177  // usually copying is non trivial, so we make this unaccessible
178 
179  /**
180  Copy constructor
181  */
183 
184  /**
185  Assignment operator
186  */
188  if (this == &rhs) return *this; // time saving self-test
189  return *this;
190  }
191 
192 public:
193 
194  /// set the function to minimize
195  virtual void SetFunction(const ROOT::Math::IMultiGenFunction & func);
196 
197  /// set gradient the function to minimize
198  virtual void SetFunction(const ROOT::Math::IMultiGradFunction & func);
199 
200 
201  /// method to perform the minimization
202  virtual bool Minimize();
203 
204 
205  /// return expected distance reached from the minimum
206  virtual double Edm() const { return fEdm; } // not impl. }
207 
208 
209  /// return pointer to gradient values at the minimum
210  virtual const double * MinGradient() const;
211 
212  /// number of function calls to reach the minimum
213  virtual unsigned int NCalls() const { return (fChi2Func) ? fChi2Func->NCalls() : 0; }
214 
215  /// number of free variables (real dimension of the problem)
216  /// this is <= Function().NDim() which is the total
217 // virtual unsigned int NFree() const { return fNFree; }
218 
219  /// minimizer provides error and error matrix
220  virtual bool ProvidesError() const { return true; }
221 
222  /// return errors at the minimum
223  virtual const double * Errors() const { return (fErrors.size() > 0) ? &fErrors.front() : 0; }
224 // {
225 // static std::vector<double> err;
226 // err.resize(fDim);
227 // return &err.front();
228 // }
229 
230  /** return covariance matrices elements
231  if the variable is fixed the matrix is zero
232  The ordering of the variables is the same as in errors
233  */
234  virtual double CovMatrix(unsigned int , unsigned int ) const;
235 
236  /// return covariance matrix status
237  virtual int CovMatrixStatus() const;
238 
239 protected:
240 
241 
242 private:
243 
244  unsigned int fNFree; // dimension of the internal function to be minimized
245  unsigned int fSize; // number of fit points (residuals)
246 
247  ROOT::Math::GSLMultiFit * fGSLMultiFit; // pointer to GSL multi fit solver
248  const ROOT::Math::FitMethodFunction * fChi2Func; // pointer to Least square function
249 
250  double fEdm; // edm value
251  double fLSTolerance; // Line Search Tolerance
252  std::vector<double> fErrors;
253  std::vector<double> fCovMatrix; // cov matrix (stored as cov[ i * dim + j]
254  std::vector<LSResidualFunc> fResiduals; //! transient Vector of the residual functions
255 
256 
257 
258 };
259 
260  } // end namespace Math
261 
262 } // end namespace ROOT
263 
264 
265 #endif /* ROOT_Math_GSLNLSMinimizer */
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Definition: IFunction.h:322
LSResidualFunc & operator=(const LSResidualFunc &rhs)
virtual const double * Errors() const
return errors at the minimum
std::vector< double > fErrors
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
const ROOT::Math::FitMethodFunction * fChi2Func
GSLMultiFit, internal class for implementing GSL non linear least square GSL fitting.
Definition: GSLMultiFit.h:52
double DoEval(const double *x) const
Implementation of the evaluation function.
GSLNLSMinimizer(const GSLNLSMinimizer &)
Copy constructor.
Base Minimizer class, which defines the basic funcionality of various minimizer implementations (apar...
ROOT::Math::GSLMultiFit * fGSLMultiFit
STL namespace.
std::vector< double > fCovMatrix
std::vector< double > fX2
Double_t x[n]
Definition: legend1.C:17
IMultiGenFunction * Clone() const
Clone a function.
virtual unsigned int NCalls() const
number of function calls to reach the minimum
virtual double Edm() const
return expected distance reached from the minimum
double DoDerivative(const double *x, unsigned int icoord) const
function to evaluate the derivative with respect each coordinate.
void Gradient(const double *x, double *g) const
Evaluate all the vector of function derivatives (gradient) at a point x.
void FdF(const double *x, double &f, double *g) const
Optimized method to evaluate at the same time the function value and derivative at a point x...
virtual unsigned int NDim() const
Number of dimension (parameters) .
LSResidualFunc class description.
double f(double x)
FitMethodFunction class Interface for objective functions (like chi2 and likelihood used in the fit) ...
Definition: Fitter.h:57
GSLNLSMinimizer class for Non Linear Least Square fitting It Uses the Levemberg-Marquardt algorithm f...
int type
Definition: TGX11.cxx:120
virtual bool ProvidesError() const
number of free variables (real dimension of the problem) this is <= Function().NDim() which is the to...
const ROOT::Math::FitMethodFunction * fChi2
double func(double *x, double *p)
Definition: stressTF1.cxx:213
std::vector< LSResidualFunc > fResiduals
LSResidualFunc(const ROOT::Math::FitMethodFunction &func, unsigned int i)
Namespace for new Math classes and functions.
LSResidualFunc(const LSResidualFunc &rhs)
unsigned int NDim() const
Retrieve the dimension of the function.
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:63
const Int_t n
Definition: legend1.C:16
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...