GeneticAlgoithm
Implementation of the genetic algorithm
Chi2FitFigureOfMerit.cxx
1 #include "Chi2FitFigureOfMerit.h"
2 
3 #include "ParametricModel.h"
4 
5 #include <stdexcept>
6 
7 
10 {
11  setAcceptThreshold(0.1);
12 }
13 
15 {
16 }
17 
18 /**
19  * @param x \f$\vec{x}\f$ coordinate.
20  * @param y \f$y\f$ coordinate.
21  * @param ey \f$\sigma_y\f$ error on \f$y\f$ coordinate.
22  */
23 void Chi2FitFigureOfMerit::addData(const std::vector<double> &x, double y, double ey)
24 {
25  m_x.push_back(x);
26  m_y.push_back(y);
27  m_ey.push_back(ey);
28 }
29 
31 {
32  m_x.clear();
33  m_y.clear();
34  m_ey.clear();
35 }
36 
37 /**
38  * @param imodel Model to be evaluated.
39  * @return \f$\chi^2/ndf\f$ score.
40  *
41  * The \f$\chi^2/ndf\f$ score is then defined as:
42  * \f[
43  * \chi^2/ndf = \frac{1}{N}\sum_{i=0}^N \frac{(y_i - f(\vec{x_i}))^2}{\sigma_{y_i}^2}
44  * \f]
45  */
47 {
48 
49  ParametricModel *model = dynamic_cast<ParametricModel*>(imodel);
50  if(!model) {
51  throw std::runtime_error("Given model is not a parametric model");
52  }
53 
54  if(m_x.size() == 0) return 0;
55 
56  double chi2 = 0;
57  int ndf = 0;
58  for(unsigned int i=0; i<m_x.size(); i++) {
59  double y = m_y[i];
60  if(y == 0) continue;
61  const std::vector<double> &x = m_x[i];
62  double ey = m_ey[i];
63  double fx = model->getFormula()->EvalPar(x.data());
64  chi2 += (fx - y)*(fx - y)/(ey*ey);
65  ndf++;
66  }
67 
68  return chi2 / ndf;
69 }
70 
71 /**
72  * @param scoreToTest Score value to be tested.
73  * @param referenceScore Score value to be compared to.
74  * @return true if scoreToTest < referenceScore
75  */
76 bool Chi2FitFigureOfMerit::isBetterThan(double scoreToTest, double referenceScore) const
77 {
78  return scoreToTest < referenceScore;
79 }
void setAcceptThreshold(double acceptThreshold)
std::vector< std::vector< double > > m_x
Stores the coordinates.
bool isBetterThan(double scoreToTest, double referenceScore) const
std::vector< double > m_ey
Stores the errors.
void addData(const std::vector< double > &x, double y, double ey)
double evaluate(IModel *model) const
Abstract class representing a figure of merit.
std::vector< double > m_y
Stores the coordinates.
Class representing a model defined by a parametric function.
Abstract class describing the interface for a model.
Definition: IModel.h:13