aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
IBNLearner.cpp
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
50
51#include <algorithm>
52#include <iterator>
53
54#include <agrum/agrum.h>
55
63
64// include the inlined functions if necessary
65#ifdef GUM_NO_INLINE
67#endif /* GUM_NO_INLINE */
68
69namespace gum::learning {
71 // get the variables names
72 const auto& var_names = _database_.variableNames();
73 const std::size_t nb_vars = var_names.size();
74 for (auto dom: _database_.domainSizes())
75 _domain_sizes_.push_back(dom);
76 for (std::size_t i = 0; i < nb_vars; ++i) {
78 }
79
80 // create the parser
82 }
83
84 IBNLearner::Database::Database(std::string_view filename,
85 const std::vector< std::string >& missing_symbols,
86 const bool induceTypes) :
87 Database(IBNLearner::readFile_(filename, missing_symbols)) {
88 // if the usr wants the best translators to be inferred, just do it
89 if (induceTypes) {
90 for (const auto& [first, second]: _database_.betterTranslators()) {
91 // change the translator
92 _database_.changeTranslator(*second, first);
93 // recompute the domain size
94 _domain_sizes_[first] = second->domainSize();
95 }
96 }
97 }
98
99 IBNLearner::Database::Database(std::string_view CSV_filename,
100 const Database& score_database,
101 const std::vector< std::string >& missing_symbols) {
102 // assign to each column name in the CSV file its column
103 IBNLearner::isCSVFileName_(CSV_filename);
104 DBInitializerFromCSV initializer{std::string(CSV_filename)};
105 const auto& prior_names = initializer.variableNames();
106 std::size_t prior_nb_vars = prior_names.size();
107 HashTable< std::string, std::size_t > prior_names2col(prior_nb_vars);
108 for (auto i = std::size_t(0); i < prior_nb_vars; ++i)
109 prior_names2col.insert(prior_names[i], i);
110
111 // check that there are at least as many variables in the a priori
112 // database as those in the score_database
113 if (prior_nb_vars < score_database._database_.nbVariables()) {
115 "the a prior database has fewer variables "
116 "than the observed database")
117 }
118
119 // get the mapping from the columns of score_database to those of
120 // the CSV file
121 const std::vector< std::string >& score_names = score_database.databaseTable().variableNames();
122 const std::size_t score_nb_vars = score_names.size();
123 HashTable< std::size_t, std::size_t > mapping(score_nb_vars);
124 for (auto i = std::size_t(0); i < score_nb_vars; ++i) {
125 try {
126 mapping.insert(i, prior_names2col[score_names[i]]);
127 } catch (Exception const&) {
129 "Variable " << score_names[i]
130 << " of the observed database does not belong to the "
131 << "prior database")
132 }
133 }
134
135 // create the translators for CSV database
136 for (auto i = std::size_t(0); i < score_nb_vars; ++i) {
137 const Variable& var = score_database.databaseTable().variable(i);
138 _database_.insertTranslator(var, mapping[i], missing_symbols);
139 }
140
141 // fill the database
142 initializer.fillDatabase(_database_);
143
144 // get the domain sizes of the variables
145 for (auto dom: _database_.domainSizes())
146 _domain_sizes_.push_back(dom);
147
148 // compute the mapping from node ids to column indices
149 _nodeId2cols_ = score_database.nodeId2Columns();
150
151 // create the parser
153 }
154
161
163 _database_(std::move(from._database_)), _domain_sizes_(std::move(from._domain_sizes_)),
164 _nodeId2cols_(std::move(from._nodeId2cols_)) {
165 // create the parser
167 }
168
170
172 if (this != &from) {
173 delete _parser_;
174 _database_ = from._database_;
177
178 // create the parser
180 }
181
182 return *this;
183 }
184
186 if (this != &from) {
187 delete _parser_;
188 _database_ = std::move(from._database_);
189 _domain_sizes_ = std::move(from._domain_sizes_);
190 _nodeId2cols_ = std::move(from._nodeId2cols_);
191
192 // create the parser
194 }
195
196 return *this;
197 }
198
199 // ===========================================================================
200
201 IBNLearner::IBNLearner(std::string_view filename,
202 const std::vector< std::string >& missing_symbols,
203 const bool induceTypes) :
204 inducedTypes_(induceTypes), scoreDatabase_(filename, missing_symbols, induceTypes),
205 filename_(filename) {
207
208 GUM_CONSTRUCTOR(IBNLearner)
209 }
210
215
243
244 GUM_CONS_CPY(IBNLearner)
245 }
246
261 algoMiic_(std::move(from.algoMiic_)), kmodeMiic_(from.kmodeMiic_),
270 scoreDatabase_(std::move(from.scoreDatabase_)), ranges_(std::move(from.ranges_)),
271 priorDbname_(std::move(from.priorDbname_)), initialDag_(std::move(from.initialDag_)),
275
276 GUM_CONS_MOV(IBNLearner)
277 }
278
280 if (score_) delete score_;
281
282 if (prior_) delete prior_;
283
284 if (noPrior_) delete noPrior_;
285
286 if (priorDatabase_) delete priorDatabase_;
287
288 if (mutualInfo_) delete mutualInfo_;
289
290 if (indepTestPC_) delete indepTestPC_;
291
292 if (indepTestFCI_) delete indepTestFCI_;
293
294 GUM_DESTRUCTOR(IBNLearner)
295 }
296
298 if (this != &from) {
299 if (score_) {
300 delete score_;
301 score_ = nullptr;
302 }
303
304 if (prior_) {
305 delete prior_;
306 prior_ = nullptr;
307 }
308
309 if (priorDatabase_) {
310 delete priorDatabase_;
311 priorDatabase_ = nullptr;
312 }
313
314 if (mutualInfo_) {
315 delete mutualInfo_;
316 mutualInfo_ = nullptr;
317 }
318
320 scoreType_ = from.scoreType_;
322 useEM_ = from.useEM_;
323 priorType_ = from.priorType_;
337 algoK2_ = from.algoK2_;
338 algoMiic_ = from.algoMiic_;
339 kmodeMiic_ = from.kmodeMiic_;
341 alphaPc_ = from.alphaPc_;
342 stablePc_ = from.stablePc_;
346 alphaFci_ = from.alphaFci_;
349 dag2BN_ = from.dag2BN_;
355 ranges_ = from.ranges_;
358 filename_ = from.filename_;
361 currentAlgorithm_ = nullptr;
362 }
363
364 return *this;
365 }
366
368 if (this != &from) {
369 if (score_) {
370 delete score_;
371 score_ = nullptr;
372 }
373
374 if (prior_) {
375 delete prior_;
376 prior_ = nullptr;
377 }
378
379 if (priorDatabase_) {
380 delete priorDatabase_;
381 priorDatabase_ = nullptr;
382 }
383
384 if (mutualInfo_) {
385 delete mutualInfo_;
386 mutualInfo_ = nullptr;
387 }
388
389 ThreadNumberManager::operator=(std::move(from));
390 scoreType_ = from.scoreType_;
391 paramEstimatorType_ = from.paramEstimatorType_;
392 useEM_ = from.useEM_;
393 priorType_ = from.priorType_;
394 priorWeight_ = from.priorWeight_;
395 constraintSliceOrder_ = std::move(from.constraintSliceOrder_);
396 constraintIndegree_ = std::move(from.constraintIndegree_);
397 constraintTabuList_ = std::move(from.constraintTabuList_);
398 constraintForbiddenArcs_ = std::move(from.constraintForbiddenArcs_);
399 constraintNoParentNodes_ = std::move(from.constraintNoParentNodes_);
400 constraintNoChildrenNodes_ = std::move(from.constraintNoChildrenNodes_);
401 constraintMandatoryArcs_ = std::move(from.constraintMandatoryArcs_);
402 selectedAlgo_ = from.selectedAlgo_;
403 allowArcAdditions_ = from.allowArcAdditions_;
404 allowArcDeletions_ = from.allowArcDeletions_;
405 allowArcReversals_ = from.allowArcReversals_;
406 allowArcTriangleDeletions_ = from.allowArcTriangleDeletions_;
407 algoK2_ = from.algoK2_;
408 algoMiic_ = std::move(from.algoMiic_);
409 kmodeMiic_ = from.kmodeMiic_;
410 indepTestTypePC_ = from.indepTestTypePC_;
411 alphaPc_ = from.alphaPc_;
412 stablePc_ = from.stablePc_;
413 maxCondSetSizePc_ = from.maxCondSetSizePc_;
414 sortedUCPc_ = from.sortedUCPc_;
415 indepTestTypeFCI_ = from.indepTestTypeFCI_;
416 alphaFci_ = from.alphaFci_;
417 maxPathLengthFci_ = from.maxPathLengthFci_;
418 exhaustiveSepSetFci_ = from.exhaustiveSepSetFci_;
419 dag2BN_ = std::move(from.dag2BN_);
420 greedyHillClimbing_ = std::move(from.greedyHillClimbing_);
421 extendedGreedyHillClimbing_ = std::move(from.extendedGreedyHillClimbing_);
422 greedyThickThinning_ = std::move(from.greedyThickThinning_);
423 localSearchWithTabuList_ = std::move(from.localSearchWithTabuList_);
424 scoreDatabase_ = std::move(from.scoreDatabase_);
425 ranges_ = std::move(from.ranges_);
426 priorDbname_ = std::move(from.priorDbname_);
427 filename_ = std::move(from.filename_);
428 initialDag_ = std::move(from.initialDag_);
429 tabuListSize_ = from.tabuListSize_;
430 nbDecreasingChanges_ = std::move(from.nbDecreasingChanges_);
431 currentAlgorithm_ = nullptr;
432 }
433
434 return *this;
435 }
436
437 DatabaseTable readFile(const std::string& filename) {
438 // get the extension of the file
439 if (auto filename_size = Size(filename.size()); filename_size < 4) {
441 "IBNLearner could not determine the "
442 "file type of the database '"
443 << filename << "'")
444 }
445
446 std::string extension = filename.substr(filename.size() - 4);
447 std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
448
449 if (extension != ".csv") {
451 "IBNLearner does not support yet this type ('" << extension
452 << "')"
453 "of database file")
454 }
455
456 DBInitializerFromCSV initializer(filename);
457
458 const auto& var_names = initializer.variableNames();
459 const std::size_t nb_vars = var_names.size();
460
461 DBTranslatorSet translator_set;
463 for (std::size_t i = 0; i < nb_vars; ++i) {
464 translator_set.insertTranslator(translator, i);
465 }
466
467 DatabaseTable database(translator_set);
468 database.setVariableNames(initializer.variableNames());
469 initializer.fillDatabase(database);
470
471 return database;
472 }
473
474 void IBNLearner::isCSVFileName_(std::string_view filename) {
475 // get the extension of the file
476
477 if (auto filename_size = Size(filename.size()); filename_size < 4) {
479 "IBNLearner could not determine the "
480 "file type of the database")
481 }
482
483 std::string extension(filename.substr(filename.size() - 4));
484 std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
485
486 if (extension != ".csv") {
487 GUM_ERROR(OperationNotAllowed, "IBNLearner does not support yet this type of database file")
488 }
489 }
490
491 DatabaseTable IBNLearner::readFile_(std::string_view filename,
492 const std::vector< std::string >& missing_symbols) {
493 // get the extension of the file
494 isCSVFileName_(filename);
495
496 DBInitializerFromCSV initializer{std::string(filename)};
497
498 const auto& var_names = initializer.variableNames();
499 const std::size_t nb_vars = var_names.size();
500
501 DBTranslatorSet translator_set;
502 DBTranslator4LabelizedVariable translator(missing_symbols);
503 for (std::size_t i = 0; i < nb_vars; ++i) {
504 translator_set.insertTranslator(translator, i);
505 }
506
507 DatabaseTable database(missing_symbols, translator_set);
509 initializer.fillDatabase(database);
510
512
513 return database;
514 }
515
517 // first, save the old score, to be delete if everything is ok
518 Score* old_score = score_;
519
520 // create the new scoring function
521 switch (scoreType_) {
522 case ScoreType::AIC :
524 *prior_,
525 ranges_,
527 break;
528
529 case ScoreType::BD :
531 *prior_,
532 ranges_,
534 break;
535
536 case ScoreType::BDeu :
538 *prior_,
539 ranges_,
541 break;
542
543 case ScoreType::BIC :
545 *prior_,
546 ranges_,
548 break;
549
550 case ScoreType::fNML :
552 *prior_,
553 ranges_,
555 break;
556
557 case ScoreType::K2 :
559 *prior_,
560 ranges_,
562 break;
563
566 *prior_,
567 ranges_,
569 break;
570
571 case ScoreType::MDL :
573 *prior_,
574 ranges_,
576 break;
577
578 default : GUM_ERROR(OperationNotAllowed, "IBNLearner does not support yet this score")
579 }
580
581 // remove the old score, if any
582 if (old_score != nullptr) delete old_score;
583
584 // assign the number of threads
586 : 0);
587 }
588
590 bool take_into_account_score) {
591 ParamEstimator* param_estimator = nullptr;
592
593 // create the new estimator
594 switch (paramEstimatorType_) {
596 if (take_into_account_score && (score_ != nullptr)) {
597 param_estimator = new ParamEstimatorML(parser,
598 *prior_,
600 ranges_,
602 } else {
603 param_estimator = new ParamEstimatorML(parser,
604 *prior_,
605 *noPrior_,
606 ranges_,
608 }
609
610 break;
611
612 default :
614 "IBNLearner does not support " << "yet this parameter estimator")
615 }
616
617 // assign the number of threads
618 param_estimator->setNumberOfThreads(
620
621 // assign the set of ranges
622 param_estimator->setRanges(ranges_);
623
624 return param_estimator;
625 }
626
627 // prepares the initial graph for constraintMiic
629 // Initialize the mixed graph to the fully connected graph
630 MixedGraph mgraph;
631 DiGraph forbiddenGraph;
632 DAG mandatoryGraph;
633
634 // GUM_CHECKPOINT
635 for (Size i = 0; i < scoreDatabase_.databaseTable().nbVariables(); ++i) {
636 mgraph.addNodeWithId(i);
637 forbiddenGraph.addNodeWithId(i);
638 mandatoryGraph.addNodeWithId(i);
639 }
640
641 const EdgeSet& possible_edges = constraintPossibleEdges_.edges();
642
643 if (possible_edges.empty()) {
644 for (const NodeId i: mgraph.nodes()) {
645 for (NodeId j = 0; j < i; ++j) {
646 // contiguous nodeIds !
647 mgraph.addEdge(j, i);
648 }
649 }
650 } else {
651 for (const auto& edge: possible_edges) {
652 mgraph.addEdge(edge.first(), edge.second());
653 }
654 }
655 // GUM_CHECKPOINT
656
657 // translating the mandatory arcs for constraintMiic
658 HashTable< std::pair< NodeId, NodeId >, char > initial_marks;
659 const ArcSet& mandatory_arcs = constraintMandatoryArcs_.arcs();
660
661 // GUM_CHECKPOINT
662 for (const auto& arc: mandatory_arcs) {
663 mandatoryGraph.addArc(arc.tail(), arc.head());
664 // MIIC's marks mechanism (orientationMiic_) handles the reverse direction;
665 // adding it to forbiddenGraph would cause DuplicateElement (unlike preparePC_).
666 }
667
668 // GUM_CHECKPOINT
669 // translating the forbidden arcs for constraintMiic
670 const ArcSet& forbidden_arcs = constraintForbiddenArcs_.arcs();
671 for (const auto& arc: forbidden_arcs) {
672 forbiddenGraph.addArc(arc.tail(), arc.head());
673 }
674
675 // GUM_CHECKPOINT
677 gum::NodeProperty< gum::Size > copyOrder = gum::HashTable(sliceOrder);
678 for (const auto& [n1, r1]: sliceOrder) {
679 for (const auto& [n2, r2]: copyOrder) {
680 if (r1 > r2) {
681 forbiddenGraph.addArc(n1, n2);
682 // initial_marks.insert({n1, n2}, '-');
683 } else if (r2 > r1) {
684 forbiddenGraph.addArc(n2, n1);
685 // initial_marks.insert({n2, n1}, '-');
686 }
687 }
688 copyOrder.erase(n1);
689 }
690
691 // GUM_CHECKPOINT
692 const auto& totalOrder = constraintTotalOrder_.totalOrder();
693 for (auto iter1 = totalOrder.begin(); iter1 != totalOrder.end(); ++iter1) {
694 const auto node1 = *iter1;
695 for (auto iter2 = iter1 + 1; iter2 != totalOrder.end(); ++iter2) {
696 forbiddenGraph.addArc(*iter2, node1);
697 }
698 }
699
700 for (const auto node: constraintNoParentNodes_.nodes()) {
701 for (const auto node2: mgraph.nodes()) {
702 if (node != node2) { forbiddenGraph.addArc(node2, node); }
703 }
704 }
705
706 for (const auto node: constraintNoChildrenNodes_.nodes()) {
707 for (const auto node2: mgraph.nodes()) {
708 if (node != node2) { forbiddenGraph.addArc(node, node2); }
709 }
710 }
711
712 // GUM_CHECKPOINT
714 algoMiic_.addConstraints(initial_marks);
715 algoMiic_.setMandatoryGraph(mandatoryGraph);
716 algoMiic_.setForbiddenGraph(forbiddenGraph);
717
718 // GUM_CHECKPOINT
719 // create the mutual entropy object
720 // if ( _mutual_info_ == nullptr) { this->useNMLCorrection(); }
722
723 // GUM_CHECKPOINT
724 return mgraph;
725 }
726
728 MixedGraph mgraph;
729 DiGraph forbiddenGraph;
730 DAG mandatoryGraph;
731
732 for (Size i = 0; i < scoreDatabase_.databaseTable().nbVariables(); ++i) {
733 mgraph.addNodeWithId(i);
734 forbiddenGraph.addNodeWithId(i);
735 mandatoryGraph.addNodeWithId(i);
736 }
737
738 const EdgeSet& possible_edges = constraintPossibleEdges_.edges();
739 if (possible_edges.empty()) {
740 for (const NodeId i: mgraph.nodes()) {
741 for (NodeId j = 0; j < i; ++j) {
742 mgraph.addEdge(j, i);
743 }
744 }
745 } else {
746 for (const auto& edge: possible_edges) {
747 mgraph.addEdge(edge.first(), edge.second());
748 }
749 }
750
751 for (const auto& arc: constraintMandatoryArcs_.arcs()) {
752 mandatoryGraph.addArc(arc.tail(), arc.head());
753 forbiddenGraph.addArc(arc.head(), arc.tail());
754 }
755 for (const auto& arc: constraintForbiddenArcs_.arcs()) {
756 forbiddenGraph.addArc(arc.tail(), arc.head());
757 }
758
760 gum::NodeProperty< gum::Size > copyOrder = gum::HashTable(sliceOrder);
761 for (const auto& [n1, r1]: sliceOrder) {
762 for (const auto& [n2, r2]: copyOrder) {
763 if (r1 > r2) {
764 forbiddenGraph.addArc(n1, n2);
765 } else if (r2 > r1) {
766 forbiddenGraph.addArc(n2, n1);
767 }
768 }
769 copyOrder.erase(n1);
770 }
771
772 const auto& totalOrder = constraintTotalOrder_.totalOrder();
773 for (auto iter1 = totalOrder.begin(); iter1 != totalOrder.end(); ++iter1) {
774 for (auto iter2 = iter1 + 1; iter2 != totalOrder.end(); ++iter2) {
775 forbiddenGraph.addArc(*iter2, *iter1);
776 }
777 }
778
779 for (const auto node: constraintNoParentNodes_.nodes()) {
780 for (const auto node2: mgraph.nodes()) {
781 if (node != node2) { forbiddenGraph.addArc(node2, node); }
782 }
783 }
784 for (const auto node: constraintNoChildrenNodes_.nodes()) {
785 for (const auto node2: mgraph.nodes()) {
786 if (node != node2) { forbiddenGraph.addArc(node, node2); }
787 }
788 }
789
790 // build the independence test (owned by IBNLearner)
791 if (indepTestPC_) {
792 delete indepTestPC_;
793 indepTestPC_ = nullptr;
794 }
797 *noPrior_,
798 ranges_,
800 } else {
802 *noPrior_,
803 ranges_,
805 }
806
808 algoPC_.setMandatoryGraph(mandatoryGraph);
809 algoPC_.setForbiddenGraph(forbiddenGraph);
816
817 return mgraph;
818 }
819
821 MixedGraph mgraph;
822 DiGraph forbiddenGraph;
823 DAG mandatoryGraph;
824
825 for (Size i = 0; i < scoreDatabase_.databaseTable().nbVariables(); ++i) {
826 mgraph.addNodeWithId(i);
827 forbiddenGraph.addNodeWithId(i);
828 mandatoryGraph.addNodeWithId(i);
829 }
830
831 const EdgeSet& possible_edges = constraintPossibleEdges_.edges();
832 if (possible_edges.empty()) {
833 for (const NodeId i: mgraph.nodes()) {
834 for (NodeId j = 0; j < i; ++j) {
835 mgraph.addEdge(j, i);
836 }
837 }
838 } else {
839 for (const auto& edge: possible_edges) {
840 mgraph.addEdge(edge.first(), edge.second());
841 }
842 }
843
844 for (const auto& arc: constraintMandatoryArcs_.arcs()) {
845 mandatoryGraph.addArc(arc.tail(), arc.head());
846 forbiddenGraph.addArc(arc.head(), arc.tail());
847 }
848 for (const auto& arc: constraintForbiddenArcs_.arcs()) {
849 forbiddenGraph.addArc(arc.tail(), arc.head());
850 }
851
853 gum::NodeProperty< gum::Size > copyOrder = gum::HashTable(sliceOrder);
854 for (const auto& [n1, r1]: sliceOrder) {
855 for (const auto& [n2, r2]: copyOrder) {
856 if (r1 > r2) {
857 forbiddenGraph.addArc(n1, n2);
858 } else if (r2 > r1) {
859 forbiddenGraph.addArc(n2, n1);
860 }
861 }
862 copyOrder.erase(n1);
863 }
864
865 const auto& totalOrder = constraintTotalOrder_.totalOrder();
866 for (auto iter1 = totalOrder.begin(); iter1 != totalOrder.end(); ++iter1) {
867 for (auto iter2 = iter1 + 1; iter2 != totalOrder.end(); ++iter2) {
868 forbiddenGraph.addArc(*iter2, *iter1);
869 }
870 }
871
872 for (const auto node: constraintNoParentNodes_.nodes()) {
873 for (const auto node2: mgraph.nodes()) {
874 if (node != node2) { forbiddenGraph.addArc(node2, node); }
875 }
876 }
877 for (const auto node: constraintNoChildrenNodes_.nodes()) {
878 for (const auto node2: mgraph.nodes()) {
879 if (node != node2) { forbiddenGraph.addArc(node, node2); }
880 }
881 }
882
883 if (indepTestFCI_) {
884 delete indepTestFCI_;
885 indepTestFCI_ = nullptr;
886 }
889 *noPrior_,
890 ranges_,
892 } else {
894 *noPrior_,
895 ranges_,
897 }
898
900 algoFCI_.setMandatoryGraph(mandatoryGraph);
901 algoFCI_.setForbiddenGraph(forbiddenGraph);
906
907 return mgraph;
908 }
909
911 const auto name = gum::config().typed< std::string >("learning", "BNLearnerPreferredAlgo");
912 if (name == "MIIC") return AlgoType::MIIC;
913 if (name == "PC") return AlgoType::PC;
914 if (name == "FCI") return AlgoType::FCI;
915 if (name == "GreedyHillClimbing") return AlgoType::GREEDY_HILL_CLIMBING;
916 if (name == "ExtendedGreedyHillClimbing") return AlgoType::EXTENDED_GREEDY_HILL_CLIMBING;
917 if (name == "LocalSearchWithTabuList") return AlgoType::LOCAL_SEARCH_WITH_TABU_LIST;
918 if (name == "GreedyThickThinning") return AlgoType::GREEDY_THICK_THINNING;
920 "'" << name << "' is not a valid value for learning.BNLearnerPreferredAlgo")
921 }
922
927 "Score-based algorithms do not build PDAG. Please use a constraint-based "
928 "algorithm instead")
929 }
930 // check that the database does not contain any missing value
933 "For the moment, the BNLearner is unable to learn "
934 << "structures with missing values in databases")
935 }
936
938 BNLearnerListener listener(this, algoPC_);
939 MixedGraph mgraph = this->preparePC_();
940 return algoPC_.learnPDAG(mgraph);
941 }
942
944 BNLearnerListener listener(this, algoFCI_);
945 MixedGraph mgraph = this->prepareFCI_();
946 return algoFCI_.learnPDAG(mgraph);
947 }
948
949 BNLearnerListener listener(this, algoMiic_);
950 // create the mixedGraph_constraint_MandatoryArcs.arcs
951 MixedGraph mgraph = this->prepareMiic_();
953 return algoMiic_.learnPDAG(mgraph);
954 }
955
957 auto pdag = learnPDAG_();
958 for (const auto i: pdag) {
959 pdag.setName(i, scoreDatabase_.databaseTable().variableName(i));
960 }
961 return pdag;
962 }
963
966 GUM_ERROR(OperationNotAllowed, "learnPAG() is only valid when using the FCI algorithm")
967 }
970 "For the moment, the BNLearner is unable to learn "
971 << "structures with missing values in databases")
972 }
973 BNLearnerListener listener(this, algoFCI_);
974 MixedGraph mgraph = this->prepareFCI_();
975 return algoFCI_.learnPAG(mgraph);
976 }
977
979 auto pag = learnPAG_();
980 for (const auto i: pag) {
981 pag.setName(i, scoreDatabase_.databaseTable().variableName(i));
982 }
983 return pag;
984 }
985
987 createPrior_();
988 createScore_();
989 auto dag = learnDag_();
990 for (const auto i: dag) {
991 dag.setName(i, scoreDatabase_.databaseTable().variableName(i));
992 }
993 return dag;
994 }
995
997 if (mutualInfo_ != nullptr) delete mutualInfo_;
998
1000 *noPrior_,
1001 ranges_,
1003 switch (kmodeMiic_) {
1005 case MDL : mutualInfo_->useMDL(); break;
1006 case NML : mutualInfo_->useNML(); break;
1007 case NoCorr : mutualInfo_->useNoCorr(); break;
1008 default :
1010 "The BNLearner's corrected mutual information class does "
1011 << "not implement yet this correction : " << int(kmodeMiic_))
1012 }
1013 }
1014
1016 // check that the database does not contain any missing value
1018 || ((priorDatabase_ != nullptr)
1022 "For the moment, the BNLearner is unable to cope "
1023 "with missing values in databases")
1024 }
1025 // add the mandatory arcs to the initial dag and remove the forbidden ones
1026 // from the initial graph
1027 DAG init_graph = initialDag_;
1028
1029 for (const auto& arc: constraintMandatoryArcs_.arcs()) {
1030 if (!init_graph.exists(arc.tail())) init_graph.addNodeWithId(arc.tail());
1031
1032 if (!init_graph.exists(arc.head())) init_graph.addNodeWithId(arc.head());
1033
1034 init_graph.addArc(arc.tail(), arc.head());
1035 }
1036
1037 for (const auto& arc: constraintForbiddenArcs_.arcs())
1038 init_graph.eraseArc(arc);
1039
1040
1041 switch (selectedAlgo_) {
1042 // ========================================================================
1043 case AlgoType::PC : {
1044 BNLearnerListener listener(this, algoPC_);
1045 MixedGraph mgraph = this->preparePC_();
1046 return algoPC_.learnDAG(mgraph);
1047 }
1048
1049 // ========================================================================
1050 case AlgoType::FCI : {
1051 BNLearnerListener listener(this, algoFCI_);
1052 MixedGraph mgraph = this->prepareFCI_();
1053 return algoFCI_.learnDAG(mgraph);
1054 }
1055
1056 // ========================================================================
1057 case AlgoType::MIIC : {
1058 BNLearnerListener listener(this, algoMiic_);
1059 // create the mixedGraph and the corrected mutual information
1060 MixedGraph mgraph = this->prepareMiic_();
1061
1063 return algoMiic_.learnDAG(mgraph);
1064 }
1065
1066 // ========================================================================
1068 BNLearnerListener listener(this, greedyHillClimbing_);
1076 invariable_constraints;
1077 static_cast< StructuralConstraintMandatoryArcs& >(invariable_constraints)
1079 static_cast< StructuralConstraintForbiddenArcs& >(invariable_constraints)
1081 static_cast< StructuralConstraintPossibleEdges& >(invariable_constraints)
1083 static_cast< StructuralConstraintSliceOrder& >(invariable_constraints)
1085 static_cast< StructuralConstraintNoParentNodes& >(invariable_constraints)
1087 static_cast< StructuralConstraintNoChildrenNodes& >(invariable_constraints)
1089 static_cast< StructuralConstraintTotalOrder& >(invariable_constraints)
1091
1093 variable_constraints;
1094 static_cast< StructuralConstraintIndegree& >(variable_constraints) = constraintIndegree_;
1095
1097 invariable_constraints,
1098 variable_constraints);
1099
1100 // enforce that greedy hill climbing uses arc additions, deletions and reversals
1101 // and only these operations
1102 selector.useArcAdditions(true);
1103 selector.useArcDeletions(true);
1104 selector.useArcReversals(true);
1105 selector.useArcTriangleDeletions(false);
1106
1107 return greedyHillClimbing_.learnStructure(selector, init_graph);
1108 }
1109
1110 // ========================================================================
1120 invariable_constraints;
1121 static_cast< StructuralConstraintMandatoryArcs& >(invariable_constraints)
1123 static_cast< StructuralConstraintForbiddenArcs& >(invariable_constraints)
1125 static_cast< StructuralConstraintPossibleEdges& >(invariable_constraints)
1127 static_cast< StructuralConstraintSliceOrder& >(invariable_constraints)
1129 static_cast< StructuralConstraintNoParentNodes& >(invariable_constraints)
1131 static_cast< StructuralConstraintNoChildrenNodes& >(invariable_constraints)
1133 static_cast< StructuralConstraintTotalOrder& >(invariable_constraints)
1135
1137 variable_constraints;
1138 static_cast< StructuralConstraintIndegree& >(variable_constraints) = constraintIndegree_;
1139
1141 invariable_constraints,
1142 variable_constraints);
1143
1148
1149 return greedyHillClimbing_.learnStructure(selector, init_graph);
1150 }
1151
1152 // ========================================================================
1162 invariable_constraints;
1163 static_cast< StructuralConstraintMandatoryArcs& >(invariable_constraints)
1165 static_cast< StructuralConstraintForbiddenArcs& >(invariable_constraints)
1167 static_cast< StructuralConstraintPossibleEdges& >(invariable_constraints)
1169 static_cast< StructuralConstraintSliceOrder& >(invariable_constraints)
1171 static_cast< StructuralConstraintNoParentNodes& >(invariable_constraints)
1173 static_cast< StructuralConstraintNoChildrenNodes& >(invariable_constraints)
1175 static_cast< StructuralConstraintTotalOrder& >(invariable_constraints)
1177
1179 variable_constraints;
1180 static_cast< StructuralConstraintIndegree& >(variable_constraints) = constraintIndegree_;
1181
1183 invariable_constraints,
1184 variable_constraints);
1185
1186 return greedyThickThinning_.learnStructure(selector, init_graph);
1187 }
1188
1189 // ========================================================================
1199 invariable_constraints;
1200 static_cast< StructuralConstraintMandatoryArcs& >(invariable_constraints)
1202 static_cast< StructuralConstraintForbiddenArcs& >(invariable_constraints)
1204 static_cast< StructuralConstraintPossibleEdges& >(invariable_constraints)
1206 static_cast< StructuralConstraintSliceOrder& >(invariable_constraints)
1208 static_cast< StructuralConstraintNoParentNodes& >(invariable_constraints)
1210 static_cast< StructuralConstraintNoChildrenNodes& >(invariable_constraints)
1212 static_cast< StructuralConstraintTotalOrder& >(invariable_constraints)
1214
1218 variable_constraints;
1219 static_cast< StructuralConstraintTabuList& >(variable_constraints) = constraintTabuList_;
1220 static_cast< StructuralConstraintIndegree& >(variable_constraints) = constraintIndegree_;
1221
1223 invariable_constraints,
1224 variable_constraints);
1225
1230
1231 return localSearchWithTabuList_.learnStructure(selector, init_graph);
1232 }
1233
1234 // ========================================================================
1235 case AlgoType::K2 : {
1243 invariable_constraints;
1244 static_cast< StructuralConstraintMandatoryArcs& >(invariable_constraints)
1246 static_cast< StructuralConstraintForbiddenArcs& >(invariable_constraints)
1248 static_cast< StructuralConstraintPossibleEdges& >(invariable_constraints)
1250 static_cast< StructuralConstraintNoParentNodes& >(invariable_constraints)
1252 static_cast< StructuralConstraintNoChildrenNodes& >(invariable_constraints)
1254 static_cast< StructuralConstraintTotalOrder& >(invariable_constraints)
1256
1257 // if some mandatory arcs are incompatible with the order, use a DAG
1258 // constraint instead of a DiGraph constraint to avoid cycles
1259 const ArcSet& mandatory_arcs
1260 = static_cast< StructuralConstraintMandatoryArcs& >(invariable_constraints).arcs();
1261 const Sequence< NodeId >& order = algoK2_.order();
1262 bool order_compatible = true;
1263
1264 for (const auto& arc: mandatory_arcs) {
1265 if (order.pos(arc.tail()) >= order.pos(arc.head())) {
1266 order_compatible = false;
1267 break;
1268 }
1269 }
1270
1271 if (order_compatible) {
1273 variable_constraints;
1274 static_cast< StructuralConstraintIndegree& >(variable_constraints) = constraintIndegree_;
1275
1277 invariable_constraints,
1278 variable_constraints);
1279
1280 return algoK2_.learnStructure(selector, init_graph);
1281 } else {
1283 variable_constraints;
1284 static_cast< StructuralConstraintIndegree& >(variable_constraints) = constraintIndegree_;
1285
1287 invariable_constraints,
1288 variable_constraints);
1289
1290 return algoK2_.learnStructure(selector, init_graph);
1291 }
1292 }
1293 }
1294
1296 "the learnDAG method has not been implemented for this "
1297 "learning algorithm")
1298 }
1299
1301 if (this->isConstraintBased()) return "";
1302
1303 const auto prior = getPriorType_();
1304
1305 switch (scoreType_) {
1307 case AIC : return ScoreAIC::isPriorCompatible(prior, priorWeight_);
1308
1309 case BD : return ScoreBD::isPriorCompatible(prior, priorWeight_);
1310
1311 case BDeu : return ScoreBDeu::isPriorCompatible(prior, priorWeight_);
1312
1313 case BIC : return ScoreBIC::isPriorCompatible(prior, priorWeight_);
1314
1315 case fNML : return ScorefNML::isPriorCompatible(prior, priorWeight_);
1316
1317 case K2 : return ScoreK2::isPriorCompatible(prior, priorWeight_);
1318
1319 case LOG2LIKELIHOOD : return ScoreLog2Likelihood::isPriorCompatible(prior, priorWeight_);
1320
1321 case MDL : return ScoreMDL::isPriorCompatible(prior, priorWeight_);
1322
1323 default : return "IBNLearner does not support yet this score";
1324 }
1325 }
1326
1328 std::pair< std::size_t, std::size_t >
1329 IBNLearner::useCrossValidationFold(const std::size_t learning_fold,
1330 const std::size_t k_fold) {
1331 if (k_fold == 0) { GUM_ERROR(OutOfBounds, "K-fold cross validation with k=0 is forbidden") }
1332
1333 if (learning_fold >= k_fold) {
1335 "In " << k_fold << "-fold cross validation, the learning "
1336 << "fold should be strictly lower than " << k_fold
1337 << " but, here, it is equal to " << learning_fold)
1338 }
1339
1340 const std::size_t db_size = scoreDatabase_.databaseTable().nbRows();
1341 if (k_fold >= db_size) {
1343 "In " << k_fold << "-fold cross validation, the database's "
1344 << "size should be strictly greater than " << k_fold
1345 << " but, here, the database has only " << db_size << "rows")
1346 }
1347
1348 // create the ranges of rows of the test database
1349 const std::size_t foldSize = db_size / k_fold;
1350 const std::size_t unfold_deb = learning_fold * foldSize;
1351 const std::size_t unfold_end = unfold_deb + foldSize;
1352
1353 ranges_.clear();
1354 if (learning_fold == std::size_t(0)) {
1355 ranges_.push_back(std::pair< std::size_t, std::size_t >(unfold_end, db_size));
1356 } else {
1357 ranges_.push_back(std::pair< std::size_t, std::size_t >(std::size_t(0), unfold_deb));
1358
1359 if (learning_fold != k_fold - 1) {
1360 ranges_.push_back(std::pair< std::size_t, std::size_t >(unfold_end, db_size));
1361 }
1362 }
1363
1364 return std::pair< std::size_t, std::size_t >(unfold_deb, unfold_end);
1365 }
1366
1367 std::pair< double, double >
1368 IBNLearner::chi2(const NodeId id1, const NodeId id2, const std::vector< NodeId >& knowing) {
1369 createPrior_();
1371
1372 return chi2score.statistics(id1, id2, knowing);
1373 }
1374
1375 std::pair< double, double > IBNLearner::chi2(std::string_view name1,
1376 std::string_view name2,
1377 const std::vector< std::string >& knowing) {
1378 std::vector< NodeId > knowingIds;
1379 std::transform(knowing.begin(),
1380 knowing.end(),
1381 std::back_inserter(knowingIds),
1382 [this](const std::string& c) { return this->idFromName(c); });
1383 return chi2(idFromName(name1), idFromName(name2), knowingIds);
1384 }
1385
1386 std::pair< double, double >
1387 IBNLearner::G2(const NodeId id1, const NodeId id2, const std::vector< NodeId >& knowing) {
1388 createPrior_();
1390 return g2score.statistics(id1, id2, knowing);
1391 }
1392
1393 std::pair< double, double > IBNLearner::G2(std::string_view name1,
1394 std::string_view name2,
1395 const std::vector< std::string >& knowing) {
1396 std::vector< NodeId > knowingIds;
1397 std::transform(knowing.begin(),
1398 knowing.end(),
1399 std::back_inserter(knowingIds),
1400 [this](const std::string& c) { return this->idFromName(c); });
1401 return G2(idFromName(name1), idFromName(name2), knowingIds);
1402 }
1403
1404 double IBNLearner::logLikelihood(const std::vector< NodeId >& vars,
1405 const std::vector< NodeId >& knowing) {
1406 createPrior_();
1408
1409 std::vector< NodeId > total(vars);
1410 total.insert(total.end(), knowing.begin(), knowing.end());
1411 double LLtotal = ll2score.score(IdCondSet(total, false, true));
1412 if (knowing.size() == (Size)0) {
1413 return LLtotal;
1414 } else {
1415 double LLknw = ll2score.score(IdCondSet(knowing, false, true));
1416 return LLtotal - LLknw;
1417 }
1418 }
1419
1420 double IBNLearner::logLikelihood(const std::vector< std::string >& vars,
1421 const std::vector< std::string >& knowing) {
1422 std::vector< NodeId > ids;
1423 std::vector< NodeId > knowingIds;
1424
1425 auto mapper = [this](const std::string& c) { return this->idFromName(c); };
1426
1427 std::transform(vars.begin(), vars.end(), std::back_inserter(ids), mapper);
1428 std::transform(knowing.begin(), knowing.end(), std::back_inserter(knowingIds), mapper);
1429
1430 return logLikelihood(ids, knowingIds);
1431 }
1432
1434 const NodeId id2,
1435 const std::vector< NodeId >& knowing) {
1436 createPrior_();
1438 *prior_,
1439 databaseRanges());
1440
1441 switch (kmodeMiic_) {
1443 case MDL : cmi.useMDL(); break;
1444
1445 case NML : cmi.useNML(); break;
1446
1447 case NoCorr : cmi.useNoCorr(); break;
1448
1449 default :
1451 "The BNLearner's corrected mutual information class does "
1452 << "not implement yet this correction : " << int(kmodeMiic_))
1453 }
1454 if (knowing.size() == (Size)0) return cmi.score(id1, id2) / scoreDatabase_.weight();
1455 else return cmi.score(id1, id2, knowing) / scoreDatabase_.weight();
1456 }
1457
1458 double IBNLearner::correctedMutualInformation(std::string_view var1,
1459 std::string_view var2,
1460 const std::vector< std::string >& knowing) {
1461 std::vector< NodeId > knowingIds;
1462
1463 auto mapper = [this](const std::string& c) { return this->idFromName(c); };
1464
1465 std::transform(knowing.begin(), knowing.end(), std::back_inserter(knowingIds), mapper);
1466
1467 return correctedMutualInformation(this->idFromName(var1), this->idFromName(var2), knowingIds);
1468 }
1469
1471 const NodeId id2,
1472 const std::vector< NodeId >& knowing) {
1475 cmi.useNoCorr();
1476
1477 if (knowing.size() == (Size)0) return cmi.score(id1, id2) / scoreDatabase_.weight();
1478 else return cmi.score(id1, id2, knowing) / scoreDatabase_.weight();
1479 }
1480
1481 double IBNLearner::mutualInformation(std::string_view var1,
1482 std::string_view var2,
1483 const std::vector< std::string >& knowing) {
1484 std::vector< NodeId > knowingIds;
1485
1486 auto mapper = [this](const std::string& c) { return this->idFromName(c); };
1487
1488 std::transform(knowing.begin(), knowing.end(), std::back_inserter(knowingIds), mapper);
1489
1490 return mutualInformation(this->idFromName(var1), this->idFromName(var2), knowingIds);
1491 }
1492
1493 double IBNLearner::score(const NodeId var, const std::vector< NodeId >& knowing) {
1494 createPrior_();
1495 createScore_();
1496
1497 return score_->score(var, knowing);
1498 }
1499
1500 double IBNLearner::score(std::string_view var, const std::vector< std::string >& knowing) {
1501 auto mapper = [this](const std::string& c) { return this->idFromName(c); };
1502
1503 const NodeId id = this->idFromName(var);
1504 std::vector< NodeId > knowingIds;
1505 knowingIds.reserve(knowing.size());
1506 std::transform(knowing.begin(), knowing.end(), std::back_inserter(knowingIds), mapper);
1507
1508 return score(id, knowingIds);
1509 }
1510
1511 std::vector< double > IBNLearner::rawPseudoCount(const std::vector< NodeId >& vars) {
1512 if (this->hasMissingValues()) {
1514 "BNLearner cannot compute pseudo-counts with missing values in the database")
1515 }
1516 if (vars.empty()) {
1517 GUM_ERROR(OutOfBounds, "BNLearner::rawPseudoCount called with an empty vector of variables")
1518 }
1519 Tensor< double > res;
1520
1521 createPrior_();
1523 return count.get(vars);
1524 }
1525
1526 std::vector< double > IBNLearner::rawPseudoCount(const std::vector< std::string >& vars) {
1527 std::vector< NodeId > ids;
1528
1529 auto mapper = [this](const std::string& c) { return this->idFromName(c); };
1530
1531 std::transform(vars.begin(), vars.end(), std::back_inserter(ids), mapper);
1532 return rawPseudoCount(ids);
1533 }
1534
1537 const std::vector< std::pair< std::size_t, std::size_t > >& new_ranges) {
1538 // use a score to detect whether the ranges are ok
1540 score.setRanges(new_ranges);
1541 ranges_ = score.ranges();
1542 }
1543} // namespace gum::learning
A listener that allows BNLearner to be used as a proxy for its inner algorithms.
A class for generic framework of learning algorithms that can easily be used.
A pack of learning algorithms that can easily be used.
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
T typed(std::string_view section, std::string_view key) const
Value of section.key converted according to its declared type.
Base class for dag.
Definition DAG.h:121
void addArc(NodeId tail, NodeId head) final
insert a new arc into the directed graph
Definition DAG_inl.h:75
Base class for all oriented graphs.
Definition diGraph.h:136
void addArc(const NodeId tail, const NodeId head) override
insert a new arc into the directed graph
Definition diGraph_inl.h:59
Base class for all aGrUM's exceptions.
Definition exceptions.h:133
Exception : a I/O format was not found.
The class for generic Hash Tables.
Definition hashTable.h:640
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Exception: at least one argument passed to a function is not what was expected.
Error: The database contains some missing values.
Error: A name of variable is not found in the database.
Base class for mixed graphs.
Definition mixedGraph.h:150
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
bool exists(const NodeId id) const
alias for existsNode
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
Exception : there is something wrong with an implementation.
Exception : operation not allowed.
Exception : out of bound.
Partial Ancestral Graph: undirected topology with endpoint marks.
Definition PAG.h:94
Base class for partially directed acyclic graphs.
Definition PDAG.h:134
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition set_tpl.h:613
bool isGumNumberOfThreadsOverriden() const override
indicates whether the class containing this ThreadNumberManager set its own number of threads
Size getNumberOfThreads() const override
returns the current max number of threads used by the class containing this ThreadNumberManager
ThreadNumberManager(Size nb_threads=0)
default constructor
ThreadNumberManager & operator=(const ThreadNumberManager &from)
copy operator
void addEdge(NodeId first, NodeId second) override
insert a new edge into the undirected graph
Base class for every random variable.
Definition variable.h:81
A class that redirects gum_signal from algorithms to the listeners of BNLearn.
void setAlpha(double alpha)
maximum conditioning set size; Size(-1) = unlimited (default)
void setIndependenceTest(IndependenceTest &test)
inject independence test (non-owning: caller manages lifetime)
void setUCPriority(UCPriority p)
collider candidate ordering (default: Standard)
@ Sorted
descending p-value order (strongest evidence first)
@ Standard
process triples in natural traversal order
void setMaxCondSetSize(Size max_k)
maximum conditioning set size; Size(-1) = unlimited (default)
void setStable(bool stable)
stable mode: defer edge removals until end of each depth (default: true)
void setExhaustiveSepSet(bool exhaustive)
exhaustive sepset mode: accumulate union of all separating sets found at each depth,...
void setMandatoryGraph(const gum::DAG &mandaGraph)
void setForbiddenGraph(const gum::DiGraph &forbidGraph)
PDAG learnPDAG(MixedGraph graph)
learns the essential graph (CPDAG)
void addConstraints(const HashTable< std::pair< NodeId, NodeId >, char > &constraints)
DAG learnDAG(MixedGraph graph)
learns a DAG
The class computing n times the corrected mutual information, as used in the MIIC algorithm.
KModeTypes
the description type for the complexity correction
double score(NodeId var1, NodeId var2)
returns the 2-point mutual information corresponding to a given nodeset
void useNML()
use the kNML penalty function
CorrectedMutualInformation(const DBRowGeneratorParser &parser, const Prior &prior, const std::vector< std::pair< std::size_t, std::size_t > > &ranges, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
void useNoCorr()
use no correction/penalty function
void useMDL()
use the MDL penalty function
The class for initializing DatabaseTable and RawDatabaseTable instances from CSV files.
the class used to read a row in the database and to transform it into a set of DBRow instances that c...
DBRowGeneratorParser(const typename DatabaseTable::Handler &handler, const DBRowGeneratorSet &generator_set)
default constructor
The class used to pack sets of generators.
The databases' cell translators for labelized variables.
the class for packing together the translators used to preprocess the datasets
std::size_t insertTranslator(const DBTranslator &translator, const std::size_t column, const bool unique_column=true)
inserts a new translator at the end of the translator set
The class representing a tabular database as used by learning tasks.
void changeTranslator(DBTranslator &new_translator, std::size_t k, bool k_is_input_col=false)
change the translator of a database column
DBVector< std::size_t > domainSizes() const
returns the domain sizes of all the variables in the database table
void setVariableNames(const std::vector< std::string > &names, const bool from_external_object=true) override
sets the names of the variables
void reorder(const std::size_t k, const bool k_is_input_col=false)
performs a reordering of the kth translator or of the first translator parsing the kth column of the ...
const Variable & variable(const std::size_t k, const bool k_is_input_col=false) const
returns either the kth variable of the database table or the first one corresponding to the kth colum...
std::vector< std::pair< Idx, std::shared_ptr< DBTranslator > > > betterTranslators() const
propose a set with translators better suited for the content of the database
PAG learnPAG(MixedGraph graph)
primary FCI output: learn a PAG from the given node-only or partial graph
Definition FCI.cpp:818
void setMaxPathLength(Size maxLen)
maximum path length for R4 discriminating-path search; Size(-1) = unlimited
Definition FCI.cpp:82
The mecanism to compute the next available graph changes for directed structure learning search algor...
void useArcAdditions(bool use)
sets whether or not the selector allows the application of arc additions
void useArcDeletions(bool use)
sets whether or not the selector allows the application of arc deletions
void useArcReversals(bool use)
sets whether or not the selector allows the application of arc reversals
void useArcTriangleDeletions(bool use)
sets whether or not the selector allows the application of arc triangle deletions
DAG learnStructure(GRAPH_CHANGES_SELECTOR &selector, DAG initial_dag=DAG())
learns the structure of a Bayes net
DAG learnStructure(GRAPH_CHANGES_SELECTOR &selector, DAG initial_dag=DAG())
learns the structure of a Bayes net
a helper to easily read databases
Definition IBNLearner.h:138
const DatabaseTable & databaseTable() const
returns the internal database table
std::vector< std::size_t > _domain_sizes_
the domain sizes of the variables (useful to speed-up computations)
Definition IBNLearner.h:280
DatabaseTable _database_
the database itself
Definition IBNLearner.h:274
double weight(const std::size_t i) const
returns the weight of the ith record
Bijection< NodeId, std::size_t > _nodeId2cols_
a bijection assigning to each variable name its NodeId
Definition IBNLearner.h:283
Database(std::string_view file, const std::vector< std::string > &missing_symbols, const bool induceTypes=false)
default constructor
const Bijection< NodeId, std::size_t > & nodeId2Columns() const
returns the mapping between node ids and their columns in the database
Database & operator=(const Database &from)
copy operator
DBRowGeneratorParser & parser()
returns the parser for the database
DBRowGeneratorParser * _parser_
the parser used for reading the database
Definition IBNLearner.h:277
StructuralConstraintPossibleEdges constraintPossibleEdges_
the constraint on possible Edges
MixedGraph preparePC_()
prepares the initial graph and independence test for PC
StructuralConstraintNoParentNodes constraintNoParentNodes_
the constraint on no parent nodes
BNLearnerPriorType priorType_
the a priorselected for the score and parameters
PAG learnPAG()
learn a PAG — only valid when useFCI() has been called
std::string priorDbname_
the filename for the Dirichlet a priori, if any
double priorWeight_
the weight of the prior. It is always written by the useXxxPrior() methods, which read learning....
std::vector< std::pair< std::size_t, std::size_t > > ranges_
the set of rows' ranges within the database in which learning is done
~IBNLearner() override
destructor
std::string checkScorePriorCompatibility() const
checks whether the current score and prior are compatible
GreedyHillClimbing extendedGreedyHillClimbing_
the extended greedy hill climbing
AlgoType
an enumeration to select easily the learning algorithm to use
Definition IBNLearner.h:126
bool allowArcTriangleDeletions_
whether we allow or not arc deletions during learning
virtual void createPrior_()=0
create the prior used for learning
K2 algoK2_
the K2 algorithm
IndepTestType indepTestTypeFCI_
independence test type for FCI (reuses IndepTestType defined above)
AlgoType selectedAlgo_
the learning algorithm ; it is initialized from the configuration, see preferredAlgo()
const std::vector< std::pair< std::size_t, std::size_t > > & databaseRanges() const
returns the current database rows' ranges used for learning
bool allowArcAdditions_
whether we allow or not arc additions during learning
double logLikelihood(const std::vector< NodeId > &vars, const std::vector< NodeId > &knowing={})
Return the loglikelihood of vars in the base, conditioned by knowing for the BNLearner.
Database scoreDatabase_
the database to be used by the scores and parameter estimators
ScoreType
an enumeration enabling to select easily the score we wish to use
Definition IBNLearner.h:110
bool useEM_
a Boolean indicating whether we should use EM for parameter learning or not
DAG2BNLearner dag2BN_
the parametric EM
Prior * prior_
the prior used
void useDatabaseRanges(const std::vector< std::pair< std::size_t, std::size_t > > &new_ranges)
use a new set of database rows' ranges to perform learning
std::pair< double, double > chi2(NodeId id1, NodeId id2, const std::vector< NodeId > &knowing={})
Return the <statistic,pvalue> pair for chi2 test in the database.
CorrectedMutualInformation * mutualInfo_
the selected correction for miic
static AlgoType preferredAlgo()
the algorithm used by the learners when none is selected explicitly
StructuralConstraintNoChildrenNodes constraintNoChildrenNodes_
the constraint on no children nodes
static void isCSVFileName_(std::string_view filename)
checks whether the extension of a CSV filename is correct
gum::learning::FCI algoFCI_
the FCI algorithm
ParamEstimatorType paramEstimatorType_
the type of the parameter estimator
ScoreType scoreType_
the score selected for learning
std::pair< double, double > G2(NodeId id1, NodeId id2, const std::vector< NodeId > &knowing={})
Return the <statistic,pvalue> pair for for G2 test in the database.
const ApproximationScheme * currentAlgorithm_
IndependenceTest * indepTestPC_
owned independence test object for PC (rebuilt before each learn call)
DAG learnDag_()
returns the DAG learnt
Database * priorDatabase_
the database used by the Dirichlet a priori
double mutualInformation(NodeId id1, NodeId id2, const std::vector< NodeId > &knowing={})
Return the mutual information of id1 and id2 in the base, conditioned by knowing for the BNLearner.
void createScore_()
create the score used for learning
PriorType getPriorType_() const
returns the type (as a string) of a given prior
double alphaFci_
FCI parameters the threshold of the tests ; it is initialized from the key learning....
double alphaPc_
PC parameters the threshold of the tests ; it is initialized from the key learning....
StructuralConstraintIndegree constraintIndegree_
the constraint for indegrees
bool allowArcDeletions_
whether we allow or not arc deletions during learning
PDAG learnPDAG()
learn a partial structure from a file (must have read the db before and must have selected miic)
static DatabaseTable readFile_(std::string_view filename, const std::vector< std::string > &missing_symbols)
reads a file and returns a databaseVectInRam
std::string filename_
the filename database
bool hasMissingValues() const
returns true if the learner's database has missing values
NodeId idFromName(std::string_view var_name) const
returns the node id corresponding to a variable name
Score * score_
the score used
StructuralConstraintMandatoryArcs constraintMandatoryArcs_
the constraint on mandatory arcs
Miic algoMiic_
the Constraint MIIC algorithm
void createCorrectedMutualInformation_()
create the Corrected Mutual Information instance for Miic
IndepTestType indepTestTypePC_
StructuralConstraintForbiddenArcs constraintForbiddenArcs_
the constraint on forbidden arcs
StructuralConstraintTotalOrder constraintTotalOrder_
the total order ing constraint
GreedyHillClimbing greedyHillClimbing_
the greedy hill climbing algorithm
DAG learnDAG()
learn a structure from a file (must have read the db before)
double score(NodeId vars, const std::vector< NodeId > &knowing={})
Return the value of the score currently in use by the BNLearner of a variable given a set of other va...
StructuralConstraintTabuList constraintTabuList_
the constraint for tabu lists
DAG initialDag_
an initial DAG given to learners
GreedyThickThinning greedyThickThinning_
the greedy thick-thinning algorithm
MixedGraph prepareFCI_()
prepares the initial graph and independence test for FCI
IBNLearner & operator=(const IBNLearner &)
copy operator
gum::learning::PC algoPC_
the PC algorithm
bool allowArcReversals_
whether we allow or not arc reversals during learning
MixedGraph prepareMiic_()
prepares the initial graph for miic
IBNLearner(std::string_view filename, const std::vector< std::string > &missingSymbols, bool induceTypes=true)
read the database file for the score / parameter estimation and var names
LocalSearchWithTabuList localSearchWithTabuList_
the local search with tabu list algorithm
IndependenceTest * indepTestFCI_
owned independence test object for FCI (rebuilt before each learn call)
std::pair< std::size_t, std::size_t > useCrossValidationFold(const std::size_t learning_fold, const std::size_t k_fold)
sets the ranges of rows to be used for cross-validation learning
ParamEstimator * createParamEstimator_(const DBRowGeneratorParser &parser, bool take_into_account_score=true)
create the parameter estimator used for learning
StructuralConstraintSliceOrder constraintSliceOrder_
the constraint for 2TBNs
const DatabaseTable & database() const
returns the database used by the BNLearner
std::vector< double > rawPseudoCount(const std::vector< NodeId > &vars)
Return the pseudo-counts of NodeIds vars in the base in a raw array.
bool inducedTypes_
the policy for typing variables
CorrectedMutualInformation::KModeTypes kmodeMiic_
the penalty used in MIIC
double correctedMutualInformation(NodeId id1, NodeId id2, const std::vector< NodeId > &knowing={})
Return the mutual information of id1 and id2 in the base, conditioned by knowing for the BNLearner.
bool isConstraintBased() const
indicate if the selected algorithm is constraint-based
void fillDatabase(DATABASE &database, const bool retry_insertion=false)
fills the rows of the database table
const std::vector< std::string > & variableNames()
returns the names of the variables in the input dataset
std::size_t nbRows() const noexcept
returns the number of records (rows) in the database
const DBVector< std::string > & variableNames() const noexcept
returns the variable names for all the columns of the database
iterator handler() const
returns a new unsafe handler pointing to the 1st record of the database
bool hasMissingValues() const
indicates whether the database contains some missing values
const std::string & variableName(const std::size_t k) const
returns the name of the kth column of the IDatabaseTable
std::size_t nbVariables() const noexcept
returns the number of variables (columns) of the database
IdCondSet()
default constructor
the class for computing Chi2 independence test scores
std::pair< double, double > statistics(NodeId var1, NodeId var2, const std::vector< NodeId > &rhs_ids={}) override
get the pair <chi2 statistic,pvalue> for a test var1 indep var2 given rhs_ids
IndepTestChi2(const DBRowGeneratorParser &parser, const Prior &external_prior, const std::vector< std::pair< std::size_t, std::size_t > > &ranges, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
the class for computing G2 independence test scores
Definition indepTestG2.h:66
std::pair< double, double > statistics(NodeId var1, NodeId var2, const std::vector< NodeId > &rhs_ids={}) override
get the pair <G2statistic,pvalue> for a test var1 indep var2 given rhs_ids
IndepTestG2(const DBRowGeneratorParser &parser, const Prior &external_prior, const std::vector< std::pair< std::size_t, std::size_t > > &ranges, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
The K2 algorithm.
Definition K2.h:63
const Sequence< NodeId > & order() const noexcept
returns the current order
DAG learnStructure(GRAPH_CHANGES_SELECTOR &selector, DAG initial_dag=DAG())
learns the structure of a Bayes net
Definition K2_tpl.h:63
ApproximationScheme & approximationScheme()
returns the approximation policy of the learning algorithm
DAG learnStructure(GRAPH_CHANGES_SELECTOR &selector, DAG initial_dag=DAG())
learns the structure of a Bayes net
void setMutualInformation(CorrectedMutualInformation &mi)
Definition Miic.cpp:89
NoPrior(const DatabaseTable &database, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
ParamEstimatorML(const DBRowGeneratorParser &parser, const Prior &external_prior, const Prior &_score_internal_prior, const std::vector< std::pair< std::size_t, std::size_t > > &ranges, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
The base class for estimating parameters of CPTs.
void setRanges(const std::vector< std::pair< std::size_t, std::size_t > > &new_ranges)
sets new ranges to perform the counts used by the parameter estimator
void setNumberOfThreads(Size nb) override
sets the number max of threads that can be used
The class for giving access to pseudo count : count in the database + prior.
Definition pseudoCount.h:67
std::vector< double > get(const std::vector< NodeId > &ids)
returns the pseudo-count of a pair of nodes given some other nodes
std::string isPriorCompatible() const final
indicates whether the prior is compatible (meaningful) with the score
ScoreAIC(const DBRowGeneratorParser &parser, const Prior &prior, const std::vector< std::pair< std::size_t, std::size_t > > &ranges, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
ScoreBD(const DBRowGeneratorParser &parser, const Prior &prior, const std::vector< std::pair< std::size_t, std::size_t > > &ranges, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
std::string isPriorCompatible() const final
indicates whether the prior is compatible (meaningful) with the score
std::string isPriorCompatible() const final
indicates whether the prior is compatible (meaningful) with the score
ScoreBDeu(const DBRowGeneratorParser &parser, const Prior &prior, const std::vector< std::pair< std::size_t, std::size_t > > &ranges, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
std::string isPriorCompatible() const final
indicates whether the prior is compatible (meaningful) with the score
ScoreBIC(const DBRowGeneratorParser &parser, const Prior &prior, const std::vector< std::pair< std::size_t, std::size_t > > &ranges, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
ScoreK2(const DBRowGeneratorParser &parser, const Prior &prior, const std::vector< std::pair< std::size_t, std::size_t > > &ranges, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
std::string isPriorCompatible() const final
indicates whether the prior is compatible (meaningful) with the score
the class for computing Log2-likelihood scores
std::string isPriorCompatible() const final
indicates whether the prior is compatible (meaningful) with the score
ScoreLog2Likelihood(const DBRowGeneratorParser &parser, const Prior &prior, const std::vector< std::pair< std::size_t, std::size_t > > &ranges, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
double score(const IdCondSet &idset)
returns the score for a given IdCondSet
The base class for all the scores used for learning (BIC, BDeu, etc).
Definition score.h:68
const std::vector< std::pair< std::size_t, std::size_t > > & ranges() const
returns the current ranges
virtual const Prior & internalPrior() const =0
returns the internal prior of the score
double score(const NodeId var)
returns the score of a single node
void setRanges(const std::vector< std::pair< std::size_t, std::size_t > > &new_ranges)
sets new ranges to perform the counts used by the score
void setNumberOfThreads(Size nb) override
sets the number max of threads that can be used
std::string isPriorCompatible() const final
indicates whether the prior is compatible (meaningful) with the score
ScorefNML(const DBRowGeneratorParser &parser, const Prior &prior, const std::vector< std::pair< std::size_t, std::size_t > > &ranges, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
The base class for structural constraints imposed by DAGs.
the structural constraint for forbidding the creation of some arcs during structure learning
const ArcSet & arcs() const
returns the set of mandatory arcs
the class for structural constraints limiting the number of parents of nodes in a directed graph
Size maxIndegree() const
sets the default max indegree for all the nodes in the property
the structural constraint indicating that some arcs shall never be removed or reversed
const ArcSet & arcs() const
returns the set of mandatory arcs
the structural constraint for forbidding children for some nodes
const NodeSet & nodes() const
returns the set of w nodes without children
the structural constraint for forbidding parents for some nodes
const NodeSet & nodes() const
returns the set of nodes without parent
the structural constraint for forbidding the creation of some arcs except those defined in the class ...
const EdgeSet & edges() const
returns the set of mandatory arcs
the "meta-programming" class for storing structural constraints
the structural constraint imposing a partial order over nodes
const NodeProperty< NodeId > & sliceOrder() const
returns the current slice order
The class imposing a N-sized tabu list as a structural constraints for learning algorithms.
the structural constraint imposing a total order over some nodes
const Sequence< NodeId > & totalOrder() const
returns the current Total order
Typed, documented, sectioned configuration singleton read from an .ini content.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Set< Edge > EdgeSet
Some typdefs and define for shortcuts ...
Size NodeId
Type for node ids.
Set< Arc > ArcSet
Some typdefs and define for shortcuts ...
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
the class for computing Chi2 scores
the class for computing G2 scores
include the inlined functions if necessary
Definition CSVParser.h:55
ScoreBIC ScoreMDL
Definition scoreMDL.h:67
DatabaseTable readFile(const std::string &filename)
template class GUM_SHARED_PUBLIC Tensor< double >
Definition tensor.cpp:53
Configuration & config()
A short name for Configuration::instance() : gum::config().epsilonStandard(). It is not inline so tha...
STL namespace.
the base class for all the independence tests used for learning
the class for computing Log2-likelihood scores