Logo ROOT  
Reference Guide
FumiliStandardChi2FCN.cxx
Go to the documentation of this file.
1 // @(#)root/minuit2:$Id$
2 // Authors: M. Winkler, F. James, L. Moneta, A. Zsenei 2003-2005
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2005 LCG ROOT Math team, CERN/PH-SFT *
7  * *
8  **********************************************************************/
9 
11 
12 #include <vector>
13 #include <cmath>
14 
15 namespace ROOT {
16 
17 namespace Minuit2 {
18 
19 std::vector<double> FumiliStandardChi2FCN::Elements(const std::vector<double> &par) const
20 {
21  // Calculate the f(i) contribution to the Chi2. Chi2 = Sum[f(i)**2]
22 
23  std::vector<double> result;
24  double tmp1 = 0.0;
25  unsigned int fPositionsSize = fPositions.size();
26 
27  for (unsigned int i = 0; i < fPositionsSize; i++) {
28 
29  const std::vector<double> &currentPosition = fPositions[i];
30 
31  // The commented line is the object-oriented way to do it
32  // but it is faster to do a single function call...
33  //(*(this->getModelFunction())).SetParameters(par);
34  tmp1 = (*(this->ModelFunction()))(par, currentPosition) - fMeasurements[i];
35 
36  result.push_back(tmp1 * fInvErrors[i]);
37 
38  // std::cout << "element " << i << " " << (*(this->getModelFunction()))(par, currentPosition) << " " <<
39  // fMeasurements[i] << " " << result[i] << std::endl;
40  }
41 
42  return result;
43 }
44 
45 const std::vector<double> &FumiliStandardChi2FCN::GetMeasurement(int index) const
46 {
47  // Return the coordinate (position) values.
48  return fPositions[index];
49 }
50 
52 {
53  // Return size
54  return fPositions.size();
55 }
56 
57 void FumiliStandardChi2FCN::EvaluateAll(const std::vector<double> &par)
58 {
59  // Evaluate chi2 value, gradient and hessian all in a single
60  // loop on the measurements
61 
62  int nmeas = GetNumberOfMeasurements();
63  std::vector<double> &grad = Gradient();
64  std::vector<double> &h = Hessian();
65  int npar = par.size();
66  double chi2 = 0;
67  grad.resize(npar);
68  h.resize(static_cast<unsigned int>(0.5 * npar * (npar + 1)));
69  // reset Elements
70  grad.assign(npar, 0.0);
71  h.assign(static_cast<unsigned int>(0.5 * npar * (npar + 1)), 0.0);
72 
73  const ParametricFunction &modelFunc = *ModelFunction();
74 
75  for (int i = 0; i < nmeas; ++i) {
76 
77  // work for multi-dimensional points
78  const std::vector<double> &currentPosition = fPositions[i];
79  modelFunc.SetParameters(currentPosition);
80  double invError = fInvErrors[i];
81  double fval = modelFunc(par);
82 
83  double element = (fval - fMeasurements[i]) * invError;
84  chi2 += element * element;
85 
86  // calc derivatives
87 
88  // this method should return a reference
89  std::vector<double> mfg = modelFunc.GetGradient(par);
90 
91  // grad is derivative of chi2 w.r.t to parameters
92  for (int j = 0; j < npar; ++j) {
93  double dfj = invError * mfg[j];
94  grad[j] += 2.0 * element * dfj;
95 
96  // in second derivative use Fumili approximation neglecting the term containing the
97  // second derivatives of the model function
98  for (int k = j; k < npar; ++k) {
99  int idx = j + k * (k + 1) / 2;
100  h[idx] += 2.0 * dfj * invError * mfg[k];
101  }
102 
103  } // end param loop
104 
105  } // end points loop
106 
107  // set Value in base class
108  SetFCNValue(chi2);
109 }
110 
111 } // namespace Minuit2
112 
113 } // namespace ROOT
ROOT::Minuit2::FumiliFCNBase::SetFCNValue
void SetFCNValue(double value)
Definition: FumiliFCNBase.h:135
ROOT::Minuit2::FumiliStandardChi2FCN::EvaluateAll
virtual void EvaluateAll(const std::vector< double > &par)
Evaluate function Value, Gradient and Hessian using Fumili approximation, for values of parameters p ...
Definition: FumiliStandardChi2FCN.cxx:57
ROOT::Minuit2::FumiliStandardChi2FCN::fMeasurements
std::vector< double > fMeasurements
Definition: FumiliStandardChi2FCN.h:179
ROOT::Minuit2::ParametricFunction::GetGradient
virtual std::vector< double > GetGradient(const std::vector< double > &x) const
Member function returning the Gradient of the function with respect to its variables (but without inc...
Definition: ParametricFunction.cxx:24
ROOT::Minuit2::ParametricFunction::SetParameters
virtual void SetParameters(const std::vector< double > &params) const
Sets the parameters of the ParametricFunction.
Definition: ParametricFunction.h:78
ROOT::Minuit2::FumiliFCNBase::Gradient
virtual const std::vector< double > & Gradient() const
Return cached Value of function Gradient estimated previously using the FumiliFCNBase::EvaluateAll me...
Definition: FumiliFCNBase.h:98
ROOT::Minuit2::ParametricFunction
Function which has parameters.
Definition: ParametricFunction.h:43
ROOT::Minuit2::FumiliChi2FCN::ModelFunction
const ParametricFunction * ModelFunction() const
Returns the model function used for the data.
Definition: FumiliChi2FCN.h:74
h
#define h(i)
Definition: RSha256.hxx:106
ROOT::Minuit2::FumiliStandardChi2FCN::fInvErrors
std::vector< double > fInvErrors
Definition: FumiliStandardChi2FCN.h:182
ROOT::Minuit2::FumiliStandardChi2FCN::GetMeasurement
virtual const std::vector< double > & GetMeasurement(int Index) const
Accessor to the position of the measurement (x coordinate).
Definition: FumiliStandardChi2FCN.cxx:45
ROOT::Minuit2::FumiliFCNBase::Hessian
std::vector< double > & Hessian()
Definition: FumiliFCNBase.h:139
ROOT::Minuit2::FumiliStandardChi2FCN::fPositions
std::vector< std::vector< double > > fPositions
Definition: FumiliStandardChi2FCN.h:181
ROOT::Minuit2::FumiliStandardChi2FCN::Elements
std::vector< double > Elements(const std::vector< double > &par) const
Evaluates the model function for the different measurement points and the Parameter values supplied,...
Definition: FumiliStandardChi2FCN.cxx:19
FumiliStandardChi2FCN.h
ROOT
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: EExecutionPolicy.hxx:4
ROOT::Minuit2::FumiliStandardChi2FCN::GetNumberOfMeasurements
virtual int GetNumberOfMeasurements() const
Accessor to the number of measurements used for calculating the chi-square.
Definition: FumiliStandardChi2FCN.cxx:51