Logo ROOT  
Reference Guide
No Matches
Go to the documentation of this file.
1// @(#)root/minuit:$Id$
2// Author: Anna Kreshuk 04/03/2005
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 *************************************************************************/
12#ifndef ROOT_TLinearFitter
13#define ROOT_TLinearFitter
17// The Linear Fitter - fitting functions that are LINEAR IN PARAMETERS
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
25// y(x) = a[0] + a[1]*f[1](x)+...a[n]*f[n](x)
27// Functions f are fixed functions of x. For example, fitting with a
28// polynomial is linear fitting in this sense.
30// The fitting method
32// The fit is performed using the Normal Equations method with Cholesky
33// decomposition.
35// Why should it be used?
37// The linear fitter is considerably faster than general non-linear
38// fitters and doesn't require to set the initial values of parameters.
40// Using the fitter:
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
61// 2.Setting the formula
62// 2.1 The linear formula syntax:
63// -Additive parts are separated by 2 plus signs "++"
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);
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.
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.
145#include "TVectorD.h"
146#include "TMatrixD.h"
147#include "TObjArray.h"
148#include "TFormula.h"
149#include "TVirtualFitter.h"
151#include <map>
156 TVectorD fParams; //vector of parameters
157 TMatrixDSym fParCovar; //matrix of parameters' covariances
158 TVectorD fTValues; //T-Values of parameters
159 TVectorD fParSign; //significance levels of parameters
160 TMatrixDSym fDesign; //matrix AtA
161 TMatrixDSym fDesignTemp; //! temporary matrix, used for num.stability
165 TVectorD fAtb; //vector Atb
166 TVectorD fAtbTemp; //! temporary vector, used for num.stability
170 static std::map<TString,TFormula*> fgFormulaMap; //! map of basis functions and formula
171 TObjArray fFunctions; //array of basis functions
172 TVectorD fY; //the values being fit
173 Double_t fY2; //sum of square of y, used for chisquare
174 Double_t fY2Temp; //! temporary variable used for num.stability
175 TMatrixD fX; //values of x
176 TVectorD fE; //the errors if they are known
177 TFormula *fInputFunction; //the function being fit
178 Double_t fVal[1000]; //! temporary
180 Int_t fNpoints; //number of points
181 Int_t fNfunctions; //number of basis functions
182 Int_t fFormulaSize; //length of the formula
183 Int_t fNdim; //number of dimensions in the formula
184 Int_t fNfixed; //number of fixed parameters
185 Int_t fSpecial; //=100+n if fitting a polynomial of deg.n
186 //=200+n if fitting an n-dimensional hyperplane
187 char *fFormula; //the formula
188 Bool_t fIsSet; //Has the formula been set?
189 Bool_t fStoreData; //Is the data stored?
190 Double_t fChisquare; //Chisquare of the fit
192 Int_t fH; //number of good points in robust fit
193 Bool_t fRobust; //true when performing a robust fit
194 TBits fFitsample; //indices of points, used in the robust fit
196 Bool_t *fFixedParams; //[fNfixed] array of fixed/released params
200 void ComputeTValues();
206 //robust fitting functions:
207 Int_t Partition(Int_t nmini, Int_t *indsubdat);
208 void RDraw(Int_t *subdat, Int_t *indsubdat);
209 void CreateSubset(Int_t ntotal, Int_t h, Int_t *index);
210 Double_t CStep(Int_t step, Int_t h, Double_t *residuals, Int_t *index, Int_t *subdat, Int_t start, Int_t end);
211 Bool_t Linf();
215 TLinearFitter(Int_t ndim, const char *formula, Option_t *opt="D");
216 TLinearFitter(Int_t ndim);
218 TLinearFitter(const TLinearFitter& tlf);
219 ~TLinearFitter() override;
222 virtual void Add(TLinearFitter *tlf);
223 virtual void AddPoint(Double_t *x, Double_t y, Double_t e=1);
224 virtual void AddTempMatrices();
225 virtual void AssignData(Int_t npoints, Int_t xncols, Double_t *x, Double_t *y, Double_t *e=nullptr);
227 void Clear(Option_t *option="") override;
228 virtual void ClearPoints();
229 virtual void Chisquare();
230 virtual Int_t Eval();
231 virtual Int_t EvalRobust(Double_t h=-1);
232 Int_t ExecuteCommand(const char *command, Double_t *args, Int_t nargs) override;
233 void FixParameter(Int_t ipar) override;
234 virtual void FixParameter(Int_t ipar, Double_t parvalue);
235 virtual void GetAtbVector(TVectorD &v);
236 virtual Double_t GetChisquare();
237 void GetConfidenceIntervals(Int_t n, Int_t ndim, const Double_t *x, Double_t *ci, Double_t cl=0.95) override;
238 void GetConfidenceIntervals(TObject *obj, Double_t cl=0.95) override;
239 Double_t* GetCovarianceMatrix() const override;
240 virtual void GetCovarianceMatrix(TMatrixD &matr);
241 Double_t GetCovarianceMatrixElement(Int_t i, Int_t j) const override {return fParCovar(i, j);}
242 virtual void GetDesignMatrix(TMatrixD &matr);
243 virtual void GetErrors(TVectorD &vpar);
244 Int_t GetNumberTotalParameters() const override {return fNfunctions;}
246 virtual Int_t GetNpoints() { return fNpoints; }
247 virtual void GetParameters(TVectorD &vpar);
248 Double_t GetParameter(Int_t ipar) const override {return fParams(ipar);}
249 Int_t GetParameter(Int_t ipar,char* name,Double_t& value,Double_t& /*verr*/,Double_t& /*vlow*/, Double_t& /*vhigh*/) const override;
250 const char *GetParName(Int_t ipar) const override;
251 Double_t GetParError(Int_t ipar) const override;
252 virtual Double_t GetParTValue(Int_t ipar);
253 virtual Double_t GetParSignificance(Int_t ipar);
254 virtual void GetFitSample(TBits& bits);
255 virtual Double_t GetY2() const {return fY2;}
256 Bool_t IsFixed(Int_t ipar) const override {return fFixedParams[ipar];}
257 virtual Int_t Merge(TCollection *list);
258 void PrintResults(Int_t level, Double_t amin=0) const override;
259 void ReleaseParameter(Int_t ipar) override;
260 virtual void SetBasisFunctions(TObjArray * functions);
261 virtual void SetDim(Int_t n);
262 virtual void SetFormula(const char* formula);
263 virtual void SetFormula(TFormula *function);
264 virtual void StoreData(Bool_t store) {fStoreData=store;}
266 virtual Bool_t UpdateMatrix();
268 //dummy functions for TVirtualFitter:
269 Double_t Chisquare(Int_t /*npar*/, Double_t * /*params*/) const override {return 0;}
270 Int_t GetErrors(Int_t /*ipar*/,Double_t & /*eplus*/, Double_t & /*eminus*/, Double_t & /*eparab*/, Double_t & /*globcc*/) const override {return 0;}
272 Int_t GetStats(Double_t& /*amin*/, Double_t& /*edm*/, Double_t& /*errdef*/, Int_t& /*nvpar*/, Int_t& /*nparx*/) const override {return 0;}
273 Double_t GetSumLog(Int_t /*i*/) override {return 0;}
274 void SetFitMethod(const char * /*name*/) override {}
275 Int_t SetParameter(Int_t /*ipar*/,const char * /*parname*/,Double_t /*value*/,Double_t /*verr*/,Double_t /*vlow*/, Double_t /*vhigh*/) override {return 0;}
277 ClassDefOverride(TLinearFitter, 2) //fit a set of data points with a linear combination of functions
#define h(i)
Definition RSha256.hxx:106
#define e(i)
Definition RSha256.hxx:103
RooAbsReal & function()
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
const char Option_t
Definition RtypesCore.h:66
#define ClassDefOverride(name, id)
Definition Rtypes.h:341
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char name[80]
Definition TGX11.cxx:110
Container of bits.
Definition TBits.h:26
Collection abstract base class.
Definition TCollection.h:65
The Formula class.
Definition TFormula.h:89
Int_t SetParameter(Int_t, const char *, Double_t, Double_t, Double_t, Double_t) override
virtual void AddTempMatrices()
virtual Int_t GetNpoints()
TMatrixDSym fDesignTemp2
temporary matrix, used for num.stability
Int_t GetStats(Double_t &, Double_t &, Double_t &, Int_t &, Int_t &) const override
Int_t GraphLinearFitter(Double_t h)
Used in TGraph::Fit().
~TLinearFitter() override
Linear fitter cleanup.
Int_t GetNumberFreeParameters() const override
TMatrixDSym fDesignTemp
virtual Double_t GetChisquare()
Get the Chisquare.
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 void GetErrors(TVectorD &vpar)
Returns parameter errors.
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 Int_t Merge(TCollection *list)
Merge objects in list.
Double_t fChisquare
void ComputeTValues()
Computes parameters' t-values and significance.
Bool_t * fFixedParams
TVectorD fParSign
Double_t GetSumLog(Int_t) override
void Clear(Option_t *option="") override
Clears everything. Used in TH1::Fit and TGraph::Fit().
default c-tor, input data is stored If you don't want to store the input data, run the function Store...
const char * GetParName(Int_t ipar) const override
Returns name of parameter #ipar
Int_t MultiGraphLinearFitter(Double_t h)
Minimisation function for a TMultiGraph.
virtual Double_t GetParSignificance(Int_t ipar)
Returns the significance of parameter #ipar
TMatrixDSym fDesign
virtual Int_t Eval()
Perform the fit and evaluate the parameters Returns 0 if the fit is ok, 1 if there are errors.
TVectorD fAtbTemp2
temporary vector, used for num.stability
void PrintResults(Int_t level, Double_t amin=0) const override
Level = 3 (to be consistent with minuit) prints parameters and parameter errors.
Int_t HistLinearFitter()
Minimization function for H1s using a Chisquare method.
Bool_t IsFixed(Int_t ipar) const override
Int_t Graph2DLinearFitter(Double_t h)
Minimisation function for a TGraph2D.
TVectorD fAtbTemp
Int_t GetNumberTotalParameters() const override
Double_t Chisquare(Int_t, Double_t *) const override
Int_t GetErrors(Int_t, Double_t &, Double_t &, Double_t &, Double_t &) const override
virtual void ClearPoints()
To be used when different sets of points are fitted with the same formula.
TMatrixDSym fDesignTemp3
TObjArray fFunctions
map of basis functions and formula
Int_t ExecuteCommand(const char *command, Double_t *args, Int_t nargs) override
To use in TGraph::Fit and TH1::Fit().
virtual void GetFitSample(TBits &bits)
For robust lts fitting, returns the sample, on which the best fit was based.
virtual void Add(TLinearFitter *tlf)
Add another linear fitter to this linear fitter.
virtual void GetDesignMatrix(TMatrixD &matr)
Returns the internal design matrix.
virtual void AssignData(Int_t npoints, Int_t xncols, Double_t *x, Double_t *y, Double_t *e=nullptr)
This function is to use when you already have all the data in arrays and don't want to copy them into...
virtual void GetParameters(TVectorD &vpar)
Returns parameter values.
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...
static std::map< TString, TFormula * > fgFormulaMap
virtual void SetDim(Int_t n)
set the number of dimensions
TFormula * fInputFunction
void GetConfidenceIntervals(Int_t n, Int_t ndim, const Double_t *x, Double_t *ci, Double_t cl=0.95) override
Computes point-by-point confidence intervals for the fitted function Parameters: n - number of points...
TMatrixD fX
temporary variable used for num.stability
virtual Bool_t UpdateMatrix()
Update the design matrix after the formula has been changed.
virtual void GetAtbVector(TVectorD &v)
Get the Atb vector - a vector, used for internal computations.
virtual void Chisquare()
Calculates the chisquare.
Double_t GetCovarianceMatrixElement(Int_t i, Int_t j) const override
virtual void SetBasisFunctions(TObjArray *functions)
set the basis functions in case the fitting function is not set directly The TLinearFitter will manag...
Int_t fNpoints
Double_t fVal[1000]
void SetFitMethod(const char *) override
virtual Int_t EvalRobust(Double_t h=-1)
Finds the parameters of the fitted function in case data contains outliers.
TVectorD fTValues
virtual Double_t GetY2() const
Double_t GetParError(Int_t ipar) const override
Returns the error of 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.
TVectorD fAtbTemp3
void FixParameter(Int_t ipar) override
Fixes paramter #ipar at its current value.
TLinearFitter & operator=(const TLinearFitter &tlf)
Assignment operator.
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 void SetFormula(const char *formula)
Additive parts should be separated by "++".
void ReleaseParameter(Int_t ipar) override
Releases parameter #ipar.
virtual void AddPoint(Double_t *x, Double_t y, Double_t e=1)
Adds 1 point to the fitter.
TMatrixDSym fParCovar
virtual Double_t GetParTValue(Int_t ipar)
Returns the t-value for parameter #ipar
Double_t * GetCovarianceMatrix() const override
Returns covariance matrix.
Double_t GetParameter(Int_t ipar) const override
virtual void StoreData(Bool_t store)
An array of TObjects.
Definition TObjArray.h:31
Mother of all ROOT objects.
Definition TObject.h:41
Abstract Base Class for Fitting.
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16