aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
leastSquareTestPolicy.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 LeastSquareTestPolicy
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_LEAST_SQUARE_TEST_POLICY_H
31 #define GUM_MULTI_DIM_FUNCTION_GRAPH_LEAST_SQUARE_TEST_POLICY_H
32 
33 // ============================================================================
34 #include <agrum/FMDP/learning/core/contingencyTable.h>
35 #include <agrum/FMDP/learning/core/testPolicy/ITestPolicy.h>
36 // ============================================================================
37 #include <agrum/tools/multidim/utils/FunctionGraphUtilities/link.h>
38 // ============================================================================
39 
40 namespace gum {
41 
42  /**
43  * @class LeastSquareTestPolicy leastSquareTestPolicy.h
44  * <agrum/tools/multidim/core/testPolicy/leastSquareTestPolicy.h>
45  *
46  * @brief LeastSquareTestPolicy implements a test policy that follows the
47  * Least Square statistic
48  *
49  * @ingroup fmdp_group
50  */
51  template < typename GUM_SCALAR >
53  public:
54  // ############################################################################
55  /// @name Constructor/Destrcutor
56  // ############################################################################
57  /// @{
58 
59  // ============================================================================
60  /// Constructor
61  // ============================================================================
63  ITestPolicy< GUM_SCALAR >(), sumO__(0.0), score__(0) {
64  GUM_CONSTRUCTOR(LeastSquareTestPolicy);
65  }
66 
67  // ============================================================================
68  /// Allocators and Deallocators redefinition
69  // ============================================================================
70  void* operator new(size_t s) {
71  return SmallObjectAllocator::instance().allocate(s);
72  }
73  void operator delete(void* p) {
74  SmallObjectAllocator::instance().deallocate(p,
75  sizeof(LeastSquareTestPolicy));
76  }
77 
78  // ============================================================================
79  /// Destructor
80  // ============================================================================
81  virtual ~LeastSquareTestPolicy();
82 
83  // ############################################################################
84  /// @name Observation insertion
85  // ############################################################################
86  /// @{
87 
88  // ============================================================================
89  /// Comptabilizes the new observation
90  // ============================================================================
91  void addObservation(Idx attr, GUM_SCALAR value);
92 
93  /// @}
94 
95 
96  // ############################################################################
97  /// @name Test relevance
98  // ############################################################################
99  /// @{
100 
101  // ============================================================================
102  /// Returns true if enough observation were made so that the test can be
103  /// relevant
104  // ============================================================================
105  bool isTestRelevant() const { return (this->nbObservation() > 20); }
106 
107  /// @}
108 
109 
110  // ############################################################################
111  /// @name Test result
112  // ############################################################################
113  /// @{
114 
115  // ============================================================================
116  /// Returns the performance of current variable according to the test
117  // ============================================================================
118  void computeScore();
119 
120  // ============================================================================
121  /// Returns the performance of current variable according to the test
122  // ============================================================================
123  double score();
124 
125  // ============================================================================
126  /// Returns a second criterion to severe ties
127  // ============================================================================
128  double secondaryscore() const;
129 
130  /// @}
131 
132 
133  // ############################################################################
134  /// @name Fusion Methods
135  // ############################################################################
136  /// @{
137 
138  // ============================================================================
139  /// Performs the merging of current LeastSquareTestPolicy instance with
140  /// given instance
141  // ============================================================================
142  void add(const LeastSquareTestPolicy< GUM_SCALAR >& src);
143 
144 
145  // ============================================================================
146  /// Returns global sum (needed for the merging)
147  // ============================================================================
148  double sumValue() const { return sumO__; }
149 
150  // ============================================================================
151  /// Returns nbobs per modality table (needed for the merging)
152  // ============================================================================
153  const HashTable< Idx, Idx >& nbObsTable() const { return nbObsTable__; }
154 
155  // ============================================================================
156  /// Returns sum per modality table (needed for the merging)
157  // ============================================================================
158  const HashTable< Idx, double >& sumAttrTable() const { return sumAttrTable__; }
159 
160  // ============================================================================
161  /// Returns global sum (needed for the merging)
162  // ============================================================================
163  const HashTable< Idx, LinkedList< double >* >& obsTable() const {
164  return obsTable__;
165  }
166 
167  private:
168  /// Global sum
169  double sumO__;
170 
171  /// Nb Observation for each modality assumed by variable
173 
174  /// Sum for each modality assumed by variable
176 
177  /// Not sure if needed
179 
180  /// Keeping computed score
181  double score__;
182  };
183 
184 } // End of namespace gum
185 
186 #include <agrum/FMDP/learning/core/testPolicy/leastSquareTestPolicy_tpl.h>
187 
188 #endif /* GUM_MULTI_DIM_FUNCTION_GRAPH_LEAST_SQUARE_TEST_POLICY_H */
const HashTable< Idx, LinkedList< double > *> & obsTable() const
Returns global sum (needed for the merging)
double score()
Returns the performance of current variable according to the test.
void operator delete(void *p)
Constructor.
const HashTable< Idx, Idx > & nbObsTable() const
Returns nbobs per modality table (needed for the merging)
virtual ~LeastSquareTestPolicy()
Destructor.
HashTable< Idx, double > sumAttrTable__
Sum for each modality assumed by variable.
double secondaryscore() const
Returns a second criterion to severe ties.
const HashTable< Idx, double > & sumAttrTable() const
Returns sum per modality table (needed for the merging)
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
void add(const LeastSquareTestPolicy< GUM_SCALAR > &src)
Performs the merging of current LeastSquareTestPolicy instance with given instance.
double sumValue() const
Returns global sum (needed for the merging)
bool isTestRelevant() const
Returns true if enough observation were made so that the test can be relevant.
void addObservation(Idx attr, GUM_SCALAR value)
Comptabilizes the new observation.
HashTable< Idx, Idx > nbObsTable__
Nb Observation for each modality assumed by variable.
double score__
Keeping computed score.
void computeScore()
Returns the performance of current variable according to the test.
void * operator new(size_t s)
Allocators and Deallocators redefinition.