aGrUM 2.3.2
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-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
41
48
50
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
54# ifdef GUM_NO_INLINE
56# endif /* GUM_NO_INLINE */
57
58namespace gum {
59
60 namespace learning {
61
64 if (this != &from) {
65 Prior* new_prior = from.prior_->clone();
66 RecordCounter new_counter = from.counter_;
67 ScoringCache new_cache = from.cache_;
68
69 if (prior_ != nullptr) delete prior_;
70
71 prior_ = new_prior;
72 counter_ = std::move(new_counter);
73 cache_ = std::move(new_cache);
74
75 use_cache_ = from.use_cache_;
76 }
77 return *this;
78 }
79
82 if (this != &from) {
83 std::swap(prior_, from.prior_);
84
85 counter_ = std::move(from.counter_);
86 cache_ = std::move(from.cache_);
87 use_cache_ = from.use_cache_;
88 }
89 return *this;
90 }
91
93
100 const std::vector< std::pair< std::size_t, std::size_t > >& new_ranges) {
101 std::vector< std::pair< std::size_t, std::size_t > > old_ranges = ranges();
102 counter_.setRanges(new_ranges);
103 if (old_ranges != ranges()) clear();
104 }
105
108 std::vector< std::pair< std::size_t, std::size_t > > old_ranges = ranges();
109 counter_.clearRanges();
110 if (old_ranges != ranges()) clear();
111 }
112
114
119 std::vector< double > IndependenceTest::marginalize_(const std::size_t node_2_marginalize,
120 const std::size_t X_size,
121 const std::size_t Y_size,
122 const std::size_t Z_size,
123 const std::vector< double >& N_xyz) const {
124 // determine the size of the output vector
125 std::size_t out_size = Z_size;
126 if (node_2_marginalize == std::size_t(0)) out_size *= Y_size;
127 else if (node_2_marginalize == std::size_t(1)) out_size *= X_size;
128
129 // allocate the output vector
130 std::vector< double > res(out_size, 0.0);
131
132 // fill the vector:
133 if (node_2_marginalize == std::size_t(0)) { // marginalize X
134 for (std::size_t yz = std::size_t(0), xyz = std::size_t(0); yz < out_size; ++yz) {
135 for (std::size_t x = std::size_t(0); x < X_size; ++x, ++xyz) {
136 res[yz] += N_xyz[xyz];
137 }
138 }
139 } else if (node_2_marginalize == std::size_t(1)) { // marginalize Y
140 for (std::size_t z = std::size_t(0), xyz = std::size_t(0), beg_xz = std::size_t(0);
141 z < Z_size;
142 ++z, beg_xz += X_size) {
143 for (std::size_t y = std::size_t(0); y < Y_size; ++y) {
144 for (std::size_t x = std::size_t(0), xz = beg_xz; x < X_size; ++x, ++xz, ++xyz) {
145 res[xz] += N_xyz[xyz];
146 }
147 }
148 }
149 } else if (node_2_marginalize == std::size_t(2)) { // marginalize X and Y
150 const std::size_t XY_size = X_size * Y_size;
151 for (std::size_t z = std::size_t(0), xyz = std::size_t(0); z < out_size; ++z) {
152 for (std::size_t xy = std::size_t(0); xy < XY_size; ++xy, ++xyz) {
153 res[z] += N_xyz[xyz];
154 }
155 }
156 } else {
157 GUM_ERROR(NotImplementedYet,
158 "_marginalize not implemented for nodeset " << node_2_marginalize);
159 }
160
161 return res;
162 }
163
164 } /* namespace learning */
165
166} /* namespace gum */
167
168#endif /* DOXYGEN_SHOULD_SKIP_THIS */
The base class for all the independence tests used for learning.
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
void setRanges(const std::vector< std::pair< std::size_t, std::size_t > > &new_ranges)
sets new ranges to perform the counts used by the independence test
RecordCounter counter_
the record counter used for the counts over discrete variables
IndependenceTest & operator=(const IndependenceTest &from)
copy operator
virtual void clear()
clears all the data structures from memory, including the cache
Prior * prior_
the expert knowledge a priorwe add to the contingency tables
bool use_cache_
a Boolean indicating whether we wish to use the cache
void clearRanges()
reset the ranges to the one range corresponding to the whole database
ScoringCache cache_
the scoring cache
const std::vector< std::pair< std::size_t, std::size_t > > & ranges() const
returns the current ranges
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46