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

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

#include <leastSquareTestPolicy.h>

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

Public Member Functions

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

Protected Member Functions

bool isModified_ () const

Private Attributes

bool _isModified_
 Booleans indicating if we have to re eval test.
Idx _nbObs_

Fusion Methods

double _sumO_
 Global sum.
HashTable< Idx, Idx_nbObsTable_
 Nb Observation for each modality assumed by variable.
HashTable< Idx, double_sumAttrTable_
 Sum for each modality assumed by variable.
HashTable< Idx, LinkedList< double > * > _obsTable_
 Not sure if needed.
double _score_
 Keeping computed score.
void add (const LeastSquareTestPolicy< GUM_ELEMENT > &src)
 Performs the merging of current LeastSquareTestPolicy instance with given instance.
double sumValue () const
 Returns global sum (needed for the merging).
const HashTable< Idx, Idx > & nbObsTable () const
 Returns nbobs per modality table (needed for the merging).
const HashTable< Idx, double > & sumAttrTable () const
 Returns sum per modality table (needed for the merging).
const HashTable< Idx, LinkedList< double > * > & obsTable () const
 Returns global sum (needed for the merging).

Detailed Description

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

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

LeastSquareTestPolicy implements a test policy that follows the Least Square statistic

Definition at line 73 of file leastSquareTestPolicy.h.

Constructor & Destructor Documentation

◆ LeastSquareTestPolicy()

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

Constructor.

Definition at line 164 of file leastSquareTestPolicy_tpl.h.

164 :
167 }
ITestPolicy()
Allocators and Deallocators redefinition.
<agrum/base/multidim/core/testPolicy/leastSquareTestPolicy.h>
double _score_
Keeping computed score.

References gum::ITestPolicy< GUM_ELEMENT >::ITestPolicy(), LeastSquareTestPolicy(), _score_, and _sumO_.

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

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

◆ ~LeastSquareTestPolicy()

template<typename GUM_ELEMENT>
gum::LeastSquareTestPolicy< GUM_ELEMENT >::~LeastSquareTestPolicy ( )
virtual

Destructor.

Definition at line 59 of file leastSquareTestPolicy_tpl.h.

59 {
60 for (auto obsIter = this->_obsTable_.cbeginSafe(); _obsTable_.cendSafe() != obsIter; ++obsIter)
61 delete obsIter.val();
62
64 }
HashTable< Idx, LinkedList< double > * > _obsTable_
Not sure if needed.

References LeastSquareTestPolicy(), and _obsTable_.

Here is the call graph for this function:

Member Function Documentation

◆ add() [1/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:

◆ add() [2/2]

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

Performs the merging of current LeastSquareTestPolicy instance with given instance.

Definition at line 138 of file leastSquareTestPolicy_tpl.h.

138 {
140
142 ++obsIter)
143 if (_nbObsTable_.exists(obsIter.key())) _nbObsTable_[obsIter.key()] += obsIter.val();
144 else _nbObsTable_.insert(obsIter.key(), obsIter.val());
145
147 ++attrIter)
148 if (_sumAttrTable_.exists(attrIter.key())) _sumAttrTable_[attrIter.key()] += attrIter.val();
149 else _sumAttrTable_.insert(attrIter.key(), attrIter.val());
150
151 for (auto obsIter = src.obsTable().cbeginSafe(); obsIter != src.obsTable().cendSafe();
152 ++obsIter) {
153 if (!_obsTable_.exists(obsIter.key()))
154 _obsTable_.insert(obsIter.key(), new LinkedList< double >());
155 const Link< double >* srcLink = obsIter.val()->list();
156 while (srcLink) {
157 _obsTable_[obsIter.key()]->addLink(srcLink->element());
158 srcLink = srcLink->nextLink();
159 }
160 }
161 }
const_iterator_safe cbeginSafe() const
Returns the safe const_iterator pointing to the beginning of the hashtable.
const const_iterator_safe & cendSafe() const noexcept
Returns the safe const_iterator pointing to the end of the hashtable.
void add(const ITestPolicy< GUM_ELEMENT > &src)
HashTable< Idx, Idx > _nbObsTable_
Nb Observation for each modality assumed by variable.
const HashTable< Idx, LinkedList< double > * > & obsTable() const
Returns global sum (needed for the merging).
const HashTable< Idx, double > & sumAttrTable() const
Returns sum per modality table (needed for the merging).
HashTable< Idx, double > _sumAttrTable_
Sum for each modality assumed by variable.
const HashTable< Idx, Idx > & nbObsTable() const
Returns nbobs per modality table (needed for the merging).

References LeastSquareTestPolicy(), _nbObsTable_, _obsTable_, _sumAttrTable_, gum::ITestPolicy< GUM_ELEMENT >::add(), gum::HashTable< Key, Val >::cbeginSafe(), gum::HashTable< Key, Val >::cendSafe(), gum::Link< T >::element(), nbObsTable(), gum::Link< T >::nextLink(), obsTable(), and sumAttrTable().

Here is the call graph for this function:

◆ addObservation()

template<typename GUM_ELEMENT>
void gum::LeastSquareTestPolicy< GUM_ELEMENT >::addObservation ( Idx attr,
GUM_ELEMENT value )
virtual

Comptabilizes the new observation.

Reimplemented from gum::ITestPolicy< GUM_ELEMENT >.

Definition at line 74 of file leastSquareTestPolicy_tpl.h.

74 {
76 _sumO_ += value;
77
79 else _sumAttrTable_.insert(attr, value);
80
81 if (_nbObsTable_.exists(attr)) _nbObsTable_[attr]++;
82 else _nbObsTable_.insert(attr, 1);
83
84 if (!_obsTable_.exists(attr)) _obsTable_.insert(attr, new LinkedList< double >());
85 _obsTable_[attr]->addLink(value);
86 }
virtual void addObservation(Idx attr, GUM_ELEMENT value)
Comptabilizes the new observation.

References _nbObsTable_, _obsTable_, _sumAttrTable_, _sumO_, and gum::ITestPolicy< GUM_ELEMENT >::addObservation().

Here is the call graph for this function:

◆ computeScore() [1/2]

template<typename GUM_ELEMENT>
void gum::ITestPolicy< GUM_ELEMENT >::computeScore ( ) const
virtualinherited

Recomputes the statistic from the beginning.

Reimplemented in gum::Chi2TestPolicy< GUM_ELEMENT >, and gum::GTestPolicy< GUM_ELEMENT >.

Definition at line 81 of file ITestPolicy_tpl.h.

81 {
82 _isModified_ = false;
83 }

References _isModified_.

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

Here is the caller graph for this function:

◆ computeScore() [2/2]

template<typename GUM_ELEMENT>
void gum::LeastSquareTestPolicy< GUM_ELEMENT >::computeScore ( )

Returns the performance of current variable according to the test.

Definition at line 96 of file leastSquareTestPolicy_tpl.h.

96 {
98 double mean = _sumO_ / (double)this->nbObservation();
99 double errorO = 0.0;
100 double sumErrorAttr = 0.0;
101 for (auto attrIter = _sumAttrTable_.cbeginSafe(); attrIter != _sumAttrTable_.cendSafe();
102 ++attrIter) {
103 Idx key = attrIter.key();
105 double errorAttr = 0.0;
106
107 const Link< double >* linky = _obsTable_[key]->list();
108 while (linky) {
109 errorAttr += std::pow(linky->element() - meanAttr, 2);
110 errorO += std::pow(linky->element() - mean, 2);
111 linky = linky->nextLink();
112 }
113
115 }
117 }
virtual void computeScore() const
Recomputes the statistic from the beginning.

References _nbObsTable_, _obsTable_, _score_, _sumAttrTable_, _sumO_, gum::ITestPolicy< GUM_ELEMENT >::computeScore(), gum::Link< T >::element(), gum::ITestPolicy< GUM_ELEMENT >::nbObservation(), and gum::Link< T >::nextLink().

Referenced by score(), and secondaryscore().

Here is the call graph for this function:
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::LeastSquareTestPolicy< GUM_ELEMENT >::isTestRelevant ( ) const
virtual

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

Implements gum::ITestPolicy< GUM_ELEMENT >.

Definition at line 180 of file leastSquareTestPolicy_tpl.h.

180 {
181 return (this->nbObservation() > 20);
182 }

References 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:

◆ nbObsTable()

template<typename GUM_ELEMENT>
const HashTable< Idx, Idx > & gum::LeastSquareTestPolicy< GUM_ELEMENT >::nbObsTable ( ) const

Returns nbobs per modality table (needed for the merging).

Definition at line 190 of file leastSquareTestPolicy_tpl.h.

190 {
191 return _nbObsTable_;
192 }

References _nbObsTable_.

Referenced by add().

Here is the caller graph for this function:

◆ obsTable()

template<typename GUM_ELEMENT>
const HashTable< Idx, LinkedList< double > * > & gum::LeastSquareTestPolicy< GUM_ELEMENT >::obsTable ( ) const

Returns global sum (needed for the merging).

Definition at line 201 of file leastSquareTestPolicy_tpl.h.

201 {
202 return _obsTable_;
203 }

References _obsTable_.

Referenced by add().

Here is the caller graph for this function:

◆ operator delete()

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

Constructor.

Definition at line 175 of file leastSquareTestPolicy_tpl.h.

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

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

Here is the call graph for this function:

◆ operator new()

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

Allocators and Deallocators redefinition.

Definition at line 170 of file leastSquareTestPolicy_tpl.h.

170 {
172 }
void * allocate(const size_t &objectSize)
Allocates a block.

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

Here is the call graph for this function:

◆ score() [1/2]

template<typename GUM_ELEMENT>
virtual double gum::ITestPolicy< GUM_ELEMENT >::score ( ) const
pure virtualinherited

Returns the performance of current variable according to the test.

Implemented in gum::Chi2TestPolicy< GUM_ELEMENT >, and gum::GTestPolicy< GUM_ELEMENT >.

◆ score() [2/2]

template<typename GUM_ELEMENT>
double gum::LeastSquareTestPolicy< GUM_ELEMENT >::score ( )

Returns the performance of current variable according to the test.

Definition at line 123 of file leastSquareTestPolicy_tpl.h.

123 {
124 if (this->isModified_()) computeScore();
125 return _score_;
126 }
bool isModified_() const
void computeScore()
Returns the performance of current variable according to the test.

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

Here is the call graph for this function:

◆ secondaryscore()

template<typename GUM_ELEMENT>
double gum::LeastSquareTestPolicy< GUM_ELEMENT >::secondaryscore ( ) const
virtual

Returns a second criterion to severe ties.

Implements gum::ITestPolicy< GUM_ELEMENT >.

Definition at line 132 of file leastSquareTestPolicy_tpl.h.

132 {
133 if (this->isModified_()) computeScore();
134 return _score_;
135 }

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

Here is the call graph for this function:

◆ sumAttrTable()

template<typename GUM_ELEMENT>
const HashTable< Idx, double > & gum::LeastSquareTestPolicy< GUM_ELEMENT >::sumAttrTable ( ) const

Returns sum per modality table (needed for the merging).

Definition at line 195 of file leastSquareTestPolicy_tpl.h.

195 {
196 return _sumAttrTable_;
197 }

References _sumAttrTable_.

Referenced by add().

Here is the caller graph for this function:

◆ sumValue()

template<typename GUM_ELEMENT>
double gum::LeastSquareTestPolicy< GUM_ELEMENT >::sumValue ( ) const

Returns global sum (needed for the merging).

Definition at line 185 of file leastSquareTestPolicy_tpl.h.

185 {
186 return _sumO_;
187 }

References _sumO_.

◆ toString()

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

Definition at line 92 of file ITestPolicy_tpl.h.

92 {
93 return std::format("\t\t\tNb Obs : {}\n", _nbObs_);
94 }

References _nbObs_.

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

Here is the caller graph for this function:

Member Data Documentation

◆ _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().

◆ _nbObsTable_

template<typename GUM_ELEMENT>
HashTable< Idx, Idx > gum::LeastSquareTestPolicy< GUM_ELEMENT >::_nbObsTable_
private

Nb Observation for each modality assumed by variable.

Definition at line 183 of file leastSquareTestPolicy.h.

Referenced by add(), addObservation(), computeScore(), and nbObsTable().

◆ _obsTable_

template<typename GUM_ELEMENT>
HashTable< Idx, LinkedList< double >* > gum::LeastSquareTestPolicy< GUM_ELEMENT >::_obsTable_
private

Not sure if needed.

Definition at line 189 of file leastSquareTestPolicy.h.

Referenced by ~LeastSquareTestPolicy(), add(), addObservation(), computeScore(), and obsTable().

◆ _score_

template<typename GUM_ELEMENT>
double gum::LeastSquareTestPolicy< GUM_ELEMENT >::_score_
private

Keeping computed score.

Definition at line 192 of file leastSquareTestPolicy.h.

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

◆ _sumAttrTable_

template<typename GUM_ELEMENT>
HashTable< Idx, double > gum::LeastSquareTestPolicy< GUM_ELEMENT >::_sumAttrTable_
private

Sum for each modality assumed by variable.

Definition at line 186 of file leastSquareTestPolicy.h.

Referenced by add(), addObservation(), computeScore(), and sumAttrTable().

◆ _sumO_

template<typename GUM_ELEMENT>
double gum::LeastSquareTestPolicy< GUM_ELEMENT >::_sumO_
private

Global sum.

Definition at line 180 of file leastSquareTestPolicy.h.

Referenced by LeastSquareTestPolicy(), addObservation(), computeScore(), and sumValue().


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