aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
gum::Chi2TestPolicy< GUM_SCALAR > Class Template Reference

<agrum/tools/multidim/core/testPolicy/Chi2TestPolicy.h> More...

#include <Chi2TestPolicy.h>

+ Inheritance diagram for gum::Chi2TestPolicy< GUM_SCALAR >:
+ Collaboration diagram for gum::Chi2TestPolicy< GUM_SCALAR >:

Public Member Functions

 Chi2TestPolicy ()
 
virtual ~Chi2TestPolicy ()
 
void * operator new (size_t s)
 Allocators and Deallocators redefinition. More...
 
void operator delete (void *p)
 
const ContingencyTable< Idx, GUM_SCALAR > & ct () const
 
void add (const Chi2TestPolicy< GUM_SCALAR > &src)
 
std::string toString () const
 
Observation insertion
void addObservation (Idx attr, GUM_SCALAR value)
 Comptabilizes the new observation. More...
 
Test relevance
bool isTestRelevant () const
 Returns true if enough observation were made so that the test can be relevant. More...
 
Test result
void computeScore () const
 Recomputes the statistic from the beginning. More...
 
double score () const
 Returns the performance of current variable according to the test. More...
 
virtual double secondaryscore () const
 Returns a second criterion to severe ties. More...
 
Observation methods
Idx nbObservation () const
 Comptabilizes the new observation. More...
 
Fusion Methods
void add (const ITestPolicy< GUM_SCALAR > &src)
 

Protected Member Functions

bool isModified_ () const
 

Detailed Description

template<typename GUM_SCALAR>
class gum::Chi2TestPolicy< GUM_SCALAR >

<agrum/tools/multidim/core/testPolicy/Chi2TestPolicy.h>

Chi2TestPolicy implements a test policy that follows the Chi2 statistic

Definition at line 51 of file Chi2TestPolicy.h.

Constructor & Destructor Documentation

◆ Chi2TestPolicy()

template<typename GUM_SCALAR >
gum::Chi2TestPolicy< GUM_SCALAR >::Chi2TestPolicy ( )
inline

Definition at line 53 of file Chi2TestPolicy.h.

53  : ITestPolicy< GUM_SCALAR >(), _conTab_(), _chi2Score_(0) {
54  GUM_CONSTRUCTOR(Chi2TestPolicy);
55  }
ContingencyTable< Idx, GUM_SCALAR > _conTab_
The contingency table used to keeps records of all observation.

◆ ~Chi2TestPolicy()

template<typename GUM_SCALAR >
virtual gum::Chi2TestPolicy< GUM_SCALAR >::~Chi2TestPolicy ( )
inlinevirtual

Definition at line 57 of file Chi2TestPolicy.h.

57  {
58  GUM_DESTRUCTOR(Chi2TestPolicy);
59  ;
60  }

Member Function Documentation

◆ add() [1/2]

template<typename GUM_SCALAR >
void gum::Chi2TestPolicy< GUM_SCALAR >::add ( const Chi2TestPolicy< GUM_SCALAR > &  src)

Definition at line 97 of file Chi2TestPolicy_tpl.h.

References gum::Set< Key, Alloc >::emplace().

97  {
99  _conTab_ += src.ct();
100  }
ContingencyTable< Idx, GUM_SCALAR > _conTab_
The contingency table used to keeps records of all observation.
void add(const ITestPolicy< GUM_SCALAR > &src)
Definition: ITestPolicy.h:139
+ Here is the call graph for this function:

◆ add() [2/2]

template<typename GUM_SCALAR >
void gum::ITestPolicy< GUM_SCALAR >::add ( const ITestPolicy< GUM_SCALAR > &  src)
inlineinherited

Definition at line 139 of file ITestPolicy.h.

References gum::ITestPolicy< GUM_SCALAR >::_isModified_.

139  {
140  _isModified_ = true;
141  _nbObs_ += src.nbObservation();
142  }
bool _isModified_
Booleans indicating if we have to re eval test.
Definition: ITestPolicy.h:168

◆ addObservation()

template<typename GUM_SCALAR >
void gum::Chi2TestPolicy< GUM_SCALAR >::addObservation ( Idx  attr,
GUM_SCALAR  value 
)
virtual

Comptabilizes the new observation.

Reimplemented from gum::ITestPolicy< GUM_SCALAR >.

Definition at line 44 of file Chi2TestPolicy_tpl.h.

References gum::Set< Key, Alloc >::emplace().

44  {
46  _conTab_.add(iattr, ivalue);
47  }
virtual void addObservation(Idx attr, GUM_SCALAR value)
Comptabilizes the new observation.
Definition: ITestPolicy.h:89
void add(GUM_SCALAR_A valueA, GUM_SCALAR_B valueB)
Increments the number of sample for case( iattr, ivalue )
ContingencyTable< Idx, GUM_SCALAR > _conTab_
The contingency table used to keeps records of all observation.
+ Here is the call graph for this function:

◆ computeScore()

template<typename GUM_SCALAR >
void gum::Chi2TestPolicy< GUM_SCALAR >::computeScore ( ) const
virtual

Recomputes the statistic from the beginning.

Reimplemented from gum::ITestPolicy< GUM_SCALAR >.

Definition at line 58 of file Chi2TestPolicy_tpl.h.

References gum::Set< Key, Alloc >::emplace().

58  {
60  _chi2Score_ = 0;
61  for (auto attrIter = _conTab_.attrABeginSafe(); attrIter != _conTab_.attrAEndSafe();
62  ++attrIter) {
63  double semiExpected = (double)(attrIter.val()) / (double)this->nbObservation();
64  for (auto valIter = _conTab_.attrBBeginSafe(); valIter != _conTab_.attrBEndSafe();
65  ++valIter) {
66  double cell = (double)_conTab_.joint(attrIter.key(), valIter.key());
67  if (cell < 5) continue;
68  double expected = semiExpected * (double)(valIter.val());
69 
70  _chi2Score_ += std::pow(cell - expected, 2.0) / expected;
71  }
72  }
73  }
virtual void computeScore() const
Recomputes the statistic from the beginning.
Definition: ITestPolicy.h:116
HashTableConstIteratorSafe< GUM_SCALAR_B, Idx > attrBEndSafe() const
Increments the number of sample for case( iattr, ivalue )
Idx joint(GUM_SCALAR_A valueA, GUM_SCALAR_B valueB) const
Returns the number of samples for case (iattr, ivalue)
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.
ContingencyTable< Idx, GUM_SCALAR > _conTab_
The contingency table used to keeps records of all observation.
HashTableConstIteratorSafe< GUM_SCALAR_B, Idx > attrBBeginSafe() const
Returns the number of samples for column ivalue.
Idx nbObservation() const
Comptabilizes the new observation.
Definition: ITestPolicy.h:97
+ Here is the call graph for this function:

◆ ct()

template<typename GUM_SCALAR >
const ContingencyTable< Idx, GUM_SCALAR >& gum::Chi2TestPolicy< GUM_SCALAR >::ct ( ) const
inline

Definition at line 121 of file Chi2TestPolicy.h.

121 { return _conTab_; }
ContingencyTable< Idx, GUM_SCALAR > _conTab_
The contingency table used to keeps records of all observation.

◆ isModified_()

template<typename GUM_SCALAR >
bool gum::ITestPolicy< GUM_SCALAR >::isModified_ ( ) const
inlineprotectedinherited

Definition at line 164 of file ITestPolicy.h.

References gum::ITestPolicy< GUM_SCALAR >::_isModified_.

164 { return _isModified_; }
bool _isModified_
Booleans indicating if we have to re eval test.
Definition: ITestPolicy.h:168

◆ isTestRelevant()

template<typename GUM_SCALAR >
bool gum::Chi2TestPolicy< GUM_SCALAR >::isTestRelevant ( ) const
inlinevirtual

Returns true if enough observation were made so that the test can be relevant.

Implements gum::ITestPolicy< GUM_SCALAR >.

Definition at line 92 of file Chi2TestPolicy.h.

92  {
93  return (this->nbObservation() > 20 && this->nbObservation() > _conTab_.attrASize() * 5);
94  }
ContingencyTable< Idx, GUM_SCALAR > _conTab_
The contingency table used to keeps records of all observation.
Idx nbObservation() const
Comptabilizes the new observation.
Definition: ITestPolicy.h:97
Idx attrASize() const
Returns the number of samples for line iattr.

◆ nbObservation()

template<typename GUM_SCALAR >
Idx gum::ITestPolicy< GUM_SCALAR >::nbObservation ( ) const
inlineinherited

Comptabilizes the new observation.

Definition at line 97 of file ITestPolicy.h.

97 { return _nbObs_; }

◆ operator delete()

template<typename GUM_SCALAR >
void gum::Chi2TestPolicy< GUM_SCALAR >::operator delete ( void *  p)
inline

Definition at line 66 of file Chi2TestPolicy.h.

66  {
68  }
void deallocate(void *pDeallocatedObject, const size_t &objectSize)
Deallocates an object.
static SmallObjectAllocator & instance()

◆ operator new()

template<typename GUM_SCALAR >
void* gum::Chi2TestPolicy< GUM_SCALAR >::operator new ( size_t  s)
inline

Allocators and Deallocators redefinition.

Definition at line 65 of file Chi2TestPolicy.h.

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

◆ score()

template<typename GUM_SCALAR >
double gum::Chi2TestPolicy< GUM_SCALAR >::score ( ) const
virtual

Returns the performance of current variable according to the test.

Implements gum::ITestPolicy< GUM_SCALAR >.

Definition at line 79 of file Chi2TestPolicy_tpl.h.

References gum::Set< Key, Alloc >::emplace().

79  {
80  if (this->isModified_()) computeScore();
81  double score
82  = 1
84  return score;
85  }
Idx attrBSize() const
Returns the number of samples for column ivalue.
bool isModified_() const
Definition: ITestPolicy.h:164
void computeScore() const
Recomputes the statistic from the beginning.
static double probaChi2(double x, Size df)
computes the probability of chi2 value (used by the cache)
Definition: chiSquare.cpp:114
ContingencyTable< Idx, GUM_SCALAR > _conTab_
The contingency table used to keeps records of all observation.
double score() const
Returns the performance of current variable according to the test.
Idx attrASize() const
Returns the number of samples for line iattr.
+ Here is the call graph for this function:

◆ secondaryscore()

template<typename GUM_SCALAR >
double gum::Chi2TestPolicy< GUM_SCALAR >::secondaryscore ( ) const
virtual

Returns a second criterion to severe ties.

Implements gum::ITestPolicy< GUM_SCALAR >.

Definition at line 91 of file Chi2TestPolicy_tpl.h.

References gum::Set< Key, Alloc >::emplace().

91  {
92  if (this->isModified_()) computeScore();
93  return _chi2Score_;
94  }
bool isModified_() const
Definition: ITestPolicy.h:164
void computeScore() const
Recomputes the statistic from the beginning.
+ Here is the call graph for this function:

◆ toString()

template<typename GUM_SCALAR >
std::string gum::Chi2TestPolicy< GUM_SCALAR >::toString ( ) const
inline

Definition at line 125 of file Chi2TestPolicy.h.

125  {
126  std::stringstream ss;
127  ss << ITestPolicy< GUM_SCALAR >::toString() << "\t\t\tContingency Table : " << std::endl
128  << _conTab_.toString() << std::endl
129  << "\t\t\tGStat : " << this->score() << std::endl
130  << "\t\t\tGStat : " << this->secondaryscore() << std::endl;
131  return ss.str();
132  }
std::string toString() const
virtual double secondaryscore() const
Returns a second criterion to severe ties.
ContingencyTable< Idx, GUM_SCALAR > _conTab_
The contingency table used to keeps records of all observation.
double score() const
Returns the performance of current variable according to the test.

Member Data Documentation

◆ _chi2Score_

template<typename GUM_SCALAR >
double gum::Chi2TestPolicy< GUM_SCALAR >::_chi2Score_
mutableprivate

Definition at line 138 of file Chi2TestPolicy.h.

◆ _conTab_

template<typename GUM_SCALAR >
ContingencyTable< Idx, GUM_SCALAR > gum::Chi2TestPolicy< GUM_SCALAR >::_conTab_
private

The contingency table used to keeps records of all observation.

Definition at line 136 of file Chi2TestPolicy.h.


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