aGrUM  0.16.0
gum::LeastSquareTestPolicy< GUM_SCALAR > Class Template Referenceabstract

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

#include <leastSquareTestPolicy.h>

+ Inheritance diagram for gum::LeastSquareTestPolicy< GUM_SCALAR >:
+ Collaboration diagram for gum::LeastSquareTestPolicy< GUM_SCALAR >:

Public Member Functions

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

Protected Member Functions

bool _isModified () const
 

Fusion Methods

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

Detailed Description

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

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

LeastSquareTestPolicy implements a test policy that follows the Least Square statistic

Definition at line 52 of file leastSquareTestPolicy.h.

Constructor & Destructor Documentation

◆ LeastSquareTestPolicy()

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

Constructor.

Definition at line 62 of file leastSquareTestPolicy.h.

62  :
63  ITestPolicy< GUM_SCALAR >(), __sumO(0.0), __score(0) {
64  GUM_CONSTRUCTOR(LeastSquareTestPolicy);
65  }
double __score
Keeping computed score.

◆ ~LeastSquareTestPolicy()

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

Destructor.

Definition at line 37 of file leastSquareTestPolicy_tpl.h.

Referenced by gum::LeastSquareTestPolicy< GUM_SCALAR >::operator delete().

37  {
38  for (auto obsIter = this->__obsTable.cbeginSafe();
39  __obsTable.cendSafe() != obsIter;
40  ++obsIter)
41  delete obsIter.val();
42 
43  GUM_DESTRUCTOR(LeastSquareTestPolicy);
44  }
HashTable< Idx, LinkedList< double > *> __obsTable
Not sure if needed.
+ Here is the caller graph for this function:

Member Function Documentation

◆ _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

◆ add() [1/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, gum::ITestPolicy< GUM_SCALAR >::__nbObs, and gum::ITestPolicy< GUM_SCALAR >::nbObservation().

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

140  {
141  __isModified = true;
142  __nbObs += src.nbObservation();
143  }
bool __isModified
Booleans indicating if we have to re eval test.
Definition: ITestPolicy.h:169
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ add() [2/2]

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

Performs the merging of current LeastSquareTestPolicy instance with given instance.

Definition at line 128 of file leastSquareTestPolicy_tpl.h.

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

Referenced by gum::LeastSquareTestPolicy< GUM_SCALAR >::isTestRelevant().

128  {
130 
131  for (auto obsIter = src.nbObsTable().cbeginSafe();
132  obsIter != src.nbObsTable().cendSafe();
133  ++obsIter)
134  if (__nbObsTable.exists(obsIter.key()))
135  __nbObsTable[obsIter.key()] += obsIter.val();
136  else
137  __nbObsTable.insert(obsIter.key(), obsIter.val());
138 
139  for (auto attrIter = src.sumAttrTable().cbeginSafe();
140  attrIter != src.sumAttrTable().cendSafe();
141  ++attrIter)
142  if (__sumAttrTable.exists(attrIter.key()))
143  __sumAttrTable[attrIter.key()] += attrIter.val();
144  else
145  __sumAttrTable.insert(attrIter.key(), attrIter.val());
146 
147  for (auto obsIter = src.obsTable().cbeginSafe();
148  obsIter != src.obsTable().cendSafe();
149  ++obsIter) {
150  if (!__obsTable.exists(obsIter.key()))
151  __obsTable.insert(obsIter.key(), new LinkedList< double >());
152  const Link< double >* srcLink = obsIter.val()->list();
153  while (srcLink) {
154  __obsTable[obsIter.key()]->addLink(srcLink->element());
155  srcLink = srcLink->nextLink();
156  }
157  }
158  }
HashTable< Idx, LinkedList< double > *> __obsTable
Not sure if needed.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
HashTable< Idx, Idx > __nbObsTable
Nb Observation for each modality assumed by variable.
HashTable< Idx, double > __sumAttrTable
Sum for each modality assumed by variable.
void add(const ITestPolicy< GUM_SCALAR > &src)
Definition: ITestPolicy.h:140
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ addObservation()

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

Comptabilizes the new observation.

Reimplemented from gum::ITestPolicy< GUM_SCALAR >.

Definition at line 55 of file leastSquareTestPolicy_tpl.h.

References gum::ITestPolicy< GUM_SCALAR >::addObservation().

Referenced by gum::LeastSquareTestPolicy< GUM_SCALAR >::operator delete().

56  {
58  __sumO += value;
59 
60  if (__sumAttrTable.exists(attr))
61  __sumAttrTable[attr] += value;
62  else
63  __sumAttrTable.insert(attr, value);
64 
65  if (__nbObsTable.exists(attr))
66  __nbObsTable[attr]++;
67  else
68  __nbObsTable.insert(attr, 1);
69 
70  if (!__obsTable.exists(attr))
71  __obsTable.insert(attr, new LinkedList< double >());
72  __obsTable[attr]->addLink(value);
73  }
HashTable< Idx, LinkedList< double > *> __obsTable
Not sure if needed.
virtual void addObservation(Idx attr, GUM_SCALAR value)
Comptabilizes the new observation.
Definition: ITestPolicy.h:90
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
HashTable< Idx, Idx > __nbObsTable
Nb Observation for each modality assumed by variable.
HashTable< Idx, double > __sumAttrTable
Sum for each modality assumed by variable.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ computeScore() [1/2]

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

Recomputes the statistic from the beginning.

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

Definition at line 117 of file ITestPolicy.h.

References gum::ITestPolicy< GUM_SCALAR >::__isModified, gum::ITestPolicy< GUM_SCALAR >::score(), and gum::ITestPolicy< GUM_SCALAR >::secondaryscore().

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

117 { __isModified = false; }
bool __isModified
Booleans indicating if we have to re eval test.
Definition: ITestPolicy.h:169
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ computeScore() [2/2]

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

Returns the performance of current variable according to the test.

Definition at line 84 of file leastSquareTestPolicy_tpl.h.

References gum::ITestPolicy< GUM_SCALAR >::computeScore(), gum::Link< T >::element(), and gum::Link< T >::nextLink().

Referenced by gum::LeastSquareTestPolicy< GUM_SCALAR >::isTestRelevant().

84  {
86  double mean = __sumO / (double)this->nbObservation();
87  double errorO = 0.0;
88  double sumErrorAttr = 0.0;
89  for (auto attrIter = __sumAttrTable.cbeginSafe();
90  attrIter != __sumAttrTable.cendSafe();
91  ++attrIter) {
92  Idx key = attrIter.key();
93  double meanAttr = __sumAttrTable[key] / (double)__nbObsTable[key];
94  double errorAttr = 0.0;
95 
96  const Link< double >* linky = __obsTable[key]->list();
97  while (linky) {
98  errorAttr += std::pow(linky->element() - meanAttr, 2);
99  errorO += std::pow(linky->element() - mean, 2);
100  linky = linky->nextLink();
101  }
102 
103  sumErrorAttr +=
104  ((double)__nbObsTable[key] / (double)this->nbObservation()) * errorAttr;
105  }
106  __score = errorO - sumErrorAttr;
107  }
virtual void computeScore() const
Recomputes the statistic from the beginning.
Definition: ITestPolicy.h:117
HashTable< Idx, LinkedList< double > *> __obsTable
Not sure if needed.
const_iterator_safe cbeginSafe() const
Returns the safe const_iterator pointing to the beginning of the hashtable.
HashTable< Idx, Idx > __nbObsTable
Nb Observation for each modality assumed by variable.
const const_iterator_safe & cendSafe() const noexcept
Returns the safe const_iterator pointing to the end of the hashtable.
double __score
Keeping computed score.
HashTable< Idx, double > __sumAttrTable
Sum for each modality assumed by variable.
Idx nbObservation() const
Comptabilizes the new observation.
Definition: ITestPolicy.h:98
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isTestRelevant()

template<typename GUM_SCALAR>
bool gum::LeastSquareTestPolicy< 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 105 of file leastSquareTestPolicy.h.

References gum::LeastSquareTestPolicy< GUM_SCALAR >::add(), gum::LeastSquareTestPolicy< GUM_SCALAR >::computeScore(), gum::ITestPolicy< GUM_SCALAR >::nbObservation(), gum::LeastSquareTestPolicy< GUM_SCALAR >::score(), and gum::LeastSquareTestPolicy< GUM_SCALAR >::secondaryscore().

105 { return (this->nbObservation() > 20); }
Idx nbObservation() const
Comptabilizes the new observation.
Definition: ITestPolicy.h:98
+ 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 98 of file ITestPolicy.h.

References gum::ITestPolicy< GUM_SCALAR >::__nbObs, and gum::ITestPolicy< GUM_SCALAR >::isTestRelevant().

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

98 { return __nbObs; }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nbObsTable()

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

Returns nbobs per modality table (needed for the merging)

Definition at line 153 of file leastSquareTestPolicy.h.

References gum::LeastSquareTestPolicy< GUM_SCALAR >::__nbObsTable.

Referenced by gum::LeastSquareTestPolicy< GUM_SCALAR >::add().

153 { return __nbObsTable; }
HashTable< Idx, Idx > __nbObsTable
Nb Observation for each modality assumed by variable.
+ Here is the caller graph for this function:

◆ obsTable()

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

Returns global sum (needed for the merging)

Definition at line 163 of file leastSquareTestPolicy.h.

References gum::LeastSquareTestPolicy< GUM_SCALAR >::__obsTable.

Referenced by gum::LeastSquareTestPolicy< GUM_SCALAR >::add().

163  {
164  return __obsTable;
165  }
HashTable< Idx, LinkedList< double > *> __obsTable
Not sure if needed.
+ Here is the caller graph for this function:

◆ operator delete()

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

Constructor.

Definition at line 73 of file leastSquareTestPolicy.h.

References gum::LeastSquareTestPolicy< GUM_SCALAR >::addObservation(), gum::SmallObjectAllocator::deallocate(), gum::SmallObjectAllocator::instance(), and gum::LeastSquareTestPolicy< GUM_SCALAR >::~LeastSquareTestPolicy().

73  {
75  sizeof(LeastSquareTestPolicy));
76  }
void deallocate(void *pDeallocatedObject, const size_t &objectSize)
Deallocates an object.
static SmallObjectAllocator & instance()
+ Here is the call graph for this function:

◆ operator new()

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

Allocators and Deallocators redefinition.

Definition at line 70 of file leastSquareTestPolicy.h.

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

70  {
72  }
void * allocate(const size_t &objectSize)
Allocates a block.
static SmallObjectAllocator & instance()
+ Here is the call graph for this function:

◆ score() [1/2]

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

Returns the performance of current variable according to the test.

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

Referenced by gum::ITestPolicy< GUM_SCALAR >::computeScore().

+ Here is the caller graph for this function:

◆ score() [2/2]

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

Returns the performance of current variable according to the test.

Definition at line 113 of file leastSquareTestPolicy_tpl.h.

Referenced by gum::LeastSquareTestPolicy< GUM_SCALAR >::isTestRelevant().

113  {
114  if (this->_isModified()) computeScore();
115  return __score;
116  }
void computeScore()
Returns the performance of current variable according to the test.
double __score
Keeping computed score.
bool _isModified() const
Definition: ITestPolicy.h:165
+ Here is the caller graph for this function:

◆ secondaryscore()

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

Returns a second criterion to severe ties.

Implements gum::ITestPolicy< GUM_SCALAR >.

Definition at line 122 of file leastSquareTestPolicy_tpl.h.

Referenced by gum::LeastSquareTestPolicy< GUM_SCALAR >::isTestRelevant().

122  {
123  if (this->_isModified()) computeScore();
124  return __score;
125  }
void computeScore()
Returns the performance of current variable according to the test.
double __score
Keeping computed score.
bool _isModified() const
Definition: ITestPolicy.h:165
+ Here is the caller graph for this function:

◆ sumAttrTable()

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

Returns sum per modality table (needed for the merging)

Definition at line 158 of file leastSquareTestPolicy.h.

References gum::LeastSquareTestPolicy< GUM_SCALAR >::__sumAttrTable.

Referenced by gum::LeastSquareTestPolicy< GUM_SCALAR >::add().

158 { return __sumAttrTable; }
HashTable< Idx, double > __sumAttrTable
Sum for each modality assumed by variable.
+ Here is the caller graph for this function:

◆ sumValue()

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

Returns global sum (needed for the merging)

Definition at line 148 of file leastSquareTestPolicy.h.

References gum::LeastSquareTestPolicy< GUM_SCALAR >::__sumO.

148 { return __sumO; }

◆ toString()

template<typename GUM_SCALAR>
std::string gum::ITestPolicy< GUM_SCALAR >::toString ( ) const
inlineinherited

Definition at line 156 of file ITestPolicy.h.

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

156  {
157  std::stringstream ss;
158  ss << "\t\t\tNb Obs : " << __nbObs << std::endl;
159  return ss.str();
160  }

Member Data Documentation

◆ __nbObsTable

template<typename GUM_SCALAR>
HashTable< Idx, Idx > gum::LeastSquareTestPolicy< GUM_SCALAR >::__nbObsTable
private

Nb Observation for each modality assumed by variable.

Definition at line 172 of file leastSquareTestPolicy.h.

Referenced by gum::LeastSquareTestPolicy< GUM_SCALAR >::nbObsTable().

◆ __obsTable

template<typename GUM_SCALAR>
HashTable< Idx, LinkedList< double >* > gum::LeastSquareTestPolicy< GUM_SCALAR >::__obsTable
private

Not sure if needed.

Definition at line 178 of file leastSquareTestPolicy.h.

Referenced by gum::LeastSquareTestPolicy< GUM_SCALAR >::obsTable().

◆ __score

template<typename GUM_SCALAR>
double gum::LeastSquareTestPolicy< GUM_SCALAR >::__score
private

Keeping computed score.

Definition at line 181 of file leastSquareTestPolicy.h.

◆ __sumAttrTable

template<typename GUM_SCALAR>
HashTable< Idx, double > gum::LeastSquareTestPolicy< GUM_SCALAR >::__sumAttrTable
private

Sum for each modality assumed by variable.

Definition at line 175 of file leastSquareTestPolicy.h.

Referenced by gum::LeastSquareTestPolicy< GUM_SCALAR >::sumAttrTable().

◆ __sumO

template<typename GUM_SCALAR>
double gum::LeastSquareTestPolicy< GUM_SCALAR >::__sumO
private

Global sum.

Definition at line 169 of file leastSquareTestPolicy.h.

Referenced by gum::LeastSquareTestPolicy< GUM_SCALAR >::sumValue().


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