aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
independenceTest_inl.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
48#ifndef DOXYGEN_SHOULD_SKIP_THIS
49
50namespace gum {
51
52 namespace learning {
53
56 const DBRowGeneratorParser& parser,
57 const Prior& prior,
58 const std::vector< std::pair< std::size_t, std::size_t > >& ranges,
59 const Bijection< NodeId, std::size_t >& nodeId2columns) :
60 prior_(prior.clone()), counter_(parser, ranges, nodeId2columns) {
61 GUM_CONSTRUCTOR(IndependenceTest);
62 }
63
65 INLINE
66 IndependenceTest::IndependenceTest(const DBRowGeneratorParser& parser,
67 const Prior& prior,
68 const Bijection< NodeId, std::size_t >& nodeId2columns) :
69 prior_(prior.clone()), counter_(parser, nodeId2columns) {
70 GUM_CONSTRUCTOR(IndependenceTest);
71 }
72
74 INLINE IndependenceTest::IndependenceTest(const IndependenceTest& from) :
75 prior_(from.prior_->clone()), counter_(from.counter_), cache_(from.cache_),
76 use_cache_(from.use_cache_) {
77 GUM_CONS_CPY(IndependenceTest);
78 }
79
81 INLINE IndependenceTest::IndependenceTest(IndependenceTest&& from) :
82 prior_(from.prior_), counter_(std::move(from.counter_)), cache_(std::move(from.cache_)),
83 use_cache_(from.use_cache_) {
84 from.prior_ = nullptr;
85 GUM_CONS_MOV(IndependenceTest);
86 }
87
89 INLINE IndependenceTest::~IndependenceTest() {
90 if (prior_ != nullptr) delete prior_;
91 GUM_DESTRUCTOR(IndependenceTest);
92 }
93
95 INLINE void IndependenceTest::setNumberOfThreads(Size nb) { counter_.setNumberOfThreads(nb); }
96
98 INLINE Size IndependenceTest::getNumberOfThreads() const {
99 return counter_.getNumberOfThreads();
100 }
101
103 INLINE bool IndependenceTest::isGumNumberOfThreadsOverriden() const {
104 return counter_.isGumNumberOfThreadsOverriden();
105 }
106
109 INLINE void IndependenceTest::setMinNbRowsPerThread(const std::size_t nb) const {
110 counter_.setMinNbRowsPerThread(nb);
111 }
112
114 INLINE std::size_t IndependenceTest::minNbRowsPerThread() const {
115 return counter_.minNbRowsPerThread();
116 }
117
119 INLINE const std::vector< std::pair< std::size_t, std::size_t > >&
120 IndependenceTest::ranges() const {
121 return counter_.ranges();
122 }
123
125 INLINE double IndependenceTest::score(const NodeId var1, const NodeId var2) {
126 IdCondSet idset(var1, var2, empty_ids_, false, true);
127 if (use_cache_) {
128 try {
129 return cache_.score(idset);
130 } catch (NotFound const&) {}
131 double the_score = score_(idset);
132 cache_.insert(std::move(idset), the_score);
133 return the_score;
134 } else {
135 return score_(std::move(idset));
136 }
137 }
138
140 INLINE double IndependenceTest::score(const NodeId var1,
141 const NodeId var2,
142 const std::vector< NodeId >& rhs_ids) {
143 IdCondSet idset(var1, var2, rhs_ids, false, false);
144 if (use_cache_) {
145 try {
146 return cache_.score(idset);
147 } catch (NotFound const&) {}
148 double the_score = score_(idset);
149 cache_.insert(std::move(idset), the_score);
150 return the_score;
151 } else {
152 return score_(idset);
153 }
154 }
155
157 INLINE void IndependenceTest::clear() {
158 counter_.clear();
159 cache_.clear();
160 }
161
163 INLINE void IndependenceTest::clearCache() { cache_.clear(); }
164
166 INLINE void IndependenceTest::useCache(const bool on_off) { use_cache_ = on_off; }
167
169 INLINE const Bijection< NodeId, std::size_t >& IndependenceTest::nodeId2Columns() const {
170 return counter_.nodeId2Columns();
171 }
172
174 INLINE const DatabaseTable& IndependenceTest::database() const { return counter_.database(); }
175
176
177 } /* namespace learning */
178
179} /* namespace gum */
180
181#endif /* DOXYGEN_SHOULD_SKIP_THIS */
Exception : the element we looked for cannot be found.
the class used to read a row in the database and to transform it into a set of DBRow instances that c...
IndependenceTest(const DBRowGeneratorParser &parser, const Prior &external_prior, const std::vector< std::pair< std::size_t, std::size_t > > &ranges, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
the base class for all a priori
Definition prior.h:83
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.