aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
Chi2TestPolicy.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 Headers of the Chi2TestPolicy
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
27  * GONZALES(@AMU)
28  *
29  */
30 #ifndef GUM_MULTI_DIM_FUNCTION_GRAPH_CHI2_TEST_POLICY_H
31 #define GUM_MULTI_DIM_FUNCTION_GRAPH_CHI2_TEST_POLICY_H
32 
33 // ============================================================================
34 #include <agrum/FMDP/learning/core/chiSquare.h>
35 #include <agrum/FMDP/learning/core/contingencyTable.h>
36 #include <agrum/FMDP/learning/core/testPolicy/ITestPolicy.h>
37 // ============================================================================
38 
39 namespace gum {
40 
41  /**
42  * @class Chi2TestPolicy Chi2TestPolicy.h
43  * <agrum/tools/multidim/core/testPolicy/Chi2TestPolicy.h>
44  *
45  * @brief Chi2TestPolicy implements a test policy that follows the Chi2
46  * statistic
47  *
48  * @ingroup fmdp_group
49  */
50  template < typename GUM_SCALAR >
52  public:
54  GUM_CONSTRUCTOR(Chi2TestPolicy);
55  }
56 
57  virtual ~Chi2TestPolicy() { GUM_DESTRUCTOR(Chi2TestPolicy); }
58 
59  // ============================================================================
60  /// Allocators and Deallocators redefinition
61  // ============================================================================
62  void* operator new(size_t s) {
63  return SmallObjectAllocator::instance().allocate(s);
64  }
65  void operator delete(void* p) {
66  SmallObjectAllocator::instance().deallocate(p, sizeof(Chi2TestPolicy));
67  }
68 
69  // ############################################################################
70  /// @name Observation insertion
71  // ############################################################################
72  /// @{
73 
74  // ============================================================================
75  /// Comptabilizes the new observation
76  // ============================================================================
77  void addObservation(Idx attr, GUM_SCALAR value);
78 
79  /// @}
80 
81 
82  // ############################################################################
83  /// @name Test relevance
84  // ############################################################################
85  /// @{
86 
87  // ============================================================================
88  /// Returns true if enough observation were made so that the test can be
89  /// relevant
90  // ============================================================================
91  bool isTestRelevant() const {
92  return (this->nbObservation() > 20
93  && this->nbObservation() > conTab__.attrASize() * 5);
94  }
95 
96  /// @}
97 
98 
99  // ############################################################################
100  /// @name Test result
101  // ############################################################################
102  /// @{
103 
104  // ============================================================================
105  /// Recomputes the statistic from the beginning
106  // ============================================================================
107  void computeScore() const;
108 
109  // ============================================================================
110  /// Returns the performance of current variable according to the test
111  // ============================================================================
112  double score() const;
113 
114  // ============================================================================
115  /// Returns a second criterion to severe ties
116  // ============================================================================
117  virtual double secondaryscore() const;
118 
119  /// @}
120 
121  const ContingencyTable< Idx, GUM_SCALAR >& ct() const { return conTab__; }
122 
123  void add(const Chi2TestPolicy< GUM_SCALAR >& src);
124 
125  std::string toString() const {
126  std::stringstream ss;
127  ss << ITestPolicy< GUM_SCALAR >::toString()
128  << "\t\t\tContingency Table : " << std::endl
129  << conTab__.toString() << std::endl
130  << "\t\t\tGStat : " << this->score() << std::endl
131  << "\t\t\tGStat : " << this->secondaryscore() << std::endl;
132  return ss.str();
133  }
134 
135  private:
136  /// The contingency table used to keeps records of all observation
138 
139  mutable double chi2Score__;
140  };
141 
142 } // End of namespace gum
143 
144 #include <agrum/FMDP/learning/core/testPolicy/Chi2TestPolicy_tpl.h>
145 
146 #endif /* GUM_MULTI_DIM_FUNCTION_GRAPH_CHI2_TEST_POLICY_H */
ContingencyTable< Idx, GUM_SCALAR > conTab__
The contingency table used to keeps records of all observation.
void * operator new(size_t s)
Allocators and Deallocators redefinition.
void addObservation(Idx attr, GUM_SCALAR value)
Comptabilizes the new observation.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
<agrum/tools/multidim/core/testPolicy/Chi2TestPolicy.h>
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)
virtual ~Chi2TestPolicy()
void computeScore() const
Recomputes the statistic from the beginning.
bool isTestRelevant() const
Returns true if enough observation were made so that the test can be relevant.
double score() const
Returns the performance of current variable according to the test.
void operator delete(void *p)
std::string toString() const