PDAG#

class causalbase.graph.PDAG(num_nodes)[source]#

A partially directed acyclic graph over integer nodes 0..num_nodes-1.

A PDAG carries two kinds of edges: directed edges u -> v and undirected edges {u, v}. At most one edge (of either kind, in either orientation) is stored between any pair of nodes.

Parameters:
num_nodesint

Number of nodes in the graph. Nodes are addressed by the integer indices 0, 1, ..., num_nodes - 1.

Attributes:
num_nodesint

Number of nodes in the graph.

Notes

Unlike DAG, add_directed does not check for cycles. A directed cycle surfaces only when to_dag() finds no consistent extension.

parents(v)[source]#

Return the parents of node v.

children(v)[source]#

Return the children of node v.

undirected_neighbors(v)[source]#

Return the undirected neighbours of node v.

adjacent(v)[source]#

Return the nodes adjacent to v (parents, children, and undirected).

has_directed(u, v)[source]#

Return whether the directed edge u -> v is present.

has_undirected(u, v)[source]#

Return whether the undirected edge {u, v} is present.

add_directed(u, v)[source]#

Add the directed edge u -> v.

Adding an edge that already exists (same orientation) is a no-op. No cycle checking is performed; consistency is enforced only by to_dag().

Parameters:
uint

Source node index.

vint

Target node index.

Raises:
ValueError

If u == v (self-loop), if the opposite directed edge v -> u already exists, or if the undirected edge {u, v} already exists – at most one edge is allowed between any pair of nodes.

add_undirected(u, v)[source]#

Add the undirected edge {u, v}.

Adding an edge that already exists is a no-op.

Parameters:
uint

One endpoint index.

vint

Other endpoint index.

Raises:
ValueError

If u == v (self-loop), or if a directed edge already exists between u and v (in either orientation) – at most one edge is allowed between any pair of nodes.

orient(u, v)[source]#

Orient the undirected edge {u, v} as the directed edge u -> v.

Parameters:
uint

Node the oriented edge points from.

vint

Node the oriented edge points to.

Raises:
KeyError

If {u, v} is not an undirected edge of the graph.

remove_edge(u, v)[source]#

Remove the edge between u and v, of either kind and orientation.

Parameters:
uint

One endpoint index.

vint

Other endpoint index.

Raises:
KeyError

If there is no edge (directed either way, or undirected) between u and v.

directed_edges()[source]#

Return all directed edges, sorted.

undirected_edges()[source]#

Return all undirected edges as (u, v) pairs with u < v, sorted.

adjacency()[source]#

Return the boolean adjacency matrix of the graph.

An undirected edge {u, v} sets both A[u, v] and A[v, u]; a directed edge u -> v sets only A[u, v].

Returns:
np.ndarray

A copy of the cached boolean adjacency matrix.

copy()[source]#

Return an independent deep copy of this graph.

Returns:
PDAG

A new PDAG with the same nodes and edges, sharing no mutable state with the original.

classmethod from_dag(dag)[source]#

Build a PDAG from a DAG, keeping every edge directed.

classmethod from_adjacency(adj)[source]#

Build a PDAG from a square matrix: A[u, v] alone is u -> v, both is u - v.

to_dag()[source]#

Return a DAG in the equivalence class this PDAG describes (Dor & Tarsi, 1992).

Returns:
DAG

Keeps every directed edge, orients every undirected one, and adds no v-structure.

Raises:
ValueError

If no such DAG exists.