Independencies#

class pgmpy.independencies.Independencies(*assertions)[source]#

Bases: object

Base class for independencies. independencies class represents a set of Conditional Independence assertions (eg: “X is independent of Y given Z” where X, Y and Z are random variables) or Independence assertions (eg: “X is independent of Y” where X and Y are random variables). Initialize the independencies Class with Conditional Independence assertions or Independence assertions.

Parameters:
assertions: Lists or tuples

Each assertion is a list or tuple of the form: [event1, event2 and event3] eg: assertion [‘X’, ‘Y’, ‘Z’] would be X is independent of Y given Z.

Examples

Creating an independencies object with one independence assertion: Random Variable X is independent of Y

>>> from pgmpy.independencies import Independencies
>>>
>>> independencies = Independencies(["X", "Y"])

Creating an independencies object with three conditional independence assertions: First assertion is Random Variable X is independent of Y given Z.

>>> independencies = Independencies(
...     ["X", "Y", "Z"], ["a", ["b", "c"], "d"], ["l", ["m", "n"], "o"]
... )
add_assertions(*assertions)[source]#

Adds assertions to independencies.

Parameters:
assertions: Lists or tuples

Each assertion is a list or tuple of variable, independent_of and given.

Examples

>>> from pgmpy.independencies import Independencies
>>> independencies = Independencies()
>>> independencies.add_assertions(["X", "Y", "Z"])
>>> independencies.add_assertions(["a", ["b", "c"], "d"])
closure()[source]#

Returns a new Independencies()-object that additionally contains those IndependenceAssertions that are implied by the current independencies (using with the semi-graphoid axioms; see (Pearl, 1989, Conditional Independence and its representations)).

Might be very slow if more than six variables are involved.

Examples

>>> from pgmpy.independencies import Independencies
>>> ind1 = Independencies(("A", ["B", "C"], "D"))
>>> result1 = ind1.closure()
>>> len(result1.get_assertions())
5
>>> all("A" in str(a) for a in result1.get_assertions())
True
>>> ind2 = Independencies(("W", ["X", "Y", "Z"]))
>>> result2 = ind2.closure()
>>> len(result2.get_assertions()) >= 9
True
contains(assertion)[source]#

Returns True if assertion is contained in this Independencies-object, otherwise False.

Parameters:
assertion: IndependenceAssertion()-object

Examples

>>> from pgmpy.independencies import Independencies, IndependenceAssertion
>>> ind = Independencies(["A", "B", ["C", "D"]])
>>> IndependenceAssertion("A", "B", ["C", "D"]) in ind
True
>>> # does not depend on variable order:
>>> IndependenceAssertion("B", "A", ["D", "C"]) in ind
True
>>> # but does not check entailment:
>>> IndependenceAssertion("X", "Y", "Z") in Independencies(["X", "Y"])
False
entails(entailed_independencies)[source]#

Returns True if the entailed_independencies are implied by this Independencies-object, otherwise False. Entailment is checked using the semi-graphoid axioms.

Might be very slow if more than six variables are involved.

Parameters:
entailed_independencies: Independencies()-object

Examples

>>> from pgmpy.independencies import Independencies
>>> ind1 = Independencies([["A", "B"], ["C", "D"], "E"])
>>> ind2 = Independencies(["A", "C", "E"])
>>> ind1.entails(ind2)
True
>>> ind2.entails(ind1)
False
get_all_variables()[source]#

Returns a set of all the variables in all the independence assertions.

get_assertions()[source]#

Returns the independencies object which is a set of IndependenceAssertion objects.

Examples

>>> from pgmpy.independencies import Independencies
>>> independencies = Independencies(["X", "Y", "Z"])
>>> independencies.get_assertions()
[(X ⟂ Y | Z)]
get_factorized_product(random_variables=None, latex=False)[source]#
is_equivalent(other)[source]#

Returns True if the two Independencies-objects are equivalent, otherwise False. (i.e. any Bayesian Network that satisfies the one set of conditional independencies also satisfies the other).

Might be very slow if more than six variables are involved.

Parameters:
other: Independencies()-object

Examples

>>> from pgmpy.independencies import Independencies
>>> ind1 = Independencies(["X", ["Y", "W"], "Z"])
>>> ind2 = Independencies(["X", "Y", "Z"], ["X", "W", "Z"])
>>> ind3 = Independencies(
...     ["X", "Y", "Z"], ["X", "W", "Z"], ["X", "Y", ["W", "Z"]]
... )
>>> ind1.is_equivalent(ind2)
False
>>> ind1.is_equivalent(ind3)
True
latex_string() list[str][source]#

Returns a list of string. Each string represents the IndependenceAssertion in latex.

reduce(inplace=False)[source]#

Return list of Independence Assertions without any duplicate or redundant Independence assertions.

Might be very slow due to bidirectional entailment being checked.

Assumption:

If an assertion A entails assertion B and assertion B doesn’t entails assertion A then assertion A is consider more informative and assertion B is removed.

Parameters:
inplace: bool (default: False)

If True, the Independencies object will permanently removes duplicate or redundant Independence Assertions.