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