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
44namespace 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
59 The class is used by ROOT::Math::GSLNLSMinimizer (GSL non linear least square fitter)
60
61 @ingroup MultiMin
62*/
64public:
65
66 //default ctor (required by CINT)
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
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
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
119private:
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
150public:
151
152 /**
153 Default constructor
154 */
155 GSLNLSMinimizer (int type = 0);
156
157 /**
158 Destructor (no operations)
159 */
161
162private:
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
178public:
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
225protected:
226
227
228private:
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 */
double
Definition: Converters.cxx:939
#define f(i)
Definition: RSha256.hxx:104
#define g(i)
Definition: RSha256.hxx:105
int type
Definition: TGX11.cxx:121
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:53
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:343
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...
{"ft_lang_label":"__label__en","ft_lang_prob":0.538967,"math_prob":0.89492375,"size":3377,"snap":"2021-43-2021-49","text_gpt3_token_len":840,"char_repetition_ratio":0.18974207,"word_repetition_ratio":0.0088691795,"special_character_ratio":0.2232751,"punctuation_ratio":0.17460318,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95201355,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-04T04:26:20Z\",\"WARC-Record-ID\":\"<urn:uuid:6b179a5a-6d07-45db-bdff-a7e97f25eb0e>\",\"Content-Length\":\"84885\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7a34fb30-936f-4d5f-8b84-d32dd4017e9f>\",\"WARC-Concurrent-To\":\"<urn:uuid:ca57e0d1-0f8e-4462-976c-876dedcabeb0>\",\"WARC-IP-Address\":\"137.138.18.236\",\"WARC-Target-URI\":\"https://root.cern.ch/doc/master/GSLNLSMinimizer_8h_source.html\",\"WARC-Payload-Digest\":\"sha1:GWIVDMIITDN6TCN5WOA5FEMZGIM6CVAB\",\"WARC-Block-Digest\":\"sha1:CRBJJ4KX74FMGY4B4PVYYVRI3OHLT5G7\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964362930.53_warc_CC-MAIN-20211204033320-20211204063320-00463.warc.gz\"}"}