Logo ROOT   6.14/05
Reference Guide
TLinearFitter.h
Go to the documentation of this file.
1 // @(#)root/minuit:$Id$
2 // Author: Anna Kreshuk 04/03/2005
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TLinearFitter
13 #define ROOT_TLinearFitter
14 
15 //////////////////////////////////////////////////////////////////////////
16 //
17 // The Linear Fitter - fitting functions that are LINEAR IN PARAMETERS
18 //
19 // Linear fitter is used to fit a set of data points with a linear
20 // combination of specified functions. Note, that "linear" in the name
21 // stands only for the model dependency on parameters, the specified
22 // functions can be nonlinear.
23 // The general form of this kind of model is
24 //
25 // y(x) = a[0] + a[1]*f[1](x)+...a[n]*f[n](x)
26 //
27 // Functions f are fixed functions of x. For example, fitting with a
28 // polynomial is linear fitting in this sense.
29 //
30 // The fitting method
31 //
32 // The fit is performed using the Normal Equations method with Cholesky
33 // decomposition.
34 //
35 // Why should it be used?
36 //
37 // The linear fitter is considerably faster than general non-linear
38 // fitters and doesn't require to set the initial values of parameters.
39 //
40 // Using the fitter:
41 //
42 // 1.Adding the data points:
43 // 1.1 To store or not to store the input data?
44 // - There are 2 options in the constructor - to store or not
45 // store the input data. The advantages of storing the data
46 // are that you'll be able to reset the fitting model without
47 // adding all the points again, and that for very large sets
48 // of points the chisquare is calculated more precisely.
49 // The obvious disadvantage is the amount of memory used to
50 // keep all the points.
51 // - Before you start adding the points, you can change the
52 // store/not store option by StoreData() method.
53 // 1.2 The data can be added:
54 // - simply point by point - AddPoint() method
55 // - an array of points at once:
56 // If the data is already stored in some arrays, this data
57 // can be assigned to the linear fitter without physically
58 // coping bytes, thanks to the Use() method of
59 // TVector and TMatrix classes - AssignData() method
60 //
61 // 2.Setting the formula
62 // 2.1 The linear formula syntax:
63 // -Additive parts are separated by 2 plus signes "++"
64 // --for example "1 ++ x" - for fitting a straight line
65 // -All standard functions, undrestood by TFormula, can be used
66 // as additive parts
67 // --TMath functions can be used too
68 // -Functions, used as additive parts, shouldn't have any parameters,
69 // even if those parameters are set.
70 // --for example, if normalizing a sum of a gaus(0, 1) and a
71 // gaus(0, 2), don't use the built-in "gaus" of TFormula,
72 // because it has parameters, take TMath::Gaus(x, 0, 1) instead.
73 // -Polynomials can be used like "pol3", .."polN"
74 // -If fitting a more than 3-dimensional formula, variables should
75 // be numbered as follows:
76 // -- x0, x1, x2... For example, to fit "1 ++ x0 ++ x1 ++ x2 ++ x3*x3"
77 // 2.2 Setting the formula:
78 // 2.2.1 If fitting a 1-2-3-dimensional formula, one can create a
79 // TF123 based on a linear expression and pass this function
80 // to the fitter:
81 // --Example:
82 // TLinearFitter *lf = new TLinearFitter();
83 // TF2 *f2 = new TF2("f2", "x ++ y ++ x*x*y*y", -2, 2, -2, 2);
84 // lf->SetFormula(f2);
85 // --The results of the fit are then stored in the function,
86 // just like when the TH1::Fit or TGraph::Fit is used
87 // --A linear function of this kind is by no means different
88 // from any other function, it can be drawn, evaluated, etc.
89 // 2.2.2 There is no need to create the function if you don't want to,
90 // the formula can be set by expression:
91 // --Example:
92 // // 2 is the number of dimensions
93 // TLinearFitter *lf = new TLinearFitter(2);
94 // lf->SetFormula("x ++ y ++ x*x*y*y");
95 // --That's the only way to go, if you want to fit in more
96 // than 3 dimensions
97 // 2.2.3 The fastest functions to compute are polynomials and hyperplanes.
98 // --Polynomials are set the usual way: "pol1", "pol2",...
99 // --Hyperplanes are set by expression "hyp3", "hyp4", ...
100 // ---The "hypN" expressions only work when the linear fitter
101 // is used directly, not through TH1::Fit or TGraph::Fit.
102 // To fit a graph or a histogram with a hyperplane, define
103 // the function as "1++x++y".
104 // ---A constant term is assumed for a hyperplane, when using
105 // the "hypN" expression, so "hyp3" is in fact fitting with
106 // "1++x++y++z" function.
107 // --Fitting hyperplanes is much faster than fitting other
108 // expressions so if performance is vital, calculate the
109 // function values beforehand and give them to the fitter
110 // as variables
111 // --Example:
112 // You want to fit "sin(x)|cos(2*x)" very fast. Calculate
113 // sin(x) and cos(2*x) beforehand and store them in array *data.
114 // Then:
115 // TLinearFitter *lf=new TLinearFitter(2, "hyp2");
116 // lf->AssignData(npoint, 2, data, y);
117 //
118 // 2.3 Resetting the formula
119 // 2.3.1 If the input data is stored (or added via AssignData() function),
120 // the fitting formula can be reset without re-adding all the points.
121 // --Example:
122 // TLinearFitter *lf=new TLinearFitter("1++x++x*x");
123 // lf->AssignData(n, 1, x, y, e);
124 // lf->Eval()
125 // //looking at the parameter significance, you see,
126 // // that maybe the fit will improve, if you take out
127 // // the constant term
128 // lf->SetFormula("x++x*x");
129 // lf->Eval();
130 // ...
131 // 2.3.2 If the input data is not stored, the fitter will have to be
132 // cleared and the data will have to be added again to try a
133 // different formula.
134 //
135 // 3.Accessing the fit results
136 // 3.1 There are methods in the fitter to access all relevant information:
137 // --GetParameters, GetCovarianceMatrix, etc
138 // --the t-values of parameters and their significance can be reached by
139 // GetParTValue() and GetParSignificance() methods
140 // 3.2 If fitting with a pre-defined TF123, the fit results are also
141 // written into this function.
142 //
143 //////////////////////////////////////////////////////////////////////////
144 
145 #include "TVectorD.h"
146 #include "TMatrixD.h"
147 #include "TFormula.h"
148 #include "TVirtualFitter.h"
149 
150 
152 
153 private:
154  TVectorD fParams; //vector of parameters
155  TMatrixDSym fParCovar; //matrix of parameters' covariances
156  TVectorD fTValues; //T-Values of parameters
157  TVectorD fParSign; //significance levels of parameters
158  TMatrixDSym fDesign; //matrix AtA
159  TMatrixDSym fDesignTemp; //! temporary matrix, used for num.stability
162 
163  TVectorD fAtb; //vector Atb
164  TVectorD fAtbTemp; //! temporary vector, used for num.stability
167 
168  static std::map<TString,TFormula*> fgFormulaMap; //! map of basis functions and formula
169  TObjArray fFunctions; //array of basis functions
170  TVectorD fY; //the values being fit
171  Double_t fY2; //sum of square of y, used for chisquare
172  Double_t fY2Temp; //! temporary variable used for num.stability
173  TMatrixD fX; //values of x
174  TVectorD fE; //the errors if they are known
175  TFormula *fInputFunction; //the function being fit
176  Double_t fVal[1000]; //! temporary
177 
178  Int_t fNpoints; //number of points
179  Int_t fNfunctions; //number of basis functions
180  Int_t fFormulaSize; //length of the formula
181  Int_t fNdim; //number of dimensions in the formula
182  Int_t fNfixed; //number of fixed parameters
183  Int_t fSpecial; //=100+n if fitting a polynomial of deg.n
184  //=200+n if fitting an n-dimensional hyperplane
185  char *fFormula; //the formula
186  Bool_t fIsSet; //Has the formula been set?
187  Bool_t fStoreData; //Is the data stored?
188  Double_t fChisquare; //Chisquare of the fit
189 
190  Int_t fH; //number of good points in robust fit
191  Bool_t fRobust; //true when performing a robust fit
192  TBits fFitsample; //indices of points, used in the robust fit
193 
194  Bool_t *fFixedParams; //[fNfixed] array of fixed/released params
195 
196 
198  void ComputeTValues();
203 
204  //robust fitting functions:
205  Int_t Partition(Int_t nmini, Int_t *indsubdat);
206  void RDraw(Int_t *subdat, Int_t *indsubdat);
207  void CreateSubset(Int_t ntotal, Int_t h, Int_t *index);
208  Double_t CStep(Int_t step, Int_t h, Double_t *residuals, Int_t *index, Int_t *subdat, Int_t start, Int_t end);
209  Bool_t Linf();
210 
211 public:
212  TLinearFitter();
213  TLinearFitter(Int_t ndim, const char *formula, Option_t *opt="D");
214  TLinearFitter(Int_t ndim);
215  TLinearFitter(TFormula *function, Option_t *opt="D");
216  TLinearFitter(const TLinearFitter& tlf);
217  virtual ~TLinearFitter();
218 
220  virtual void Add(TLinearFitter *tlf);
221  virtual void AddPoint(Double_t *x, Double_t y, Double_t e=1);
222  virtual void AddTempMatrices();
223  virtual void AssignData(Int_t npoints, Int_t xncols, Double_t *x, Double_t *y, Double_t *e=0);
224 
225  virtual void Clear(Option_t *option="");
226  virtual void ClearPoints();
227  virtual void Chisquare();
228  virtual Int_t Eval();
229  virtual Int_t EvalRobust(Double_t h=-1);
230  virtual Int_t ExecuteCommand(const char *command, Double_t *args, Int_t nargs);
231  virtual void FixParameter(Int_t ipar);
232  virtual void FixParameter(Int_t ipar, Double_t parvalue);
233  virtual void GetAtbVector(TVectorD &v);
234  virtual Double_t GetChisquare();
235  virtual void GetConfidenceIntervals(Int_t n, Int_t ndim, const Double_t *x, Double_t *ci, Double_t cl=0.95);
236  virtual void GetConfidenceIntervals(TObject *obj, Double_t cl=0.95);
237  virtual Double_t* GetCovarianceMatrix() const;
238  virtual void GetCovarianceMatrix(TMatrixD &matr);
239  virtual Double_t GetCovarianceMatrixElement(Int_t i, Int_t j) const {return fParCovar(i, j);}
240  virtual void GetDesignMatrix(TMatrixD &matr);
241  virtual void GetErrors(TVectorD &vpar);
242  virtual Int_t GetNumberTotalParameters() const {return fNfunctions;}
243  virtual Int_t GetNumberFreeParameters() const {return fNfunctions-fNfixed;}
244  virtual Int_t GetNpoints() { return fNpoints; }
245  virtual void GetParameters(TVectorD &vpar);
246  virtual Double_t GetParameter(Int_t ipar) const {return fParams(ipar);}
247  virtual Int_t GetParameter(Int_t ipar,char* name,Double_t& value,Double_t& /*verr*/,Double_t& /*vlow*/, Double_t& /*vhigh*/) const;
248  virtual const char *GetParName(Int_t ipar) const;
249  virtual Double_t GetParError(Int_t ipar) const;
250  virtual Double_t GetParTValue(Int_t ipar);
251  virtual Double_t GetParSignificance(Int_t ipar);
252  virtual void GetFitSample(TBits& bits);
253  virtual Double_t GetY2() const {return fY2;}
254  virtual Bool_t IsFixed(Int_t ipar) const {return fFixedParams[ipar];}
255  virtual Int_t Merge(TCollection *list);
256  virtual void PrintResults(Int_t level, Double_t amin=0) const;
257  virtual void ReleaseParameter(Int_t ipar);
258  virtual void SetBasisFunctions(TObjArray * functions);
259  virtual void SetDim(Int_t n);
260  virtual void SetFormula(const char* formula);
261  virtual void SetFormula(TFormula *function);
262  virtual void StoreData(Bool_t store) {fStoreData=store;}
263 
264  virtual Bool_t UpdateMatrix();
265 
266  //dummy functions for TVirtualFitter:
267  virtual Double_t Chisquare(Int_t /*npar*/, Double_t * /*params*/) const {return 0;}
268  virtual Int_t GetErrors(Int_t /*ipar*/,Double_t & /*eplus*/, Double_t & /*eminus*/, Double_t & /*eparab*/, Double_t & /*globcc*/) const {return 0;}
269 
270  virtual Int_t GetStats(Double_t& /*amin*/, Double_t& /*edm*/, Double_t& /*errdef*/, Int_t& /*nvpar*/, Int_t& /*nparx*/) const {return 0;}
271  virtual Double_t GetSumLog(Int_t /*i*/) {return 0;}
272  virtual void SetFitMethod(const char * /*name*/) {;}
273  virtual Int_t SetParameter(Int_t /*ipar*/,const char * /*parname*/,Double_t /*value*/,Double_t /*verr*/,Double_t /*vlow*/, Double_t /*vhigh*/) {return 0;}
274 
275  ClassDef(TLinearFitter, 2) //fit a set of data points with a linear combination of functions
276 };
277 
278 #endif
virtual void GetAtbVector(TVectorD &v)
Get the Atb vector - a vector, used for internal computations.
virtual void GetErrors(TVectorD &vpar)
Returns parameter errors.
virtual void StoreData(Bool_t store)
void RDraw(Int_t *subdat, Int_t *indsubdat)
Draws ngroup nonoverlapping subdatasets out of a dataset of size n such that the selected case number...
An array of TObjects.
Definition: TObjArray.h:37
virtual void FixParameter(Int_t ipar)
Fixes paramter #ipar at its current value.
virtual Int_t GetStats(Double_t &, Double_t &, Double_t &, Int_t &, Int_t &) const
const char Option_t
Definition: RtypesCore.h:62
The Linear Fitter - For fitting functions that are LINEAR IN PARAMETERS.
virtual void AddTempMatrices()
TObjArray fFunctions
map of basis functions and formula
virtual void GetConfidenceIntervals(Int_t n, Int_t ndim, const Double_t *x, Double_t *ci, Double_t cl=0.95)
Computes point-by-point confidence intervals for the fitted function Parameters: n - number of points...
virtual Int_t GetNpoints()
virtual void ClearPoints()
To be used when different sets of points are fitted with the same formula.
Double_t fVal[1000]
virtual Int_t ExecuteCommand(const char *command, Double_t *args, Int_t nargs)
To use in TGraph::Fit and TH1::Fit().
Int_t Graph2DLinearFitter(Double_t h)
Minimisation function for a TGraph2D.
static std::map< TString, TFormula * > fgFormulaMap
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual void GetDesignMatrix(TMatrixD &matr)
Returns the internal design matrix.
Int_t Partition(Int_t nmini, Int_t *indsubdat)
divides the elements into approximately equal subgroups number of elements in each subgroup is stored...
virtual Double_t * GetCovarianceMatrix() const
Returns covariance matrix.
virtual const char * GetParName(Int_t ipar) const
Returns name of parameter #ipar.
TFormula * fInputFunction
virtual Double_t GetCovarianceMatrixElement(Int_t i, Int_t j) const
virtual Bool_t IsFixed(Int_t ipar) const
virtual Int_t GetErrors(Int_t, Double_t &, Double_t &, Double_t &, Double_t &) const
Double_t fY2Temp
Double_t x[n]
Definition: legend1.C:17
#define ClassDef(name, id)
Definition: Rtypes.h:320
virtual void PrintResults(Int_t level, Double_t amin=0) const
Level = 3 (to be consistent with minuit) prints parameters and parameter errors.
Bool_t * fFixedParams
virtual void GetFitSample(TBits &bits)
For robust lts fitting, returns the sample, on which the best fit was based.
Double_t CStep(Int_t step, Int_t h, Double_t *residuals, Int_t *index, Int_t *subdat, Int_t start, Int_t end)
The CStep procedure, as described in the article.
virtual void Clear(Option_t *option="")
Clears everything. Used in TH1::Fit and TGraph::Fit().
TMatrixDSym fDesign
virtual Int_t GetNumberFreeParameters() const
virtual void Chisquare()
Calculates the chisquare.
void CreateSubset(Int_t ntotal, Int_t h, Int_t *index)
Creates a p-subset to start ntotal - total number of points from which the subset is chosen...
virtual Double_t GetParTValue(Int_t ipar)
Returns the t-value for parameter #ipar.
void AddToDesign(Double_t *x, Double_t y, Double_t e)
Add a point to the AtA matrix and to the Atb vector.
Double_t fChisquare
TVectorD fTValues
TVectorD fAtbTemp
Int_t HistLinearFitter()
Minimization function for H1s using a Chisquare method.
virtual Double_t GetParameter(Int_t ipar) const
virtual Double_t GetParSignificance(Int_t ipar)
Returns the significance of parameter #ipar.
SVector< double, 2 > v
Definition: Dict.h:5
Int_t MultiGraphLinearFitter(Double_t h)
Minimisation function for a TMultiGraph.
The Formula class.
Definition: TFormula.h:83
virtual Int_t EvalRobust(Double_t h=-1)
Finds the parameters of the fitted function in case data contains outliers.
Collection abstract base class.
Definition: TCollection.h:63
virtual Double_t GetChisquare()
Get the Chisquare.
Int_t GraphLinearFitter(Double_t h)
Used in TGraph::Fit().
virtual void ReleaseParameter(Int_t ipar)
Releases parameter #ipar.
TLinearFitter()
default c-tor, input data is stored If you don&#39;t want to store the input data, run the function Store...
TMatrixD fX
temporary variable used for num.stability
virtual Double_t Chisquare(Int_t, Double_t *) const
virtual void SetBasisFunctions(TObjArray *functions)
set the basis functions in case the fitting function is not set directly The TLinearFitter will manag...
virtual void SetFitMethod(const char *)
#define h(i)
Definition: RSha256.hxx:106
virtual Int_t Eval()
Perform the fit and evaluate the parameters Returns 0 if the fit is ok, 1 if there are errors...
TVectorD fAtbTemp3
TMatrixDSym fDesignTemp2
temporary matrix, used for num.stability
virtual void Add(TLinearFitter *tlf)
Add another linear fitter to this linear fitter.
virtual Double_t GetY2() const
virtual ~TLinearFitter()
Linear fitter cleanup.
double Double_t
Definition: RtypesCore.h:55
TVectorD fParSign
TLinearFitter & operator=(const TLinearFitter &tlf)
Assignment operator.
virtual void GetParameters(TVectorD &vpar)
Returns parameter values.
virtual Double_t GetParError(Int_t ipar) const
Returns the error of parameter #ipar.
Double_t y[n]
Definition: legend1.C:17
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
virtual void AssignData(Int_t npoints, Int_t xncols, Double_t *x, Double_t *y, Double_t *e=0)
This function is to use when you already have all the data in arrays and don&#39;t want to copy them into...
virtual void SetFormula(const char *formula)
Additive parts should be separated by "++".
TVectorD fParams
virtual void SetDim(Int_t n)
set the number of dimensions
TMatrixDSym fDesignTemp
void ComputeTValues()
Computes parameters&#39; t-values and significance.
Abstract Base Class for Fitting.
Mother of all ROOT objects.
Definition: TObject.h:37
TMatrixDSym fParCovar
Container of bits.
Definition: TBits.h:29
virtual Int_t SetParameter(Int_t, const char *, Double_t, Double_t, Double_t, Double_t)
virtual Bool_t UpdateMatrix()
Update the design matrix after the formula has been changed.
Int_t fNpoints
temporary
TMatrixDSym fDesignTemp3
virtual Double_t GetSumLog(Int_t)
TVectorD fAtbTemp2
temporary vector, used for num.stability
virtual Int_t Merge(TCollection *list)
Merge objects in list.
const Int_t n
Definition: legend1.C:16
char name[80]
Definition: TGX11.cxx:109
virtual Int_t GetNumberTotalParameters() const
virtual void AddPoint(Double_t *x, Double_t y, Double_t e=1)
Adds 1 point to the fitter.