aGrUM  0.16.0
Chi2TestPolicy_tpl.h
Go to the documentation of this file.
1 
29 // =========================================================================
31 // =========================================================================
32 
33 
34 namespace gum {
35 
36 
37  // ##########################################################################
38  //
39  // ##########################################################################
40 
41  // ==========================================================================
42  //
43  // ==========================================================================
44  template < typename GUM_SCALAR >
45  void Chi2TestPolicy< GUM_SCALAR >::addObservation(Idx iattr, GUM_SCALAR ivalue) {
47  __conTab.add(iattr, ivalue);
48  }
49 
50 
51  // ############################################################################
52  // @name Test result
53  // ############################################################################
54 
55  // ============================================================================
56  // Computes the GStat of current variable according to the test
57  // ============================================================================
58  template < typename GUM_SCALAR >
61  __chi2Score = 0;
62  for (auto attrIter = __conTab.attrABeginSafe();
63  attrIter != __conTab.attrAEndSafe();
64  ++attrIter) {
65  double semiExpected =
66  (double)(attrIter.val()) / (double)this->nbObservation();
67  for (auto valIter = __conTab.attrBBeginSafe();
68  valIter != __conTab.attrBEndSafe();
69  ++valIter) {
70  double cell = (double)__conTab.joint(attrIter.key(), valIter.key());
71  if (cell < 5) continue;
72  double expected = semiExpected * (double)(valIter.val());
73 
74  __chi2Score += std::pow(cell - expected, 2.0) / expected;
75  }
76  }
77  }
78 
79  // ============================================================================
80  // Returns the performance of current variable according to the test
81  // ============================================================================
82  template < typename GUM_SCALAR >
84  if (this->_isModified()) computeScore();
85  double score =
86  1
88  __chi2Score, (__conTab.attrASize() - 1) * (__conTab.attrBSize() - 1));
89  return score;
90  }
91 
92  // ============================================================================
93  // Returns a second criterion to severe ties
94  // ============================================================================
95  template < typename GUM_SCALAR >
97  if (this->_isModified()) computeScore();
98  return __chi2Score;
99  }
100 
101  template < typename GUM_SCALAR >
104  __conTab += src.ct();
105  }
106 
107 } // End of namespace gum
virtual void computeScore() const
Recomputes the statistic from the beginning.
Definition: ITestPolicy.h:117
void addObservation(Idx attr, GUM_SCALAR value)
Comptabilizes the new observation.
virtual void addObservation(Idx attr, GUM_SCALAR value)
Comptabilizes the new observation.
Definition: ITestPolicy.h:90
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
<agrum/multidim/core/testPolicy/Chi2TestPolicy.h>
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const ContingencyTable< Idx, GUM_SCALAR > & ct() const
virtual double secondaryscore() const
Returns a second criterion to severe ties.
void add(const Chi2TestPolicy< GUM_SCALAR > &src)
void computeScore() const
Recomputes the statistic from the beginning.
static double probaChi2(double x, Size df)
computes the probability of chi2 value (used by the cache)
Definition: chiSquare.cpp:118
void add(const ITestPolicy< GUM_SCALAR > &src)
Definition: ITestPolicy.h:140
double score() const
Returns the performance of current variable according to the test.
Size Idx
Type for indexes.
Definition: types.h:53