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

<agrum/tools/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, IdxnbObsTable__
 Nb Observation for each modality assumed by variable. More...
 
HashTable< Idx, doublesumAttrTable__
 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/tools/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.

References gum::LeastSquareTestPolicy< GUM_SCALAR >::LeastSquareTestPolicy().

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

62  :
63  ITestPolicy< GUM_SCALAR >(), sumO__(0.0), score__(0) {
64  GUM_CONSTRUCTOR(LeastSquareTestPolicy);
65  }
double score__
Keeping computed score.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ~LeastSquareTestPolicy()

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

Destructor.

Definition at line 36 of file leastSquareTestPolicy_tpl.h.

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

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

Member Function Documentation

◆ 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__.

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

◆ 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 127 of file leastSquareTestPolicy_tpl.h.

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

127  {
129 
130  for (auto obsIter = src.nbObsTable().cbeginSafe();
131  obsIter != src.nbObsTable().cendSafe();
132  ++obsIter)
133  if (nbObsTable__.exists(obsIter.key()))
134  nbObsTable__[obsIter.key()] += obsIter.val();
135  else
136  nbObsTable__.insert(obsIter.key(), obsIter.val());
137 
138  for (auto attrIter = src.sumAttrTable().cbeginSafe();
139  attrIter != src.sumAttrTable().cendSafe();
140  ++attrIter)
141  if (sumAttrTable__.exists(attrIter.key()))
142  sumAttrTable__[attrIter.key()] += attrIter.val();
143  else
144  sumAttrTable__.insert(attrIter.key(), attrIter.val());
145 
146  for (auto obsIter = src.obsTable().cbeginSafe();
147  obsIter != src.obsTable().cendSafe();
148  ++obsIter) {
149  if (!obsTable__.exists(obsIter.key()))
150  obsTable__.insert(obsIter.key(), new LinkedList< double >());
151  const Link< double >* srcLink = obsIter.val()->list();
152  while (srcLink) {
153  obsTable__[obsIter.key()]->addLink(srcLink->element());
154  srcLink = srcLink->nextLink();
155  }
156  }
157  }
HashTable< Idx, double > sumAttrTable__
Sum for each modality assumed by variable.
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.
void add(const ITestPolicy< GUM_SCALAR > &src)
Definition: ITestPolicy.h:140
HashTable< Idx, LinkedList< double > *> obsTable__
Not sure if needed.
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:

◆ 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 54 of file leastSquareTestPolicy_tpl.h.

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

55  {
57  sumO__ += value;
58 
59  if (sumAttrTable__.exists(attr))
60  sumAttrTable__[attr] += value;
61  else
62  sumAttrTable__.insert(attr, value);
63 
64  if (nbObsTable__.exists(attr))
65  nbObsTable__[attr]++;
66  else
67  nbObsTable__.insert(attr, 1);
68 
69  if (!obsTable__.exists(attr))
70  obsTable__.insert(attr, new LinkedList< double >());
71  obsTable__[attr]->addLink(value);
72  }
HashTable< Idx, double > sumAttrTable__
Sum for each modality assumed by variable.
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, LinkedList< double > *> obsTable__
Not sure if needed.
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:

◆ 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__.

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

◆ 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 83 of file leastSquareTestPolicy_tpl.h.

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

83  {
85  double mean = sumO__ / (double)this->nbObservation();
86  double errorO = 0.0;
87  double sumErrorAttr = 0.0;
88  for (auto attrIter = sumAttrTable__.cbeginSafe();
89  attrIter != sumAttrTable__.cendSafe();
90  ++attrIter) {
91  Idx key = attrIter.key();
92  double meanAttr = sumAttrTable__[key] / (double)nbObsTable__[key];
93  double errorAttr = 0.0;
94 
95  const Link< double >* linky = obsTable__[key]->list();
96  while (linky) {
97  errorAttr += std::pow(linky->element() - meanAttr, 2);
98  errorO += std::pow(linky->element() - mean, 2);
99  linky = linky->nextLink();
100  }
101 
102  sumErrorAttr += ((double)nbObsTable__[key] / (double)this->nbObservation())
103  * errorAttr;
104  }
105  score__ = errorO - sumErrorAttr;
106  }
virtual void computeScore() const
Recomputes the statistic from the beginning.
Definition: ITestPolicy.h:117
HashTable< Idx, double > sumAttrTable__
Sum for each modality assumed by variable.
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, LinkedList< double > *> obsTable__
Not sure if needed.
Idx nbObservation() const
Comptabilizes the new observation.
Definition: ITestPolicy.h:98
+ Here is the call graph for this function:

◆ 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::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.

105 { return (this->nbObservation() > 20); }
Idx nbObservation() const
Comptabilizes the new observation.
Definition: ITestPolicy.h:98

◆ 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__; }

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

153 { return nbObsTable__; }
HashTable< Idx, Idx > nbObsTable__
Nb Observation for each modality assumed by variable.

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

163  {
164  return obsTable__;
165  }
HashTable< Idx, LinkedList< double > *> obsTable__
Not sure if needed.
+ Here is the call graph for this function:
+ 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.

73  {
75  sizeof(LeastSquareTestPolicy));
76  }
void deallocate(void *pDeallocatedObject, const size_t &objectSize)
Deallocates an object.
static SmallObjectAllocator & instance()

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

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

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

◆ 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 112 of file leastSquareTestPolicy_tpl.h.

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

112  {
113  if (this->isModified_()) computeScore();
114  return score__;
115  }
bool isModified_() const
Definition: ITestPolicy.h:165
double score__
Keeping computed score.
void computeScore()
Returns the performance of current variable according to the test.
+ Here is the call 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 121 of file leastSquareTestPolicy_tpl.h.

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

121  {
122  if (this->isModified_()) computeScore();
123  return score__;
124  }
bool isModified_() const
Definition: ITestPolicy.h:165
double score__
Keeping computed score.
void computeScore()
Returns the performance of current variable according to the test.
+ Here is the call 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.

158 { return sumAttrTable__; }
HashTable< Idx, double > sumAttrTable__
Sum for each modality assumed by variable.

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

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.

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.

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

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

◆ sumO__

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

Global sum.

Definition at line 169 of file leastSquareTestPolicy.h.


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