ADMG#
- class pgmpy.base.ADMG(*args, backend=None, **kwargs)[source]#
Bases:
_GraphRolesMixin,MultiDiGraphA class representing an Acyclic Directed Mixed Graph (ADMG).
An ADMG is a directed graph that allows for both directed and bidirected edges. This class extends the networkx.MultiDiGraph and provides additional functionality for operations involving directed and bidirected edges.
- Parameters:
- directed_ebunchlist of tuple, optional
List of directed edges to initialize the graph, where each tuple is (u, v).
- bidirected_ebunchlist of tuple, optional
List of bidirected edges to initialize the graph, where each tuple is (u, v).
- latentsset of str, optional
Set of latent variables in the graph. These are not directly represented as nodes but are used to indicate the presence of bidirected edges.
- rolesdict, optional (default: None)
A dictionary mapping roles to node names. The keys are roles, and the values are role names (strings or iterables of str). If provided, this will automatically assign roles to the nodes in the graph. Passing a key-value pair via
rolesis equivalent to callingwith_role(role, variables)for each key-value pair in the dictionary.
Examples
>>> from pgmpy.base.ADMG import ADMG >>> admg = ADMG( ... directed_ebunch=[("X", "Y"), ("Z", "Y")], bidirected_ebunch=[("X", "Z")] ... ) >>> sorted(admg.nodes()) ['X', 'Y', 'Z'] >>> sorted(admg.edges()) [('X', 'Y'), ('X', 'Z'), ('Z', 'X'), ('Z', 'Y')] >>> admg.latents set()
- add_bidirected_edges(ebunch)[source]#
Add bidirected edges (u <-> v) to the ADMG.
- Parameters:
- ebunchlist of tuple
List of bidirected edges, where each tuple is (u, v).
Examples
>>> from pgmpy.base.ADMG import ADMG >>> admg = ADMG() >>> admg.add_bidirected_edges([("X", "Z")]) >>> sorted(admg.nodes()) ['X', 'Z'] >>> sorted(admg.edges()) [('X', 'Z'), ('Z', 'X')]
- add_directed_edges(ebunch)[source]#
Add directed edges (u -> v) to the ADMG.
- Parameters:
- ebunchlist of tuple
List of directed edges, where each tuple is (u, v).
Examples
>>> from pgmpy.base.ADMG import ADMG >>> admg = ADMG() >>> admg.add_directed_edges([("X", "Y"), ("Y", "Z")]) >>> sorted(admg.nodes()) ['X', 'Y', 'Z'] >>> sorted(admg.edges()) [('X', 'Y'), ('Y', 'Z')]
- add_edge(u, v, **kwargs)[source]#
Raise an error if trying to add a regular edge.
Examples
>>> from pgmpy.base.ADMG import ADMG >>> admg = ADMG() >>> admg.add_edge("X", "Y") Traceback (most recent call last): ... NotImplementedError: Use add_directed_edge or add_bidirected_edge to add edges.
- get_ancestors(nodes)[source]#
Get ancestors of given nodes via directed paths.
- Parameters:
- nodesstr or iterable of str
Node or list of nodes.
- Returns:
- set
Set of ancestor nodes including the input nodes.
Examples
>>> from pgmpy.base.ADMG import ADMG >>> admg = ADMG(directed_ebunch=[("X", "Y"), ("Y", "Z")]) >>> sorted(admg.get_ancestors("Z")) ['X', 'Y', 'Z'] >>> sorted(admg.get_ancestors("X")) ['X']
- get_ancestral_graph(nodes)[source]#
Return the ancestral graph induced by the input nodes.
- Parameters:
- nodesstr or iterable of str
Node or list of nodes to induce subgraph on.
- Returns:
- ADMG
Subgraph induced by ancestors of the given nodes.
- Raises:
- ValueError
If any input node is not in the graph.
Examples
>>> from pgmpy.base.ADMG import ADMG >>> admg = ADMG( ... directed_ebunch=[("X", "Y"), ("Y", "Z")], bidirected_ebunch=[("X", "Z")] ... ) >>> anc = admg.get_ancestral_graph(["Y", "Z"]) >>> sorted(anc.nodes()) ['Y', 'Z'] >>> anc2 = admg.get_ancestral_graph(["X", "Y", "Z"]) >>> sorted(anc2.nodes()) ['X', 'Y', 'Z']
- get_bidirected_parents(nodes)[source]#
Get bidirected parents (nodes connected via bidirected edge) of the given nodes.
- Parameters:
- nodesstr or iterable of str
Node or list of nodes to query.
- Returns:
- set
Set of bidirected parents.
Examples
>>> from pgmpy.base.ADMG import ADMG >>> admg = ADMG(directed_ebunch=[("X", "Y")], bidirected_ebunch=[("X", "Z")]) >>> sorted(admg.get_bidirected_parents("X")) ['Z'] >>> admg.get_bidirected_parents("Y") set()
- get_children(nodes)[source]#
Get children of given nodes (i.e., targets of outgoing directed edges).
- Parameters:
- nodesstr or iterable of str
Node or list of nodes.
- Returns:
- set
Set of children nodes.
Examples
>>> from pgmpy.base.ADMG import ADMG >>> admg = ADMG(directed_ebunch=[("X", "Y"), ("X", "Z")]) >>> sorted(admg.get_children("X")) ['Y', 'Z'] >>> admg.get_children("Y") set()
- get_descendants(nodes)[source]#
Get descendants of given nodes via directed paths.
- Parameters:
- nodesstr or iterable of str
Node or list of nodes.
- Returns:
- set
Set of descendant nodes including the input nodes.
Examples
>>> from pgmpy.base.ADMG import ADMG >>> admg = ADMG(directed_ebunch=[("X", "Y"), ("Y", "Z")]) >>> sorted(admg.get_descendants("X")) ['X', 'Y', 'Z'] >>> sorted(admg.get_descendants("Z")) ['Z']
- get_directed_parents(nodes)[source]#
Get directed parents of given nodes.
- Parameters:
- nodesstr or iterable of str
Node or list of nodes to query.
- Returns:
- set
Set of directed parents.
Examples
>>> from pgmpy.base.ADMG import ADMG >>> admg = ADMG(directed_ebunch=[("X", "Y"), ("Z", "Y")]) >>> sorted(admg.get_directed_parents("Y")) ['X', 'Z'] >>> admg.get_directed_parents("X") set()
- get_district(nodes)[source]#
Return district of a node: maximal set connected via bidirected edges.
- Parameters:
- nodesstr or iterable of str
Node or list of nodes.
- Returns:
- set
Nodes in the same bidirected-connected component.
Examples
>>> from pgmpy.base.ADMG import ADMG >>> admg = ADMG(directed_ebunch=[("X", "Y")], bidirected_ebunch=[("X", "Z")]) >>> sorted(admg.get_district("X")) ['X', 'Z'] >>> admg.get_district("Y") {'Y'}
- get_markov_blanket(nodes)[source]#
Compute the Markov blanket for the given node(s).
Includes: - Parents - Children - Spouses (nodes sharing a child) - Parents of nodes in the district
- Parameters:
- nodesstr or iterable of str
Node or list of nodes.
- Returns:
- set
Set of nodes in the Markov blanket.
Examples
>>> from pgmpy.base.ADMG import ADMG >>> admg = ADMG( ... directed_ebunch=[("X", "Y"), ("Z", "Y")], bidirected_ebunch=[("X", "Z")] ... ) >>> sorted(admg.get_markov_blanket("Y")) ['X', 'Z']
- get_spouses(nodes)[source]#
Get spouses of given nodes (i.e., nodes connected via bidirected edges).
- Parameters:
- nodesstr or iterable of str
Node or list of nodes.
- Returns:
- set
Set of spouses.
Examples
>>> from pgmpy.base.ADMG import ADMG >>> admg = ADMG(directed_ebunch=[("X", "Y")], bidirected_ebunch=[("X", "Z")]) >>> sorted(admg.get_spouses("X")) ['Z'] >>> admg.get_spouses("Y") set()
- is_mconnected(nodes_u, nodes_v, conditional_set=None)[source]#
Test m-connectedness between two node sets given a conditioning set.
- Parameters:
- nodes_ustr or iterable of str
First set of nodes.
- nodes_vstr or iterable of str
Second set of nodes.
- conditional_setset of str, optional
Conditioning set.
- Returns:
- bool
True if m-connected; False if m-separated.
Examples
>>> from pgmpy.base.ADMG import ADMG >>> admg = ADMG(directed_ebunch=[("X", "Y"), ("Z", "Y")]) >>> admg.is_mconnected("X", "Z", conditional_set={"Y"}) True >>> admg.is_mconnected("X", "Z") False
- is_mseparated(nodes_u, nodes_v, conditional_set=None)[source]#
Test m-separation between two sets of nodes given a conditioning set.
- Parameters:
- nodes_ustr or iterable of str
First set of nodes.
- nodes_vstr or iterable of str
Second set of nodes.
- conditional_setset of str, optional
Conditioning set (default is empty set).
- Returns:
- bool
True if nodes_u and nodes_v are m-separated; False otherwise.
Examples
>>> from pgmpy.base.ADMG import ADMG >>> admg = ADMG(directed_ebunch=[("X", "Y"), ("Z", "Y")]) >>> admg.is_mseparated("X", "Z") True >>> admg.is_mseparated("X", "Z", conditional_set={"Y"}) False
- mconnected_nodes(nodes_u, nodes_v=None, conditional_set=None)[source]#
Find all nodes m-connected to nodes in nodes_u given conditional_set.
- Parameters:
- nodes_ustr or iterable of str
Set of source nodes.
- nodes_vstr or iterable of str, optional
If provided, filters the result to this set.
- conditional_setset of str, optional
Conditioning set (default is empty set).
- Returns:
- set
Nodes m-connected to nodes_u (or their intersection with nodes_v if provided).
Examples
>>> from pgmpy.base.ADMG import ADMG >>> admg = ADMG(directed_ebunch=[("X", "Y"), ("Y", "Z")]) >>> sorted(admg.mconnected_nodes("X", nodes_v=["Y", "Z"])) ['Y', 'Z'] >>> sorted(admg.mconnected_nodes("X", nodes_v=["Z"])) ['Z']
- to_dag()[source]#
Project ADMG into a DAG by introducing latent variables for bidirected edges.
- Returns:
- pgmpy.base.DAG.DAG
DAG with latent variables replacing bidirected edges.
Examples
>>> from pgmpy.base.ADMG import ADMG >>> admg = ADMG(directed_ebunch=[("X", "Y")], bidirected_ebunch=[("X", "Z")]) >>> dag = admg.to_dag() >>> "L_X_Z" in dag.nodes() True >>> ("X", "Y") in dag.edges() True