aGrUM  0.14.2
Chi2TestPolicy_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 // =========================================================================
28 // =========================================================================
29 
30 
31 namespace gum {
32 
33 
34  // ##########################################################################
35  //
36  // ##########################################################################
37 
38  // ==========================================================================
39  //
40  // ==========================================================================
41  template < typename GUM_SCALAR >
42  void Chi2TestPolicy< GUM_SCALAR >::addObservation(Idx iattr, GUM_SCALAR ivalue) {
44  __conTab.add(iattr, ivalue);
45  }
46 
47 
48  // ############################################################################
49  // @name Test result
50  // ############################################################################
51 
52  // ============================================================================
53  // Computes the GStat of current variable according to the test
54  // ============================================================================
55  template < typename GUM_SCALAR >
58  __chi2Score = 0;
59  for (auto attrIter = __conTab.attrABeginSafe();
60  attrIter != __conTab.attrAEndSafe();
61  ++attrIter) {
62  double semiExpected =
63  (double)(attrIter.val()) / (double)this->nbObservation();
64  for (auto valIter = __conTab.attrBBeginSafe();
65  valIter != __conTab.attrBEndSafe();
66  ++valIter) {
67  double cell = (double)__conTab.joint(attrIter.key(), valIter.key());
68  if (cell < 5) continue;
69  double expected = semiExpected * (double)(valIter.val());
70 
71  __chi2Score += std::pow(cell - expected, 2.0) / expected;
72  }
73  }
74  }
75 
76  // ============================================================================
77  // Returns the performance of current variable according to the test
78  // ============================================================================
79  template < typename GUM_SCALAR >
81  if (this->_isModified()) computeScore();
82  double score =
83  1
85  __chi2Score, (__conTab.attrASize() - 1) * (__conTab.attrBSize() - 1));
86  return score;
87  }
88 
89  // ============================================================================
90  // Returns a second criterion to severe ties
91  // ============================================================================
92  template < typename GUM_SCALAR >
94  if (this->_isModified()) computeScore();
95  return __chi2Score;
96  }
97 
98  template < typename GUM_SCALAR >
101  __conTab += src.ct();
102  }
103 
104 } // End of namespace gum
virtual void computeScore() const
Recomputes the statistic from the beginning.
Definition: ITestPolicy.h:114
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:87
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
<agrum/multidim/core/testPolicy/Chi2TestPolicy.h>
Headers of the Chi2TestPolicy.
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:115
void add(const ITestPolicy< GUM_SCALAR > &src)
Definition: ITestPolicy.h:137
double score() const
Returns the performance of current variable according to the test.
Size Idx
Type for indexes.
Definition: types.h:50