aGrUM 3.1.1
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-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
45
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
53namespace gum::learning {
54
56 template < GUM_Numeric GUM_SCALAR >
58 const BayesNet< GUM_SCALAR >* priorbn) :
59 Prior(learning_db), _prior_bn_(priorbn) {
60 GUM_CONSTRUCTOR(DirichletPriorFromBN)
61 }
62
64 template < GUM_Numeric GUM_SCALAR >
65 DirichletPriorFromBN< GUM_SCALAR >::DirichletPriorFromBN(const DirichletPriorFromBN& from) :
66 Prior(from), _prior_bn_(from._prior_bn_) {
67 GUM_CONS_CPY(DirichletPriorFromBN)
68 }
69
71 template < GUM_Numeric GUM_SCALAR >
72 DirichletPriorFromBN< GUM_SCALAR >::DirichletPriorFromBN(DirichletPriorFromBN&& from) noexcept :
73 Prior(std::move(from)), _prior_bn_(std::move(from._prior_bn_)) {
74 GUM_CONS_MOV(DirichletPriorFromBN)
75 }
76
78 template < GUM_Numeric GUM_SCALAR >
79 DirichletPriorFromBN< GUM_SCALAR >* DirichletPriorFromBN< GUM_SCALAR >::clone() const {
80 return new DirichletPriorFromBN(*this);
81 }
82
84
85 template < GUM_Numeric GUM_SCALAR >
86 DirichletPriorFromBN< GUM_SCALAR >::~DirichletPriorFromBN() {
87 GUM_DESTRUCTOR(DirichletPriorFromBN)
88 }
89
91 template < GUM_Numeric GUM_SCALAR >
92 DirichletPriorFromBN< GUM_SCALAR >&
93 DirichletPriorFromBN< GUM_SCALAR >::operator=(const DirichletPriorFromBN& from) {
94 if (this != &from) {
95 Prior::operator=(from);
96 _prior_bn_ = from._prior_bn_;
97 }
98 return *this;
99 }
100
102 template < GUM_Numeric GUM_SCALAR >
103 DirichletPriorFromBN< GUM_SCALAR >&
104 DirichletPriorFromBN< GUM_SCALAR >::operator=(DirichletPriorFromBN&& from) {
105 if (this != &from) {
106 Prior::operator=(std::move(from));
107 _prior_bn_ = from._prior_bn_;
108 }
109 return *this;
110 }
111
113
114 template < GUM_Numeric GUM_SCALAR >
115 PriorType DirichletPriorFromBN< GUM_SCALAR >::getType() const {
116 return PriorType::DirichletPriorType;
117 }
118
120
121 template < GUM_Numeric GUM_SCALAR >
122 bool DirichletPriorFromBN< GUM_SCALAR >::isInformative() const {
123 return (this->weight_ != 0.0);
124 }
125
127 template < GUM_Numeric GUM_SCALAR >
128 void DirichletPriorFromBN< GUM_SCALAR >::setWeight(const double weight) {
129 Prior::setWeight(weight);
130 }
131
133 template < GUM_Numeric GUM_SCALAR >
134 void 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 < GUM_Numeric GUM_SCALAR >
149 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 < GUM_Numeric 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:81
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
include the inlined functions if necessary
Definition CSVParser.h:55