aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
GTestPolicy.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 GTestPolicy
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_G_TEST_POLICY_H
31 #define GUM_MULTI_DIM_FUNCTION_GRAPH_G_TEST_POLICY_H
32 // ============================================================================
33 #include <agrum/FMDP/learning/core/chiSquare.h>
34 #include <agrum/FMDP/learning/core/contingencyTable.h>
35 #include <agrum/FMDP/learning/core/testPolicy/ITestPolicy.h>
36 #include <agrum/FMDP/learning/core/tupleHashFunc.h>
37 // ============================================================================
38 
39 namespace gum {
40 
41  // template <typename GUM_SCALAR>
42  // using ConTab = ContingencyTable<Idx, GUM_SCALAR>;
43 
44  /**
45  * @class GTestPolicy GTestPolicy.h
46  * <agrum/tools/multidim/core/testPolicies/GTestPolicy.h>
47  *
48  * @brief G implements a test policy that follows the G statistic
49  *
50  * @ingroup fmdp_group
51  */
52  template < typename GUM_SCALAR >
53  class GTestPolicy: public ITestPolicy< GUM_SCALAR > {
54  public:
55  // ############################################################################
56  /// @name Constructor/Destrcutor
57  // ############################################################################
58  /// @{
59 
60  // ============================================================================
61  /// Constructor
62  // ============================================================================
64  GUM_CONSTRUCTOR(GTestPolicy);
65  }
66 
67  // ============================================================================
68  /// Destructor
69  // ============================================================================
70  virtual ~GTestPolicy() {
71  GUM_DESTRUCTOR(GTestPolicy);
72  ;
73  }
74 
75  // ============================================================================
76  /// Allocators and Deallocators redefinition
77  // ============================================================================
78  void* operator new(size_t s) { return SmallObjectAllocator::instance().allocate(s); }
79 
80  void operator delete(void* p) {
81  SmallObjectAllocator::instance().deallocate(p, sizeof(GTestPolicy));
82  }
83 
84  /// @}
85 
86  // ############################################################################
87  /// @name Observation insertion
88  // ############################################################################
89  /// @{
90 
91  // ============================================================================
92  /// Comptabilizes the new observation
93  // ============================================================================
94  void addObservation(Idx iattr, GUM_SCALAR ivalue);
95 
96  /// @}
97 
98 
99  // ############################################################################
100  /// @name Test methods
101  // ############################################################################
102  /// @{
103 
104  // ============================================================================
105  /// Returns true if enough observation were made so that the test can be
106  /// relevant
107  // ============================================================================
108  bool isTestRelevant() const {
109  return (this->nbObservation() > 20 && this->nbObservation() > _conTab_.attrASize() * 5);
110  }
111 
112  // ============================================================================
113  /// Computes the GStat of current variable according to the test
114  // ============================================================================
115  void computeScore() const;
116 
117  // ============================================================================
118  /// Returns the performance of current variable according to the test
119  // ============================================================================
120  double score() const;
121 
122  // ============================================================================
123  /// Returns a second criterion to severe ties
124  // ============================================================================
125  double secondaryscore() const;
126 
127 
128  /// @}
129 
130 
131  // ############################################################################
132  /// @name Fusion Methods
133  // ############################################################################
134  /// @{
135 
136  // ============================================================================
137  /// Performs the merging of current GTestPolicy instance with given instance
138  // ============================================================================
139  void add(const GTestPolicy< GUM_SCALAR >& src);
140 
141  // ============================================================================
142  /// Returns contingency table (needed for the merging of GTestPolicy
143  /// instances)
144  // ============================================================================
145  const ContingencyTable< Idx, GUM_SCALAR >& ct() const { return _conTab_; }
146 
147 
148  /// @}
149 
150 
151  // ############################################################################
152  /// @name Miscelleanous Methods
153  // ############################################################################
154  /// @{
155 
156  std::string toString() const {
157  std::stringstream ss;
158  ss << ITestPolicy< GUM_SCALAR >::toString() << "\t\t\tContingency Table : " << std::endl
159  << _conTab_.toString() << std::endl
160  << "\t\t\tGStat : " << _GStat_ << std::endl
161  << "\t\t\tGStat : " << this->secondaryscore() << std::endl;
162  return ss.str();
163  }
164 
165  /// @}
166 
167  private:
168  /// The contingency table used to keeps records of all observation
170  mutable double _GStat_;
171  };
172 
173 } // End of namespace gum
174 
175 #include <agrum/FMDP/learning/core/testPolicy/GTestPolicy_tpl.h>
176 
177 #endif /* GUM_MULTI_DIM_FUNCTION_GRAPH_G_TEST_POLICY_H */
void computeScore() const
Computes the GStat of current variable according to the test.
virtual ~GTestPolicy()
Destructor.
Definition: GTestPolicy.h:70
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:145
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
ContingencyTable< Idx, GUM_SCALAR > _conTab_
The contingency table used to keeps records of all observation.
Definition: GTestPolicy.h:169
std::string toString() const
Definition: GTestPolicy.h:156
void * operator new(size_t s)
Allocators and Deallocators redefinition.
Definition: GTestPolicy.h:78
double secondaryscore() const
Returns a second criterion to severe ties.
<agrum/tools/multidim/core/testPolicies/GTestPolicy.h>
Definition: GTestPolicy.h:53
double score() const
Returns the performance of current variable according to the test.
bool isTestRelevant() const
Returns true if enough observation were made so that the test can be relevant.
Definition: GTestPolicy.h:108
void add(const GTestPolicy< GUM_SCALAR > &src)
Performs the merging of current GTestPolicy instance with given instance.
GTestPolicy()
Constructor.
Definition: GTestPolicy.h:63
void operator delete(void *p)
Constructor.
Definition: GTestPolicy.h:80