aGrUM  0.14.2
gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B > Class Template Reference

<agrum/FMDP/learning/core/contingencyTable.h> More...

#include <contingencyTable.h>

+ Collaboration diagram for gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >:

Public Member Functions

ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B > & operator+= (const ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B > &src)
 
std::string toString () const
 
Constructor & destructor.
 ContingencyTable ()
 Default constructor. More...
 
 ~ContingencyTable ()
 Default destructor. More...
 
void * operator new (size_t s)
 Allocators and Deallocators redefinition. More...
 
void operator delete (void *p)
 Default constructor. More...
 
void add (GUM_SCALAR_A valueA, GUM_SCALAR_B valueB)
 Increments the number of sample for case( iattr, ivalue ) More...
 
Idx joint (GUM_SCALAR_A valueA, GUM_SCALAR_B valueB) const
 Returns the number of samples for case (iattr, ivalue) More...
 
Idx attrAMarginal (GUM_SCALAR_A valueA) const
 Returns the number of samples for case (iattr, ivalue) More...
 
Idx attrBMarginal (GUM_SCALAR_B valueB) const
 Returns the number of samples for case (iattr, ivalue) More...
 
HashTableConstIteratorSafe< GUM_SCALAR_A, IdxattrABeginSafe () const
 Returns the number of samples for line iattr. More...
 
HashTableConstIteratorSafe< GUM_SCALAR_A, IdxattrAEndSafe () const
 Increments the number of sample for case( iattr, ivalue ) More...
 
HashTableConstIteratorSafe< GUM_SCALAR_B, IdxattrBBeginSafe () const
 Returns the number of samples for column ivalue. More...
 
HashTableConstIteratorSafe< GUM_SCALAR_B, IdxattrBEndSafe () const
 Increments the number of sample for case( iattr, ivalue ) More...
 
Idx attrASize () const
 Returns the number of samples for line iattr. More...
 
Idx attrBSize () const
 Returns the number of samples for column ivalue. More...
 

Detailed Description

template<typename GUM_SCALAR_A, typename GUM_SCALAR_B>
class gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >

<agrum/FMDP/learning/core/contingencyTable.h>

Definition at line 54 of file contingencyTable.h.

Constructor & Destructor Documentation

◆ ContingencyTable()

template<typename GUM_SCALAR_A , typename GUM_SCALAR_B >
gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::ContingencyTable ( )

Default constructor.

Definition at line 41 of file contingencyTable_tpl.h.

41  {
42  GUM_CONSTRUCTOR(ContingencyTable);
43  }
ContingencyTable()
Default constructor.

◆ ~ContingencyTable()

template<typename GUM_SCALAR_A , typename GUM_SCALAR_B >
gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::~ContingencyTable ( )

Default destructor.

Definition at line 50 of file contingencyTable_tpl.h.

50  {
51  GUM_DESTRUCTOR(ContingencyTable);
52  }
ContingencyTable()
Default constructor.

Member Function Documentation

◆ add()

template<typename GUM_SCALAR_A, typename GUM_SCALAR_B>
void gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::add ( GUM_SCALAR_A  valueA,
GUM_SCALAR_B  valueB 
)

Increments the number of sample for case( iattr, ivalue )

Definition at line 63 of file contingencyTable_tpl.h.

Referenced by gum::ContingencyTable< Idx, GUM_SCALAR >::operator delete().

64  {
65  // Updating
66  if (__attrAMarginalTable.exists(valueA))
67  __attrAMarginalTable[valueA]++;
68  else
69  __attrAMarginalTable.insert(valueA, 1);
70 
71  if (__attrBMarginalTable.exists(valueB))
72  __attrBMarginalTable[valueB]++;
73  else
74  __attrBMarginalTable.insert(valueB, 1);
75 
76  std::pair< GUM_SCALAR_A, GUM_SCALAR_B > cell(valueA, valueB);
77  if (__jointTable.exists(cell))
78  __jointTable[cell]++;
79  else
80  __jointTable.insert(cell, 1);
81  }
HashTable< GUM_SCALAR_B, Idx > __attrBMarginalTable
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
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...
HashTable< GUM_SCALAR_A, Idx > __attrAMarginalTable
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
+ Here is the caller graph for this function:

◆ attrABeginSafe()

template<typename GUM_SCALAR_A, typename GUM_SCALAR_B>
HashTableConstIteratorSafe< GUM_SCALAR_A, Idx > gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::attrABeginSafe ( ) const
inline

Returns the number of samples for line iattr.

Definition at line 125 of file contingencyTable.h.

Referenced by gum::ContingencyTable< Idx, GUM_SCALAR >::operator+=().

125  {
127  }
const_iterator_safe cbeginSafe() const
Returns the safe const_iterator pointing to the beginning of the hashtable.
HashTable< GUM_SCALAR_A, Idx > __attrAMarginalTable
+ Here is the caller graph for this function:

◆ attrAEndSafe()

template<typename GUM_SCALAR_A, typename GUM_SCALAR_B>
HashTableConstIteratorSafe< GUM_SCALAR_A, Idx > gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::attrAEndSafe ( ) const
inline

Increments the number of sample for case( iattr, ivalue )

Definition at line 128 of file contingencyTable.h.

Referenced by gum::ContingencyTable< Idx, GUM_SCALAR >::operator+=().

128  {
130  }
const const_iterator_safe & cendSafe() const noexcept
Returns the safe const_iterator pointing to the end of the hashtable.
HashTable< GUM_SCALAR_A, Idx > __attrAMarginalTable
+ Here is the caller graph for this function:

◆ attrAMarginal()

template<typename GUM_SCALAR_A, typename GUM_SCALAR_B>
Idx gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::attrAMarginal ( GUM_SCALAR_A  valueA) const
inline

Returns the number of samples for case (iattr, ivalue)

Definition at line 107 of file contingencyTable.h.

107  {
108  return __attrAMarginalTable.exists(valueA) ? __attrAMarginalTable[valueA]
109  : 0;
110  }
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
HashTable< GUM_SCALAR_A, Idx > __attrAMarginalTable

◆ attrASize()

template<typename GUM_SCALAR_A, typename GUM_SCALAR_B>
Idx gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::attrASize ( ) const
inline

Returns the number of samples for line iattr.

Definition at line 147 of file contingencyTable.h.

Referenced by gum::Chi2TestPolicy< GUM_SCALAR >::isTestRelevant(), and gum::GTestPolicy< GUM_SCALAR >::isTestRelevant().

147 { return __attrAMarginalTable.size(); }
Size size() const noexcept
Returns the number of elements stored into the hashtable.
HashTable< GUM_SCALAR_A, Idx > __attrAMarginalTable
+ Here is the caller graph for this function:

◆ attrBBeginSafe()

template<typename GUM_SCALAR_A, typename GUM_SCALAR_B>
HashTableConstIteratorSafe< GUM_SCALAR_B, Idx > gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::attrBBeginSafe ( ) const
inline

Returns the number of samples for column ivalue.

Definition at line 137 of file contingencyTable.h.

Referenced by gum::ContingencyTable< Idx, GUM_SCALAR >::operator+=().

137  {
139  }
const_iterator_safe cbeginSafe() const
Returns the safe const_iterator pointing to the beginning of the hashtable.
HashTable< GUM_SCALAR_B, Idx > __attrBMarginalTable
+ Here is the caller graph for this function:

◆ attrBEndSafe()

template<typename GUM_SCALAR_A, typename GUM_SCALAR_B>
HashTableConstIteratorSafe< GUM_SCALAR_B, Idx > gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::attrBEndSafe ( ) const
inline

Increments the number of sample for case( iattr, ivalue )

Definition at line 140 of file contingencyTable.h.

Referenced by gum::ContingencyTable< Idx, GUM_SCALAR >::operator+=().

140  {
142  }
HashTable< GUM_SCALAR_B, Idx > __attrBMarginalTable
const const_iterator_safe & cendSafe() const noexcept
Returns the safe const_iterator pointing to the end of the hashtable.
+ Here is the caller graph for this function:

◆ attrBMarginal()

template<typename GUM_SCALAR_A, typename GUM_SCALAR_B>
Idx gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::attrBMarginal ( GUM_SCALAR_B  valueB) const
inline

Returns the number of samples for case (iattr, ivalue)

Definition at line 115 of file contingencyTable.h.

115  {
116  return __attrAMarginalTable.exists(valueB) ? __attrAMarginalTable[valueB]
117  : 0;
118  }
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
HashTable< GUM_SCALAR_A, Idx > __attrAMarginalTable

◆ attrBSize()

template<typename GUM_SCALAR_A, typename GUM_SCALAR_B>
Idx gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::attrBSize ( ) const
inline

Returns the number of samples for column ivalue.

Definition at line 152 of file contingencyTable.h.

152 { return __attrBMarginalTable.size(); }
Size size() const noexcept
Returns the number of elements stored into the hashtable.
HashTable< GUM_SCALAR_B, Idx > __attrBMarginalTable

◆ joint()

template<typename GUM_SCALAR_A, typename GUM_SCALAR_B>
Idx gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::joint ( GUM_SCALAR_A  valueA,
GUM_SCALAR_B  valueB 
) const
inline

Returns the number of samples for case (iattr, ivalue)

Definition at line 96 of file contingencyTable.h.

Referenced by gum::ContingencyTable< Idx, GUM_SCALAR >::operator+=().

96  {
97  return __jointTable.exists(
98  std::pair< GUM_SCALAR_A, GUM_SCALAR_B >(valueA, valueB))
99  ? __jointTable[std::pair< GUM_SCALAR_A, GUM_SCALAR_B >(valueA,
100  valueB)]
101  : 0;
102  }
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
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...
+ Here is the caller graph for this function:

◆ operator delete()

template<typename GUM_SCALAR_A, typename GUM_SCALAR_B>
void gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::operator delete ( void *  p)
inline

Default constructor.

Definition at line 77 of file contingencyTable.h.

77  {
79  }
void deallocate(void *pDeallocatedObject, const size_t &objectSize)
Deallocates an object.
ContingencyTable()
Default constructor.
static SmallObjectAllocator & instance()

◆ operator new()

template<typename GUM_SCALAR_A, typename GUM_SCALAR_B>
void* gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::operator new ( size_t  s)
inline

Allocators and Deallocators redefinition.

Definition at line 74 of file contingencyTable.h.

74  {
76  }
void * allocate(const size_t &objectSize)
Allocates a block.
static SmallObjectAllocator & instance()

◆ operator+=()

template<typename GUM_SCALAR_A, typename GUM_SCALAR_B>
ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B > & gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::operator+= ( const ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B > &  src)

Definition at line 86 of file contingencyTable_tpl.h.

Referenced by gum::ContingencyTable< Idx, GUM_SCALAR >::add(), and gum::ContingencyTable< Idx, GUM_SCALAR >::attrBSize().

86  {
87  // Ajout dans marginal A et table joint des valeurs pour src
88  for (auto aTer = src.attrABeginSafe(); aTer != src.attrAEndSafe(); ++aTer) {
89  if (__attrAMarginalTable.exists(aTer.key()))
90  __attrAMarginalTable[aTer.key()] += aTer.val();
91  else
92  __attrAMarginalTable.insert(aTer.key(), aTer.val());
93 
94  for (auto bTer = src.attrBBeginSafe(); bTer != src.attrBEndSafe(); ++bTer) {
95  std::pair< GUM_SCALAR_A, GUM_SCALAR_B > cell(aTer.key(), bTer.key());
96  if (__jointTable.exists(cell))
97  __jointTable[cell] += src.joint(aTer.key(), bTer.key());
98  else
99  __jointTable.insert(cell, src.joint(aTer.key(), bTer.key()));
100  }
101  }
102 
103  // Ajout dans marginal B des valeurs de src
104  for (auto bTer = src.attrBBeginSafe(); bTer != src.attrBEndSafe(); ++bTer) {
105  if (__attrBMarginalTable.exists(bTer.key()))
106  __attrBMarginalTable[bTer.key()] += bTer.val();
107  else
108  __attrBMarginalTable.insert(bTer.key(), bTer.val());
109  }
110  return *this;
111  }
HashTable< GUM_SCALAR_B, Idx > __attrBMarginalTable
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
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...
HashTable< GUM_SCALAR_A, Idx > __attrAMarginalTable
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
+ Here is the caller graph for this function:

◆ toString()

template<typename GUM_SCALAR_A, typename GUM_SCALAR_B>
std::string gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::toString ( ) const
inline

Definition at line 159 of file contingencyTable.h.

Referenced by gum::Chi2TestPolicy< GUM_SCALAR >::toString(), and gum::GTestPolicy< GUM_SCALAR >::toString().

159  {
160  std::stringstream ss;
161  ss << "\t\t\t\t" << __attrAMarginalTable << std::endl
162  << "\t\t\t\t" << __attrBMarginalTable << std::endl
163  << "\t\t\t\t" << __jointTable << std::endl;
164  return ss.str();
165  }
HashTable< GUM_SCALAR_B, Idx > __attrBMarginalTable
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...
HashTable< GUM_SCALAR_A, Idx > __attrAMarginalTable
+ Here is the caller graph for this function:

Member Data Documentation

◆ __attrAMarginalTable

◆ __attrBMarginalTable

template<typename GUM_SCALAR_A, typename GUM_SCALAR_B>
HashTable< GUM_SCALAR_B, Idx > gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::__attrBMarginalTable
private

◆ __jointTable

template<typename GUM_SCALAR_A, typename GUM_SCALAR_B>
HashTable< std::pair< GUM_SCALAR_A, GUM_SCALAR_B >, Idx > gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::__jointTable
private

The contingency table used to compute the GStat Left Idx is for the attribute Right Idx for the value.

NB: This is a silly and in a hurry implementation of contingency table If someone ever use this class and has time to correctly implements a efficient contingency table, you're welcome

Definition at line 177 of file contingencyTable.h.

Referenced by gum::ContingencyTable< Idx, GUM_SCALAR >::joint(), and gum::ContingencyTable< Idx, GUM_SCALAR >::toString().


The documentation for this class was generated from the following files: