aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
contingencyTable_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 // ##########################################################################
58 // Constructor & destructor.
59 // ##########################################################################
60
61 // ==========================================================================
62 // Default constructor
63 // ==========================================================================
64 template < typename GUM_SCALAR_A, typename GUM_SCALAR_B >
68
69 // ==========================================================================
70 // Default destructor
71 // ==========================================================================
72 template < typename GUM_SCALAR_A, typename GUM_SCALAR_B >
76
77 // ##########################################################################
78 //
79 // ##########################################################################
80
81 // ==========================================================================
82 //
83 // ==========================================================================
84 template < typename GUM_SCALAR_A, typename GUM_SCALAR_B >
86 GUM_SCALAR_B valueB) {
87 // Updating
88 if (_attrAMarginalTable_.exists(valueA)) _attrAMarginalTable_[valueA]++;
89 else _attrAMarginalTable_.insert(valueA, 1);
90
91 if (_attrBMarginalTable_.exists(valueB)) _attrBMarginalTable_[valueB]++;
92 else _attrBMarginalTable_.insert(valueB, 1);
93
94 std::pair< GUM_SCALAR_A, GUM_SCALAR_B > cell(valueA, valueB);
95 if (_jointTable_.exists(cell)) _jointTable_[cell]++;
96 else _jointTable_.insert(cell, 1);
97 }
98
99 template < typename GUM_SCALAR_A, typename GUM_SCALAR_B >
103 // Ajout dans marginal A et table joint des valeurs pour src
104 for (auto aTer = src.attrABeginSafe(); aTer != src.attrAEndSafe(); ++aTer) {
105 if (_attrAMarginalTable_.exists(aTer.key())) _attrAMarginalTable_[aTer.key()] += aTer.val();
106 else _attrAMarginalTable_.insert(aTer.key(), aTer.val());
108 for (auto bTer = src.attrBBeginSafe(); bTer != src.attrBEndSafe(); ++bTer) {
109 std::pair< GUM_SCALAR_A, GUM_SCALAR_B > cell(aTer.key(), bTer.key());
110 if (_jointTable_.exists(cell)) _jointTable_[cell] += src.joint(aTer.key(), bTer.key());
111 else _jointTable_.insert(cell, src.joint(aTer.key(), bTer.key()));
112 }
113 }
114
115 // Ajout dans marginal B des valeurs de src
116 for (auto bTer = src.attrBBeginSafe(); bTer != src.attrBEndSafe(); ++bTer) {
117 if (_attrBMarginalTable_.exists(bTer.key())) _attrBMarginalTable_[bTer.key()] += bTer.val();
118 else _attrBMarginalTable_.insert(bTer.key(), bTer.val());
119 }
120 return *this;
121 }
122
123} // End of namespace gum
<agrum/FMDP/learning/core/contingencyTable.h>
HashTable< GUM_SCALAR, Idx > _attrBMarginalTable_
HashTable< std::pair< Idx, GUM_SCALAR >, Idx > _jointTable_
void add(GUM_SCALAR_A valueA, GUM_SCALAR_B valueB)
Increments the number of sample for case( iattr, ivalue ).
HashTableConstIteratorSafe< GUM_SCALAR_B, Idx > attrBEndSafe() const
Increments the number of sample for case( iattr, ivalue ).
HashTableConstIteratorSafe< GUM_SCALAR_A, Idx > attrABeginSafe() const
Returns the number of samples for line iattr.
HashTableConstIteratorSafe< GUM_SCALAR_A, Idx > attrAEndSafe() const
Increments the number of sample for case( iattr, ivalue ).
Idx joint(GUM_SCALAR_A valueA, GUM_SCALAR_B valueB) const
Returns the number of samples for case (iattr, ivalue).
HashTableConstIteratorSafe< GUM_SCALAR_B, Idx > attrBBeginSafe() const
Returns the number of samples for column ivalue.
ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B > & operator+=(const ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B > &src)
~ContingencyTable()
Default destructor.
ContingencyTable()
Default constructor.
Headers of the ContingencyTable class.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46