aGrUM  0.14.2
leastSquareTestPolicy_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 // =========================================================================
28 // =========================================================================
29 
30 
31 namespace gum {
32 
33  template < typename GUM_SCALAR >
35  for (auto obsIter = this->__obsTable.cbeginSafe();
36  __obsTable.cendSafe() != obsIter;
37  ++obsIter)
38  delete obsIter.val();
39 
40  GUM_DESTRUCTOR(LeastSquareTestPolicy);
41  }
42 
43 
44  // ##########################################################################
45  //
46  // ##########################################################################
47 
48  // ==========================================================================
49  //
50  // ==========================================================================
51  template < typename GUM_SCALAR >
53  GUM_SCALAR value) {
55  __sumO += value;
56 
57  if (__sumAttrTable.exists(attr))
58  __sumAttrTable[attr] += value;
59  else
60  __sumAttrTable.insert(attr, value);
61 
62  if (__nbObsTable.exists(attr))
63  __nbObsTable[attr]++;
64  else
65  __nbObsTable.insert(attr, 1);
66 
67  if (!__obsTable.exists(attr))
68  __obsTable.insert(attr, new LinkedList< double >());
69  __obsTable[attr]->addLink(value);
70  }
71 
72 
73  // ############################################################################
74  // @name Test result
75  // ############################################################################
76 
77  // ============================================================================
78  // Computes the GStat of current variable according to the test
79  // ============================================================================
80  template < typename GUM_SCALAR >
83  double mean = __sumO / (double)this->nbObservation();
84  double errorO = 0.0;
85  double sumErrorAttr = 0.0;
86  for (auto attrIter = __sumAttrTable.cbeginSafe();
87  attrIter != __sumAttrTable.cendSafe();
88  ++attrIter) {
89  Idx key = attrIter.key();
90  double meanAttr = __sumAttrTable[key] / (double)__nbObsTable[key];
91  double errorAttr = 0.0;
92 
93  const Link< double >* linky = __obsTable[key]->list();
94  while (linky) {
95  errorAttr += std::pow(linky->element() - meanAttr, 2);
96  errorO += std::pow(linky->element() - mean, 2);
97  linky = linky->nextLink();
98  }
99 
100  sumErrorAttr +=
101  ((double)__nbObsTable[key] / (double)this->nbObservation()) * errorAttr;
102  }
103  __score = errorO - sumErrorAttr;
104  }
105 
106  // ============================================================================
107  // Returns the performance of current variable according to the test
108  // ============================================================================
109  template < typename GUM_SCALAR >
111  if (this->_isModified()) computeScore();
112  return __score;
113  }
114 
115  // ============================================================================
116  // Returns a second criterion to severe ties
117  // ============================================================================
118  template < typename GUM_SCALAR >
120  if (this->_isModified()) computeScore();
121  return __score;
122  }
123 
124  template < typename GUM_SCALAR >
127 
128  for (auto obsIter = src.nbObsTable().cbeginSafe();
129  obsIter != src.nbObsTable().cendSafe();
130  ++obsIter)
131  if (__nbObsTable.exists(obsIter.key()))
132  __nbObsTable[obsIter.key()] += obsIter.val();
133  else
134  __nbObsTable.insert(obsIter.key(), obsIter.val());
135 
136  for (auto attrIter = src.sumAttrTable().cbeginSafe();
137  attrIter != src.sumAttrTable().cendSafe();
138  ++attrIter)
139  if (__sumAttrTable.exists(attrIter.key()))
140  __sumAttrTable[attrIter.key()] += attrIter.val();
141  else
142  __sumAttrTable.insert(attrIter.key(), attrIter.val());
143 
144  for (auto obsIter = src.obsTable().cbeginSafe();
145  obsIter != src.obsTable().cendSafe();
146  ++obsIter) {
147  if (!__obsTable.exists(obsIter.key()))
148  __obsTable.insert(obsIter.key(), new LinkedList< double >());
149  const Link< double >* srcLink = obsIter.val()->list();
150  while (srcLink) {
151  __obsTable[obsIter.key()]->addLink(srcLink->element());
152  srcLink = srcLink->nextLink();
153  }
154  }
155  }
156 
157 } // 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:114
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:87
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.
gum is the global namespace for all aGrUM entities
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:137
Chain list allocated using the SmallObjectAllocator.
Definition: link.h:131
Headers of the LeastSquareTestPolicy.
Size Idx
Type for indexes.
Definition: types.h:50