aGrUM  0.13.2
gum::Chi2TestPolicy< GUM_SCALAR > Class Template Reference

<agrum/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/multidim/core/testPolicy/Chi2TestPolicy.h>

Chi2TestPolicy implements a test policy that follows the Chi2 statistic

Definition at line 48 of file Chi2TestPolicy.h.

Constructor & Destructor Documentation

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

Definition at line 50 of file Chi2TestPolicy.h.

50  : ITestPolicy< GUM_SCALAR >(), __conTab(), __chi2Score(0) {
51  GUM_CONSTRUCTOR(Chi2TestPolicy)
52  }
ContingencyTable< Idx, GUM_SCALAR > __conTab
The contingency table used to keeps records of all observation.
template<typename GUM_SCALAR>
virtual gum::Chi2TestPolicy< GUM_SCALAR >::~Chi2TestPolicy ( )
inlinevirtual

Definition at line 54 of file Chi2TestPolicy.h.

54 { GUM_DESTRUCTOR(Chi2TestPolicy) }

Member Function Documentation

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

Definition at line 162 of file ITestPolicy.h.

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

162 { return __isModified; }
bool __isModified
Booleans indicating if we have to re eval test.
Definition: ITestPolicy.h:166
template<typename GUM_SCALAR >
void gum::Chi2TestPolicy< GUM_SCALAR >::add ( const Chi2TestPolicy< GUM_SCALAR > &  src)

Definition at line 99 of file Chi2TestPolicy_tpl.h.

References gum::ITestPolicy< GUM_SCALAR >::add(), and gum::Chi2TestPolicy< GUM_SCALAR >::ct().

Referenced by gum::Chi2TestPolicy< GUM_SCALAR >::ct().

99  {
101  __conTab += src.ct();
102  }
void add(const ITestPolicy< GUM_SCALAR > &src)
Definition: ITestPolicy.h:137
ContingencyTable< Idx, GUM_SCALAR > __conTab
The contingency table used to keeps records of all observation.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 137 of file ITestPolicy.h.

References gum::ITestPolicy< GUM_SCALAR >::__isModified, gum::ITestPolicy< GUM_SCALAR >::__nbObs, and gum::ITestPolicy< GUM_SCALAR >::nbObservation().

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

137  {
138  __isModified = true;
139  __nbObs += src.nbObservation();
140  }
bool __isModified
Booleans indicating if we have to re eval test.
Definition: ITestPolicy.h:166

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 42 of file Chi2TestPolicy_tpl.h.

References gum::ITestPolicy< GUM_SCALAR >::addObservation().

Referenced by gum::Chi2TestPolicy< GUM_SCALAR >::operator delete().

42  {
44  __conTab.add(iattr, ivalue);
45  }
virtual void addObservation(Idx attr, GUM_SCALAR value)
Comptabilizes the new observation.
Definition: ITestPolicy.h:87
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:

+ Here is the caller graph for this function:

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 56 of file Chi2TestPolicy_tpl.h.

References gum::ITestPolicy< GUM_SCALAR >::computeScore().

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

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 118 of file Chi2TestPolicy.h.

References gum::Chi2TestPolicy< GUM_SCALAR >::__conTab, and gum::Chi2TestPolicy< GUM_SCALAR >::add().

Referenced by gum::Chi2TestPolicy< GUM_SCALAR >::add().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 88 of file Chi2TestPolicy.h.

References gum::Chi2TestPolicy< GUM_SCALAR >::__conTab, gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::attrASize(), gum::Chi2TestPolicy< GUM_SCALAR >::computeScore(), gum::ITestPolicy< GUM_SCALAR >::nbObservation(), gum::Chi2TestPolicy< GUM_SCALAR >::score(), and gum::Chi2TestPolicy< GUM_SCALAR >::secondaryscore().

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

+ Here is the call graph for this function:

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

Comptabilizes the new observation.

Definition at line 95 of file ITestPolicy.h.

References gum::ITestPolicy< GUM_SCALAR >::__nbObs, and gum::ITestPolicy< GUM_SCALAR >::isTestRelevant().

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

95 { return __nbObs; }

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 62 of file Chi2TestPolicy.h.

References gum::Chi2TestPolicy< GUM_SCALAR >::addObservation(), gum::SmallObjectAllocator::deallocate(), and gum::SmallObjectAllocator::instance().

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

+ Here is the call graph for this function:

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

Allocators and Deallocators redefinition.

Definition at line 59 of file Chi2TestPolicy.h.

References gum::SmallObjectAllocator::allocate(), and gum::SmallObjectAllocator::instance().

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

+ Here is the call graph for this function:

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 80 of file Chi2TestPolicy_tpl.h.

References gum::ChiSquare::probaChi2().

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

80  {
81  if (this->_isModified()) computeScore();
82  double score =
83  1
85  __chi2Score, (__conTab.attrASize() - 1) * (__conTab.attrBSize() - 1));
86  return score;
87  }
Idx attrBSize() const
Returns the number of samples for column ivalue.
double score() const
Returns the performance of current variable according to the test.
Idx attrASize() const
Returns the number of samples for line iattr.
static double probaChi2(double x, Size df)
computes the probability of chi2 value (used by the cache)
Definition: chiSquare.cpp:116
bool _isModified() const
Definition: ITestPolicy.h:162
void computeScore() const
Recomputes the statistic from the beginning.
ContingencyTable< Idx, GUM_SCALAR > __conTab
The contingency table used to keeps records of all observation.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 93 of file Chi2TestPolicy_tpl.h.

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

93  {
94  if (this->_isModified()) computeScore();
95  return __chi2Score;
96  }
bool _isModified() const
Definition: ITestPolicy.h:162
void computeScore() const
Recomputes the statistic from the beginning.

+ Here is the caller graph for this function:

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

Definition at line 122 of file Chi2TestPolicy.h.

References gum::Chi2TestPolicy< GUM_SCALAR >::__conTab, gum::Chi2TestPolicy< GUM_SCALAR >::score(), gum::Chi2TestPolicy< GUM_SCALAR >::secondaryscore(), and gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::toString().

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

+ Here is the call graph for this function:

Member Data Documentation

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

Definition at line 136 of file Chi2TestPolicy.h.

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

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


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