aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
DirichletPriorFromDatabase_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-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
50#ifndef DOXYGEN_SHOULD_SKIP_THIS
51
52namespace gum {
53
54 namespace learning {
55
58 const DirichletPriorFromDatabase& from) :
59 Prior(from), _counter_(from._counter_), _internal_weight_(from._internal_weight_) {
60 GUM_CONS_CPY(DirichletPriorFromDatabase);
61 }
62
64 INLINE
65 DirichletPriorFromDatabase::DirichletPriorFromDatabase(
66 DirichletPriorFromDatabase&& from) noexcept :
67 Prior(std::move(from)), _counter_(std::move(from._counter_)),
68 _internal_weight_(from._internal_weight_) {
69 GUM_CONS_MOV(DirichletPriorFromDatabase);
70 }
71
73 INLINE DirichletPriorFromDatabase* DirichletPriorFromDatabase::clone() const {
74 return new DirichletPriorFromDatabase(*this);
75 }
76
78 INLINE DirichletPriorFromDatabase::~DirichletPriorFromDatabase() {
79 GUM_DESTRUCTOR(DirichletPriorFromDatabase);
80 }
81
83 INLINE DirichletPriorFromDatabase&
84 DirichletPriorFromDatabase::operator=(const DirichletPriorFromDatabase& from) {
85 if (this != &from) {
86 Prior::operator=(from);
87 _counter_ = from._counter_;
88 _internal_weight_ = from._internal_weight_;
89 }
90 return *this;
91 }
92
94 INLINE DirichletPriorFromDatabase&
95 DirichletPriorFromDatabase::operator=(DirichletPriorFromDatabase&& from) {
96 if (this != &from) {
97 Prior::operator=(std::move(from));
98 _counter_ = std::move(from._counter_);
99 _internal_weight_ = from._internal_weight_;
100 }
101 return *this;
102 }
103
105 INLINE PriorType DirichletPriorFromDatabase::getType() const {
106 return PriorType::DirichletPriorType;
107 }
108
110 INLINE bool DirichletPriorFromDatabase::isInformative() const { return (this->weight_ != 0.0); }
111
113 INLINE void DirichletPriorFromDatabase::setWeight(const double weight) {
114 Prior::setWeight(weight);
115 if (_counter_.database().nbRows() == 0) _internal_weight_ = 0.0;
116 else _internal_weight_ = this->weight_ / double(_counter_.database().nbRows());
117 }
118
120 INLINE void DirichletPriorFromDatabase::addJointPseudoCount(const IdCondSet& idset,
121 std::vector< double >& counts) {
122 if (this->weight_ == 0.0) return;
123
124 const auto& prior = _counter_.counts(idset);
125 const std::size_t size = prior.size();
126 if (_internal_weight_ != 1.0) {
127 for (auto i = std::size_t(0); i < size; ++i) {
128 counts[i] += prior[i] * _internal_weight_;
129 }
130 } else {
131 for (auto i = std::size_t(0); i < size; ++i) {
132 counts[i] += prior[i];
133 }
134 }
135 }
136
138 INLINE void
139 DirichletPriorFromDatabase::addConditioningPseudoCount(const IdCondSet& idset,
140 std::vector< double >& counts) {
141 if (_internal_weight_ == 0.0) return;
142
143 const auto& prior = _counter_.counts(idset.conditionalIdCondSet());
144 const std::size_t size = prior.size();
145 if (_internal_weight_ != 1.0) {
146 for (std::size_t i = std::size_t(0); i < size; ++i) {
147 counts[i] += prior[i] * _internal_weight_;
148 }
149 } else {
150 for (std::size_t i = std::size_t(0); i < size; ++i) {
151 counts[i] += prior[i];
152 }
153 }
154 }
155
156
157 } /* namespace learning */
158
159} /* namespace gum */
160
161#endif /* DOXYGEN_SHOULD_SKIP_THIS */
A dirichlet priori: computes its N'_ijk from a database.
A dirichlet priori: computes its N'_ijk from a database.
DirichletPriorFromDatabase(const DatabaseTable &learning_db, const DBRowGeneratorParser &prior_parser, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
the base class for all a priori
Definition prior.h:81
include the inlined functions if necessary
Definition CSVParser.h:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46