aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
gum::GTestPolicy< GUM_ELEMENT > Class Template Referencefinal

<agrum/base/multidim/core/testPolicies/GTestPolicy.h> More...

#include <GTestPolicy.h>

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

Public Member Functions

Constructor/Destrcutor
 GTestPolicy ()
 Constructor.
 ~GTestPolicy () override
 Destructor.
void * operator new (size_t s)
 Allocators and Deallocators redefinition.
void operator delete (void *p)
 Constructor.
Observation insertion
void addObservation (Idx iattr, GUM_ELEMENT ivalue) override
 Comptabilizes the new observation.
Test methods
bool isTestRelevant () const override
 Returns true if enough observation were made so that the test can be relevant.
void computeScore () const override
 Computes the GStat of current variable according to the test.
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.
Fusion Methods
void add (const GTestPolicy< GUM_ELEMENT > &src)
 Performs the merging of current GTestPolicy instance with given instance.
const ContingencyTable< Idx, GUM_ELEMENT > & ct () const
 Returns contingency table (needed for the merging of GTestPolicy instances).
Miscelleanous Methods
std::string toString () const
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 _GStat_
bool _isModified_
 Booleans indicating if we have to re eval test.
Idx _nbObs_

Detailed Description

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

<agrum/base/multidim/core/testPolicies/GTestPolicy.h>

G implements a test policy that follows the G statistic

Definition at line 74 of file GTestPolicy.h.

Constructor & Destructor Documentation

◆ GTestPolicy()

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

Constructor.

Definition at line 136 of file GTestPolicy_tpl.h.

138 }
<agrum/base/multidim/core/testPolicies/GTestPolicy.h>
Definition GTestPolicy.h:74
GTestPolicy()
Constructor.
ContingencyTable< Idx, GUM_ELEMENT > _conTab_
The contingency table used to keeps records of all observation.
ITestPolicy()
Allocators and Deallocators redefinition.

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

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

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

◆ ~GTestPolicy()

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

Destructor.

Definition at line 141 of file GTestPolicy_tpl.h.

141 {
143 ;
144 }

References GTestPolicy().

Here is the call graph for this function:

Member Function Documentation

◆ add() [1/2]

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

Performs the merging of current GTestPolicy instance with given instance.

Definition at line 130 of file GTestPolicy_tpl.h.

130 {
132 _conTab_ += src.ct();
133 }
const ContingencyTable< Idx, GUM_ELEMENT > & ct() const
Returns contingency table (needed for the merging of GTestPolicy instances).
void add(const ITestPolicy< GUM_ELEMENT > &src)

References GTestPolicy(), _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::GTestPolicy< GUM_ELEMENT >::addObservation ( Idx iattr,
GUM_ELEMENT ivalue )
overridevirtual

Comptabilizes the new observation.

Reimplemented from gum::ITestPolicy< GUM_ELEMENT >.

Definition at line 69 of file GTestPolicy_tpl.h.

69 {
71 _conTab_.add(iattr, ivalue);
72 }
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::GTestPolicy< GUM_ELEMENT >::computeScore ( ) const
overridevirtual

Computes the GStat of current variable according to the test.

Reimplemented from gum::ITestPolicy< GUM_ELEMENT >.

Definition at line 82 of file GTestPolicy_tpl.h.

82 {
84 _GStat_ = 0;
85
86 // Itération sur l'axe 1 du tableau
87 for (auto attrIter = _conTab_.attrABeginSafe(); attrIter != _conTab_.attrAEndSafe();
88 ++attrIter) {
89 // Mise en cache de valeur utile
90 double semiExpected = (double)(attrIter.val()) / (double)this->nbObservation();
91
92 // Itération sur l'axe 2 du tableau
93 for (auto valIter = _conTab_.attrBBeginSafe(); valIter != _conTab_.attrBEndSafe();
94 ++valIter) {
95 // Récupération de la valeur en cellule
96 Idx cell = _conTab_.joint(attrIter.key(), valIter.key());
97 if (cell < 5) continue;
98
99 // Récupération de la valeur en axe 2
100 double expected = semiExpected * (double)valIter.val();
101
102 _GStat_ += 2 * cell * log(cell / expected);
103 }
104 }
105 }
virtual void computeScore() const
Recomputes the statistic from the beginning.

References _conTab_, _GStat_, 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::GTestPolicy< GUM_ELEMENT >::ct ( ) const

Returns contingency table (needed for the merging of GTestPolicy instances).

Definition at line 162 of file GTestPolicy_tpl.h.

162 {
163 return _conTab_;
164 }

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::GTestPolicy< 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 157 of file GTestPolicy_tpl.h.

157 {
158 return (this->nbObservation() > 20 && this->nbObservation() > _conTab_.attrASize() * 5);
159 }

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::GTestPolicy< GUM_ELEMENT >::operator delete ( void * p)

Constructor.

Definition at line 152 of file GTestPolicy_tpl.h.

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

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

Here is the call graph for this function:

◆ operator new()

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

Allocators and Deallocators redefinition.

Definition at line 147 of file GTestPolicy_tpl.h.

147 {
149 }
void * allocate(const size_t &objectSize)
Allocates a block.

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

Here is the call graph for this function:

◆ score()

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

Returns the performance of current variable according to the test.

Implements gum::ITestPolicy< GUM_ELEMENT >.

Definition at line 111 of file GTestPolicy_tpl.h.

111 {
112 if (this->isModified_()) computeScore();
113 // std::cout << this->toString() << std::endl;
114 double score
115 = 1
116 - ChiSquare::probaChi2(_GStat_, (_conTab_.attrASize() - 1) * (_conTab_.attrBSize() - 1));
117 return score;
118 }
static double probaChi2(double x, Size df)
computes the probability of chi2 value (used by the cache)
double score() const override
Returns the performance of current variable according to the test.
void computeScore() const override
Computes the GStat of current variable according to the test.
bool isModified_() const

References _conTab_, _GStat_, 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::GTestPolicy< GUM_ELEMENT >::secondaryscore ( ) const
overridevirtual

Returns a second criterion to severe ties.

Implements gum::ITestPolicy< GUM_ELEMENT >.

Definition at line 124 of file GTestPolicy_tpl.h.

124 {
125 if (this->isModified_()) computeScore();
126 return _GStat_;
127 }

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

Here is the call graph for this function:

◆ toString()

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

Definition at line 167 of file GTestPolicy_tpl.h.

167 {
168 return std::format("{}\t\t\tContingency Table : \n{}\n\t\t\tGStat : {}\n\t\t\tGStat : {}\n",
170 _conTab_.toString(),
171 _GStat_,
173 }
double secondaryscore() const override
Returns a second criterion to severe ties.

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

Here is the call graph for this function:

Member Data Documentation

◆ _conTab_

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

The contingency table used to keeps records of all observation.

Definition at line 173 of file GTestPolicy.h.

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

◆ _GStat_

template<typename GUM_ELEMENT>
double gum::GTestPolicy< GUM_ELEMENT >::_GStat_
mutableprivate

Definition at line 174 of file GTestPolicy.h.

Referenced by GTestPolicy(), computeScore(), score(), secondaryscore(), 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: