aGrUM  0.20.3
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, 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/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.

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

◆ ~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(); _obsTable_.cendSafe() != obsIter; ++obsIter)
38  delete obsIter.val();
39 
40  GUM_DESTRUCTOR(LeastSquareTestPolicy);
41  }
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::LeastSquareTestPolicy< GUM_SCALAR >::add ( const LeastSquareTestPolicy< GUM_SCALAR > &  src)

Performs the merging of current LeastSquareTestPolicy instance with given instance.

Definition at line 121 of file leastSquareTestPolicy_tpl.h.

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

121  {
123 
124  for (auto obsIter = src.nbObsTable().cbeginSafe(); obsIter != src.nbObsTable().cendSafe();
125  ++obsIter)
126  if (_nbObsTable_.exists(obsIter.key()))
127  _nbObsTable_[obsIter.key()] += obsIter.val();
128  else
129  _nbObsTable_.insert(obsIter.key(), obsIter.val());
130 
131  for (auto attrIter = src.sumAttrTable().cbeginSafe(); attrIter != src.sumAttrTable().cendSafe();
132  ++attrIter)
133  if (_sumAttrTable_.exists(attrIter.key()))
134  _sumAttrTable_[attrIter.key()] += attrIter.val();
135  else
136  _sumAttrTable_.insert(attrIter.key(), attrIter.val());
137 
138  for (auto obsIter = src.obsTable().cbeginSafe(); obsIter != src.obsTable().cendSafe();
139  ++obsIter) {
140  if (!_obsTable_.exists(obsIter.key()))
141  _obsTable_.insert(obsIter.key(), new LinkedList< double >());
142  const Link< double >* srcLink = obsIter.val()->list();
143  while (srcLink) {
144  _obsTable_[obsIter.key()]->addLink(srcLink->element());
145  srcLink = srcLink->nextLink();
146  }
147  }
148  }
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
HashTable< Idx, LinkedList< double > *> _obsTable_
Not sure if needed.
HashTable< Idx, Idx > _nbObsTable_
Nb Observation for each modality assumed by variable.
void add(const ITestPolicy< GUM_SCALAR > &src)
Definition: ITestPolicy.h:139
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
HashTable< Idx, double > _sumAttrTable_
Sum for each modality assumed by variable.
+ 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 139 of file ITestPolicy.h.

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

139  {
140  _isModified_ = true;
141  _nbObs_ += src.nbObservation();
142  }
bool _isModified_
Booleans indicating if we have to re eval test.
Definition: ITestPolicy.h:168

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

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

52  {
54  _sumO_ += value;
55 
56  if (_sumAttrTable_.exists(attr))
57  _sumAttrTable_[attr] += value;
58  else
59  _sumAttrTable_.insert(attr, value);
60 
61  if (_nbObsTable_.exists(attr))
62  _nbObsTable_[attr]++;
63  else
64  _nbObsTable_.insert(attr, 1);
65 
66  if (!_obsTable_.exists(attr)) _obsTable_.insert(attr, new LinkedList< double >());
67  _obsTable_[attr]->addLink(value);
68  }
virtual void addObservation(Idx attr, GUM_SCALAR value)
Comptabilizes the new observation.
Definition: ITestPolicy.h:89
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
HashTable< Idx, LinkedList< double > *> _obsTable_
Not sure if needed.
HashTable< Idx, Idx > _nbObsTable_
Nb Observation 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.
HashTable< Idx, double > _sumAttrTable_
Sum for each modality assumed by variable.
+ Here is the call graph for this function:

◆ computeScore() [1/2]

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

Returns the performance of current variable according to the test.

Definition at line 79 of file leastSquareTestPolicy_tpl.h.

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

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

◆ computeScore() [2/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 116 of file ITestPolicy.h.

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

116 { _isModified_ = false; }
bool _isModified_
Booleans indicating if we have to re eval test.
Definition: ITestPolicy.h:168

◆ isModified_()

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

Definition at line 164 of file ITestPolicy.h.

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

164 { return _isModified_; }
bool _isModified_
Booleans indicating if we have to re eval test.
Definition: ITestPolicy.h:168

◆ 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 101 of file leastSquareTestPolicy.h.

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

◆ nbObservation()

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

Comptabilizes the new observation.

Definition at line 97 of file ITestPolicy.h.

97 { 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 149 of file leastSquareTestPolicy.h.

149 { 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 159 of file leastSquareTestPolicy.h.

159 { return _obsTable_; }
HashTable< Idx, LinkedList< double > *> _obsTable_
Not sure if needed.

◆ operator delete()

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

Constructor.

Definition at line 70 of file leastSquareTestPolicy.h.

70  {
72  }
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 69 of file leastSquareTestPolicy.h.

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

◆ score() [1/2]

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

Returns the performance of current variable according to the test.

Definition at line 106 of file leastSquareTestPolicy_tpl.h.

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

106  {
107  if (this->isModified_()) computeScore();
108  return _score_;
109  }
double _score_
Keeping computed score.
bool isModified_() const
Definition: ITestPolicy.h:164
void computeScore()
Returns the performance of current variable according to the test.
+ Here is the call graph for this function:

◆ score() [2/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 >.

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

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

115  {
116  if (this->isModified_()) computeScore();
117  return _score_;
118  }
double _score_
Keeping computed score.
bool isModified_() const
Definition: ITestPolicy.h:164
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 154 of file leastSquareTestPolicy.h.

154 { 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 144 of file leastSquareTestPolicy.h.

144 { return _sumO_; }

◆ toString()

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

Definition at line 155 of file ITestPolicy.h.

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

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 166 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 172 of file leastSquareTestPolicy.h.

◆ _score_

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

Keeping computed score.

Definition at line 175 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 169 of file leastSquareTestPolicy.h.

◆ _sumO_

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

Global sum.

Definition at line 163 of file leastSquareTestPolicy.h.


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