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

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

#include <Chi2TestPolicy.h>

Inheritance diagram for gum::Chi2TestPolicy< GUM_ELEMENT >:
Collaboration diagram for gum::Chi2TestPolicy< GUM_ELEMENT >:

Public Member Functions

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

Protected Member Functions

bool isModified_ () const

Private Attributes

ContingencyTable< Idx, GUM_ELEMENT > _conTab_
 The contingency table used to keeps records of all observation.
double _chi2Score_
bool _isModified_
 Booleans indicating if we have to re eval test.
Idx _nbObs_

Detailed Description

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

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

Chi2TestPolicy implements a test policy that follows the Chi2 statistic

Definition at line 72 of file Chi2TestPolicy.h.

Constructor & Destructor Documentation

◆ Chi2TestPolicy()

template<typename GUM_ELEMENT>
gum::Chi2TestPolicy< GUM_ELEMENT >::Chi2TestPolicy ( )

Definition at line 125 of file Chi2TestPolicy_tpl.h.

125 :
128 }
<agrum/base/multidim/core/testPolicy/Chi2TestPolicy.h>
ContingencyTable< Idx, GUM_ELEMENT > _conTab_
The contingency table used to keeps records of all observation.
ITestPolicy()
Allocators and Deallocators redefinition.

References Chi2TestPolicy(), gum::ITestPolicy< GUM_ELEMENT >::ITestPolicy(), _chi2Score_, and _conTab_.

Referenced by Chi2TestPolicy(), ~Chi2TestPolicy(), add(), operator delete(), and operator new().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~Chi2TestPolicy()

template<typename GUM_ELEMENT>
gum::Chi2TestPolicy< GUM_ELEMENT >::~Chi2TestPolicy ( )
override

Definition at line 131 of file Chi2TestPolicy_tpl.h.

131 {
133 ;
134 }

References Chi2TestPolicy().

Here is the call graph for this function:

Member Function Documentation

◆ add() [1/2]

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

Definition at line 119 of file Chi2TestPolicy_tpl.h.

119 {
121 _conTab_ += src.ct();
122 }
const ContingencyTable< Idx, GUM_ELEMENT > & ct() const
void add(const ITestPolicy< GUM_ELEMENT > &src)

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

Here is the call graph for this function:

◆ add() [2/2]

template<typename GUM_ELEMENT>
void gum::ITestPolicy< GUM_ELEMENT >::add ( const ITestPolicy< GUM_ELEMENT > & src)
inherited

Definition at line 86 of file ITestPolicy_tpl.h.

86 {
87 _isModified_ = true;
89 }
<agrum/base/multidim/core/testPolicies/ITestPolicy.h>
Definition ITestPolicy.h:71
bool _isModified_
Booleans indicating if we have to re eval test.
Idx nbObservation() const
Comptabilizes the new observation.

References ITestPolicy(), _isModified_, _nbObs_, and nbObservation().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addObservation()

template<typename GUM_ELEMENT>
void gum::Chi2TestPolicy< GUM_ELEMENT >::addObservation ( Idx attr,
GUM_ELEMENT value )
overridevirtual

Comptabilizes the new observation.

Reimplemented from gum::ITestPolicy< GUM_ELEMENT >.

Definition at line 67 of file Chi2TestPolicy_tpl.h.

67 {
69 _conTab_.add(iattr, ivalue);
70 }
virtual void addObservation(Idx attr, GUM_ELEMENT value)
Comptabilizes the new observation.

References _conTab_, and gum::ITestPolicy< GUM_ELEMENT >::addObservation().

Here is the call graph for this function:

◆ computeScore()

template<typename GUM_ELEMENT>
void gum::Chi2TestPolicy< GUM_ELEMENT >::computeScore ( ) const
overridevirtual

Recomputes the statistic from the beginning.

Reimplemented from gum::ITestPolicy< GUM_ELEMENT >.

Definition at line 80 of file Chi2TestPolicy_tpl.h.

80 {
82 _chi2Score_ = 0;
83 for (auto attrIter = _conTab_.attrABeginSafe(); attrIter != _conTab_.attrAEndSafe();
84 ++attrIter) {
85 double semiExpected = (double)(attrIter.val()) / (double)this->nbObservation();
86 for (auto valIter = _conTab_.attrBBeginSafe(); valIter != _conTab_.attrBEndSafe();
87 ++valIter) {
88 double cell = (double)_conTab_.joint(attrIter.key(), valIter.key());
89 if (cell < 5) continue;
90 double expected = semiExpected * (double)(valIter.val());
91
93 }
94 }
95 }
virtual void computeScore() const
Recomputes the statistic from the beginning.

References _chi2Score_, _conTab_, gum::ITestPolicy< GUM_ELEMENT >::computeScore(), and gum::ITestPolicy< GUM_ELEMENT >::nbObservation().

Referenced by score(), and secondaryscore().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ct()

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

Definition at line 152 of file Chi2TestPolicy_tpl.h.

152 {
153 return _conTab_;
154 }

References _conTab_.

Referenced by add().

Here is the caller graph for this function:

◆ isModified_()

template<typename GUM_ELEMENT>
bool gum::ITestPolicy< GUM_ELEMENT >::isModified_ ( ) const
protectedinherited

Definition at line 97 of file ITestPolicy_tpl.h.

97 {
98 return _isModified_;
99 }

References _isModified_.

Referenced by gum::Chi2TestPolicy< GUM_ELEMENT >::score(), gum::GTestPolicy< GUM_ELEMENT >::score(), gum::LeastSquareTestPolicy< GUM_ELEMENT >::score(), gum::Chi2TestPolicy< GUM_ELEMENT >::secondaryscore(), gum::GTestPolicy< GUM_ELEMENT >::secondaryscore(), and gum::LeastSquareTestPolicy< GUM_ELEMENT >::secondaryscore().

Here is the caller graph for this function:

◆ isTestRelevant()

template<typename GUM_ELEMENT>
bool gum::Chi2TestPolicy< GUM_ELEMENT >::isTestRelevant ( ) const
overridevirtual

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

Implements gum::ITestPolicy< GUM_ELEMENT >.

Definition at line 147 of file Chi2TestPolicy_tpl.h.

147 {
148 return (this->nbObservation() > 20 && this->nbObservation() > _conTab_.attrASize() * 5);
149 }

References _conTab_, and gum::ITestPolicy< GUM_ELEMENT >::nbObservation().

Here is the call graph for this function:

◆ nbObservation()

template<typename GUM_ELEMENT>
Idx gum::ITestPolicy< GUM_ELEMENT >::nbObservation ( ) const
inherited

Comptabilizes the new observation.

Definition at line 76 of file ITestPolicy_tpl.h.

76 {
77 return _nbObs_;
78 }

References _nbObs_.

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

Here is the caller graph for this function:

◆ operator delete()

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

Definition at line 142 of file Chi2TestPolicy_tpl.h.

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

References Chi2TestPolicy(), gum::SmallObjectAllocator::deallocate(), and gum::SmallObjectAllocator::instance().

Here is the call graph for this function:

◆ operator new()

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

Allocators and Deallocators redefinition.

Definition at line 137 of file Chi2TestPolicy_tpl.h.

137 {
139 }
void * allocate(const size_t &objectSize)
Allocates a block.

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

Here is the call graph for this function:

◆ score()

template<typename GUM_ELEMENT>
double gum::Chi2TestPolicy< GUM_ELEMENT >::score ( ) const
overridevirtual

Returns the performance of current variable according to the test.

Implements gum::ITestPolicy< GUM_ELEMENT >.

Definition at line 101 of file Chi2TestPolicy_tpl.h.

101 {
102 if (this->isModified_()) computeScore();
103 double score = 1
105 (_conTab_.attrASize() - 1) * (_conTab_.attrBSize() - 1));
106 return score;
107 }
void computeScore() const override
Recomputes the statistic from the beginning.
double score() const override
Returns the performance of current variable according to the test.
static double probaChi2(double x, Size df)
computes the probability of chi2 value (used by the cache)
bool isModified_() const

References _chi2Score_, _conTab_, computeScore(), gum::ITestPolicy< GUM_ELEMENT >::isModified_(), gum::ChiSquare::probaChi2(), and score().

Referenced by score().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ secondaryscore()

template<typename GUM_ELEMENT>
double gum::Chi2TestPolicy< GUM_ELEMENT >::secondaryscore ( ) const
overridevirtual

Returns a second criterion to severe ties.

Implements gum::ITestPolicy< GUM_ELEMENT >.

Definition at line 113 of file Chi2TestPolicy_tpl.h.

113 {
114 if (this->isModified_()) computeScore();
115 return _chi2Score_;
116 }

References _chi2Score_, computeScore(), and gum::ITestPolicy< GUM_ELEMENT >::isModified_().

Here is the call graph for this function:

◆ toString()

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

Definition at line 157 of file Chi2TestPolicy_tpl.h.

157 {
158 return std::format("{}\t\t\tContingency Table : \n{}\n\t\t\tGStat : {}\n\t\t\tGStat : {}\n",
160 _conTab_.toString(),
161 this->score(),
163 }
double secondaryscore() const override
Returns a second criterion to severe ties.

References _conTab_, and gum::ITestPolicy< GUM_ELEMENT >::toString().

Here is the call graph for this function:

Member Data Documentation

◆ _chi2Score_

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

Definition at line 144 of file Chi2TestPolicy.h.

Referenced by Chi2TestPolicy(), computeScore(), score(), and secondaryscore().

◆ _conTab_

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

The contingency table used to keeps records of all observation.

Definition at line 142 of file Chi2TestPolicy.h.

Referenced by Chi2TestPolicy(), add(), addObservation(), computeScore(), ct(), isTestRelevant(), score(), and toString().

◆ _isModified_

template<typename GUM_ELEMENT>
bool gum::ITestPolicy< GUM_ELEMENT >::_isModified_
mutableprivateinherited

Booleans indicating if we have to re eval test.

Definition at line 175 of file ITestPolicy.h.

Referenced by ITestPolicy(), add(), addObservation(), computeScore(), and isModified_().

◆ _nbObs_

template<typename GUM_ELEMENT>
Idx gum::ITestPolicy< GUM_ELEMENT >::_nbObs_
privateinherited

Definition at line 178 of file ITestPolicy.h.

Referenced by ITestPolicy(), add(), addObservation(), nbObservation(), and toString().


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