pub struct CausalGraph { /* private fields */ }Expand description
A Directed Acyclic Graph (DAG) representing causal structure among variables.
Nodes are identified by string names; edges encode direct causal relationships
(parent → child). The graph enforces acyclicity lazily via CausalGraph::is_acyclic.
Implementations§
Source§impl CausalGraph
impl CausalGraph
Sourcepub fn new(nodes: Vec<String>) -> Self
pub fn new(nodes: Vec<String>) -> Self
Create a new causal graph with the given variable names.
Sourcepub fn node_index(&self, name: &str) -> Option<usize>
pub fn node_index(&self, name: &str) -> Option<usize>
Return the index of a node by name, or None if it does not exist.
Sourcepub fn add_edge(&mut self, parent: &str, child: &str) -> Result<(), CausalError>
pub fn add_edge(&mut self, parent: &str, child: &str) -> Result<(), CausalError>
Add a directed edge parent → child.
Returns CausalError::NodeNotFound if either node is absent.
Does not check for cycles — call CausalGraph::is_acyclic separately.
Sourcepub fn parents_of(&self, node: &str) -> Vec<String>
pub fn parents_of(&self, node: &str) -> Vec<String>
Return direct parents of node.
Sourcepub fn children_of(&self, node: &str) -> Vec<String>
pub fn children_of(&self, node: &str) -> Vec<String>
Return direct children of node.
Sourcepub fn ancestors_of(&self, node: &str) -> Vec<String>
pub fn ancestors_of(&self, node: &str) -> Vec<String>
Return all ancestors of node (transitive parents), excluding the node itself.
Sourcepub fn descendants_of(&self, node: &str) -> Vec<String>
pub fn descendants_of(&self, node: &str) -> Vec<String>
Return all descendants of node (transitive children), excluding the node itself.
Sourcepub fn is_acyclic(&self) -> bool
pub fn is_acyclic(&self) -> bool
Check whether the graph is acyclic using Kahn’s BFS topological sort algorithm.
Returns true if the graph is a valid DAG.
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Return the number of nodes.
Sourcepub fn edge_count(&self) -> usize
pub fn edge_count(&self) -> usize
Return the number of directed edges.
Sourcepub fn d_separated(&self, x: &str, y: &str, observed: &[&str]) -> bool
pub fn d_separated(&self, x: &str, y: &str, observed: &[&str]) -> bool
Test d-separation: is x d-separated from y given the observed set observed?
Uses the Bayes-Ball algorithm on the moral graph / active path traversal.
A path is active given observed if:
- At every non-collider on the path, the node is NOT in
observed. - At every collider, the collider OR one of its descendants IS in
observed.
Sourcepub fn has_directed_path(&self, src: &str, dst: &str) -> bool
pub fn has_directed_path(&self, src: &str, dst: &str) -> bool
Check whether there is a directed path from src to dst.
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<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more