aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
Chi2TestPolicy_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Template implementations for the Chi2TestPolicy class.
25  *
26  * @author Jean-Christophe MAGNAN
27  */
28 // =========================================================================
29 #include <agrum/FMDP/learning/core/testPolicy/Chi2TestPolicy.h>
30 // =========================================================================
31 
32 
33 namespace gum {
34 
35 
36  // ##########################################################################
37  //
38  // ##########################################################################
39 
40  // ==========================================================================
41  //
42  // ==========================================================================
43  template < typename GUM_SCALAR >
44  void Chi2TestPolicy< GUM_SCALAR >::addObservation(Idx iattr, GUM_SCALAR ivalue) {
47  }
48 
49 
50  // ############################################################################
51  // @name Test result
52  // ############################################################################
53 
54  // ============================================================================
55  // Computes the GStat of current variable according to the test
56  // ============================================================================
57  template < typename GUM_SCALAR >
60  chi2Score__ = 0;
61  for (auto attrIter = conTab__.attrABeginSafe();
63  ++attrIter) {
64  double semiExpected
65  = (double)(attrIter.val()) / (double)this->nbObservation();
66  for (auto valIter = conTab__.attrBBeginSafe();
68  ++valIter) {
69  double cell = (double)conTab__.joint(attrIter.key(), valIter.key());
70  if (cell < 5) continue;
71  double expected = semiExpected * (double)(valIter.val());
72 
73  chi2Score__ += std::pow(cell - expected, 2.0) / expected;
74  }
75  }
76  }
77 
78  // ============================================================================
79  // Returns the performance of current variable according to the test
80  // ============================================================================
81  template < typename GUM_SCALAR >
82  double Chi2TestPolicy< GUM_SCALAR >::score() const {
83  if (this->isModified_()) computeScore();
84  double score = 1
86  (conTab__.attrASize() - 1)
87  * (conTab__.attrBSize() - 1));
88  return score;
89  }
90 
91  // ============================================================================
92  // Returns a second criterion to severe ties
93  // ============================================================================
94  template < typename GUM_SCALAR >
96  if (this->isModified_()) computeScore();
97  return chi2Score__;
98  }
99 
100  template < typename GUM_SCALAR >
103  conTab__ += src.ct();
104  }
105 
106 } // End of namespace gum
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669