aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
BNLearner.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
50#ifndef GUM_LEARNING_BN_LEARNER_H
51#define GUM_LEARNING_BN_LEARNER_H
52
53#include <algorithm>
54#include <sstream>
55#include <vector>
56
57#include <agrum/agrum.h>
58
61
62namespace gum {
63 namespace learning {
65
73 template < GUM_Numeric GUM_SCALAR >
74 class BNLearner final: public IBNLearner {
75 public:
76 // ##########################################################################
78 // ##########################################################################
80
82
95 BNLearner(std::string_view filename,
96 const std::vector< std::string >& missingSymbols = {"?"},
97 const bool induceTypes = true);
98
100
107
112 BNLearner(std::string_view filename,
114 const std::vector< std::string >& missing_symbols = {"?"});
115
118
121
123 ~BNLearner() override;
124
126
127 // ##########################################################################
129 // ##########################################################################
131
134
137
139
141 BayesNet< GUM_SCALAR > learnBN();
142
164 BayesNet< GUM_SCALAR > learnParameters(const DAG& dag, bool takeIntoAccountScore = true);
165
196 BayesNet< GUM_SCALAR > learnParameters(const BayesNet< GUM_SCALAR >& bn,
197 bool takeIntoAccountScore = true);
198
221 BayesNet< GUM_SCALAR > learnParameters(bool take_into_account_score = true);
222
224 std::string toString() const;
225
227 std::vector< std::tuple< std::string, std::string, std::string > > state() const;
228
233 void copyState(const BNLearner< GUM_SCALAR >& learner);
234
235 //=== === add return to certain methods in order to chain command
236 BNLearner< GUM_SCALAR >& setInitialDAG(const DAG& dag);
237
258 BNLearner< GUM_SCALAR >& useEM(const double epsilon, const double noise = default_EM_noise);
259
277 BNLearner< GUM_SCALAR >& useEMWithRateCriterion(const double epsilon,
278 const double noise = default_EM_noise);
279
294 BNLearner< GUM_SCALAR >& useEMWithDiffCriterion(const double epsilon,
295 const double noise = default_EM_noise);
296
298 BNLearner< GUM_SCALAR >& forbidEM();
299
308 BNLearner< GUM_SCALAR >& EMsetEpsilon(const double eps);
309
311 BNLearner< GUM_SCALAR >& EMdisableEpsilon();
312
318 BNLearner< GUM_SCALAR >& EMenableEpsilon();
319
328 BNLearner< GUM_SCALAR >& EMsetMinEpsilonRate(const double rate);
329
332
339
345 BNLearner< GUM_SCALAR >& EMsetMaxIter(const Size max);
346
348 BNLearner< GUM_SCALAR >& EMdisableMaxIter();
349
351 BNLearner< GUM_SCALAR >& EMenableMaxIter();
352
358 BNLearner< GUM_SCALAR >& EMsetMaxTime(const double timeout);
359
361 BNLearner< GUM_SCALAR >& EMdisableMaxTime();
362
364 BNLearner< GUM_SCALAR >& EMenableMaxTime();
365
370 BNLearner< GUM_SCALAR >& EMsetPeriodSize(const Size p);
371
372 // for pyagrum support
374
376 BNLearner< GUM_SCALAR >& EMsetVerbosity(const bool v);
377
378 BNLearner< GUM_SCALAR >& useScoreAIC();
379
380 BNLearner< GUM_SCALAR >& useScoreBD();
381
382 BNLearner< GUM_SCALAR >& useScoreBDeu();
383
384 BNLearner< GUM_SCALAR >& useScoreBIC();
385
386 BNLearner< GUM_SCALAR >& useScoreK2();
387
389
390 BNLearner< GUM_SCALAR >& useNoPrior();
391
392 BNLearner< GUM_SCALAR >& useBDeuPrior(double weight = 1.0);
393
394 BNLearner< GUM_SCALAR >& useSmoothingPrior(double weight = 1);
395
396 BNLearner< GUM_SCALAR >& useDirichletPrior(std::string_view filename, double weight = 1);
397
398 BNLearner< GUM_SCALAR >& useDirichletPrior(const gum::BayesNet< GUM_SCALAR >& bn,
399 double weight = 1);
400
402
404
406
407 BNLearner< GUM_SCALAR >& setGreedyThickThinningReversals(bool allow);
408
410
411 BNLearner< GUM_SCALAR >& useLocalSearchWithTabuList(Size tabu_size = 100,
412 Size nb_decrease = 2);
413
414 BNLearner< GUM_SCALAR >& useK2(const Sequence< NodeId >& order);
415
416 BNLearner< GUM_SCALAR >& useK2(const std::vector< NodeId >& order);
417
418 BNLearner< GUM_SCALAR >& useMIIC();
419
420 BNLearner< GUM_SCALAR >& usePC();
421
422 BNLearner< GUM_SCALAR >& useFCI();
423
424 BNLearner< GUM_SCALAR >& useFCIChi2Test();
425
426 BNLearner< GUM_SCALAR >& useFCIG2Test();
427
428 BNLearner< GUM_SCALAR >& setFCIAlpha(double alpha);
429
430 BNLearner< GUM_SCALAR >& setFCIMaxPathLength(Size max_len);
431
432 BNLearner< GUM_SCALAR >& setFCIExhaustiveSepSet(bool exhaustive);
433
435
436 BNLearner< GUM_SCALAR >& useChi2Test();
437
438 BNLearner< GUM_SCALAR >& useG2Test();
439
440 BNLearner< GUM_SCALAR >& setPCAlpha(double alpha);
441
442 BNLearner< GUM_SCALAR >& setPCStable(bool stable);
443
444 BNLearner< GUM_SCALAR >& setPCMaxCondSetSize(Size max_k);
445
446 BNLearner< GUM_SCALAR >& setPCUnshieldedColliderSorted(bool sorted);
447
448 BNLearner< GUM_SCALAR >& useNMLCorrection();
449
450 BNLearner< GUM_SCALAR >& useMDLCorrection();
451
452 BNLearner< GUM_SCALAR >& useNoCorrection();
453
454 BNLearner< GUM_SCALAR >& setMaxIndegree(Size max_indegree);
455
456 BNLearner< GUM_SCALAR >& setSliceOrder(const NodeProperty< NodeId >& slice_order);
457
458 BNLearner< GUM_SCALAR >&
459 setSliceOrder(const std::vector< std::vector< std::string > >& slices);
460
461 BNLearner< GUM_SCALAR >& setTotalOrder(const std::vector< std::string >& order);
462
463 BNLearner< GUM_SCALAR >& setTotalOrder(const Sequence< NodeId >& order);
464
465 BNLearner< GUM_SCALAR >& setForbiddenArcs(const ArcSet& set);
466
467 BNLearner< GUM_SCALAR >& addForbiddenArc(const Arc& arc);
468
469 BNLearner< GUM_SCALAR >& addForbiddenArc(NodeId tail, NodeId head);
470
471 BNLearner< GUM_SCALAR >& addForbiddenArc(std::string_view tail, std::string_view head);
472
473 BNLearner< GUM_SCALAR >& eraseForbiddenArc(const Arc& arc);
474
475 BNLearner< GUM_SCALAR >& eraseForbiddenArc(NodeId tail, NodeId head);
476
477 BNLearner< GUM_SCALAR >& eraseForbiddenArc(std::string_view tail, std::string_view head);
478
479 BNLearner< GUM_SCALAR >& addMandatoryArc(const Arc& arc);
480
481 BNLearner< GUM_SCALAR >& addMandatoryArc(NodeId tail, NodeId head);
482
483 BNLearner< GUM_SCALAR >& addMandatoryArc(std::string_view tail, std::string_view head);
484
485 BNLearner< GUM_SCALAR >& eraseMandatoryArc(const Arc& arc);
486
487 BNLearner< GUM_SCALAR >& eraseMandatoryArc(NodeId tail, NodeId head);
488
489 BNLearner< GUM_SCALAR >& eraseMandatoryArc(std::string_view tail, std::string_view head);
490
491 BNLearner< GUM_SCALAR >& addPossibleEdge(const Edge& edge);
492
493 BNLearner< GUM_SCALAR >& addPossibleEdge(NodeId tail, NodeId head);
494
495 BNLearner< GUM_SCALAR >& addPossibleEdge(std::string_view tail, std::string_view head);
496
497 BNLearner< GUM_SCALAR >& erasePossibleEdge(const Edge& edge);
498
499 BNLearner< GUM_SCALAR >& erasePossibleEdge(NodeId tail, NodeId head);
500
501 BNLearner< GUM_SCALAR >& erasePossibleEdge(std::string_view tail, std::string_view head);
502
503 BNLearner< GUM_SCALAR >& setMandatoryArcs(const ArcSet& set);
504
505 BNLearner< GUM_SCALAR >& setPossibleEdges(const EdgeSet& set);
506
507 BNLearner< GUM_SCALAR >& setPossibleSkeleton(const UndiGraph& skeleton);
508
509 BNLearner< GUM_SCALAR >& addNoParentNode(NodeId node);
510
511 BNLearner< GUM_SCALAR >& addNoParentNode(std::string_view name);
512
513 BNLearner< GUM_SCALAR >& eraseNoParentNode(NodeId node);
514
515 BNLearner< GUM_SCALAR >& eraseNoParentNode(std::string_view name);
516
517 BNLearner< GUM_SCALAR >& addNoChildrenNode(NodeId node);
518
519 BNLearner< GUM_SCALAR >& addNoChildrenNode(std::string_view name);
520
522
523 BNLearner< GUM_SCALAR >& eraseNoChildrenNode(std::string_view name);
524
525 BNLearner< GUM_SCALAR >& allowArcAdditions(bool allow);
526
527 BNLearner< GUM_SCALAR >& allowArcDeletions(bool allow);
528
529 BNLearner< GUM_SCALAR >& allowArcReversals(bool allow);
530
531 BNLearner< GUM_SCALAR >& allowArcTriangleDeletions(bool allow);
532
533 bool isConstraintBased() const;
534
535 bool isScoreBased() const;
536
537 protected:
539 void createPrior_() override;
540
541 private:
542 BayesNet< GUM_SCALAR > _prior_bn_;
543
545 NodeProperty< Sequence< std::string > > _labelsFromBN_(std::string_view filename,
546 const BayesNet< GUM_SCALAR >& src);
547
554 void _checkDAGCompatibility_(const DAG& dag);
555
574 BayesNet< GUM_SCALAR > _learnParameters_(const DAG& dag, bool takeIntoAccountScore);
575
595 std::pair< std::shared_ptr< ParamEstimator >, std::shared_ptr< ParamEstimator > >
596 _initializeEMParameterLearning_(const DAG& dag, bool takeIntoAccountScore);
597
618 BayesNet< GUM_SCALAR > _learnParametersWithEM_(const DAG& dag, bool takeIntoAccountScore);
619
646 BayesNet< GUM_SCALAR > _learnParametersWithEM_(const BayesNet< GUM_SCALAR >& bn,
647 bool takeIntoAccountScore);
648 };
649
651 template < GUM_Numeric GUM_SCALAR >
652 std::ostream& operator<<(std::ostream& output, const BNLearner< GUM_SCALAR >& learner);
653 } /* namespace learning */
654} /* namespace gum */
655
657#include <agrum/BN/learning/BNLearner_tpl.h>
658
659#endif /* GUM_LEARNING_BN_LEARNER_H */
A dirichlet priori: computes its N'_ijk from a bayesian network.
A class for generic framework of learning algorithms that can easily be used.
The base class for all directed edges.
Class representing a Bayesian network.
Definition BayesNet.h:99
Base class for dag.
Definition DAG.h:121
The base class for all undirected edges.
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:994
Base class for undirected graphs.
Definition undiGraph.h:130
A class that redirects gum_signal from algorithms to the listeners of BNLearn.
A pack of learning algorithms that can easily be used.
Definition BNLearner.h:74
BNLearner< GUM_SCALAR > & useNoPrior()
BNLearner< GUM_SCALAR > & EMdisableMaxIter()
Disable stopping criterion on max iterations.
BNLearner< GUM_SCALAR > & useFCIChi2Test()
BNLearner< GUM_SCALAR > & EMenableMinEpsilonRate()
Enable the log-likelihood evolution rate stopping criterion.
BNLearner< GUM_SCALAR > & useGreedyThickThinning()
BNLearner< GUM_SCALAR > & addForbiddenArc(const Arc &arc)
BNLearner< GUM_SCALAR > & useScoreAIC()
BNLearner< GUM_SCALAR > & eraseMandatoryArc(const Arc &arc)
BNLearner< GUM_SCALAR > & setTotalOrder(const std::vector< std::string > &order)
BNLearner< GUM_SCALAR > & EMdisableEpsilon()
Disable the min log-likelihood diff stopping criterion.
BNLearner< GUM_SCALAR > & addPossibleEdge(const Edge &edge)
BNLearner< GUM_SCALAR > & useScoreBD()
BNLearner< GUM_SCALAR > & allowArcReversals(bool allow)
std::vector< std::tuple< std::string, std::string, std::string > > state() const
BNLearner< GUM_SCALAR > & setPCStable(bool stable)
BNLearner< GUM_SCALAR > & addMandatoryArc(const Arc &arc)
BNLearner< GUM_SCALAR > & useBDeuPrior(double weight=1.0)
BNLearner< GUM_SCALAR > & useScoreBIC()
BNLearner< GUM_SCALAR > & usePC()
BNLearner< GUM_SCALAR > & useNoCorrection()
std::pair< std::shared_ptr< ParamEstimator >, std::shared_ptr< ParamEstimator > > _initializeEMParameterLearning_(const DAG &dag, bool takeIntoAccountScore)
initializes EM and returns a pair containing, first, a bootstrap estimator and, second,...
BNLearner< GUM_SCALAR > & useG2Test()
BNLearner< GUM_SCALAR > & setFCIMaxPathLength(Size max_len)
BNLearner(std::string_view filename, const gum::BayesNet< GUM_SCALAR > &src, const std::vector< std::string > &missing_symbols={"?"})
Wrapper for BNLearner (filename,modalities,parse_database) using a bn to find those modalities and no...
BNLearner< GUM_SCALAR > & useSmoothingPrior(double weight=1)
BNLearner< GUM_SCALAR > & EMdisableMinEpsilonRate()
Disable the log-likelihood evolution rate stopping criterion.
BNLearner(std::string_view filename, const std::vector< std::string > &missingSymbols={"?"}, const bool induceTypes=true)
default constructor
BNLearner(const BNLearner &)
copy constructor
BNLearner< GUM_SCALAR > & erasePossibleEdge(const Edge &edge)
BNLearner< GUM_SCALAR > & setGreedyThickThinningReversals(bool allow)
BNLearner & operator=(BNLearner &&) noexcept
move operator
BNLearner< GUM_SCALAR > & setPossibleSkeleton(const UndiGraph &skeleton)
BayesNet< GUM_SCALAR > _learnParameters_(const DAG &dag, bool takeIntoAccountScore)
learns a BN (its parameters) with the structure passed in argument using a single pass estimation (no...
BNLearner(BNLearner &&)
move constructor
~BNLearner() override
destructor
BNLearner< GUM_SCALAR > & EMenableEpsilon()
Enable the log-likelihood min diff stopping criterion in EM.
BNLearner< GUM_SCALAR > & setPCMaxCondSetSize(Size max_k)
BNLearner< GUM_SCALAR > & setPossibleEdges(const EdgeSet &set)
BNLearner< GUM_SCALAR > & allowArcDeletions(bool allow)
BNLearner< GUM_SCALAR > & setFCIExhaustiveSepSet(bool exhaustive)
BNLearner< GUM_SCALAR > & EMenableMaxTime()
enable EM's timeout stopping criterion
BayesNet< GUM_SCALAR > _learnParametersWithEM_(const DAG &dag, bool takeIntoAccountScore)
learns a BN (its parameters) with the structure passed in argument using the EM algorithm initialized...
BNLearner< GUM_SCALAR > & addNoChildrenNode(NodeId node)
BNLearner< GUM_SCALAR > & EMsetEpsilon(const double eps)
sets the stopping criterion of EM as being the minimal difference between two consecutive log-likelih...
BNLearner< GUM_SCALAR > & EMdisableMaxTime()
Disable EM's timeout stopping criterion.
BNLearner & operator=(const BNLearner &)
copy operator
bool greedyThickThinningReversals() const
BNLearner< GUM_SCALAR > & useGreedyHillClimbing()
BNLearner< GUM_SCALAR > & forbidEM()
prevent using the EM algorithm for parameter learning
BNLearner< GUM_SCALAR > & useK2(const Sequence< NodeId > &order)
BNLearner< GUM_SCALAR > & useExtendedGreedyHillClimbing()
BNLearner< GUM_SCALAR > & useScoreK2()
BNLearner< GUM_SCALAR > & setMandatoryArcs(const ArcSet &set)
BNLearner< GUM_SCALAR > & eraseNoChildrenNode(NodeId node)
BNLearner< GUM_SCALAR > & useMIIC()
bool fciExhaustiveSepSet() const
BNLearner< GUM_SCALAR > & EMsetPeriodSize(const Size p)
how many samples between 2 stoppings isEnabled
BayesNet< GUM_SCALAR > learnBN()
learn a Bayes Net from a file (must have read the db before)
BNLearner< GUM_SCALAR > & setForbiddenArcs(const ArcSet &set)
bool isConstraintBased() const
BNLearner< GUM_SCALAR > & useEMWithRateCriterion(const double epsilon, const double noise=default_EM_noise)
use The EM algorithm to learn parameters with the rate stopping criterion
BNLearner< GUM_SCALAR > & setInitialDAG(const DAG &dag)
BNLearner< GUM_SCALAR > & addNoParentNode(NodeId node)
BNLearner< GUM_SCALAR > & setPCAlpha(double alpha)
BNLearner< GUM_SCALAR > & useEMWithDiffCriterion(const double epsilon, const double noise=default_EM_noise)
use The EM algorithm to learn parameters with the diff stopping criterion
BNLearner< GUM_SCALAR > & useScoreLog2Likelihood()
BayesNet< GUM_SCALAR > _prior_bn_
Definition BNLearner.h:542
BNLearner< GUM_SCALAR > & EMsetMaxIter(const Size max)
add a max iteration stopping criterion
BNLearner< GUM_SCALAR > & useEM(const double epsilon, const double noise=default_EM_noise)
use The EM algorithm to learn parameters
BNLearner< GUM_SCALAR > & useFCI()
BNLearner< GUM_SCALAR > & useDirichletPrior(std::string_view filename, double weight=1)
BNLearner< GUM_SCALAR > & useLocalSearchWithTabuList(Size tabu_size=100, Size nb_decrease=2)
BNLearner(const DatabaseTable &db)
default constructor
BNLearner< GUM_SCALAR > & EMsetMinEpsilonRate(const double rate)
sets the stopping criterion of EM as being the minimal log-likelihood's evolution rate
BNLearner< GUM_SCALAR > & allowArcTriangleDeletions(bool allow)
NodeProperty< Sequence< std::string > > _labelsFromBN_(std::string_view filename, const BayesNet< GUM_SCALAR > &src)
read the first line of a file to find column names
BNLearner< GUM_SCALAR > & useMDLCorrection()
BNLearner< GUM_SCALAR > & eraseNoParentNode(NodeId node)
BNLearner< GUM_SCALAR > & allowArcAdditions(bool allow)
BNLearner< GUM_SCALAR > & EMsetVerbosity(const bool v)
sets or unsets EM's verbosity
BNLearner< GUM_SCALAR > & eraseForbiddenArc(const Arc &arc)
BayesNet< GUM_SCALAR > learnParameters(const DAG &dag, bool takeIntoAccountScore=true)
learns a BN (its parameters) with the structure passed in argument
BNLearner< GUM_SCALAR > & setFCIAlpha(double alpha)
BNLearner< GUM_SCALAR > & useFCIG2Test()
void _checkDAGCompatibility_(const DAG &dag)
check that the database contains the nodes of the dag, else raise an exception
BNLearner< GUM_SCALAR > & useNMLCorrection()
BNLearner< GUM_SCALAR > & setSliceOrder(const NodeProperty< NodeId > &slice_order)
std::string toString() const
void createPrior_() override
create the prior used for learning
BNLearner< GUM_SCALAR > & EMsetMaxTime(const double timeout)
add a stopping criterion on timeout
void copyState(const BNLearner< GUM_SCALAR > &learner)
copy the states of the BNLearner
BNLearner< GUM_SCALAR > & setPCUnshieldedColliderSorted(bool sorted)
BNLearner< GUM_SCALAR > & EMenableMaxIter()
Enable stopping criterion on max iterations.
BNLearner< GUM_SCALAR > & useChi2Test()
BNLearner< GUM_SCALAR > & useScoreBDeu()
BNLearner< GUM_SCALAR > & setMaxIndegree(Size max_indegree)
The class representing a tabular database as used by learning tasks.
double epsilon() const override
Get the value of epsilon.
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
static constexpr double default_EM_noise
the default noise amount added to CPTs during EM's initialization (see method useEM())
Definition IBNLearner.h:135
The base class for estimating parameters of CPTs.
Complete concept for GUM_SCALAR template parameter.
Definition concepts.h:148
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.
include the inlined functions if necessary
Definition CSVParser.h:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.