aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
contingencyTable.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 ContingencyTable class.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
27  * GONZALES(@AMU)
28  */
29 
30 
31 #ifndef GUM_CONTINGENCY_TABLE_H
32 #define GUM_CONTINGENCY_TABLE_H
33 
34 #include <agrum/tools/core/math/math_utils.h>
35 #include <agrum/tools/core/hashTable.h>
36 #include <agrum/tools/core/set.h>
37 #include <agrum/tools/core/smallobjectallocator/smallObjectAllocator.h>
38 
39 #include <agrum/FMDP/learning/observation.h>
40 
41 #include <agrum/tools/variables/discreteVariable.h>
42 
43 
44 namespace gum {
45 
46  /**
47  * @class ContingencyTable contingencyTable.h
48  * <agrum/FMDP/learning/core/contingencyTable.h>
49  * @brief
50  * @ingroup fmdp_group
51  *
52  *
53  *
54  */
55 
56  template < typename GUM_SCALAR_A, typename GUM_SCALAR_B >
58  public:
59  // ##########################################################################
60  /// @name Constructor & destructor.
61  // ##########################################################################
62  /// @{
63 
64 
65  /// Default constructor
66 
68 
69 
70  /// Default destructor
71 
73 
74  // ============================================================================
75  /// Allocators and Deallocators redefinition
76  // ============================================================================
77  void* operator new(size_t s) {
78  return SmallObjectAllocator::instance().allocate(s);
79  }
80  void operator delete(void* p) {
81  SmallObjectAllocator::instance().deallocate(p, sizeof(ContingencyTable));
82  }
83 
84  /// @}
85 
86 
87  /// @name
88 
89  /// @{
90 
91 
92  /// Increments the number of sample for case( iattr, ivalue )
93 
94  void add(GUM_SCALAR_A valueA, GUM_SCALAR_B valueB);
95 
96 
97  /// Returns the number of samples for case (iattr, ivalue)
98 
100  return jointTable__.exists(
101  std::pair< GUM_SCALAR_A, GUM_SCALAR_B >(valueA, valueB))
102  ? jointTable__[std::pair< GUM_SCALAR_A, GUM_SCALAR_B >(valueA,
103  valueB)]
104  : 0;
105  }
106 
107 
108  /// Returns the number of samples for case (iattr, ivalue)
109 
111  return attrAMarginalTable__.exists(valueA) ? attrAMarginalTable__[valueA]
112  : 0;
113  }
114 
115 
116  /// Returns the number of samples for case (iattr, ivalue)
117 
119  return attrAMarginalTable__.exists(valueB) ? attrAMarginalTable__[valueB]
120  : 0;
121  }
122 
123 
124  /// Returns the number of samples for line iattr
125 
126  // Idx aMarginal( GUM_SCALAR_A iattr ) { return
127  // attrMarginalTable__[iattr]; }
129  return attrAMarginalTable__.cbeginSafe();
130  }
132  return attrAMarginalTable__.cendSafe();
133  }
134 
135 
136  /// Returns the number of samples for column ivalue
137 
138  // Idx vMarginal( GUM_SCALAR_B ivalue ) { return
139  // valueMarginalTable__[ivalue]; }
141  return attrBMarginalTable__.cbeginSafe();
142  }
144  return attrBMarginalTable__.cendSafe();
145  }
146 
147 
148  /// Returns the number of samples for line iattr
149 
150  Idx attrASize() const { return attrAMarginalTable__.size(); }
151 
152 
153  /// Returns the number of samples for column ivalue
154 
155  Idx attrBSize() const { return attrBMarginalTable__.size(); }
156 
157  /// @}
158 
159  ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >&
160  operator+=(const ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >& src);
161 
162  std::string toString() const {
163  std::stringstream ss;
164  ss << "\t\t\t\t" << attrAMarginalTable__ << std::endl
165  << "\t\t\t\t" << attrBMarginalTable__ << std::endl
166  << "\t\t\t\t" << jointTable__ << std::endl;
167  return ss.str();
168  }
169 
170  private:
171  /**
172  * The contingency table used to compute the GStat
173  * Left Idx is for the attribute
174  * Right Idx for the value
175  *
176  * NB: This is a silly and in a hurry implementation of contingency table
177  * If someone ever use this class and has time to correctly implements
178  * a efficient contingency table, you're welcome
179  */
183  };
184 
185 } /* namespace gum */
186 
187 #include <agrum/FMDP/learning/core/contingencyTable_tpl.h>
188 #endif // GUM_CONTINGENCY_TABLE_H
HashTableConstIteratorSafe< GUM_SCALAR_B, Idx > attrBEndSafe() const
Increments the number of sample for case( iattr, ivalue )
Idx attrBSize() const
Returns the number of samples for column ivalue.
Idx attrAMarginal(GUM_SCALAR_A valueA) const
Returns the number of samples for case (iattr, ivalue)
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
void add(GUM_SCALAR_A valueA, GUM_SCALAR_B valueB)
Increments the number of sample for case( iattr, ivalue )
std::string toString() const
HashTable< GUM_SCALAR_A, Idx > attrAMarginalTable__
HashTable< GUM_SCALAR_B, Idx > attrBMarginalTable__
Idx joint(GUM_SCALAR_A valueA, GUM_SCALAR_B valueB) const
Returns the number of samples for case (iattr, ivalue)
~ContingencyTable()
Default destructor.
HashTableConstIteratorSafe< GUM_SCALAR_A, Idx > attrAEndSafe() const
Increments the number of sample for case( iattr, ivalue )
HashTableConstIteratorSafe< GUM_SCALAR_A, Idx > attrABeginSafe() const
Returns the number of samples for line iattr.
void * operator new(size_t s)
Allocators and Deallocators redefinition.
HashTable< std::pair< GUM_SCALAR_A, GUM_SCALAR_B >, Idx > jointTable__
The contingency table used to compute the GStat Left Idx is for the attribute Right Idx for the value...
HashTableConstIteratorSafe< GUM_SCALAR_B, Idx > attrBBeginSafe() const
Returns the number of samples for column ivalue.
ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B > & operator+=(const ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B > &src)
ContingencyTable()
Default constructor.
Idx attrASize() const
Returns the number of samples for line iattr.
Idx attrBMarginal(GUM_SCALAR_B valueB) const
Returns the number of samples for case (iattr, ivalue)
void operator delete(void *p)
Default constructor.