aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
leastSquareTestPolicy_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Template implementations for the LeastSquareTestPolicy class.
25  *
26  * @author Jean-Christophe MAGNAN
27  */
28 // =========================================================================
29 #include <agrum/FMDP/learning/core/testPolicy/leastSquareTestPolicy.h>
30 // =========================================================================
31 
32 
33 namespace gum {
34 
35  template < typename GUM_SCALAR >
37  for (auto obsIter = this->obsTable__.cbeginSafe();
39  ++obsIter)
40  delete obsIter.val();
41 
43  }
44 
45 
46  // ##########################################################################
47  //
48  // ##########################################################################
49 
50  // ==========================================================================
51  //
52  // ==========================================================================
53  template < typename GUM_SCALAR >
55  GUM_SCALAR value) {
57  sumO__ += value;
58 
61  else
63 
65  nbObsTable__[attr]++;
66  else
68 
69  if (!obsTable__.exists(attr))
70  obsTable__.insert(attr, new LinkedList< double >());
72  }
73 
74 
75  // ############################################################################
76  // @name Test result
77  // ############################################################################
78 
79  // ============================================================================
80  // Computes the GStat of current variable according to the test
81  // ============================================================================
82  template < typename GUM_SCALAR >
85  double mean = sumO__ / (double)this->nbObservation();
86  double errorO = 0.0;
87  double sumErrorAttr = 0.0;
88  for (auto attrIter = sumAttrTable__.cbeginSafe();
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  }
106  }
107 
108  // ============================================================================
109  // Returns the performance of current variable according to the test
110  // ============================================================================
111  template < typename GUM_SCALAR >
113  if (this->isModified_()) computeScore();
114  return score__;
115  }
116 
117  // ============================================================================
118  // Returns a second criterion to severe ties
119  // ============================================================================
120  template < typename GUM_SCALAR >
122  if (this->isModified_()) computeScore();
123  return score__;
124  }
125 
126  template < typename GUM_SCALAR >
129 
130  for (auto obsIter = src.nbObsTable().cbeginSafe();
132  ++obsIter)
135  else
137 
138  for (auto attrIter = src.sumAttrTable().cbeginSafe();
140  ++attrIter)
143  else
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) {
154  srcLink = srcLink->nextLink();
155  }
156  }
157  }
158 
159 } // End of namespace gum
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669