pub struct CausalGraph {
pub n_nodes: usize,
pub parents: Vec<Vec<usize>>,
pub children: Vec<Vec<usize>>,
pub names: Vec<String>,
}Expand description
A directed acyclic graph (DAG) representing causal relationships between variables.
Nodes are identified by usize indices. Edges represent direct causal effects
from parent to child. The graph must be acyclic for causal semantics to be valid.
Fields§
§n_nodes: usizeNumber of nodes in the graph.
parents: Vec<Vec<usize>>Adjacency list: parents[v] gives all parent nodes of v.
children: Vec<Vec<usize>>Adjacency list: children[v] gives all children of v.
names: Vec<String>Optional variable names.
Implementations§
Source§impl CausalGraph
impl CausalGraph
Sourcepub fn add_edge(&mut self, from: usize, to: usize)
pub fn add_edge(&mut self, from: usize, to: usize)
Add a directed edge from from (parent/cause) to to (child/effect).
§Panics
Panics if adding this edge would create a cycle.
Sourcepub fn creates_cycle(&self, from: usize, to: usize) -> bool
pub fn creates_cycle(&self, from: usize, to: usize) -> bool
Check whether adding edge from→to would create a cycle.
Sourcepub fn topological_sort(&self) -> Option<Vec<usize>>
pub fn topological_sort(&self) -> Option<Vec<usize>>
Return nodes in topological order (Kahn’s algorithm).
Returns None if the graph has a cycle (should not happen if edges are
added via add_edge).
Sourcepub fn ancestors(&self, v: usize) -> HashSet<usize>
pub fn ancestors(&self, v: usize) -> HashSet<usize>
Return all ancestors of node v (nodes from which v is reachable).
Sourcepub fn descendants(&self, v: usize) -> HashSet<usize>
pub fn descendants(&self, v: usize) -> HashSet<usize>
Return all descendants of node v.
Sourcepub fn d_separated(&self, x: &[usize], y: &[usize], z: &[usize]) -> bool
pub fn d_separated(&self, x: &[usize], y: &[usize], z: &[usize]) -> bool
Test d-separation: are node sets x and y d-separated by conditioning set z?
Uses the Bayes Ball algorithm. Returns true if x ⊥ y | z.
Sourcepub fn markov_blanket(&self, v: usize) -> HashSet<usize>
pub fn markov_blanket(&self, v: usize) -> HashSet<usize>
Return the Markov blanket of node v: parents, children, and co-parents.
Sourcepub fn is_acyclic(&self) -> bool
pub fn is_acyclic(&self) -> bool
Check if the graph is acyclic.
Trait Implementations§
Source§impl Clone for CausalGraph
impl Clone for CausalGraph
Source§fn clone(&self) -> CausalGraph
fn clone(&self) -> CausalGraph
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for CausalGraph
impl RefUnwindSafe for CausalGraph
impl Send for CausalGraph
impl Sync for CausalGraph
impl Unpin for CausalGraph
impl UnsafeUnpin for CausalGraph
impl UnwindSafe for CausalGraph
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.