aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
GTestPolicy_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
53
54// =========================================================================
55
56
57namespace gum {
58
59
60 // ##########################################################################
61 //
62 // ##########################################################################
63
64 // ==========================================================================
65 //
66 // ==========================================================================
67 template < typename GUM_SCALAR >
68 void GTestPolicy< GUM_SCALAR >::addObservation(Idx iattr, GUM_SCALAR ivalue) {
70 _conTab_.add(iattr, ivalue);
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 _GStat_ = 0;
84
85 // Itération sur l'axe 1 du tableau
86 for (auto attrIter = _conTab_.attrABeginSafe(); attrIter != _conTab_.attrAEndSafe();
87 ++attrIter) {
88 // Mise en cache de valeur utile
89 double semiExpected = (double)(attrIter.val()) / (double)this->nbObservation();
90
91 // Itération sur l'axe 2 du tableau
92 for (auto valIter = _conTab_.attrBBeginSafe(); valIter != _conTab_.attrBEndSafe();
93 ++valIter) {
94 // Récupération de la valeur en cellule
95 Idx cell = _conTab_.joint(attrIter.key(), valIter.key());
96 if (cell < 5) continue;
97
98 // Récupération de la valeur en axe 2
99 double expected = semiExpected * (double)valIter.val();
100
101 _GStat_ += 2 * cell * log(cell / expected);
102 }
103 }
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 // std::cout << this->toString() << std::endl;
113 double score
114 = 1
115 - ChiSquare::probaChi2(_GStat_, (_conTab_.attrASize() - 1) * (_conTab_.attrBSize() - 1));
116 return score;
117 }
118
119 // ============================================================================
120 // Returns a second criterion to severe ties
121 // ============================================================================
122 template < typename GUM_SCALAR >
124 if (this->isModified_()) computeScore();
125 return _GStat_;
126 }
127
128 template < typename GUM_SCALAR >
133
134} // End of namespace gum
Headers of the GTestPolicy.
static double probaChi2(double x, Size df)
computes the probability of chi2 value (used by the cache)
GTestPolicy()
Constructor.
Definition GTestPolicy.h:84
const ContingencyTable< Idx, GUM_SCALAR > & ct() const
Returns contingency table (needed for the merging of GTestPolicy instances).
double secondaryscore() const
Returns a second criterion to severe ties.
void addObservation(Idx iattr, GUM_SCALAR ivalue)
Comptabilizes the new observation.
double score() const
Returns the performance of current variable according to the test.
void add(const GTestPolicy< GUM_SCALAR > &src)
Performs the merging of current GTestPolicy instance with given instance.
void computeScore() const
Computes the GStat of current variable according to the test.
ContingencyTable< Idx, GUM_SCALAR > _conTab_
The contingency table used to keeps records of all observation.
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)
Size Idx
Type for indexes.
Definition types.h:79
Useful macros for maths.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46