aGrUM  0.16.0
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 57 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 44 of file contingencyTable_tpl.h.

44  {
45  GUM_CONSTRUCTOR(ContingencyTable);
46  }
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 53 of file contingencyTable_tpl.h.

53  {
54  GUM_DESTRUCTOR(ContingencyTable);
55  }
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 66 of file contingencyTable_tpl.h.

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

67  {
68  // Updating
69  if (__attrAMarginalTable.exists(valueA))
70  __attrAMarginalTable[valueA]++;
71  else
72  __attrAMarginalTable.insert(valueA, 1);
73 
74  if (__attrBMarginalTable.exists(valueB))
75  __attrBMarginalTable[valueB]++;
76  else
77  __attrBMarginalTable.insert(valueB, 1);
78 
79  std::pair< GUM_SCALAR_A, GUM_SCALAR_B > cell(valueA, valueB);
80  if (__jointTable.exists(cell))
81  __jointTable[cell]++;
82  else
83  __jointTable.insert(cell, 1);
84  }
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 128 of file contingencyTable.h.

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

128  {
130  }
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 131 of file contingencyTable.h.

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

131  {
133  }
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 110 of file contingencyTable.h.

110  {
111  return __attrAMarginalTable.exists(valueA) ? __attrAMarginalTable[valueA]
112  : 0;
113  }
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 150 of file contingencyTable.h.

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

150 { 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 140 of file contingencyTable.h.

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

140  {
142  }
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 143 of file contingencyTable.h.

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

143  {
145  }
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 118 of file contingencyTable.h.

118  {
119  return __attrAMarginalTable.exists(valueB) ? __attrAMarginalTable[valueB]
120  : 0;
121  }
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 155 of file contingencyTable.h.

155 { 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 99 of file contingencyTable.h.

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

99  {
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  }
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 80 of file contingencyTable.h.

80  {
82  }
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 77 of file contingencyTable.h.

77  {
79  }
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 89 of file contingencyTable_tpl.h.

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

89  {
90  // Ajout dans marginal A et table joint des valeurs pour src
91  for (auto aTer = src.attrABeginSafe(); aTer != src.attrAEndSafe(); ++aTer) {
92  if (__attrAMarginalTable.exists(aTer.key()))
93  __attrAMarginalTable[aTer.key()] += aTer.val();
94  else
95  __attrAMarginalTable.insert(aTer.key(), aTer.val());
96 
97  for (auto bTer = src.attrBBeginSafe(); bTer != src.attrBEndSafe(); ++bTer) {
98  std::pair< GUM_SCALAR_A, GUM_SCALAR_B > cell(aTer.key(), bTer.key());
99  if (__jointTable.exists(cell))
100  __jointTable[cell] += src.joint(aTer.key(), bTer.key());
101  else
102  __jointTable.insert(cell, src.joint(aTer.key(), bTer.key()));
103  }
104  }
105 
106  // Ajout dans marginal B des valeurs de src
107  for (auto bTer = src.attrBBeginSafe(); bTer != src.attrBEndSafe(); ++bTer) {
108  if (__attrBMarginalTable.exists(bTer.key()))
109  __attrBMarginalTable[bTer.key()] += bTer.val();
110  else
111  __attrBMarginalTable.insert(bTer.key(), bTer.val());
112  }
113  return *this;
114  }
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 162 of file contingencyTable.h.

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

162  {
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  }
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 180 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: