Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
15namespace ROOT {
16
17namespace Minuit2 {
18
19std::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
45const 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
57void 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
#define h(i)
Definition RSha256.hxx:106
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
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
const ParametricFunction * ModelFunction() const
Returns the model function used for the data.
std::vector< double > & Hessian()
virtual const std::vector< double > & Gradient() const
Return cached Value of function Gradient estimated previously using the FumiliFCNBase::EvaluateAll me...
void SetFCNValue(double value)
const std::vector< double > & GetMeasurement(int Index) const override
Accessor to the position of the measurement (x coordinate).
int GetNumberOfMeasurements() const override
Accessor to the number of measurements used for calculating the chi-square.
std::vector< std::vector< double > > fPositions
std::vector< double > Elements(const std::vector< double > &par) const override
Evaluates the model function for the different measurement points and the Parameter values supplied,...
void EvaluateAll(const std::vector< double > &par) override
Evaluate function Value, Gradient and Hessian using Fumili approximation, for values of parameters p ...
Function which has parameters.
virtual void SetParameters(const std::vector< double > &params) const
Sets the parameters of the ParametricFunction.
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...
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...