aGrUM 3.1.1
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-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 // ##########################################################################
59 // Constructor & destructor.
60 // ##########################################################################
61
62 // ==========================================================================
63 // Default constructor
64 // ==========================================================================
65 template < typename GUM_ELEMENT_A, typename GUM_ELEMENT_B >
69
70 // ==========================================================================
71 // Default destructor
72 // ==========================================================================
73 template < typename GUM_ELEMENT_A, typename GUM_ELEMENT_B >
77
78 // ##########################################################################
79 //
80 // ##########################################################################
82 // ==========================================================================
83 //
84 // ==========================================================================
85 template < typename GUM_ELEMENT_A, typename GUM_ELEMENT_B >
87 GUM_ELEMENT_B valueB) {
88 // Updating
89 if (_attrAMarginalTable_.exists(valueA)) _attrAMarginalTable_[valueA]++;
90 else _attrAMarginalTable_.insert(valueA, 1);
92 if (_attrBMarginalTable_.exists(valueB)) _attrBMarginalTable_[valueB]++;
93 else _attrBMarginalTable_.insert(valueB, 1);
94
95 std::pair< GUM_ELEMENT_A, GUM_ELEMENT_B > cell(valueA, valueB);
96 if (_jointTable_.exists(cell)) _jointTable_[cell]++;
97 else _jointTable_.insert(cell, 1);
98 }
99
100 template < typename GUM_ELEMENT_A, typename GUM_ELEMENT_B >
104 // Ajout dans marginal A et table joint des valeurs pour src
105 for (auto aTer = src.attrABeginSafe(); aTer != src.attrAEndSafe(); ++aTer) {
106 if (_attrAMarginalTable_.exists(aTer.key())) _attrAMarginalTable_[aTer.key()] += aTer.val();
107 else _attrAMarginalTable_.insert(aTer.key(), aTer.val());
108
109 for (auto bTer = src.attrBBeginSafe(); bTer != src.attrBEndSafe(); ++bTer) {
110 std::pair< GUM_ELEMENT_A, GUM_ELEMENT_B > cell(aTer.key(), bTer.key());
111 if (_jointTable_.exists(cell)) _jointTable_[cell] += src.joint(aTer.key(), bTer.key());
112 else _jointTable_.insert(cell, src.joint(aTer.key(), bTer.key()));
114 }
115
116 // Ajout dans marginal B des valeurs de src
117 for (auto bTer = src.attrBBeginSafe(); bTer != src.attrBEndSafe(); ++bTer) {
118 if (_attrBMarginalTable_.exists(bTer.key())) _attrBMarginalTable_[bTer.key()] += bTer.val();
119 else _attrBMarginalTable_.insert(bTer.key(), bTer.val());
120 }
121 return *this;
122 }
124 template < typename GUM_ELEMENT_A, typename GUM_ELEMENT_B >
127 }
128
129 template < typename GUM_ELEMENT_A, typename GUM_ELEMENT_B >
132 }
134 template < typename GUM_ELEMENT_A, typename GUM_ELEMENT_B >
136 GUM_ELEMENT_B valueB) const {
137 return _jointTable_.exists(std::pair< GUM_ELEMENT_A, GUM_ELEMENT_B >(valueA, valueB))
138 ? _jointTable_[std::pair< GUM_ELEMENT_A, GUM_ELEMENT_B >(valueA, valueB)]
139 : 0;
140 }
142 template < typename GUM_ELEMENT_A, typename GUM_ELEMENT_B >
144 return _attrAMarginalTable_.exists(valueA) ? _attrAMarginalTable_[valueA] : 0;
145 }
147 template < typename GUM_ELEMENT_A, typename GUM_ELEMENT_B >
149 return _attrAMarginalTable_.exists(valueB) ? _attrAMarginalTable_[valueB] : 0;
150 }
151
152 template < typename GUM_ELEMENT_A, typename GUM_ELEMENT_B >
157
158 template < typename GUM_ELEMENT_A, typename GUM_ELEMENT_B >
163
164 template < typename GUM_ELEMENT_A, typename GUM_ELEMENT_B >
169
170 template < typename GUM_ELEMENT_A, typename GUM_ELEMENT_B >
175
176 template < typename GUM_ELEMENT_A, typename GUM_ELEMENT_B >
180
181 template < typename GUM_ELEMENT_A, typename GUM_ELEMENT_B >
185
186 template < typename GUM_ELEMENT_A, typename GUM_ELEMENT_B >
188 std::ostringstream ss;
189 ss << "\t\t\t\t" << _attrAMarginalTable_ << "\n"
190 << "\t\t\t\t" << _attrBMarginalTable_ << "\n"
191 << "\t\t\t\t" << _jointTable_ << "\n";
192 return ss.str();
193 }
194
195} // End of namespace gum
<agrum/FMDP/learning/core/contingencyTable.h>
HashTable< GUM_ELEMENT, Idx > _attrBMarginalTable_
HashTableConstIteratorSafe< GUM_ELEMENT_B, Idx > attrBEndSafe() const
Increments the number of sample for case( iattr, ivalue ).
void add(GUM_ELEMENT_A valueA, GUM_ELEMENT_B valueB)
Increments the number of sample for case( iattr, ivalue ).
std::string toString() const
HashTable< GUM_ELEMENT_A, Idx > _attrAMarginalTable_
~ContingencyTable()
Default destructor.
ContingencyTable()
Default constructor.
Idx attrBMarginal(GUM_ELEMENT_B valueB) const
Returns the number of samples for case (iattr, ivalue).
HashTableConstIteratorSafe< GUM_ELEMENT_B, Idx > attrBBeginSafe() const
Returns the number of samples for column ivalue.
Idx attrAMarginal(GUM_ELEMENT_A valueA) const
Returns the number of samples for case (iattr, ivalue).
HashTableConstIteratorSafe< GUM_ELEMENT_A, Idx > attrAEndSafe() const
Increments the number of sample for case( iattr, ivalue ).
Idx attrASize() const
Returns the number of samples for line iattr.
HashTable< std::pair< Idx, GUM_ELEMENT >, Idx > _jointTable_
Idx attrBSize() const
Returns the number of samples for column ivalue.
HashTableConstIteratorSafe< GUM_ELEMENT_A, Idx > attrABeginSafe() const
Returns the number of samples for line iattr.
Idx joint(GUM_ELEMENT_A valueA, GUM_ELEMENT_B valueB) const
Returns the number of samples for case (iattr, ivalue).
ContingencyTable< GUM_ELEMENT_A, GUM_ELEMENT_B > & operator+=(const ContingencyTable< GUM_ELEMENT_A, GUM_ELEMENT_B > &src)
static SmallObjectAllocator & instance()
void * allocate(const size_t &objectSize)
Allocates a block.
void deallocate(void *pDeallocatedObject, const size_t &objectSize)
Deallocates an object.
Headers of the ContingencyTable class.
Size Idx
Type for indexes.
Definition types.h:79
gum is the global namespace for all aGrUM entities
Definition agrum.h:46