| 23 |
#include <vector> |
#include <vector> |
| 24 |
#include <string> |
#include <string> |
| 25 |
#include <cmath> |
#include <cmath> |
| 26 |
|
#include <cassert> |
| 27 |
|
|
| 28 |
namespace ROOT { |
namespace ROOT { |
| 29 |
|
|
| 35 |
namespace Fit { |
namespace Fit { |
| 36 |
|
|
| 37 |
class FitConfig; |
class FitConfig; |
| 38 |
|
class BinData; |
| 39 |
|
|
| 40 |
//___________________________________________________________________________________ |
//___________________________________________________________________________________ |
| 41 |
/** |
/** |
| 60 |
/** |
/** |
| 61 |
Construct from a Minimizer instance |
Construct from a Minimizer instance |
| 62 |
*/ |
*/ |
| 63 |
FitResult(ROOT::Math::Minimizer & min, const FitConfig & fconfig, const IModelFunction & f, bool isValid, unsigned int sizeOfData = 0, const ROOT::Math::IMultiGenFunction * chi2func = 0, bool minosErr = false, unsigned int ncalls = 0); |
FitResult(ROOT::Math::Minimizer & min, const FitConfig & fconfig, IModelFunction & f, bool isValid, unsigned int sizeOfData = 0, const ROOT::Math::IMultiGenFunction * chi2func = 0, bool minosErr = false, unsigned int ncalls = 0); |
| 64 |
|
|
| 65 |
// use default copy constructor and assignment operator |
/** |
| 66 |
|
Copy constructor. |
| 67 |
|
*/ |
| 68 |
|
FitResult(const FitResult &); |
| 69 |
|
|
| 70 |
|
/** |
| 71 |
|
Assignment operator |
| 72 |
|
*/ |
| 73 |
|
FitResult & operator = (const FitResult & rhs); |
| 74 |
|
|
| 75 |
/** |
/** |
| 76 |
Destructor (no operations) |
Destructor (no operations) |
| 89 |
/// True if fit successful, otherwise false. |
/// True if fit successful, otherwise false. |
| 90 |
bool IsValid() const { return fValid; } |
bool IsValid() const { return fValid; } |
| 91 |
|
|
| 92 |
|
/// True if a fit result does not exist (even invalid) with parameter values |
| 93 |
|
bool IsEmpty() const { return (fParams.size() == 0); } |
| 94 |
|
|
| 95 |
/// Return value of the objective function (chi2 or likelihood) used in the fit |
/// Return value of the objective function (chi2 or likelihood) used in the fit |
| 96 |
double MinFcnValue() const { return fVal; } |
double MinFcnValue() const { return fVal; } |
| 97 |
|
|
| 101 |
///Expected distance from minimum |
///Expected distance from minimum |
| 102 |
double Edm() const { return fEdm; } |
double Edm() const { return fEdm; } |
| 103 |
|
|
| 104 |
|
/// get total number of parameters |
| 105 |
|
unsigned int NTotalParameters() const { return fParams.size(); } |
| 106 |
|
|
| 107 |
|
/// get total number of free parameters |
| 108 |
|
unsigned int NFreeParameters() const { return fNFree; } |
| 109 |
|
|
| 110 |
|
/// minimizer status code |
| 111 |
|
int Status() const { return fStatus; } |
| 112 |
|
|
| 113 |
/** fitting quantities **/ |
/** fitting quantities **/ |
| 114 |
|
|
| 115 |
/// Return pointer to model (fit) function with fitted parameter values. |
/// Return pointer to model (fit) function with fitted parameter values. |
| 125 |
/// p value of the fit (chi2 probability) |
/// p value of the fit (chi2 probability) |
| 126 |
double Prob() const; |
double Prob() const; |
| 127 |
|
|
|
|
|
| 128 |
/// parameter errors |
/// parameter errors |
| 129 |
const std::vector<double> & Errors() const { return fErrors; } |
const std::vector<double> & Errors() const { return fErrors; } |
| 130 |
|
|
| 135 |
double Value(unsigned int i) const { return fParams[i]; } |
double Value(unsigned int i) const { return fParams[i]; } |
| 136 |
|
|
| 137 |
/// parameter error by index |
/// parameter error by index |
| 138 |
double Error(unsigned int i) const { return fErrors[i]; } |
double Error(unsigned int i) const { |
| 139 |
|
return (i < fErrors.size() ) ? fErrors[i] : 0; |
| 140 |
|
} |
| 141 |
|
|
| 142 |
// /// Minos Errors |
// /// Minos Errors |
| 143 |
// const std::vector<std::pair<double, double> > MinosErrors() const; |
// const std::vector<std::pair<double, double> > MinosErrors() const; |
| 144 |
|
|
| 145 |
/// lower Minos error |
/// lower Minos error |
| 146 |
double LowerError(unsigned int i) const { return fMinosErrors[i].first; } |
double LowerError(unsigned int i) const { |
| 147 |
|
return (i < fMinosErrors.size() ) ? fMinosErrors[i].first : fErrors[i]; |
| 148 |
|
} |
| 149 |
|
|
| 150 |
/// upper Minos error |
/// upper Minos error |
| 151 |
double UpperError(unsigned int i) const { return fMinosErrors[i].second; } |
double UpperError(unsigned int i) const { |
| 152 |
|
return (i < fMinosErrors.size() ) ? fMinosErrors[i].second : fErrors[i]; |
| 153 |
|
} |
| 154 |
|
|
| 155 |
|
/// parameter global correlation coefficient |
| 156 |
|
double GlobalCC(unsigned int i) const { |
| 157 |
|
return (i < fGlobalCC.size() ) ? fGlobalCC[i] : -1; |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
|
| 161 |
/// retrieve covariance matrix element |
/// retrieve covariance matrix element |
| 162 |
double CovMatrix (unsigned int i, unsigned int j) const { |
double CovMatrix (unsigned int i, unsigned int j) const { |
| 173 |
if ( i >= fErrors.size() || j >= fErrors.size() ) return 0; |
if ( i >= fErrors.size() || j >= fErrors.size() ) return 0; |
| 174 |
if (fCovMatrix.size() == 0) return 0; // no matrix is available in case of non-valid fits |
if (fCovMatrix.size() == 0) return 0; // no matrix is available in case of non-valid fits |
| 175 |
double tmp = CovMatrix(i,i)*CovMatrix(j,j); |
double tmp = CovMatrix(i,i)*CovMatrix(j,j); |
| 176 |
return ( tmp < 0) ? 0 : CovMatrix(i,j)/ std::sqrt(tmp); |
return ( tmp > 0) ? CovMatrix(i,j)/ std::sqrt(tmp) : 0; |
| 177 |
} |
} |
| 178 |
|
|
| 179 |
/// fill covariance matrix elements using a generic symmetric matrix class implementing operator(i,j) |
/// fill covariance matrix elements using a generic symmetric matrix class implementing operator(i,j) |
| 181 |
template<class Matrix> |
template<class Matrix> |
| 182 |
void GetCovarianceMatrix(Matrix & mat) { |
void GetCovarianceMatrix(Matrix & mat) { |
| 183 |
int npar = fErrors.size(); |
int npar = fErrors.size(); |
| 184 |
|
assert(fCovMatrix.size() == npar*(npar+1)/2); |
| 185 |
for (int i = 0; i< npar; ++i) { |
for (int i = 0; i< npar; ++i) { |
| 186 |
for (int j = 0; j<=i; ++i) { |
for (int j = 0; j<=i; ++i) { |
| 187 |
mat(i,j) = fCovMatrix[j + i*(i+1)/2 ]; |
mat(i,j) = fCovMatrix[j + i*(i+1)/2 ]; |
| 194 |
template<class Matrix> |
template<class Matrix> |
| 195 |
void GetCorrelationMatrix(Matrix & mat) { |
void GetCorrelationMatrix(Matrix & mat) { |
| 196 |
int npar = fErrors.size(); |
int npar = fErrors.size(); |
| 197 |
|
assert(fCovMatrix.size() == npar*(npar+1)/2); |
| 198 |
for (int i = 0; i< npar; ++i) { |
for (int i = 0; i< npar; ++i) { |
| 199 |
for (int j = 0; j<=i; ++i) { |
for (int j = 0; j<=i; ++i) { |
| 200 |
double tmp = fCovMatrix[i * (i +3)/2 ] * fCovMatrix[ j * (j+3)/2 ]; |
double tmp = fCovMatrix[i * (i +3)/2 ] * fCovMatrix[ j * (j+3)/2 ]; |
| 206 |
} |
} |
| 207 |
} |
} |
| 208 |
|
|
| 209 |
|
/** |
| 210 |
|
get confidence intervals for an array of n points x. |
| 211 |
|
stride1 indicates the stride in the coordinate space while stride2 the stride in dimension space. |
| 212 |
|
For 1-dim points : stride1=1, stride2=1 |
| 213 |
|
for multi-dim points arranged as (x0,x1,...,xN,y0,....yN) stride1=1 stride2=n |
| 214 |
|
for multi-dim points arraged as (x0,y0,..,x1,y1,...,xN,yN,..) stride1=ndim, stride2=1 |
| 215 |
|
|
| 216 |
|
the confidence interval are returned in the array ci |
| 217 |
|
cl is the desired confidedence interval value |
| 218 |
|
|
| 219 |
|
*/ |
| 220 |
|
void GetConfidenceIntervals(unsigned int n, unsigned int stride1, unsigned int stride2, const double * x, double * ci, double cl=0.95 ) const; |
| 221 |
|
|
| 222 |
|
/** |
| 223 |
|
evaluate confidence interval for the point specified in the passed data sets |
| 224 |
|
the confidence interval are returned in the array ci |
| 225 |
|
cl is the desired confidence interval value |
| 226 |
|
*/ |
| 227 |
|
void GetConfidenceIntervals(const BinData & data, double * ci, double cl=0.95 ) const; |
| 228 |
|
|
| 229 |
|
|
| 230 |
/// get index for parameter name (return -1 if not found) |
/// get index for parameter name (return -1 if not found) |
| 231 |
int Index(const std::string & name) const; |
int Index(const std::string & name) const; |
| 237 |
/// flag to chek if errors are normalized |
/// flag to chek if errors are normalized |
| 238 |
bool NormalizedErrors() { return fNormalized; } |
bool NormalizedErrors() { return fNormalized; } |
| 239 |
|
|
| 240 |
|
/// get confidence level given an array of x data points |
| 241 |
|
|
| 242 |
|
|
| 243 |
/// print the result and optionaly covariance matrix and correlations |
/// print the result and optionaly covariance matrix and correlations |
| 244 |
void Print(std::ostream & os, bool covmat = false) const; |
void Print(std::ostream & os, bool covmat = false) const; |
| 246 |
///print error matrix and correlations |
///print error matrix and correlations |
| 247 |
void PrintCovMatrix(std::ostream & os) const; |
void PrintCovMatrix(std::ostream & os) const; |
| 248 |
|
|
| 249 |
|
/// query if a parameter is bound |
| 250 |
|
bool IsParameterBound(unsigned int ipar) const; |
| 251 |
|
|
| 252 |
|
/// query if a parameter is fixed |
| 253 |
|
bool IsParameterFixed(unsigned int ipar) const; |
| 254 |
|
|
| 255 |
protected: |
protected: |
| 256 |
|
|
| 257 |
|
|
| 258 |
private: |
private: |
| 259 |
|
|
| 260 |
bool fValid; |
|
| 261 |
bool fNormalized; |
/// Return pointer non const pointer to model (fit) function with fitted parameter values. |
| 262 |
double fVal; |
/// used by Fitter class |
| 263 |
double fEdm; |
IModelFunction * ModelFunction() { return fFitFunc; } |
| 264 |
double fChi2; |
void SetModelFunction(IModelFunction * func) { fFitFunc = func; } |
| 265 |
std::vector<double> fCov; |
|
| 266 |
unsigned int fNdf; |
friend class Fitter; |
| 267 |
unsigned int fNCalls; |
|
| 268 |
std::vector<double> fParams; |
|
| 269 |
std::vector<double> fErrors; |
bool fValid; // flag for indicating valid fit |
| 270 |
std::vector<double> fCovMatrix; |
bool fNormalized; // flag for indicating is errors are normalized |
| 271 |
std::vector<std::pair<double,double> > fMinosErrors; |
unsigned int fNFree; // number of fit free parameters (total parameters are in size of parameter vector) |
| 272 |
|
unsigned int fNdf; // number of degree of freedom |
| 273 |
unsigned int fDataSize; |
unsigned int fNCalls; // number of function calls |
| 274 |
const IModelFunction * fFitFunc; |
int fStatus; // minimizer status code |
| 275 |
std::string fMinimType; |
double fVal; // minimum function value |
| 276 |
|
double fEdm; // expected distance from mimimum |
| 277 |
|
double fChi2; // fit chi2 value (different than fval in case of chi2 fits) |
| 278 |
|
IModelFunction * fFitFunc; // model function result of the fit. It is given by Fitter but it is managed by FitResult |
| 279 |
|
std::vector<unsigned int> fFixedParams; // list of fixed parameters |
| 280 |
|
std::vector<unsigned int> fBoundParams; // list of limited parameters |
| 281 |
|
std::vector<double> fParams; // parameter values. Size is total number of parameters |
| 282 |
|
std::vector<double> fErrors; // errors |
| 283 |
|
std::vector<double> fCovMatrix; // covariance matrix (size is npar*(npar+1)/2) where npar is total parameters |
| 284 |
|
std::vector<double> fGlobalCC; // global Correlation coefficient |
| 285 |
|
std::vector<std::pair<double,double> > fMinosErrors; // vector contains the two Minos errors |
| 286 |
|
std::string fMinimType; // string indicating type of minimizer |
| 287 |
|
|
| 288 |
|
|
| 289 |
}; |
}; |
| 290 |
|
|