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