aGrUM 3.1.1
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-2026 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-2026 *
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
41#pragma once
42
43
50// =========================================================================
52
53// =========================================================================
54
55
56namespace gum {
57
58 template < typename GUM_ELEMENT >
60 for (auto obsIter = this->_obsTable_.cbeginSafe(); _obsTable_.cendSafe() != obsIter; ++obsIter)
61 delete obsIter.val();
62
63 GUM_DESTRUCTOR(LeastSquareTestPolicy);
64 }
65
66 // ##########################################################################
67 //
68 // ##########################################################################
69
70 // ==========================================================================
71 //
72 // ==========================================================================
73 template < typename GUM_ELEMENT >
76 _sumO_ += value;
77
78 if (_sumAttrTable_.exists(attr)) _sumAttrTable_[attr] += value;
79 else _sumAttrTable_.insert(attr, value);
80
81 if (_nbObsTable_.exists(attr)) _nbObsTable_[attr]++;
82 else _nbObsTable_.insert(attr, 1);
83
84 if (!_obsTable_.exists(attr)) _obsTable_.insert(attr, new LinkedList< double >());
85 _obsTable_[attr]->addLink(value);
86 }
87
88 // ############################################################################
89 // @name Test result
90 // ############################################################################
91
92 // ============================================================================
93 // Computes the GStat of current variable according to the test
94 // ============================================================================
95 template < typename GUM_ELEMENT >
98 double mean = _sumO_ / (double)this->nbObservation();
99 double errorO = 0.0;
100 double sumErrorAttr = 0.0;
101 for (auto attrIter = _sumAttrTable_.cbeginSafe(); attrIter != _sumAttrTable_.cendSafe();
102 ++attrIter) {
103 Idx key = attrIter.key();
104 double meanAttr = _sumAttrTable_[key] / (double)_nbObsTable_[key];
105 double errorAttr = 0.0;
106
107 const Link< double >* linky = _obsTable_[key]->list();
108 while (linky) {
109 errorAttr += std::pow(linky->element() - meanAttr, 2);
110 errorO += std::pow(linky->element() - mean, 2);
111 linky = linky->nextLink();
112 }
113
114 sumErrorAttr += ((double)_nbObsTable_[key] / (double)this->nbObservation()) * errorAttr;
115 }
116 _score_ = errorO - sumErrorAttr;
117 }
118
119 // ============================================================================
120 // Returns the performance of current variable according to the test
121 // ============================================================================
122 template < typename GUM_ELEMENT >
124 if (this->isModified_()) computeScore();
125 return _score_;
126 }
127
128 // ============================================================================
129 // Returns a second criterion to severe ties
130 // ============================================================================
131 template < typename GUM_ELEMENT >
133 if (this->isModified_()) computeScore();
134 return _score_;
135 }
136
137 template < typename GUM_ELEMENT >
140
141 for (auto obsIter = src.nbObsTable().cbeginSafe(); obsIter != src.nbObsTable().cendSafe();
142 ++obsIter)
143 if (_nbObsTable_.exists(obsIter.key())) _nbObsTable_[obsIter.key()] += obsIter.val();
144 else _nbObsTable_.insert(obsIter.key(), obsIter.val());
145
146 for (auto attrIter = src.sumAttrTable().cbeginSafe(); attrIter != src.sumAttrTable().cendSafe();
147 ++attrIter)
148 if (_sumAttrTable_.exists(attrIter.key())) _sumAttrTable_[attrIter.key()] += attrIter.val();
149 else _sumAttrTable_.insert(attrIter.key(), attrIter.val());
150
151 for (auto obsIter = src.obsTable().cbeginSafe(); obsIter != src.obsTable().cendSafe();
152 ++obsIter) {
153 if (!_obsTable_.exists(obsIter.key()))
154 _obsTable_.insert(obsIter.key(), new LinkedList< double >());
155 const Link< double >* srcLink = obsIter.val()->list();
156 while (srcLink) {
157 _obsTable_[obsIter.key()]->addLink(srcLink->element());
158 srcLink = srcLink->nextLink();
159 }
160 }
161 }
162
163 template < typename GUM_ELEMENT >
168
169 template < typename GUM_ELEMENT >
173
174 template < typename GUM_ELEMENT >
178
179 template < typename GUM_ELEMENT >
181 return (this->nbObservation() > 20);
182 }
183
184 template < typename GUM_ELEMENT >
186 return _sumO_;
187 }
188
189 template < typename GUM_ELEMENT >
193
194 template < typename GUM_ELEMENT >
198
199 template < typename GUM_ELEMENT >
204
205} // End of namespace gum
const_iterator_safe cbeginSafe() const
Returns the safe const_iterator pointing to the beginning of the hashtable.
const const_iterator_safe & cendSafe() const noexcept
Returns the safe const_iterator pointing to the end of the hashtable.
ITestPolicy()
Allocators and Deallocators redefinition.
bool isModified_() const
virtual void addObservation(Idx attr, GUM_ELEMENT value)
Comptabilizes the new observation.
virtual void computeScore() const
Recomputes the statistic from the beginning.
void add(const ITestPolicy< GUM_ELEMENT > &src)
Idx nbObservation() const
Comptabilizes the new observation.
void addObservation(Idx attr, GUM_ELEMENT value)
Comptabilizes the new observation.
double _score_
Keeping computed score.
void add(const LeastSquareTestPolicy< GUM_ELEMENT > &src)
Performs the merging of current LeastSquareTestPolicy instance with given instance.
double sumValue() const
Returns global sum (needed for the merging).
virtual ~LeastSquareTestPolicy()
Destructor.
double secondaryscore() const
Returns a second criterion to severe ties.
HashTable< Idx, Idx > _nbObsTable_
Nb Observation for each modality assumed by variable.
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).
const HashTable< Idx, double > & sumAttrTable() const
Returns sum per modality table (needed for the merging).
HashTable< Idx, double > _sumAttrTable_
Sum for each modality assumed by variable.
bool isTestRelevant() const
Returns true if enough observation were made so that the test can be relevant.
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).
HashTable< Idx, LinkedList< double > * > _obsTable_
Not sure if needed.
static SmallObjectAllocator & instance()
void * allocate(const size_t &objectSize)
Allocates a block.
void deallocate(void *pDeallocatedObject, const size_t &objectSize)
Deallocates an object.
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