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

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

#include <GTestPolicy.h>

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

Public Member Functions

Constructor/Destrcutor
 GTestPolicy ()
 Constructor.
virtual ~GTestPolicy ()
 Destructor.
void * operator new (size_t s)
 Allocators and Deallocators redefinition.
void operator delete (void *p)
 Constructor.
Observation insertion
void addObservation (Idx iattr, GUM_SCALAR ivalue)
 Comptabilizes the new observation.
Test methods
bool isTestRelevant () const
 Returns true if enough observation were made so that the test can be relevant.
void computeScore () const
 Computes the GStat of current variable according to the test.
double score () const
 Returns the performance of current variable according to the test.
double secondaryscore () const
 Returns a second criterion to severe ties.
Fusion Methods
void add (const GTestPolicy< GUM_SCALAR > &src)
 Performs the merging of current GTestPolicy instance with given instance.
const ContingencyTable< Idx, GUM_SCALAR > & 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_SCALAR > &src)

Protected Member Functions

bool isModified_ () const

Private Attributes

ContingencyTable< Idx, GUM_SCALAR > _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_SCALAR>
class gum::GTestPolicy< GUM_SCALAR >

<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_SCALAR>
gum::GTestPolicy< GUM_SCALAR >::GTestPolicy ( )
inline

Constructor.

Definition at line 84 of file GTestPolicy.h.

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

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

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

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

◆ ~GTestPolicy()

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

Destructor.

Definition at line 91 of file GTestPolicy.h.

91 {
93 ;
94 }

References GTestPolicy().

Here is the call graph for this function:

Member Function Documentation

◆ add() [1/2]

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

Performs the merging of current GTestPolicy instance with given instance.

Definition at line 129 of file GTestPolicy_tpl.h.

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

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

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 161 of file ITestPolicy.h.

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

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

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

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

◆ addObservation()

template<typename GUM_SCALAR>
void gum::GTestPolicy< GUM_SCALAR >::addObservation ( Idx iattr,
GUM_SCALAR ivalue )
virtual

Comptabilizes the new observation.

Reimplemented from gum::ITestPolicy< GUM_SCALAR >.

Definition at line 68 of file GTestPolicy_tpl.h.

68 {
70 _conTab_.add(iattr, ivalue);
71 }
virtual void addObservation(Idx attr, GUM_SCALAR value)
Comptabilizes the new observation.

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

Here is the call graph for this function:

◆ computeScore()

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

Computes the GStat of current variable according to the test.

Reimplemented from gum::ITestPolicy< GUM_SCALAR >.

Definition at line 81 of file GTestPolicy_tpl.h.

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

References _conTab_, _GStat_, gum::ITestPolicy< GUM_SCALAR >::computeScore(), and gum::ITestPolicy< GUM_SCALAR >::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_SCALAR>
const ContingencyTable< Idx, GUM_SCALAR > & gum::GTestPolicy< GUM_SCALAR >::ct ( ) const
inline

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

Definition at line 166 of file GTestPolicy.h.

166{ return _conTab_; }

References _conTab_.

Referenced by add().

Here is the caller graph for this function:

◆ isModified_()

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

Definition at line 186 of file ITestPolicy.h.

186{ return _isModified_; }

References _isModified_.

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

Here is the caller graph for this function:

◆ isTestRelevant()

template<typename GUM_SCALAR>
bool gum::GTestPolicy< 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 129 of file GTestPolicy.h.

129 {
130 return (this->nbObservation() > 20 && this->nbObservation() > _conTab_.attrASize() * 5);
131 }

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

Here is the call graph for this function:

◆ nbObservation()

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

Comptabilizes the new observation.

Definition at line 119 of file ITestPolicy.h.

119{ return _nbObs_; }

References _nbObs_.

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

Here is the caller graph for this function:

◆ operator delete()

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

Constructor.

Definition at line 101 of file GTestPolicy.h.

101 {
103 }
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_SCALAR>
void * gum::GTestPolicy< GUM_SCALAR >::operator new ( size_t s)
inline

Allocators and Deallocators redefinition.

Definition at line 99 of file GTestPolicy.h.

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

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

Here is the call graph for this function:

◆ score()

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

Returns the performance of current variable according to the test.

Implements gum::ITestPolicy< GUM_SCALAR >.

Definition at line 110 of file GTestPolicy_tpl.h.

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

References _conTab_, _GStat_, computeScore(), gum::ITestPolicy< GUM_SCALAR >::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_SCALAR>
double gum::GTestPolicy< GUM_SCALAR >::secondaryscore ( ) const
virtual

Returns a second criterion to severe ties.

Implements gum::ITestPolicy< GUM_SCALAR >.

Definition at line 123 of file GTestPolicy_tpl.h.

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

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

Referenced by toString().

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

◆ toString()

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

Definition at line 176 of file GTestPolicy.h.

176 {
178 ss << ITestPolicy< GUM_SCALAR >::toString() << "\t\t\tContingency Table : " << std::endl
179 << _conTab_.toString() << std::endl
180 << "\t\t\tGStat : " << _GStat_ << std::endl
181 << "\t\t\tGStat : " << this->secondaryscore() << std::endl;
182 return ss.str();
183 }
double secondaryscore() const
Returns a second criterion to severe ties.

References _conTab_, _GStat_, and secondaryscore().

Here is the call graph for this function:

Member Data Documentation

◆ _conTab_

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

The contingency table used to keeps records of all observation.

Definition at line 189 of file GTestPolicy.h.

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

◆ _GStat_

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

Definition at line 190 of file GTestPolicy.h.

Referenced by GTestPolicy(), computeScore(), score(), secondaryscore(), and toString().

◆ _isModified_

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

Booleans indicating if we have to re eval test.

Definition at line 190 of file ITestPolicy.h.

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

◆ _nbObs_

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

Definition at line 193 of file ITestPolicy.h.

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


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