aGrUM  0.14.2
GTestPolicy_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 // =========================================================================
27 #include <agrum/core/math/math.h>
29 // =========================================================================
30 
31 
32 namespace gum {
33 
34 
35  // ##########################################################################
36  //
37  // ##########################################################################
38 
39  // ==========================================================================
40  //
41  // ==========================================================================
42  template < typename GUM_SCALAR >
43  void GTestPolicy< GUM_SCALAR >::addObservation(Idx iattr, GUM_SCALAR ivalue) {
45  __conTab.add(iattr, ivalue);
46  }
47 
48 
49  // ############################################################################
50  // @name Test result
51  // ############################################################################
52 
53  // ============================================================================
54  // Computes the GStat of current variable according to the test
55  // ============================================================================
56  template < typename GUM_SCALAR >
59  __GStat = 0;
60 
61  // Itération sur l'axe 1 du tableau
62  for (auto attrIter = __conTab.attrABeginSafe();
63  attrIter != __conTab.attrAEndSafe();
64  ++attrIter) {
65  // Mise en cache de valeur utile
66  double semiExpected =
67  (double)(attrIter.val()) / (double)this->nbObservation();
68 
69  // Itération sur l'axe 2 du tableau
70  for (auto valIter = __conTab.attrBBeginSafe();
71  valIter != __conTab.attrBEndSafe();
72  ++valIter) {
73  // Récupération de la valeur en cellule
74  Idx cell = __conTab.joint(attrIter.key(), valIter.key());
75  if (cell < 5) continue;
76 
77  // Récupération de la valeur en axe 2
78  double expected = semiExpected * (double)valIter.val();
79 
80  __GStat += 2 * cell * log(cell / expected);
81  }
82  }
83  }
84 
85  // ============================================================================
86  // Returns the performance of current variable according to the test
87  // ============================================================================
88  template < typename GUM_SCALAR >
90  if (this->_isModified()) computeScore();
91  // std::cout << this->toString() << std::endl;
92  double score =
93  1
95  __GStat, (__conTab.attrASize() - 1) * (__conTab.attrBSize() - 1));
96  return score;
97  }
98 
99  // ============================================================================
100  // Returns a second criterion to severe ties
101  // ============================================================================
102  template < typename GUM_SCALAR >
104  if (this->_isModified()) computeScore();
105  return __GStat;
106  }
107 
108  template < typename GUM_SCALAR >
111  __conTab += src.ct();
112  }
113 
114 } // End of namespace gum
Useful macros for maths.
void computeScore() const
Computes the GStat of current variable according to the test.
virtual void computeScore() const
Recomputes the statistic from the beginning.
Definition: ITestPolicy.h:114
void addObservation(Idx iattr, GUM_SCALAR ivalue)
Comptabilizes the new observation.
const ContingencyTable< Idx, GUM_SCALAR > & ct() const
Returns contingency table (needed for the merging of GTestPolicy instances)
Definition: GTestPolicy.h:142
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
Headers of the GTestPolicy.
double secondaryscore() const
Returns a second criterion to severe ties.
static double probaChi2(double x, Size df)
computes the probability of chi2 value (used by the cache)
Definition: chiSquare.cpp:115
<agrum/multidim/core/testPolicies/GTestPolicy.h>
Definition: GTestPolicy.h:50
double score() const
Returns the performance of current variable according to the test.
void add(const ITestPolicy< GUM_SCALAR > &src)
Definition: ITestPolicy.h:137
void add(const GTestPolicy< GUM_SCALAR > &src)
Performs the merging of current GTestPolicy instance with given instance.
Size Idx
Type for indexes.
Definition: types.h:50