BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
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  // BornAgain -> G.P. Fast derivative calculation using values caching
121  f = DoEval(x);
122  fChi2->DataElement(x, fIndex, g);
123  }
124 
125 
126 private:
127 
128  double DoEval (const double * x) const {
129  return fChi2->DataElement(x, fIndex);
130  }
131 
132  double DoDerivative(const double * x, unsigned int icoord) const {
133  //return ROOT::Math::Derivator::Eval(*this, x, icoord, 1E-8);
134  std::copy(x,x+NDim(),fX2.begin());
135  const double kEps = 1.0E-4;
136  fX2[icoord] += kEps;
137  return ( DoEval(&fX2.front()) - DoEval(x) )/kEps;
138  }
139 
140  unsigned int fIndex;
142  mutable std::vector<double> fX2; // cached vector
143 };
144 
145 
146 //_____________________________________________________________________________________________________
147 /**
148  GSLNLSMinimizer class for Non Linear Least Square fitting
149  It Uses the Levemberg-Marquardt algorithm from
150  <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Nonlinear-Least_002dSquares-Fitting.html">
151  GSL Non Linear Least Square fitting</A>.
152 
153  @ingroup MultiMin
154 */
156 
157 public:
158 
159  /**
160  Default constructor
161  */
162  GSLNLSMinimizer (int type = 0);
163 
164  /**
165  Destructor (no operations)
166  */
168 
169 private:
170  // usually copying is non trivial, so we make this unaccessible
171 
172  /**
173  Copy constructor
174  */
176 
177  /**
178  Assignment operator
179  */
181  if (this == &rhs) return *this; // time saving self-test
182  return *this;
183  }
184 
185 public:
186 
187  /// set the function to minimize
188  virtual void SetFunction(const ROOT::Math::IMultiGenFunction & func);
189 
190  /// set gradient the function to minimize
191  virtual void SetFunction(const ROOT::Math::IMultiGradFunction & func);
192 
193 
194  /// method to perform the minimization
195  virtual bool Minimize();
196 
197 
198  /// return expected distance reached from the minimum
199  virtual double Edm() const { return fEdm; } // not impl. }
200 
201 
202  /// return pointer to gradient values at the minimum
203  virtual const double * MinGradient() const;
204 
205  /// number of function calls to reach the minimum
206  virtual unsigned int NCalls() const { return (fChi2Func) ? fChi2Func->NCalls() : 0; }
207 
208  /// number of free variables (real dimension of the problem)
209  /// this is <= Function().NDim() which is the total
210 // virtual unsigned int NFree() const { return fNFree; }
211 
212  /// minimizer provides error and error matrix
213  virtual bool ProvidesError() const { return true; }
214 
215  /// return errors at the minimum
216  virtual const double * Errors() const { return (fErrors.size() > 0) ? &fErrors.front() : 0; }
217 // {
218 // static std::vector<double> err;
219 // err.resize(fDim);
220 // return &err.front();
221 // }
222 
223  /** return covariance matrices elements
224  if the variable is fixed the matrix is zero
225  The ordering of the variables is the same as in errors
226  */
227  virtual double CovMatrix(unsigned int , unsigned int ) const;
228 
229  /// return covariance matrix status
230  virtual int CovMatrixStatus() const;
231 
232 protected:
233 
234 
235 private:
236 
237  unsigned int fNFree; // dimension of the internal function to be minimized
238  unsigned int fSize; // number of fit points (residuals)
239 
240  ROOT::Math::GSLMultiFit * fGSLMultiFit; // pointer to GSL multi fit solver
241  const ROOT::Math::FitMethodFunction * fChi2Func; // pointer to Least square function
242 
243  double fEdm; // edm value
244  double fLSTolerance; // Line Search Tolerance
245  std::vector<double> fErrors;
246  std::vector<double> fCovMatrix; // cov matrix (stored as cov[ i * dim + j]
247  std::vector<LSResidualFunc> fResiduals; //! transient Vector of the residual functions
248 
249 
250 
251 };
252 
253  } // end namespace Math
254 
255 } // end namespace ROOT
256 
257 
258 #endif /* ROOT_Math_GSLNLSMinimizer */
virtual double DataElement(const double *x, unsigned int i, double *g=0) const =0
virtual unsigned int NCalls() const
virtual unsigned int NDim() const
const ROOT::Math::FitMethodFunction * fChi2Func
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)
set the function to minimize
virtual double CovMatrix(unsigned int, unsigned int) const
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 void SetFunction(const ROOT::Math::IMultiGradFunction &func)
set gradient the function to minimize
std::vector< double > fCovMatrix
virtual double Edm() const
return expected distance reached from the minimum
GSLNLSMinimizer & operator=(const GSLNLSMinimizer &rhs)
virtual const double * Errors() const
return errors at the minimum
virtual unsigned int NCalls() const
number of function calls to reach the minimum
virtual int CovMatrixStatus() const
return covariance matrix status
GSLNLSMinimizer(const GSLNLSMinimizer &)
virtual const double * MinGradient() const
return pointer to gradient values at the minimum
ROOT::Math::GSLMultiFit * fGSLMultiFit
virtual bool Minimize()
method to perform the minimization
double DoEval(const double *x) const
LSResidualFunc(const LSResidualFunc &rhs)
LSResidualFunc(const ROOT::Math::FitMethodFunction &func, unsigned int i)
unsigned int NDim() const
std::vector< double > fX2
void Gradient(const double *x, double *g) const
const ROOT::Math::FitMethodFunction * fChi2
LSResidualFunc & operator=(const LSResidualFunc &rhs)
void FdF(const double *x, double &f, double *g) const
IMultiGenFunction * Clone() const
double DoDerivative(const double *x, unsigned int icoord) const
Various mathematical functions.
Definition: Bessel.h:26
Definition: TUUID.h:7