aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
contingencyTable.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 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) { return SmallObjectAllocator::instance().allocate(s); }
78  void operator delete(void* p) {
79  SmallObjectAllocator::instance().deallocate(p, sizeof(ContingencyTable));
80  }
81 
82  /// @}
83 
84 
85  /// @name
86 
87  /// @{
88 
89 
90  /// Increments the number of sample for case( iattr, ivalue )
91 
92  void add(GUM_SCALAR_A valueA, GUM_SCALAR_B valueB);
93 
94 
95  /// Returns the number of samples for case (iattr, ivalue)
96 
98  return _jointTable_.exists(std::pair< GUM_SCALAR_A, GUM_SCALAR_B >(valueA, valueB))
99  ? _jointTable_[std::pair< GUM_SCALAR_A, GUM_SCALAR_B >(valueA, valueB)]
100  : 0;
101  }
102 
103 
104  /// Returns the number of samples for case (iattr, ivalue)
105 
107  return _attrAMarginalTable_.exists(valueA) ? _attrAMarginalTable_[valueA] : 0;
108  }
109 
110 
111  /// Returns the number of samples for case (iattr, ivalue)
112 
114  return _attrAMarginalTable_.exists(valueB) ? _attrAMarginalTable_[valueB] : 0;
115  }
116 
117 
118  /// Returns the number of samples for line iattr
119 
120  // Idx aMarginal( GUM_SCALAR_A iattr ) { return
121  // _attrMarginalTable_[iattr]; }
123  return _attrAMarginalTable_.cbeginSafe();
124  }
126  return _attrAMarginalTable_.cendSafe();
127  }
128 
129 
130  /// Returns the number of samples for column ivalue
131 
132  // Idx vMarginal( GUM_SCALAR_B ivalue ) { return
133  // _valueMarginalTable_[ivalue]; }
135  return _attrBMarginalTable_.cbeginSafe();
136  }
138  return _attrBMarginalTable_.cendSafe();
139  }
140 
141 
142  /// Returns the number of samples for line iattr
143 
144  Idx attrASize() const { return _attrAMarginalTable_.size(); }
145 
146 
147  /// Returns the number of samples for column ivalue
148 
149  Idx attrBSize() const { return _attrBMarginalTable_.size(); }
150 
151  /// @}
152 
153  ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >&
154  operator+=(const ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >& src);
155 
156  std::string toString() const {
157  std::stringstream ss;
158  ss << "\t\t\t\t" << _attrAMarginalTable_ << std::endl
159  << "\t\t\t\t" << _attrBMarginalTable_ << std::endl
160  << "\t\t\t\t" << _jointTable_ << std::endl;
161  return ss.str();
162  }
163 
164  private:
165  /**
166  * The contingency table used to compute the GStat
167  * Left Idx is for the attribute
168  * Right Idx for the value
169  *
170  * NB: This is a silly and in a hurry implementation of contingency table
171  * If someone ever use this class and has time to correctly implements
172  * a efficient contingency table, you're welcome
173  */
177  };
178 
179 } /* namespace gum */
180 
181 #include <agrum/FMDP/learning/core/contingencyTable_tpl.h>
182 #endif // GUM_CONTINGENCY_TABLE_H
HashTable< GUM_SCALAR_B, Idx > _attrBMarginalTable_
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:643
void add(GUM_SCALAR_A valueA, GUM_SCALAR_B valueB)
Increments the number of sample for case( iattr, ivalue )
std::string toString() const
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.
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...
void * operator new(size_t s)
Allocators and Deallocators redefinition.
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.
HashTable< GUM_SCALAR_A, Idx > _attrAMarginalTable_
Idx attrBMarginal(GUM_SCALAR_B valueB) const
Returns the number of samples for case (iattr, ivalue)
void operator delete(void *p)
Default constructor.