aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
Chi2TestPolicy.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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() {
58  GUM_DESTRUCTOR(Chi2TestPolicy);
59  ;
60  }
61 
62  // ============================================================================
63  /// Allocators and Deallocators redefinition
64  // ============================================================================
65  void* operator new(size_t s) { return SmallObjectAllocator::instance().allocate(s); }
66  void operator delete(void* p) {
67  SmallObjectAllocator::instance().deallocate(p, sizeof(Chi2TestPolicy));
68  }
69 
70  // ############################################################################
71  /// @name Observation insertion
72  // ############################################################################
73  /// @{
74 
75  // ============================================================================
76  /// Comptabilizes the new observation
77  // ============================================================================
78  void addObservation(Idx attr, GUM_SCALAR value);
79 
80  /// @}
81 
82 
83  // ############################################################################
84  /// @name Test relevance
85  // ############################################################################
86  /// @{
87 
88  // ============================================================================
89  /// Returns true if enough observation were made so that the test can be
90  /// relevant
91  // ============================================================================
92  bool isTestRelevant() const {
93  return (this->nbObservation() > 20 && 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() << "\t\t\tContingency Table : " << std::endl
128  << _conTab_.toString() << std::endl
129  << "\t\t\tGStat : " << this->score() << std::endl
130  << "\t\t\tGStat : " << this->secondaryscore() << std::endl;
131  return ss.str();
132  }
133 
134  private:
135  /// The contingency table used to keeps records of all observation
137 
138  mutable double _chi2Score_;
139  };
140 
141 } // End of namespace gum
142 
143 #include <agrum/FMDP/learning/core/testPolicy/Chi2TestPolicy_tpl.h>
144 
145 #endif /* GUM_MULTI_DIM_FUNCTION_GRAPH_CHI2_TEST_POLICY_H */
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:643
<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.
ContingencyTable< Idx, GUM_SCALAR > _conTab_
The contingency table used to keeps records of all observation.
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