aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
DirichletPriorFromDatabase.cpp
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
41
48
50
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
54# ifdef GUM_NO_INLINE
56# endif /* GUM_NO_INLINE */
57
58namespace gum {
59
60 namespace learning {
61
64 const DatabaseTable& learning_db,
65 const DBRowGeneratorParser& prior_parser,
66 const Bijection< NodeId, std::size_t >& nodeId2columns) :
67 Prior(prior_parser.database(), Bijection< NodeId, std::size_t >()),
68 _counter_(prior_parser,
69 std::vector< std::pair< std::size_t, std::size_t > >(),
70 nodeId2columns) {
71 // we check that the variables in the learning database also exist in the
72 // prior database and that they are precisely equal.
73 const DatabaseTable& prior_db = prior_parser.database();
74 const auto& prior_names = prior_db.variableNames();
75 const std::size_t prior_size = prior_names.size();
76 HashTable< std::string, std::size_t > names2col(prior_size);
77 for (std::size_t i = std::size_t(0); i < prior_size; ++i)
78 names2col.insert(prior_names[i], i);
79
80 const auto& learning_names = learning_db.variableNames();
81 const std::size_t learning_size = learning_names.size();
82 HashTable< std::size_t, std::size_t > learning2prior_index(learning_size);
83 bool different_index = false;
84 for (auto i = std::size_t(0); i < learning_size; ++i) {
85 // get the column of the variable in the prior database
86 std::size_t prior_col;
87 try {
88 prior_col = names2col[learning_names[i]];
89 } catch (...) {
91 "Variable " << learning_names[i]
92 << " could not be found in the prior database");
93 }
94
95 // check that both variables are the same
96 const Variable& learning_var = learning_db.variable(i);
97 const Variable& prior_var = prior_db.variable(prior_col);
98 if (learning_var.varType() != prior_var.varType()) {
100 "Variable " << learning_names[i]
101 << " has not the same type in the learning database "
102 " and the prior database");
103 }
104 if (learning_var.domain() != prior_var.domain()) {
106 "Variable " << learning_names[i] << " has domain " << learning_var.domain()
107 << " in the learning database and domain " << prior_var.domain()
108 << " in the aprioi database");
109 }
110
111 // save the mapping from i to col
112 learning2prior_index.insert(i, prior_col);
113 if (i != prior_col) different_index = true;
114 }
115
116 // here we are guaranteed that the variables in the learning database
117 // have their equivalent in the a priordatabase. Now, we should
118 // fill the bijection from ids to columns
119 if (!different_index) {
120 this->nodeId2columns_ = nodeId2columns;
121 } else {
122 if (nodeId2columns.empty()) {
123 for (auto i = std::size_t(0); i < learning_size; ++i) {
124 this->nodeId2columns_.insert(NodeId(i), learning2prior_index[i]);
125 }
126 } else {
127 for (auto iter = nodeId2columns.begin(); iter != nodeId2columns.end(); ++iter) {
128 this->nodeId2columns_.insert(iter.first(), learning2prior_index[iter.second()]);
129 }
130 }
131 }
132
133 // recreate the record counter with the appropriate node2col mapping
134 std::vector< std::pair< std::size_t, std::size_t > > ranges;
135 RecordCounter good_counter(prior_parser, ranges, this->nodeId2columns_);
136 _counter_ = std::move(good_counter);
137
138 if (prior_db.nbRows() == std::size_t(0)) _internal_weight_ = 0.0;
139 else _internal_weight_ = this->weight_ / double(prior_db.nbRows());
140
141 GUM_CONSTRUCTOR(DirichletPriorFromDatabase);
142 }
143
144 } /* namespace learning */
145
146} /* namespace gum */
147
148#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.
Error: An unknown error occurred while accessing a database.
the class used to read a row in the database and to transform it into a set of DBRow instances that c...
The class representing a tabular database as used by learning tasks.
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
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
Size NodeId
Type for node ids.
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46