aGrUM  0.16.0
leastSquareTestPolicy_tpl.h
Go to the documentation of this file.
1 
29 // =========================================================================
31 // =========================================================================
32 
33 
34 namespace gum {
35 
36  template < typename GUM_SCALAR >
38  for (auto obsIter = this->__obsTable.cbeginSafe();
39  __obsTable.cendSafe() != obsIter;
40  ++obsIter)
41  delete obsIter.val();
42 
43  GUM_DESTRUCTOR(LeastSquareTestPolicy);
44  }
45 
46 
47  // ##########################################################################
48  //
49  // ##########################################################################
50 
51  // ==========================================================================
52  //
53  // ==========================================================================
54  template < typename GUM_SCALAR >
56  GUM_SCALAR value) {
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  }
74 
75 
76  // ############################################################################
77  // @name Test result
78  // ############################################################################
79 
80  // ============================================================================
81  // Computes the GStat of current variable according to the test
82  // ============================================================================
83  template < typename GUM_SCALAR >
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  }
108 
109  // ============================================================================
110  // Returns the performance of current variable according to the test
111  // ============================================================================
112  template < typename GUM_SCALAR >
114  if (this->_isModified()) computeScore();
115  return __score;
116  }
117 
118  // ============================================================================
119  // Returns a second criterion to severe ties
120  // ============================================================================
121  template < typename GUM_SCALAR >
123  if (this->_isModified()) computeScore();
124  return __score;
125  }
126 
127  template < typename GUM_SCALAR >
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  }
159 
160 } // End of namespace gum
const HashTable< Idx, LinkedList< double > *> & obsTable() const
Returns global sum (needed for the merging)
virtual void computeScore() const
Recomputes the statistic from the beginning.
Definition: ITestPolicy.h:117
double score()
Returns the performance of current variable according to the test.
const HashTable< Idx, Idx > & nbObsTable() const
Returns nbobs per modality table (needed for the merging)
virtual ~LeastSquareTestPolicy()
Destructor.
double secondaryscore() const
Returns a second criterion to severe ties.
const HashTable< Idx, double > & sumAttrTable() const
Returns sum per modality table (needed for the merging)
virtual void addObservation(Idx attr, GUM_SCALAR value)
Comptabilizes the new observation.
Definition: ITestPolicy.h:90
const_iterator_safe cbeginSafe() const
Returns the safe const_iterator pointing to the beginning of the hashtable.
void add(const LeastSquareTestPolicy< GUM_SCALAR > &src)
Performs the merging of current LeastSquareTestPolicy instance with given instance.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
<agrum/multidim/core/testPolicy/leastSquareTestPolicy.h>
void addObservation(Idx attr, GUM_SCALAR value)
Comptabilizes the new observation.
const const_iterator_safe & cendSafe() const noexcept
Returns the safe const_iterator pointing to the end of the hashtable.
void computeScore()
Returns the performance of current variable according to the test.
void add(const ITestPolicy< GUM_SCALAR > &src)
Definition: ITestPolicy.h:140
Chain list allocated using the SmallObjectAllocator.
Definition: link.h:134
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Size Idx
Type for indexes.
Definition: types.h:53