def test_estimate_from_independencies(self):
        ind = Independencies(['B', 'C'], ['A', ['B', 'C'], 'D'])
        ind = ind.closure()
        model = ConstraintBasedEstimator.estimate_from_independencies("ABCD", ind)

        self.assertSetEqual(set(model.edges()),
                            set([('B', 'D'), ('A', 'D'), ('C', 'D')]))

        model1 = BayesianModel([('A', 'C'), ('B', 'C'), ('B', 'D'), ('C', 'E')])
        model2 = ConstraintBasedEstimator.estimate_from_independencies(
                            model1.nodes(),
                            model1.get_independencies())

        self.assertTrue(set(model2.edges()) == set(model1.edges()) or
                        set(model2.edges()) == set([('B', 'C'), ('A', 'C'), ('C', 'E'), ('D', 'B')]))
Exemple #2
0
    def test_estimate_from_independencies(self):
        ind = Independencies(['B', 'C'], ['A', ['B', 'C'], 'D'])
        ind = ind.closure()
        model = ConstraintBasedEstimator.estimate_from_independencies(
            "ABCD", ind)

        self.assertSetEqual(set(model.edges()),
                            set([('B', 'D'), ('A', 'D'), ('C', 'D')]))

        model1 = BayesianModel([('A', 'C'), ('B', 'C'), ('B', 'D'),
                                ('C', 'E')])
        model2 = ConstraintBasedEstimator.estimate_from_independencies(
            model1.nodes(), model1.get_independencies())

        self.assertTrue(
            set(model2.edges()) == set(model1.edges())
            or set(model2.edges()) == set([('B', 'C'), ('A', 'C'), ('C', 'E'),
                                           ('D', 'B')]))
    def test_estimate_from_independencies(self):
        ind = Independencies(["B", "C"], ["A", ["B", "C"], "D"])
        ind = ind.closure()
        model = ConstraintBasedEstimator.estimate_from_independencies(
            "ABCD", ind)

        self.assertSetEqual(set(model.edges()),
                            set([("B", "D"), ("A", "D"), ("C", "D")]))

        model1 = BayesianModel([("A", "C"), ("B", "C"), ("B", "D"),
                                ("C", "E")])
        model2 = ConstraintBasedEstimator.estimate_from_independencies(
            model1.nodes(), model1.get_independencies())

        self.assertTrue(
            set(model2.edges()) == set(model1.edges())
            or set(model2.edges()) == set([("B", "C"), ("A", "C"), ("C", "E"),
                                           ("D", "B")]))
estModel.edges()
# %% codecell
drawGraph(estModel)

# %% markdown [markdown]
# The `estimate_from_independencies()` method can be used to create a `BayesianModel` from a provided *set of independencies*.
#
# Estimates a DAG from an `Independencies()`-object or a decision function for conditional independencies. This requires that the set of independencies admits a faithful representation (e.g. is a set of d-separation for some BN or is closed under the semi-graphoid axioms).
#
# * NOTE: **Meaning of Faithful**: PC PDAG construction is only guaranteed to work under the assumption that the identified set of independencies is **faithful**, i.e. there exists a DAG that exactly corresponds to it. Spurious dependencies in the data set can cause the reported independencies to violate faithfulness. It can happen that the estimated PDAG does not have any faithful completions (i.e. edge orientations that do not introduce new v-structures). In that case a warning is issued.
# %% codecell
ind: Independencies = Independencies(['B', 'C'], ['A', ['B', 'C'], 'D'])

indClosure: Independencies = ind.closure()  #required for faithfulness

indModel = ConstraintBasedEstimator.estimate_from_independencies(
    nodes="ABCD", independencies=indClosure)

indModel.edges()

# %% codecell
drawGraph(indModel)

# %% markdown [markdown]
# ### Hybrid Structure Learning
# The MMHC algorithm combines constraint-based and score-based structure learning methods. It has two parts:
#
# 1. Learn an undirected graph skeleton using the constraint-based construction procedure MMPC
# 2. Orient edges using score-based optimization (BDeu score + modified hill-climbing)
#
# Can perform these two steps somewhat separately:
# %% codecell