TLinearFitter.h

Go to the documentation of this file.
00001 // @(#)root/minuit:$Id: TLinearFitter.h 27022 2008-12-19 10:34:54Z pcanal $
00002 // Author: Anna Kreshuk 04/03/2005
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TLinearFitter
00013 #define ROOT_TLinearFitter
00014 
00015 //////////////////////////////////////////////////////////////////////////
00016 //
00017 // The Linear Fitter - fitting functions that are LINEAR IN PARAMETERS
00018 //
00019 // Linear fitter is used to fit a set of data points with a linear
00020 // combination of specified functions. Note, that "linear" in the name
00021 // stands only for the model dependency on parameters, the specified
00022 // functions can be nonlinear.
00023 // The general form of this kind of model is
00024 //
00025 //          y(x) = a[0] + a[1]*f[1](x)+...a[n]*f[n](x)
00026 //
00027 // Functions f are fixed functions of x. For example, fitting with a
00028 // polynomial is linear fitting in this sense.
00029 //
00030 //                         The fitting method
00031 //
00032 // The fit is performed using the Normal Equations method with Cholesky
00033 // decomposition.
00034 //
00035 //                         Why should it be used?
00036 //
00037 // The linear fitter is considerably faster than general non-linear
00038 // fitters and doesn't require to set the initial values of parameters.
00039 //
00040 //                          Using the fitter:
00041 //
00042 // 1.Adding the data points:
00043 //  1.1 To store or not to store the input data?
00044 //      - There are 2 options in the constructor - to store or not
00045 //        store the input data. The advantages of storing the data
00046 //        are that you'll be able to reset the fitting model without
00047 //        adding all the points again, and that for very large sets
00048 //        of points the chisquare is calculated more precisely.
00049 //        The obvious disadvantage is the amount of memory used to
00050 //        keep all the points.
00051 //      - Before you start adding the points, you can change the
00052 //        store/not store option by StoreData() method.
00053 //  1.2 The data can be added:
00054 //      - simply point by point - AddPoint() method
00055 //      - an array of points at once:
00056 //        If the data is already stored in some arrays, this data
00057 //        can be assigned to the linear fitter without physically
00058 //        coping bytes, thanks to the Use() method of
00059 //        TVector and TMatrix classes - AssignData() method
00060 //
00061 // 2.Setting the formula
00062 //  2.1 The linear formula syntax:
00063 //      -Additive parts are separated by 2 plus signes "++"
00064 //       --for example "1 ++ x" - for fitting a straight line
00065 //      -All standard functions, undrestood by TFormula, can be used
00066 //       as additive parts
00067 //       --TMath functions can be used too
00068 //      -Functions, used as additive parts, shouldn't have any parameters,
00069 //       even if those parameters are set.
00070 //       --for example, if normalizing a sum of a gaus(0, 1) and a
00071 //         gaus(0, 2), don't use the built-in "gaus" of TFormula,
00072 //         because it has parameters, take TMath::Gaus(x, 0, 1) instead.
00073 //      -Polynomials can be used like "pol3", .."polN"
00074 //      -If fitting a more than 3-dimensional formula, variables should
00075 //       be numbered as follows:
00076 //       -- x0, x1, x2... For example, to fit  "1 ++ x0 ++ x1 ++ x2 ++ x3*x3"
00077 //  2.2 Setting the formula:
00078 //    2.2.1 If fitting a 1-2-3-dimensional formula, one can create a
00079 //          TF123 based on a linear expression and pass this function
00080 //          to the fitter:
00081 //          --Example:
00082 //            TLinearFitter *lf = new TLinearFitter();
00083 //            TF2 *f2 = new TF2("f2", "x ++ y ++ x*x*y*y", -2, 2, -2, 2);
00084 //            lf->SetFormula(f2);
00085 //          --The results of the fit are then stored in the function,
00086 //            just like when the TH1::Fit or TGraph::Fit is used
00087 //          --A linear function of this kind is by no means different
00088 //            from any other function, it can be drawn, evaluated, etc.
00089 //    2.2.2 There is no need to create the function if you don't want to,
00090 //          the formula can be set by expression:
00091 //          --Example:
00092 //            // 2 is the number of dimensions
00093 //            TLinearFitter *lf = new TLinearFitter(2);
00094 //            lf->SetFormula("x ++ y ++ x*x*y*y");
00095 //          --That's the only way to go, if you want to fit in more
00096 //            than 3 dimensions
00097 //    2.2.3 The fastest functions to compute are polynomials and hyperplanes.
00098 //          --Polynomials are set the usual way: "pol1", "pol2",...
00099 //          --Hyperplanes are set by expression "hyp3", "hyp4", ...
00100 //          ---The "hypN" expressions only work when the linear fitter
00101 //             is used directly, not through TH1::Fit or TGraph::Fit.
00102 //             To fit a graph or a histogram with a hyperplane, define
00103 //             the function as "1++x++y".
00104 //          ---A constant term is assumed for a hyperplane, when using
00105 //             the "hypN" expression, so "hyp3" is in fact fitting with
00106 //             "1++x++y++z" function.
00107 //          --Fitting hyperplanes is much faster than fitting other
00108 //            expressions so if performance is vital, calculate the
00109 //            function values beforehand and give them to the fitter
00110 //            as variables
00111 //          --Example:
00112 //            You want to fit "sin(x)|cos(2*x)" very fast. Calculate
00113 //            sin(x) and cos(2*x) beforehand and store them in array *data.
00114 //            Then:
00115 //            TLinearFitter *lf=new TLinearFitter(2, "hyp2");
00116 //            lf->AssignData(npoint, 2, data, y);
00117 //
00118 //  2.3 Resetting the formula
00119 //    2.3.1 If the input data is stored (or added via AssignData() function),
00120 //          the fitting formula can be reset without re-adding all the points.
00121 //          --Example:
00122 //            TLinearFitter *lf=new TLinearFitter("1++x++x*x");
00123 //            lf->AssignData(n, 1, x, y, e);
00124 //            lf->Eval()
00125 //            //looking at the parameter significance, you see,
00126 //            // that maybe the fit will improve, if you take out
00127 //            // the constant term
00128 //            lf->SetFormula("x++x*x");
00129 //            lf->Eval();
00130 //            ...
00131 //    2.3.2 If the input data is not stored, the fitter will have to be
00132 //          cleared and the data will have to be added again to try a
00133 //          different formula.
00134 //
00135 // 3.Accessing the fit results
00136 //  3.1 There are methods in the fitter to access all relevant information:
00137 //      --GetParameters, GetCovarianceMatrix, etc
00138 //      --the t-values of parameters and their significance can be reached by
00139 //        GetParTValue() and GetParSignificance() methods
00140 //  3.2 If fitting with a pre-defined TF123, the fit results are also
00141 //      written into this function.
00142 //
00143 //////////////////////////////////////////////////////////////////////////
00144 
00145 #ifndef ROOT_TVectorD
00146 #include "TVectorD.h"
00147 #endif
00148 #ifndef ROOT_TMatrixD
00149 #include "TMatrixD.h"
00150 #endif
00151 #ifndef ROOT_TFormula
00152 #include "TFormula.h"
00153 #endif
00154 #ifndef ROOT_TVirtualFitter
00155 #include "TVirtualFitter.h"
00156 #endif
00157 
00158 
00159 class TLinearFitter: public TVirtualFitter {
00160 
00161 private:
00162    TVectorD     fParams;         //vector of parameters
00163    TMatrixDSym  fParCovar;       //matrix of parameters' covariances
00164    TVectorD     fTValues;        //T-Values of parameters
00165    TVectorD     fParSign;        //significance levels of parameters
00166    TMatrixDSym  fDesign;         //matrix AtA
00167    TMatrixDSym  fDesignTemp;     //! temporary matrix, used for num.stability
00168    TMatrixDSym  fDesignTemp2;    //!
00169    TMatrixDSym  fDesignTemp3;    //!
00170 
00171    TVectorD     fAtb;            //vector Atb
00172    TVectorD     fAtbTemp;        //! temporary vector, used for num.stability
00173    TVectorD     fAtbTemp2;       //!
00174    TVectorD     fAtbTemp3;       //!
00175 
00176    TObjArray    fFunctions;      //array of basis functions
00177    TVectorD     fY;              //the values being fit
00178    Double_t     fY2;             //sum of square of y, used for chisquare
00179    Double_t     fY2Temp;         //! temporary variable used for num.stability
00180    TMatrixD     fX;              //values of x
00181    TVectorD     fE;              //the errors if they are known
00182    TFormula     *fInputFunction; //the function being fit
00183    Double_t     fVal[1000];      //! temporary
00184 
00185    Int_t        fNpoints;        //number of points
00186    Int_t        fNfunctions;     //number of basis functions
00187    Int_t        fFormulaSize;    //length of the formula
00188    Int_t        fNdim;           //number of dimensions in the formula
00189    Int_t        fNfixed;         //number of fixed parameters
00190    Int_t        fSpecial;        //=100+n if fitting a polynomial of deg.n
00191                                  //=200+n if fitting an n-dimensional hyperplane
00192    char         *fFormula;       //the formula
00193    Bool_t       fIsSet;          //Has the formula been set?
00194    Bool_t       fStoreData;      //Is the data stored?
00195    Double_t     fChisquare;      //Chisquare of the fit
00196 
00197    Int_t        fH;              //number of good points in robust fit
00198    Bool_t       fRobust;         //true when performing a robust fit
00199    TBits        fFitsample;      //indices of points, used in the robust fit
00200 
00201    Bool_t       *fFixedParams;   //[fNfixed] array of fixed/released params
00202 
00203    void  AddToDesign(Double_t *x, Double_t y, Double_t e);
00204    void  ComputeTValues();
00205    Int_t GraphLinearFitter(Double_t h);
00206    Int_t Graph2DLinearFitter(Double_t h);
00207    Int_t HistLinearFitter();
00208    Int_t MultiGraphLinearFitter(Double_t h);
00209 
00210    //robust fitting functions:
00211    Int_t     Partition(Int_t nmini, Int_t *indsubdat);
00212    void      RDraw(Int_t *subdat, Int_t *indsubdat);
00213    void      CreateSubset(Int_t ntotal, Int_t h, Int_t *index);
00214    Double_t  CStep(Int_t step, Int_t h, Double_t *residuals, Int_t *index, Int_t *subdat, Int_t start, Int_t end);
00215    Bool_t    Linf();
00216 
00217 public:
00218    TLinearFitter();
00219    TLinearFitter(Int_t ndim, const char *formula, Option_t *opt="D");
00220    TLinearFitter(Int_t ndim);
00221    TLinearFitter(TFormula *function, Option_t *opt="D");
00222    TLinearFitter(const TLinearFitter& tlf);
00223    virtual ~TLinearFitter();
00224 
00225    TLinearFitter& operator=(const TLinearFitter& tlf);
00226    virtual void       Add(TLinearFitter *tlf);
00227    virtual void       AddPoint(Double_t *x, Double_t y, Double_t e=1);
00228    virtual void       AddTempMatrices();
00229    virtual void       AssignData(Int_t npoints, Int_t xncols, Double_t *x, Double_t *y, Double_t *e=0);
00230 
00231    virtual void       Clear(Option_t *option="");
00232    virtual void       ClearPoints();
00233    virtual void       Chisquare();
00234    virtual Int_t      Eval();
00235    virtual Int_t      EvalRobust(Double_t h=-1);
00236    virtual Int_t      ExecuteCommand(const char *command, Double_t *args, Int_t nargs);
00237    virtual void       FixParameter(Int_t ipar);
00238    virtual void       FixParameter(Int_t ipar, Double_t parvalue);
00239    virtual void       GetAtbVector(TVectorD &v);
00240    virtual Double_t   GetChisquare();
00241    virtual void       GetConfidenceIntervals(Int_t n, Int_t ndim, const Double_t *x, Double_t *ci, Double_t cl=0.95);
00242    virtual void       GetConfidenceIntervals(TObject *obj, Double_t cl=0.95);
00243    virtual Double_t*  GetCovarianceMatrix() const;
00244    virtual void       GetCovarianceMatrix(TMatrixD &matr);
00245    virtual Double_t   GetCovarianceMatrixElement(Int_t i, Int_t j) const {return fParCovar(i, j);}
00246    virtual void       GetDesignMatrix(TMatrixD &matr); 
00247    virtual void       GetErrors(TVectorD &vpar);
00248    virtual Int_t      GetNumberTotalParameters() const {return fNfunctions;}
00249    virtual Int_t      GetNumberFreeParameters() const {return fNfunctions-fNfixed;}
00250    virtual Int_t      GetNpoints() { return fNpoints; }
00251    virtual void       GetParameters(TVectorD &vpar);
00252    virtual Double_t   GetParameter(Int_t ipar) const {return fParams(ipar);}
00253    virtual Int_t      GetParameter(Int_t ipar,char* name,Double_t& value,Double_t& /*verr*/,Double_t& /*vlow*/, Double_t& /*vhigh*/) const;
00254    virtual const char *GetParName(Int_t ipar) const;
00255    virtual Double_t   GetParError(Int_t ipar) const;
00256    virtual Double_t   GetParTValue(Int_t ipar);
00257    virtual Double_t   GetParSignificance(Int_t ipar);
00258    virtual void       GetFitSample(TBits& bits);
00259    virtual Double_t   GetY2() const {return fY2;}
00260    virtual Bool_t     IsFixed(Int_t ipar) const {return fFixedParams[ipar];}
00261    virtual Int_t      Merge(TCollection *list);
00262    virtual void       PrintResults(Int_t level, Double_t amin=0) const;
00263    virtual void       ReleaseParameter(Int_t ipar);
00264    virtual void       SetBasisFunctions(TObjArray * functions); 
00265    virtual void       SetDim(Int_t n);
00266    virtual void       SetFormula(const char* formula);
00267    virtual void       SetFormula(TFormula *function);
00268    virtual void       StoreData(Bool_t store) {fStoreData=store;}
00269 
00270    virtual Bool_t     UpdateMatrix();
00271 
00272    //dummy functions for TVirtualFitter:
00273    virtual Double_t  Chisquare(Int_t /*npar*/, Double_t * /*params*/) const {return 0;}
00274    virtual Int_t     GetErrors(Int_t /*ipar*/,Double_t & /*eplus*/, Double_t & /*eminus*/, Double_t & /*eparab*/, Double_t & /*globcc*/) const {return 0;}
00275 
00276    virtual Int_t     GetStats(Double_t& /*amin*/, Double_t& /*edm*/, Double_t& /*errdef*/, Int_t& /*nvpar*/, Int_t& /*nparx*/) const {return 0;}
00277    virtual Double_t  GetSumLog(Int_t /*i*/) {return 0;}
00278    virtual void      SetFitMethod(const char * /*name*/) {;}
00279    virtual Int_t     SetParameter(Int_t /*ipar*/,const char * /*parname*/,Double_t /*value*/,Double_t /*verr*/,Double_t /*vlow*/, Double_t /*vhigh*/) {return 0;}
00280 
00281    ClassDef(TLinearFitter, 2) //fit a set of data points with a linear combination of functions
00282 };
00283 
00284 #endif

Generated on Tue Jul 5 14:26:59 2011 for ROOT_528-00b_version by  doxygen 1.5.1