aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
Miic.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
41
59#ifndef GUM_LEARNING_MIIC_H
60#define GUM_LEARNING_MIIC_H
61
62#include <string>
63#include <vector>
64
65#include <agrum/config.h>
66
71
73
74#define GUM_SL_EMIT(x, y, action, explain) \
75 { \
76 std::ostringstream action_stream; \
77 action_stream << action; \
78 std::ostringstream explain_stream; \
79 explain_stream << explain; \
80 GUM_EMIT4(onStructuralModification, x, y, action_stream.str(), explain_stream.str()); \
81 }
82
83namespace gum {
84
85 namespace learning {
86 using CondThreePoints = std::tuple< NodeId, NodeId, NodeId, std::vector< NodeId > >;
87 using CondRanking = std::pair< CondThreePoints*, double >;
88
89 using ThreePoints = std::tuple< NodeId, NodeId, NodeId >;
90 using Ranking = std::pair< ThreePoints*, double >;
91
92 using ProbabilisticRanking = std::tuple< ThreePoints*, double, double, double >;
93
95 public:
96 bool operator()(const CondRanking& e1, const CondRanking& e2) const;
97 };
98
100 public:
101 bool operator()(const Ranking& e1, const Ranking& e2) const;
102 };
103
105 public:
106 bool operator()(const ProbabilisticRanking& e1, const ProbabilisticRanking& e2) const;
107 };
108
126 class Miic: public ApproximationScheme {
127 public:
128 // ##########################################################################
130 // ##########################################################################
132
134 Miic();
135
137 explicit Miic(int maxLog);
138
140 Miic(const Miic& from);
141
143 Miic(Miic&& from);
144
146 ~Miic() override;
147
149
151 Miic& operator=(const Miic& from);
152
154 Miic& operator=(Miic&& from);
155
156 // ##########################################################################
158 // ##########################################################################
160
162
167
169
174 MixedGraph graph);
175
177
181 PDAG learnPDAG(CorrectedMutualInformation& mutualInformation, MixedGraph graph);
182
185
190
192
202 template < typename GUM_SCALAR = double,
203 typename GRAPH_CHANGES_SELECTOR,
204 typename PARAM_ESTIMATOR >
205 BayesNet< GUM_SCALAR > learnBN(GRAPH_CHANGES_SELECTOR& selector,
206 PARAM_ESTIMATOR& estimator,
207 DAG initial_dag = DAG());
208
210 const std::vector< Arc > latentVariables() const;
211
213 void addConstraints(HashTable< std::pair< NodeId, NodeId >, char > constraints);
215
218 void setForbiddenGraph(gum::DiGraph forbidGraph);
219 void setMandatoryGraph(gum::DAG mandaGraph);
221
223 // void testNodeProperty(const NodeProperty< NodeId >& order);
224
225 protected:
226 // ##########################################################################
228 // ##########################################################################
230
232
242 void initiation_(CorrectedMutualInformation& mutualInformation,
243 MixedGraph& graph,
244 HashTable< std::pair< NodeId, NodeId >, std::vector< NodeId > >& sepSet,
246
248
259 void iteration_(CorrectedMutualInformation& mutualInformation,
260 MixedGraph& graph,
261 HashTable< std::pair< NodeId, NodeId >, std::vector< NodeId > >& sepSet,
263
266
272 void orientationMiic_(
273 CorrectedMutualInformation& mutualInformation,
274 MixedGraph& graph,
275 const HashTable< std::pair< NodeId, NodeId >, std::vector< NodeId > >& sepSet);
277
279
288 NodeId y,
289 const std::vector< NodeId >& ui,
290 const MixedGraph& graph,
291 CorrectedMutualInformation& mutualInformation,
293
296
300 std::vector< Ranking > unshieldedTriples_(
301 const MixedGraph& graph,
302 CorrectedMutualInformation& mutualInformation,
303 const HashTable< std::pair< NodeId, NodeId >, std::vector< NodeId > >& sepSet);
304
307
312 std::vector< ProbabilisticRanking > unshieldedTriplesMiic_(
313 const MixedGraph& graph,
314 CorrectedMutualInformation& mutualInformation,
315 const HashTable< std::pair< NodeId, NodeId >, std::vector< NodeId > >& sepSet,
316 HashTable< std::pair< NodeId, NodeId >, char >& marks);
317
319
322 std::vector< ProbabilisticRanking >
323 updateProbaTriples_(const MixedGraph& graph,
324 std::vector< ProbabilisticRanking > probaTriples);
325
327
330
333
335 bool isForbiddenArc_(NodeId x, NodeId y) const;
336 bool isForbiddenEdge_(NodeId x, NodeId y);
337 bool isMaxIndegree_(MixedGraph graph, NodeId x);
338 bool isArcValid_(MixedGraph graph, NodeId x, NodeId y);
339
340 private:
342 int _maxLog_ = 100;
344 const std::vector< NodeId > _emptySet_;
346 std::vector< Arc > _latentCouples_;
347
350
353
356
359
362
365
373 static bool _existsNonTrivialDirectedPath_(const MixedGraph& graph, NodeId n1, NodeId n2);
374
380 static bool _existsDirectedPath_(const MixedGraph& graph, NodeId n1, NodeId n2);
381
383 HashTable< std::pair< NodeId, NodeId >, char >& marks,
384 NodeId x,
385 NodeId y,
386 NodeId z,
387 double p1,
388 double p2);
389
391 HashTable< std::pair< NodeId, NodeId >, char >& marks,
392 NodeId x,
393 NodeId y,
394 NodeId z,
395 double p1,
396 double p2);
397
399
400 public:
401 Signaler4< gum::NodeId, gum::NodeId, std::string, std::string > onStructuralModification;
402 };
403
404 } /* namespace learning */
405
406} /* namespace gum */
407
408#endif /* GUM_LEARNING_CONSTRAINT_MIIC_H */
Class for Meek Rules.
Base classes for partially directed acyclic graphs.
This file contains general scheme for iteratively convergent algorithms.
ApproximationScheme(bool verbosity=false)
Base class for dag.
Definition DAG.h:121
Base class for all oriented graphs.
Definition diGraph.h:130
The class for generic Hash Tables.
Definition hashTable.h:637
Heap data structure.
Definition heap.h:141
Base class for mixed graphs.
Definition mixedGraph.h:146
Base class for partially directed acyclic graphs.
Definition PDAG.h:130
The class computing n times the corrected mutual information, as used in the MIIC algorithm.
bool operator()(const Ranking &e1, const Ranking &e2) const
Definition Miic.cpp:97
bool operator()(const CondRanking &e1, const CondRanking &e2) const
Definition Miic.cpp:93
bool operator()(const ProbabilisticRanking &e1, const ProbabilisticRanking &e2) const
Definition Miic.cpp:101
static bool _existsDirectedPath_(const MixedGraph &graph, NodeId n1, NodeId n2)
checks for directed paths in a graph, consider double arcs like edges
Definition Miic.cpp:930
std::vector< ProbabilisticRanking > updateProbaTriples_(const MixedGraph &graph, std::vector< ProbabilisticRanking > probaTriples)
Gets the orientation probabilities like MIIC for the orientation phase.
Definition Miic.cpp:872
gum::DAG _mandatoryGraph_
Graph that contains the mandatories arcs.
Definition Miic.h:361
bool isMaxIndegree_(MixedGraph graph, NodeId x)
Definition Miic.cpp:980
void _orientingVstructureMiic_(MixedGraph &graph, HashTable< std::pair< NodeId, NodeId >, char > &marks, NodeId x, NodeId y, NodeId z, double p1, double p2)
Definition Miic.cpp:420
void orientationMiic_(CorrectedMutualInformation &mutualInformation, MixedGraph &graph, const HashTable< std::pair< NodeId, NodeId >, std::vector< NodeId > > &sepSet)
Orientation phase from the MIIC algorithm, returns a mixed graph that may contain circles.
Definition Miic.cpp:309
gum::DiGraph _forbiddenGraph_
Graph that contains the forbidden arcs.
Definition Miic.h:364
void setMandatoryGraph(gum::DAG mandaGraph)
Definition Miic.cpp:972
Miic & operator=(const Miic &from)
copy operator
Definition Miic.cpp:82
MixedGraph learnMixedStructure(CorrectedMutualInformation &mutualInformation, MixedGraph graph)
learns the structure of a MixedGraph (Meek rules not used here).
Definition Miic.cpp:147
void setMaxIndegree(gum::Size n)
Definition Miic.cpp:978
gum::MeekRules meekRules_
Object that can propagates orientations to non-oriented edges.
Definition Miic.h:332
gum::Size _maxIndegree_
maximum number of parents
Definition Miic.h:349
const std::vector< NodeId > _emptySet_
an empty conditioning set
Definition Miic.h:344
PDAG learnPDAG(CorrectedMutualInformation &mutualInformation, MixedGraph graph)
learns the structure of an Essential Graph
Definition Miic.cpp:178
~Miic() override
destructor
Definition Miic.cpp:79
std::vector< Arc > _latentCouples_
an empty vector of arcs
Definition Miic.h:346
void addConstraints(HashTable< std::pair< NodeId, NodeId >, char > constraints)
Set a ensemble of constraints for the orientation phase.
Definition Miic.cpp:913
void _propagatingOrientationMiic_(MixedGraph &graph, HashTable< std::pair< NodeId, NodeId >, char > &marks, NodeId x, NodeId y, NodeId z, double p1, double p2)
Definition Miic.cpp:548
void iteration_(CorrectedMutualInformation &mutualInformation, MixedGraph &graph, HashTable< std::pair< NodeId, NodeId >, std::vector< NodeId > > &sepSet, Heap< CondRanking, GreaterPairOn2nd > &rank)
Iteration phase.
Definition Miic.cpp:253
bool isArcValid_(MixedGraph graph, NodeId x, NodeId y)
Definition Miic.cpp:988
void findBestContributor_(NodeId x, NodeId y, const std::vector< NodeId > &ui, const MixedGraph &graph, CorrectedMutualInformation &mutualInformation, Heap< CondRanking, GreaterPairOn2nd > &rank)
finds the best contributor node for a pair given a conditioning set
Definition Miic.cpp:718
void setForbiddenGraph(gum::DiGraph forbidGraph)
Set ForbiddenGraph (resp. MadatoryGraph) which contains the forbidden (resp. mandatory) arcs.
Definition Miic.cpp:974
std::vector< ProbabilisticRanking > unshieldedTriplesMiic_(const MixedGraph &graph, CorrectedMutualInformation &mutualInformation, const HashTable< std::pair< NodeId, NodeId >, std::vector< NodeId > > &sepSet, HashTable< std::pair< NodeId, NodeId >, char > &marks)
gets the list of unshielded triples in the graph in decreasing value of |I'(x, y, z|{ui}...
Definition Miic.cpp:831
bool isForbiddenArc_(NodeId x, NodeId y) const
Check constraints.
Definition Miic.cpp:984
Miic()
default constructor
Definition Miic.cpp:63
BayesNet< GUM_SCALAR > learnBN(GRAPH_CHANGES_SELECTOR &selector, PARAM_ESTIMATOR &estimator, DAG initial_dag=DAG())
learns the structure and the parameters of a BN
Definition Miic.cpp:190
std::vector< Ranking > unshieldedTriples_(const MixedGraph &graph, CorrectedMutualInformation &mutualInformation, const HashTable< std::pair< NodeId, NodeId >, std::vector< NodeId > > &sepSet)
gets the list of unshielded triples in the graph in decreasing value of |I'(x, y, z|{ui}...
Definition Miic.cpp:794
HashTable< std::pair< NodeId, NodeId >, char > _initialMarks_
Initial marks for the orientation phase, used to convey constraints.
Definition Miic.h:358
bool isForbiddenEdge_(NodeId x, NodeId y)
Definition Miic.cpp:992
ArcProperty< double > _arcProbas_
Storing the propabilities for each arc set in the graph.
Definition Miic.h:355
Signaler4< gum::NodeId, gum::NodeId, std::string, std::string > onStructuralModification
Definition Miic.h:401
void initiation_(CorrectedMutualInformation &mutualInformation, MixedGraph &graph, HashTable< std::pair< NodeId, NodeId >, std::vector< NodeId > > &sepSet, Heap< CondRanking, GreaterPairOn2nd > &rank)
Initiation phase.
Definition Miic.cpp:204
static bool _existsNonTrivialDirectedPath_(const MixedGraph &graph, NodeId n1, NodeId n2)
checks for directed paths in a graph, considering double arcs like edges, not considering arc as a di...
Definition Miic.cpp:917
Size _size_
size of the database
Definition Miic.h:352
bool _isNotLatentCouple_(NodeId x, NodeId y)
Definition Miic.cpp:964
void orientDoubleHeadedArcs_(MixedGraph &mg)
Orient double headed arcs to avoid cycles.
Definition Miic.cpp:653
const std::vector< Arc > latentVariables() const
get the list of arcs hiding latent variables
Definition Miic.cpp:911
int _maxLog_
Fixes the maximum log that we accept in exponential computations.
Definition Miic.h:342
MixedGraph learnSkeleton(CorrectedMutualInformation &mutualInformation, MixedGraph graph)
learns the skeleton of a MixedGraph (no orientation).
Definition Miic.cpp:124
DAG learnStructure(CorrectedMutualInformation &I, MixedGraph graph)
learns the structure of a Bayesian network, i.e. a DAG, by first learning an Essential graph and then...
Definition Miic.cpp:183
The class computing n times the corrected mutual information (where n is the size (or the weight) of ...
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
HashTable< Arc, VAL > ArcProperty
Property on graph elements.
Heaps definition.
std::pair< ThreePoints *, double > Ranking
Definition Miic.h:90
std::pair< CondThreePoints *, double > CondRanking
Definition Miic.h:87
std::tuple< NodeId, NodeId, NodeId, std::vector< NodeId > > CondThreePoints
Definition Miic.h:86
std::tuple< NodeId, NodeId, NodeId > ThreePoints
Definition Miic.h:89
std::tuple< ThreePoints *, double, double, double > ProbabilisticRanking
Definition Miic.h:92
gum is the global namespace for all aGrUM entities
Definition agrum.h:46