aGrUM 2.3.2
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-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
49#ifndef DOXYGEN_SHOULD_SKIP_THIS
50
51namespace gum {
52
53 namespace learning {
54
57 const DirichletPriorFromDatabase& from) :
58 Prior(from), _counter_(from._counter_), _internal_weight_(from._internal_weight_) {
59 GUM_CONS_CPY(DirichletPriorFromDatabase);
60 }
61
63 INLINE
64 DirichletPriorFromDatabase::DirichletPriorFromDatabase(
65 DirichletPriorFromDatabase&& from) noexcept :
66 Prior(std::move(from)), _counter_(std::move(from._counter_)),
67 _internal_weight_(from._internal_weight_) {
68 GUM_CONS_MOV(DirichletPriorFromDatabase);
69 }
70
72 INLINE DirichletPriorFromDatabase* DirichletPriorFromDatabase::clone() const {
73 return new DirichletPriorFromDatabase(*this);
74 }
75
77 INLINE DirichletPriorFromDatabase::~DirichletPriorFromDatabase() {
78 GUM_DESTRUCTOR(DirichletPriorFromDatabase);
79 }
80
82 INLINE DirichletPriorFromDatabase&
83 DirichletPriorFromDatabase::operator=(const DirichletPriorFromDatabase& from) {
84 if (this != &from) {
85 Prior::operator=(from);
86 _counter_ = from._counter_;
87 _internal_weight_ = from._internal_weight_;
88 }
89 return *this;
90 }
91
93 INLINE DirichletPriorFromDatabase&
94 DirichletPriorFromDatabase::operator=(DirichletPriorFromDatabase&& from) {
95 if (this != &from) {
96 Prior::operator=(std::move(from));
97 _counter_ = std::move(from._counter_);
98 _internal_weight_ = from._internal_weight_;
99 }
100 return *this;
101 }
102
104 INLINE PriorType DirichletPriorFromDatabase::getType() const {
105 return PriorType::DirichletPriorType;
106 }
107
109 INLINE bool DirichletPriorFromDatabase::isInformative() const { return (this->weight_ != 0.0); }
110
112 INLINE void DirichletPriorFromDatabase::setWeight(const double weight) {
113 Prior::setWeight(weight);
114 if (_counter_.database().nbRows() == 0) _internal_weight_ = 0.0;
115 else _internal_weight_ = this->weight_ / double(_counter_.database().nbRows());
116 }
117
119 INLINE void DirichletPriorFromDatabase::addJointPseudoCount(const IdCondSet& idset,
120 std::vector< double >& counts) {
121 if (this->weight_ == 0.0) return;
122
123 const auto& prior = _counter_.counts(idset);
124 const std::size_t size = prior.size();
125 if (_internal_weight_ != 1.0) {
126 for (auto i = std::size_t(0); i < size; ++i) {
127 counts[i] += prior[i] * _internal_weight_;
128 }
129 } else {
130 for (auto i = std::size_t(0); i < size; ++i) {
131 counts[i] += prior[i];
132 }
133 }
134 }
135
137 INLINE void
138 DirichletPriorFromDatabase::addConditioningPseudoCount(const IdCondSet& idset,
139 std::vector< double >& counts) {
140 if (_internal_weight_ == 0.0) return;
141
142 const auto& prior = _counter_.counts(idset.conditionalIdCondSet());
143 const std::size_t size = prior.size();
144 if (_internal_weight_ != 1.0) {
145 for (std::size_t i = std::size_t(0); i < size; ++i) {
146 counts[i] += prior[i] * _internal_weight_;
147 }
148 } else {
149 for (std::size_t i = std::size_t(0); i < size; ++i) {
150 counts[i] += prior[i];
151 }
152 }
153 }
154
155
156 } /* namespace learning */
157
158} /* namespace gum */
159
160#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:83
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46