aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
independenceTest.cpp
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
48
49#include <limits>
50
52
53#ifndef DOXYGEN_SHOULD_SKIP_THIS
54
56# ifdef GUM_NO_INLINE
58# endif /* GUM_NO_INLINE */
59
60namespace gum {
61
62 namespace learning {
63
66 if (this != &from) {
68 _domain_sizes_ = from._domain_sizes_;
69 }
70 return *this;
71 }
72
75 if (this != &from) {
77 _domain_sizes_ = std::move(from._domain_sizes_);
78 }
79 return *this;
80 }
81
83
88 std::vector< double > IndependenceTest::marginalize_(const std::size_t node_2_marginalize,
89 const std::size_t X_size,
90 const std::size_t Y_size,
91 const std::size_t Z_size,
92 const std::vector< double >& N_xyz) const {
93 // determine the size of the output vector
94 std::size_t out_size = Z_size;
95 if (node_2_marginalize == std::size_t(0)) {
96 if (Z_size != 0 && Y_size > std::numeric_limits< std::size_t >::max() / Z_size)
97 GUM_ERROR(OutOfBounds, "marginalize_: out_size overflow (Z_size * Y_size)")
98 out_size *= Y_size;
99 } else if (node_2_marginalize == std::size_t(1)) {
100 if (Z_size != 0 && X_size > std::numeric_limits< std::size_t >::max() / Z_size)
101 GUM_ERROR(OutOfBounds, "marginalize_: out_size overflow (Z_size * X_size)")
102 out_size *= X_size;
103 }
104
105 // allocate the output vector
106 std::vector< double > res(out_size, 0.0);
107
108 // fill the vector:
109 if (node_2_marginalize == std::size_t(0)) { // marginalize X
110 for (std::size_t yz = std::size_t(0), xyz = std::size_t(0); yz < out_size; ++yz) {
111 for (std::size_t x = std::size_t(0); x < X_size; ++x, ++xyz) {
112 res[yz] += N_xyz[xyz];
113 }
114 }
115 } else if (node_2_marginalize == std::size_t(1)) { // marginalize Y
116 for (std::size_t z = std::size_t(0), xyz = std::size_t(0), beg_xz = std::size_t(0);
117 z < Z_size;
118 ++z, beg_xz += X_size) {
119 for (std::size_t y = std::size_t(0); y < Y_size; ++y) {
120 for (std::size_t x = std::size_t(0), xz = beg_xz; x < X_size; ++x, ++xz, ++xyz) {
121 res[xz] += N_xyz[xyz];
122 }
123 }
124 }
125 } else if (node_2_marginalize == std::size_t(2)) { // marginalize X and Y
126 const std::size_t XY_size = X_size * Y_size;
127 for (std::size_t z = std::size_t(0), xyz = std::size_t(0); z < out_size; ++z) {
128 for (std::size_t xy = std::size_t(0); xy < XY_size; ++xy, ++xyz) {
129 res[z] += N_xyz[xyz];
130 }
131 }
132 } else {
133 GUM_ERROR(NotImplementedYet,
134 "_marginalize not implemented for nodeset " << node_2_marginalize);
135 }
136
137 return res;
138 }
139
140 } /* namespace learning */
141
142} /* namespace gum */
143
144#endif /* DOXYGEN_SHOULD_SKIP_THIS */
CachedContingencyCounter & operator=(const CachedContingencyCounter &from)
copy operator
The base class for all the independence tests used for learning.
std::vector< std::size_t > _domain_sizes_
the domain sizes of the variables (indexed by column id in the database)
std::vector< double > marginalize_(const std::size_t node_2_marginalize, const std::size_t X_size, const std::size_t Y_size, const std::size_t Z_size, const std::vector< double > &N_xyz) const
returns a counting vector where variables are marginalized from N_xyz
IndependenceTest & operator=(const IndependenceTest &from)
copy operator
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
the base class for all the independence tests used for learning
the base class for all the independence tests used for learning
include the inlined functions if necessary
Definition CSVParser.h:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46