aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
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 43 of file contingencyTable_tpl.h.

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

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

66  {
67  // Updating
68  if (_attrAMarginalTable_.exists(valueA))
69  _attrAMarginalTable_[valueA]++;
70  else
71  _attrAMarginalTable_.insert(valueA, 1);
72 
73  if (_attrBMarginalTable_.exists(valueB))
74  _attrBMarginalTable_[valueB]++;
75  else
76  _attrBMarginalTable_.insert(valueB, 1);
77 
78  std::pair< GUM_SCALAR_A, GUM_SCALAR_B > cell(valueA, valueB);
79  if (_jointTable_.exists(cell))
80  _jointTable_[cell]++;
81  else
82  _jointTable_.insert(cell, 1);
83  }
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...
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
HashTable< GUM_SCALAR_A, Idx > _attrAMarginalTable_

◆ 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 122 of file contingencyTable.h.

122  {
124  }
const_iterator_safe cbeginSafe() const
Returns the safe const_iterator pointing to the beginning of the hashtable.
HashTable< GUM_SCALAR_A, Idx > _attrAMarginalTable_

◆ 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 125 of file contingencyTable.h.

125  {
127  }
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_

◆ 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 106 of file contingencyTable.h.

106  {
107  return _attrAMarginalTable_.exists(valueA) ? _attrAMarginalTable_[valueA] : 0;
108  }
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 144 of file contingencyTable.h.

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

◆ 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 134 of file contingencyTable.h.

134  {
136  }
HashTable< GUM_SCALAR_B, Idx > _attrBMarginalTable_
const_iterator_safe cbeginSafe() const
Returns the safe const_iterator pointing to the beginning of the hashtable.

◆ 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 137 of file contingencyTable.h.

137  {
139  }
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.

◆ 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 113 of file contingencyTable.h.

113  {
114  return _attrAMarginalTable_.exists(valueB) ? _attrAMarginalTable_[valueB] : 0;
115  }
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 149 of file contingencyTable.h.

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

◆ 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 97 of file contingencyTable.h.

97  {
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  }
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...

◆ 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 78 of file contingencyTable.h.

78  {
80  }
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.

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 87 of file contingencyTable_tpl.h.

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

◆ 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 156 of file contingencyTable.h.

156  {
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  }
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_

Member Data Documentation

◆ _attrAMarginalTable_

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

Definition at line 175 of file contingencyTable.h.

◆ _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

Definition at line 176 of file contingencyTable.h.

◆ _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 174 of file contingencyTable.h.


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