FitResult.h

Go to the documentation of this file.
00001 // @(#)root/mathcore:$Id: FitResult.h 37438 2010-12-09 14:14:30Z moneta $
00002 // Author: L. Moneta Wed Aug 30 11:05:34 2006
00003 
00004 /**********************************************************************
00005  *                                                                    *
00006  * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
00007  *                                                                    *
00008  *                                                                    *
00009  **********************************************************************/
00010 
00011 // Header file for class FitResult
00012 
00013 #ifndef ROOT_Fit_FitResult
00014 #define ROOT_Fit_FitResult
00015 
00016 #ifndef ROOT_Fit_IFunctionfwd
00017 #include "Math/IFunctionfwd.h"
00018 #endif
00019 #ifndef ROOT_Fit_IParamFunctionfwd
00020 #include "Math/IParamFunctionfwd.h"
00021 #endif
00022 
00023 #include <vector>
00024 #include <map>
00025 #include <string>
00026 #include <cmath>
00027 #include <cassert>
00028 
00029 namespace ROOT { 
00030 
00031    namespace Math { 
00032       class Minimizer; 
00033    }
00034 
00035 
00036    namespace Fit { 
00037 
00038       class FitConfig; 
00039       class BinData;
00040 
00041 //___________________________________________________________________________________
00042 /** 
00043    class containg the result of the fit and all the related information 
00044    (fitted parameter values, error, covariance matrix and minimizer result information)
00045    Contains a pointer also to the fitted (model) function, modified with the fit parameter values.  
00046    When the fit is valid, it is constructed from a  Minimizer and a model function pointer 
00047 
00048    @ingroup FitMain
00049 */ 
00050 class FitResult {
00051 
00052 public: 
00053 
00054    typedef  ROOT::Math::IParamMultiFunction IModelFunction; 
00055 
00056    /** 
00057       Default constructor for an empty (non valid) fit result
00058    */ 
00059    FitResult (); 
00060 
00061    /**
00062       Construct from a Minimizer instance after fitting
00063       Run also Minos if requested from the configuration
00064     */
00065    FitResult(ROOT::Math::Minimizer & min, const FitConfig & fconfig, const IModelFunction * f, bool isValid, unsigned int sizeOfData = 0, bool binFit = true, const ROOT::Math::IMultiGenFunction * chi2func = 0, unsigned int ncalls = 0);
00066 
00067    /** 
00068       Copy constructor. 
00069    */ 
00070    FitResult(const FitResult &);
00071 
00072    /** 
00073       Assignment operator
00074    */ 
00075    FitResult & operator = (const FitResult & rhs);  
00076 
00077    /** 
00078       Destructor 
00079    */ 
00080    ~FitResult (); 
00081 
00082 
00083 public: 
00084 
00085    /**
00086       Update the fit result with a new minimization status
00087       To be run only if same fit is performed with same configuration 
00088       Note that in this case MINOS is not re-run. If one wants to run also MINOS
00089       a new result must be created 
00090     */
00091    bool Update(const ROOT::Math::Minimizer & min, bool isValid, unsigned int ncalls = 0 );
00092 
00093    /** minimization quantities **/
00094 
00095    /// minimizer type 
00096    const std::string & MinimizerType() const { return fMinimType; } 
00097 
00098    /// True if fit successful, otherwise false.
00099    bool IsValid() const { return fValid; }
00100 
00101    /// True if a fit result does not exist (even invalid) with parameter values 
00102    bool IsEmpty() const { return (fParams.size() == 0);  }
00103  
00104    /// Return value of the objective function (chi2 or likelihood) used in the fit
00105    double MinFcnValue() const { return fVal; } 
00106 
00107    ///Number of function calls to find minimum
00108    unsigned int NCalls() const { return fNCalls; }
00109    
00110    ///Expected distance from minimum 
00111    double Edm() const { return fEdm; }
00112 
00113    ///   get total number of parameters 
00114    unsigned int NTotalParameters() const { return fParams.size(); } 
00115    /// total number of parameters (abbreviation)
00116    unsigned int NPar() const { return NTotalParameters(); }
00117    
00118    /// get total number of free parameters
00119    unsigned int NFreeParameters() const { return fNFree; }
00120 
00121    /// minimizer status code 
00122    int Status() const { return fStatus; } 
00123 
00124    ///covariance matrix status code
00125    /// using Minuit convention : =0 not calculated, =1 approximated, =2 made pos def , =3 accurate
00126 
00127    int CovMatrixStatus() const { return fCovStatus; }
00128  
00129    /** fitting quantities **/
00130 
00131    /// Return pointer to model (fit) function with fitted parameter values.
00132    const IModelFunction * FittedFunction() const { return fFitFunc; }
00133 
00134    /// Chi2 fit value
00135    /// in case of likelihood must be computed ? 
00136    double Chi2() const { return fChi2; } 
00137 
00138    /// Number of degree of freedom
00139    unsigned int Ndf() const { return fNdf; } 
00140 
00141    /// p value of the fit (chi2 probability)
00142    double Prob() const;  
00143 
00144    /// parameter errors (return st::vector) 
00145    const std::vector<double> & Errors() const { return fErrors; }
00146    /// parameter errors (return const pointer)
00147    const double * GetErrors() const { return (fErrors.empty()) ? 0 : &fErrors.front(); }
00148 
00149    /// parameter values (return std::vector)
00150    const std::vector<double> & Parameters() const { return fParams; }
00151    /// parameter values (return const pointer)
00152    const double * GetParams() const { return &fParams.front(); }
00153 
00154    /// parameter value by index
00155    double Value(unsigned int i) const { return fParams[i]; }
00156    /// parameter value by index 
00157    double Parameter(unsigned int i) const { return fParams[i]; }
00158 
00159    /// parameter error by index 
00160    // (NOTE: this due to conflict with TObject::Error cannot used in derived class which 
00161    // inherits from TObject. Use instead ParError (or Errors()[i] )
00162    double Error(unsigned int i) const { 
00163       return (i < fErrors.size() ) ? fErrors[i] : 0; 
00164    } 
00165    /// parameter error by index 
00166    double ParError(unsigned int i) const {
00167       return (i < fErrors.size() ) ? fErrors[i] : 0; 
00168    }
00169 
00170    /// name of the parameter
00171    std::string ParName(unsigned int i) const; 
00172 
00173    /// set the Minos errors for parameter i (called by the Fitter class when running Minos)
00174    void SetMinosError(unsigned int i, double elow, double eup);
00175 
00176    /// lower Minos error. If Minos has not run for parameter i return the parabolic error 
00177    double LowerError(unsigned int i) const;
00178 
00179    /// upper Minos error. If Minos has not run for parameter i return the parabolic error 
00180    double UpperError(unsigned int i) const;
00181 
00182    /// parameter global correlation coefficient 
00183    double GlobalCC(unsigned int i) const { 
00184       return (i < fGlobalCC.size() ) ? fGlobalCC[i] : -1; 
00185    } 
00186 
00187 
00188    /// retrieve covariance matrix element 
00189    double CovMatrix (unsigned int i, unsigned int j) const { 
00190       if ( i >= fErrors.size() || j >= fErrors.size() ) return 0; 
00191       if (fCovMatrix.size() == 0) return 0; // no matrix is available in case of non-valid fits
00192       if ( j < i ) 
00193          return fCovMatrix[j + i* (i+1) / 2];
00194       else 
00195          return fCovMatrix[i + j* (j+1) / 2];
00196    }
00197 
00198    /// retrieve correlation elements 
00199    double Correlation(unsigned int i, unsigned int j ) const { 
00200       if ( i >= fErrors.size() || j >= fErrors.size() ) return 0; 
00201       if (fCovMatrix.size() == 0) return 0; // no matrix is available in case of non-valid fits
00202       double tmp = CovMatrix(i,i)*CovMatrix(j,j); 
00203       return ( tmp > 0) ? CovMatrix(i,j)/ std::sqrt(tmp) : 0; 
00204    }
00205    
00206    /// fill covariance matrix elements using a generic matrix class implementing operator(i,j)
00207    /// the matrix must be previously allocates with right size (npar * npar) 
00208    template<class Matrix> 
00209    void GetCovarianceMatrix(Matrix & mat) const { 
00210       unsigned int npar = fErrors.size();
00211       if (fCovMatrix.size() != npar*(npar+1)/2 ) return; // do nothing 
00212       for (unsigned int i = 0; i< npar; ++i) { 
00213          for (unsigned int j = 0; j<=i; ++j) { 
00214             mat(i,j) = fCovMatrix[j + i*(i+1)/2 ];
00215             if (i != j) mat(j,i) = mat(i,j);  
00216          }
00217       }
00218    }
00219 
00220    /// fill a correlation matrix elements using a generic symmetric matrix class implementing operator(i,j)
00221    /// the matrix must be previously allocates with right size (npar * npar) 
00222    template<class Matrix> 
00223    void GetCorrelationMatrix(Matrix & mat) const { 
00224       unsigned int npar = fErrors.size(); 
00225       if (fCovMatrix.size() != npar*(npar+1)/2) return; // do nothing
00226       for (unsigned int i = 0; i< npar; ++i) { 
00227          for (unsigned int j = 0; j<=i; ++j) { 
00228             double tmp = fCovMatrix[i * (i +3)/2 ] * fCovMatrix[ j * (j+3)/2 ]; 
00229             mat(i,j) = (tmp > 0) ? fCovMatrix[j + i*(i+1)/2 ] / std::sqrt(tmp) : 0; 
00230             if (i != j) mat(j,i) = mat(i,j); 
00231          }
00232       }
00233    }
00234 
00235    /**
00236       get confidence intervals for an array of n points x. 
00237       stride1 indicates the stride in the coordinate space while stride2 the stride in dimension space. 
00238       For 1-dim points : stride1=1, stride2=1
00239       for multi-dim points arranged as (x0,x1,...,xN,y0,....yN)          stride1=1      stride2=n
00240       for multi-dim points arraged  as (x0,y0,..,x1,y1,...,xN,yN,..)     stride1=ndim,  stride2=1
00241       
00242       the confidence interval are returned in the array ci
00243       cl is the desired confidedence interval value
00244       norm is a flag to control if the intervals need to be normalized to the chi2/ndf value
00245       By default the intervals are corrected using the chi2/ndf value of the fit if a chi2 fit is performed
00246     */
00247    void GetConfidenceIntervals(unsigned int n, unsigned int stride1, unsigned int stride2, const double * x,  double * ci, double cl=0.95, bool norm = true ) const;     
00248 
00249    /**
00250       evaluate confidence interval for the point specified in the passed data sets
00251       the confidence interval are returned in the array ci
00252       cl is the desired confidence interval value
00253     */
00254    void GetConfidenceIntervals(const BinData & data, double * ci, double cl=0.95, bool norm = true ) const;
00255 
00256 
00257    /// get index for parameter name (return -1 if not found)
00258    int Index(const std::string & name) const; 
00259 
00260 
00261    ///normalize errors using chi2/ndf for chi2 fits
00262    void NormalizeErrors();
00263 
00264    /// flag to chek if errors are normalized
00265    bool NormalizedErrors() { return fNormalized; }
00266 
00267    /// print the result and optionaly covariance matrix and correlations
00268    void Print(std::ostream & os, bool covmat = false) const;
00269 
00270    ///print error matrix and correlations
00271    void PrintCovMatrix(std::ostream & os) const; 
00272 
00273    /// query if a parameter is bound 
00274    bool IsParameterBound(unsigned int ipar) const; 
00275 
00276    /// query if a parameter is fixed 
00277    bool IsParameterFixed(unsigned int ipar) const; 
00278 
00279    /// get name of parameter (deprecated)
00280    std::string GetParameterName(unsigned int ipar) const { 
00281       return ParName(ipar);
00282    }
00283 
00284 
00285 protected: 
00286 
00287 
00288    /// Return pointer non const pointer to model (fit) function with fitted parameter values.
00289    /// used by Fitter class 
00290    IModelFunction * ModelFunction()  { return fFitFunc; }
00291    void SetModelFunction(IModelFunction * func) { fFitFunc = func; }
00292 
00293    friend class Fitter; 
00294 
00295 
00296    bool fValid;             // flag for indicating valid fit
00297    bool fNormalized;        // flag for indicating is errors are normalized
00298    unsigned int fNFree;     // number of fit free parameters (total parameters are in size of parameter vector)  
00299    unsigned int fNdf;       // number of degree of freedom
00300    unsigned int fNCalls;    // number of function calls
00301    int fStatus;             // minimizer status code
00302    int fCovStatus;          // covariance matrix status code
00303    double fVal;             // minimum function value
00304    double fEdm;             // expected distance from mimimum
00305    double fChi2;            // fit chi2 value (different than fval in case of chi2 fits)
00306    IModelFunction * fFitFunc; //! model function resulting  from the fit. It is given by Fitter but it is managed by FitResult
00307    std::vector<unsigned int>   fFixedParams; // list of fixed parameters
00308    std::vector<unsigned int>   fBoundParams; // list of limited parameters
00309    std::vector<double>         fParams;  // parameter values. Size is total number of parameters
00310    std::vector<double>         fErrors;  // errors 
00311    std::vector<double>         fCovMatrix;  // covariance matrix (size is npar*(npar+1)/2) where npar is total parameters
00312    std::vector<double>         fGlobalCC;   // global Correlation coefficient
00313    std::map<unsigned int, std::pair<double,double> > fMinosErrors;   // map contains the two Minos errors
00314    std::string fMinimType;              // string indicating type of minimizer
00315    std::vector<std::string> fParNames;  // parameter names (only with FCN only fits, when fFitFunc=0)
00316 
00317 }; 
00318 
00319    } // end namespace Fit
00320 
00321 } // end namespace ROOT
00322 
00323 
00324 #endif /* ROOT_Fit_FitResult */

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