aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
DirichletPriorFromBN_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
44
50#ifndef DOXYGEN_SHOULD_SKIP_THIS
51
52namespace gum::learning {
53
55 template < typename GUM_SCALAR >
57 const BayesNet< GUM_SCALAR >* priorbn) :
58 Prior(learning_db), _prior_bn_(priorbn) {
59 GUM_CONSTRUCTOR(DirichletPriorFromBN)
60 }
61
63 template < typename GUM_SCALAR >
64 DirichletPriorFromBN< GUM_SCALAR >::DirichletPriorFromBN(const DirichletPriorFromBN& from) :
65 Prior(from), _prior_bn_(from._prior_bn_) {
66 GUM_CONS_CPY(DirichletPriorFromBN)
67 }
68
70 template < typename GUM_SCALAR >
71 DirichletPriorFromBN< GUM_SCALAR >::DirichletPriorFromBN(DirichletPriorFromBN&& from) noexcept :
72 Prior(std::move(from)), _prior_bn_(std::move(from._prior_bn_)) {
73 GUM_CONS_MOV(DirichletPriorFromBN)
74 }
75
77 template < typename GUM_SCALAR >
78 DirichletPriorFromBN< GUM_SCALAR >* DirichletPriorFromBN< GUM_SCALAR >::clone() const {
79 return new DirichletPriorFromBN(*this);
80 }
81
83
84 template < typename GUM_SCALAR >
85 DirichletPriorFromBN< GUM_SCALAR >::~DirichletPriorFromBN() {
86 GUM_DESTRUCTOR(DirichletPriorFromBN)
87 }
88
90 template < typename GUM_SCALAR >
91 DirichletPriorFromBN< GUM_SCALAR >&
92 DirichletPriorFromBN< GUM_SCALAR >::operator=(const DirichletPriorFromBN& from) {
93 if (this != &from) {
94 Prior::operator=(from);
95 _prior_bn_ = from._prior_bn_;
96 }
97 return *this;
98 }
99
101 template < typename GUM_SCALAR >
102 DirichletPriorFromBN< GUM_SCALAR >&
103 DirichletPriorFromBN< GUM_SCALAR >::operator=(DirichletPriorFromBN&& from) {
104 if (this != &from) {
105 Prior::operator=(std::move(from));
106 _prior_bn_ = from._prior_bn_;
107 }
108 return *this;
109 }
110
112
113 template < typename GUM_SCALAR >
114 INLINE PriorType DirichletPriorFromBN< GUM_SCALAR >::getType() const {
115 return PriorType::DirichletPriorType;
116 }
117
119
120 template < typename GUM_SCALAR >
121 INLINE bool DirichletPriorFromBN< GUM_SCALAR >::isInformative() const {
122 return (this->weight_ != 0.0);
123 }
124
126 template < typename GUM_SCALAR >
127 INLINE void DirichletPriorFromBN< GUM_SCALAR >::setWeight(const double weight) {
128 Prior::setWeight(weight);
129 }
130
132 template < typename GUM_SCALAR >
133 INLINE void
134 DirichletPriorFromBN< GUM_SCALAR >::addJointPseudoCount(const IdCondSet& idset,
135 std::vector< double >& counts) {
136 if (this->weight_ == 0.0) return;
137 const auto [X, Y] = idset.toNodeSets();
138
139 gum::Instantiation Ijoint;
140 for (auto i = std::size_t(0); i < idset.size(); i++) {
141 Ijoint.add(_prior_bn_->variable(idset.ids()[i]));
142 }
143
144 _addCountsForJoint_(Ijoint, X + Y, counts);
145 }
146
148 template < typename GUM_SCALAR >
149 INLINE void DirichletPriorFromBN< GUM_SCALAR >::addConditioningPseudoCount(
150 const IdCondSet& idset,
151 std::vector< double >& counts) {
152 if (this->weight_ == 0.0) return;
153 const auto [X, Y] = idset.toNodeSets();
154 gum::Instantiation Ijoint;
155 for (auto i = idset.nbLHSIds(); i < idset.size(); i++)
156 Ijoint.add(_prior_bn_->variable(idset.ids()[i]));
157 _addCountsForJoint_(Ijoint, Y, counts);
158 }
159
160 template < typename GUM_SCALAR >
161 void DirichletPriorFromBN< GUM_SCALAR >::_addCountsForJoint_(Instantiation& Ijoint,
162 const NodeSet& joint,
163 std::vector< double >& counts) {
164 const auto size = counts.size();
165 if (size != Ijoint.domainSize())
167 "The size of counts ("
168 << size << ") does not match with the size of the asked prior (" << joint
169 << " : " << Ijoint.domainSize() << ")")
170
171 if (joint.empty()) {
172 counts[0] += weight_;
173 return;
174 }
175
176 LazyPropagation lazy(_prior_bn_);
177
178 Tensor< GUM_SCALAR > p;
179 if (joint.size() == 1) {
180 const auto& target = *joint.begin();
181 lazy.addTarget(target);
182 lazy.makeInference();
183 p = lazy.posterior(target);
184 } else {
185 lazy.addJointTarget(joint);
186 lazy.makeInference();
187 p = lazy.jointPosterior(joint);
188 }
189 if (weight_ != 1) p.scale(weight_);
190
191 Ijoint.setFirst();
192 for (auto i = std::size_t(0); i < size; ++i) {
193 counts[i] += p[Ijoint];
194 Ijoint.inc();
195 }
196 }
197} // namespace gum::learning
198
199#endif /* DOXYGEN_SHOULD_SKIP_THIS */
A dirichlet priori: computes its N'_ijk from a bayesian network.
Exception base for argument error.
Class for assigning/browsing values to tuples of discrete variables.
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
The class representing a tabular database as used by learning tasks.
DirichletPriorFromBN(const DatabaseTable &learning_db, const BayesNet< GUM_SCALAR > *priorbn)
default constructor
the base class for all a priori
Definition prior.h:83
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
include the inlined functions if necessary
Definition CSVParser.h:54