Partial Directed Acyclic Graph (PDAG)

class pgmpy.base.PDAG(directed_ebunch: list[tuple[Hashable, Hashable]] = [], undirected_ebunch: list[tuple[Hashable, Hashable]] = [], latents: Iterable[Hashable] = [])[source]

Class for representing PDAGs (also known as CPDAG). PDAGs are the equivalence classes of DAGs and contain both directed and undirected edges.

Note: In this class, undirected edges are represented using two edges in both direction i.e. an undirected edge between X - Y is represented using X -> Y and X <- Y.

all_neighbors(node)[source]

Returns a set of all neighbors of a node in the PDAG. This includes both directed and undirected edges.

Parameters:

node (any hashable python object) – The node for which to get the neighboring nodes.

Returns:

set

Return type:

A set of neighboring nodes.

Examples

>>> from pgmpy.base import PDAG
>>> pdag = PDAG(
...     directed_ebunch=[("A", "C"), ("D", "C")],
...     undirected_ebunch=[("B", "A"), ("B", "D")],
... )
>>> pdag.all_neighbors("A")
{'B', 'C'}
apply_meeks_rules(apply_r4=False, inplace=False, debug=False)[source]

Applies the Meek’s rules to orient the undirected edges of a PDAG to return a CPDAG.

Parameters:
  • apply_r4 (boolean (default=False)) – If True, applies Rules 1 - 4 of Meek’s rules. If False, applies only Rules 1 - 3.

  • inplace (boolean (default=False)) – If True, the PDAG object is modified inplace, otherwise a new modified copy is returned.

  • debug (boolean (default=False)) – If True, prints the rules being applied to the PDAG.

Returns:

None or pgmpy.base.PDAG – If inplace=True, returns None and the object itself is modified. If inplace=False, returns a PDAG object.

Return type:

The modified PDAG object.

Examples

>>> from pgmpy.base import PDAG
>>> pdag = PDAG(
...     directed_ebunch=[("A", "B")], undirected_ebunch=[("B", "C"), ("C", "B")]
... )
>>> pdag.apply_meeks_rules()
>>> pdag.directed_edges
{('A', 'B'), ('B', 'C')}
copy()[source]

Returns a copy of the object instance.

Returns:

Copy of PDAG – Returns a copy of self.

Return type:

pgmpy.dag.PDAG

directed_children(node)[source]

Returns a set of children of node such that there is a directed edge from node to child.

directed_parents(node)[source]

Returns a set of parents of node such that there is a directed edge from the parent to node.

has_directed_edge(u, v)[source]

Returns True if there is a directed edge u -> v in the PDAG.

has_undirected_edge(u, v)[source]

Returns True if there is an undirected edge u - v in the PDAG.

is_adjacent(u, v)[source]

Returns True if there is an edge between u and v. This can be either of u - v, u -> v, or u <- v.

orient_undirected_edge(u, v, inplace=False)[source]

Orients an undirected edge u - v as u -> v.

Parameters:
  • u (Any hashable python objects) – The node names.

  • v (Any hashable python objects) – The node names.

  • inplace (boolean (default=False)) – If True, the PDAG object is modified inplace, otherwise a new modified copy is returned.

Returns:

None or pgmpy.base.PDAG – If inplace=True, returns None and the object itself is modified. If inplace=False, returns a PDAG object.

Return type:

The modified PDAG object.

to_dag() DAG[source]

Returns one possible DAG which is represented using the PDAG.

Returns:

pgmpy.base.DAG

Return type:

Returns an instance of DAG.

Examples

>>> pdag = PDAG(
...     directed_ebunch=[("A", "B"), ("C", "B")],
...     undirected_ebunch=[("C", "D"), ("D", "A")],
... )
>>> dag = pdag.to_dag()
>>> print(dag.edges())
OutEdgeView([('A', 'B'), ('C', 'B'), ('D', 'C'), ('A', 'D')])

References

[1] Dor, Dorit, and Michael Tarsi.
“A simple algorithm to construct a consistent extension of a partially oriented graph.”

Technicial Report R-185, Cognitive Systems Laboratory, UCLA (1992): 45.

to_graphviz() object[source]

Retuns a pygraphviz object for the DAG. pygraphviz is useful for visualizing the network structure.

Examples

>>> from pgmpy.utils import get_example_model
>>> model = get_example_model("alarm")
>>> model.to_graphviz()
<AGraph <Swig Object of type 'Agraph_t *' at 0x7fdea4cde040>>
undirected_neighbors(node)[source]

Returns a set of neighboring nodes such that all of them have an undirected edge with node.

Parameters:

node (any hashable python object) – The node for which to get the undirected neighboring nodes.

Returns:

set

Return type:

A set of neighboring nodes.

Examples

>>> from pgmpy.base import PDAG
>>> pdag = PDAG(
...     directed_ebunch=[("A", "C"), ("D", "C")],
...     undirected_ebunch=[("B", "A"), ("B", "D")],
... )
>>> pdag.undirected_neighbors("A")
{'B'}