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

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

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

G implements a test policy that follows the G statistic

Definition at line 53 of file GTestPolicy.h.

Constructor & Destructor Documentation

◆ GTestPolicy()

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

Constructor.

Definition at line 63 of file GTestPolicy.h.

63  : ITestPolicy< GUM_SCALAR >(), conTab__(), GStat__(0) {
64  GUM_CONSTRUCTOR(GTestPolicy);
65  }
GTestPolicy()
Constructor.
Definition: GTestPolicy.h:63
ContingencyTable< Idx, GUM_SCALAR > conTab__
The contingency table used to keeps records of all observation.
Definition: GTestPolicy.h:170

◆ ~GTestPolicy()

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

Destructor.

Definition at line 70 of file GTestPolicy.h.

70 { GUM_DESTRUCTOR(GTestPolicy); }
GTestPolicy()
Constructor.
Definition: GTestPolicy.h:63

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 111 of file GTestPolicy_tpl.h.

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

111  {
113  conTab__ += src.ct();
114  }
void add(const ITestPolicy< GUM_SCALAR > &src)
Definition: ITestPolicy.h:140
ContingencyTable< Idx, GUM_SCALAR > conTab__
The contingency table used to keeps records of all observation.
Definition: GTestPolicy.h:170
+ 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 140 of file ITestPolicy.h.

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

140  {
141  isModified__ = true;
142  nbObs__ += src.nbObservation();
143  }
bool isModified__
Booleans indicating if we have to re eval test.
Definition: ITestPolicy.h:169

◆ 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 45 of file GTestPolicy_tpl.h.

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

45  {
47  conTab__.add(iattr, ivalue);
48  }
virtual void addObservation(Idx attr, GUM_SCALAR value)
Comptabilizes the new observation.
Definition: ITestPolicy.h:90
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.
Definition: GTestPolicy.h:170
+ 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 59 of file GTestPolicy_tpl.h.

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

59  {
61  GStat__ = 0;
62 
63  // Itération sur l'axe 1 du tableau
64  for (auto attrIter = conTab__.attrABeginSafe();
65  attrIter != conTab__.attrAEndSafe();
66  ++attrIter) {
67  // Mise en cache de valeur utile
68  double semiExpected
69  = (double)(attrIter.val()) / (double)this->nbObservation();
70 
71  // Itération sur l'axe 2 du tableau
72  for (auto valIter = conTab__.attrBBeginSafe();
73  valIter != conTab__.attrBEndSafe();
74  ++valIter) {
75  // Récupération de la valeur en cellule
76  Idx cell = conTab__.joint(attrIter.key(), valIter.key());
77  if (cell < 5) continue;
78 
79  // Récupération de la valeur en axe 2
80  double expected = semiExpected * (double)valIter.val();
81 
82  GStat__ += 2 * cell * log(cell / expected);
83  }
84  }
85  }
virtual void computeScore() const
Recomputes the statistic from the beginning.
Definition: ITestPolicy.h:117
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.
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:98
ContingencyTable< Idx, GUM_SCALAR > conTab__
The contingency table used to keeps records of all observation.
Definition: GTestPolicy.h:170
+ Here is the call 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 145 of file GTestPolicy.h.

145 { return conTab__; }
ContingencyTable< Idx, GUM_SCALAR > conTab__
The contingency table used to keeps records of all observation.
Definition: GTestPolicy.h:170

◆ isModified_()

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

Definition at line 165 of file ITestPolicy.h.

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

165 { return isModified__; }
bool isModified__
Booleans indicating if we have to re eval test.
Definition: ITestPolicy.h:169

◆ 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 107 of file GTestPolicy.h.

107  {
108  return (this->nbObservation() > 20
109  && this->nbObservation() > conTab__.attrASize() * 5);
110  }
Idx nbObservation() const
Comptabilizes the new observation.
Definition: ITestPolicy.h:98
Idx attrASize() const
Returns the number of samples for line iattr.
ContingencyTable< Idx, GUM_SCALAR > conTab__
The contingency table used to keeps records of all observation.
Definition: GTestPolicy.h:170

◆ nbObservation()

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

Comptabilizes the new observation.

Definition at line 98 of file ITestPolicy.h.

98 { return nbObs__; }

◆ operator delete()

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

Constructor.

Definition at line 79 of file GTestPolicy.h.

79  {
81  }
void deallocate(void *pDeallocatedObject, const size_t &objectSize)
Deallocates an object.
GTestPolicy()
Constructor.
Definition: GTestPolicy.h:63
static SmallObjectAllocator & instance()

◆ operator new()

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

Allocators and Deallocators redefinition.

Definition at line 75 of file GTestPolicy.h.

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

◆ 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 91 of file GTestPolicy_tpl.h.

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

91  {
92  if (this->isModified_()) computeScore();
93  // std::cout << this->toString() << std::endl;
94  double score = 1
96  (conTab__.attrASize() - 1)
97  * (conTab__.attrBSize() - 1));
98  return score;
99  }
void computeScore() const
Computes the GStat of current variable according to the test.
Idx attrBSize() const
Returns the number of samples for column ivalue.
bool isModified_() const
Definition: ITestPolicy.h:165
static double probaChi2(double x, Size df)
computes the probability of chi2 value (used by the cache)
Definition: chiSquare.cpp:117
double score() const
Returns the performance of current variable according to the test.
Idx attrASize() const
Returns the number of samples for line iattr.
ContingencyTable< Idx, GUM_SCALAR > conTab__
The contingency table used to keeps records of all observation.
Definition: GTestPolicy.h:170
+ Here is the call 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 105 of file GTestPolicy_tpl.h.

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

105  {
106  if (this->isModified_()) computeScore();
107  return GStat__;
108  }
void computeScore() const
Computes the GStat of current variable according to the test.
bool isModified_() const
Definition: ITestPolicy.h:165
+ Here is the call graph for this function:

◆ toString()

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

Definition at line 156 of file GTestPolicy.h.

156  {
157  std::stringstream ss;
158  ss << ITestPolicy< GUM_SCALAR >::toString()
159  << "\t\t\tContingency Table : " << std::endl
160  << conTab__.toString() << std::endl
161  << "\t\t\tGStat : " << GStat__ << std::endl
162  << "\t\t\tGStat : " << this->secondaryscore() << std::endl;
163  return ss.str();
164  }
std::string toString() const
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.
Definition: GTestPolicy.h:170

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 170 of file GTestPolicy.h.

◆ GStat__

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

Definition at line 171 of file GTestPolicy.h.


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