aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
leastSquareTestPolicy_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
42
49// =========================================================================
51
52// =========================================================================
53
54
55namespace gum {
56
57 template < typename GUM_SCALAR >
59 for (auto obsIter = this->_obsTable_.cbeginSafe(); _obsTable_.cendSafe() != obsIter; ++obsIter)
60 delete obsIter.val();
61
62 GUM_DESTRUCTOR(LeastSquareTestPolicy);
63 }
64
65 // ##########################################################################
66 //
67 // ##########################################################################
68
69 // ==========================================================================
70 //
71 // ==========================================================================
72 template < typename GUM_SCALAR >
75 _sumO_ += value;
76
77 if (_sumAttrTable_.exists(attr)) _sumAttrTable_[attr] += value;
78 else _sumAttrTable_.insert(attr, value);
79
80 if (_nbObsTable_.exists(attr)) _nbObsTable_[attr]++;
81 else _nbObsTable_.insert(attr, 1);
82
83 if (!_obsTable_.exists(attr)) _obsTable_.insert(attr, new LinkedList< double >());
84 _obsTable_[attr]->addLink(value);
85 }
86
87 // ############################################################################
88 // @name Test result
89 // ############################################################################
90
91 // ============================================================================
92 // Computes the GStat of current variable according to the test
93 // ============================================================================
94 template < typename GUM_SCALAR >
97 double mean = _sumO_ / (double)this->nbObservation();
98 double errorO = 0.0;
99 double sumErrorAttr = 0.0;
100 for (auto attrIter = _sumAttrTable_.cbeginSafe(); attrIter != _sumAttrTable_.cendSafe();
101 ++attrIter) {
102 Idx key = attrIter.key();
103 double meanAttr = _sumAttrTable_[key] / (double)_nbObsTable_[key];
104 double errorAttr = 0.0;
105
106 const Link< double >* linky = _obsTable_[key]->list();
107 while (linky) {
108 errorAttr += std::pow(linky->element() - meanAttr, 2);
109 errorO += std::pow(linky->element() - mean, 2);
110 linky = linky->nextLink();
111 }
112
113 sumErrorAttr += ((double)_nbObsTable_[key] / (double)this->nbObservation()) * errorAttr;
114 }
115 _score_ = errorO - sumErrorAttr;
116 }
117
118 // ============================================================================
119 // Returns the performance of current variable according to the test
120 // ============================================================================
121 template < typename GUM_SCALAR >
123 if (this->isModified_()) computeScore();
124 return _score_;
125 }
126
127 // ============================================================================
128 // Returns a second criterion to severe ties
129 // ============================================================================
130 template < typename GUM_SCALAR >
132 if (this->isModified_()) computeScore();
133 return _score_;
134 }
135
136 template < typename GUM_SCALAR >
139
140 for (auto obsIter = src.nbObsTable().cbeginSafe(); obsIter != src.nbObsTable().cendSafe();
141 ++obsIter)
142 if (_nbObsTable_.exists(obsIter.key())) _nbObsTable_[obsIter.key()] += obsIter.val();
143 else _nbObsTable_.insert(obsIter.key(), obsIter.val());
144
145 for (auto attrIter = src.sumAttrTable().cbeginSafe(); attrIter != src.sumAttrTable().cendSafe();
146 ++attrIter)
147 if (_sumAttrTable_.exists(attrIter.key())) _sumAttrTable_[attrIter.key()] += attrIter.val();
148 else _sumAttrTable_.insert(attrIter.key(), attrIter.val());
149
150 for (auto obsIter = src.obsTable().cbeginSafe(); obsIter != src.obsTable().cendSafe();
151 ++obsIter) {
152 if (!_obsTable_.exists(obsIter.key()))
153 _obsTable_.insert(obsIter.key(), new LinkedList< double >());
154 const Link< double >* srcLink = obsIter.val()->list();
155 while (srcLink) {
156 _obsTable_[obsIter.key()]->addLink(srcLink->element());
157 srcLink = srcLink->nextLink();
158 }
159 }
160 }
161
162} // End of namespace gum
const const_iterator_safe & cendSafe() const noexcept
Returns the safe const_iterator pointing to the end of the hashtable.
const_iterator_safe cbeginSafe() const
Returns the safe const_iterator pointing to the beginning of the hashtable.
Idx nbObservation() const
Comptabilizes the new observation.
virtual void computeScore() const
Recomputes the statistic from the beginning.
bool isModified_() const
virtual void addObservation(Idx attr, GUM_SCALAR value)
Comptabilizes the new observation.
void add(const ITestPolicy< GUM_SCALAR > &src)
void add(const LeastSquareTestPolicy< GUM_SCALAR > &src)
Performs the merging of current LeastSquareTestPolicy instance with given instance.
virtual ~LeastSquareTestPolicy()
Destructor.
double _score_
Keeping computed score.
HashTable< Idx, Idx > _nbObsTable_
Nb Observation for each modality assumed by variable.
HashTable< Idx, double > _sumAttrTable_
Sum for each modality assumed by variable.
const HashTable< Idx, Idx > & nbObsTable() const
Returns nbobs per modality table (needed for the merging).
HashTable< Idx, LinkedList< double > * > _obsTable_
Not sure if needed.
void addObservation(Idx attr, GUM_SCALAR value)
Comptabilizes the new observation.
const HashTable< Idx, double > & sumAttrTable() const
Returns sum per modality table (needed for the merging).
void computeScore()
Returns the performance of current variable according to the test.
const HashTable< Idx, LinkedList< double > * > & obsTable() const
Returns global sum (needed for the merging).
double secondaryscore() const
Returns a second criterion to severe ties.
double score()
Returns the performance of current variable according to the test.
Size Idx
Type for indexes.
Definition types.h:79
Headers of the LeastSquareTestPolicy.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46