1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//! Causal graph (DAG) structure and graph-theoretic queries.
//!
//! Defines [`CausalGraph`] plus its impl block: d-separation, ancestors,
//! descendants, and backdoor-path reachability primitives.
use std::collections::{HashMap, HashSet, VecDeque};
use super::error::CausalError;
// ---------------------------------------------------------------------------
// CausalGraph
// ---------------------------------------------------------------------------
/// 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`].
#[derive(Debug, Clone)]
pub struct CausalGraph {
pub(super) nodes: Vec<String>,
/// Directed edges stored as (parent_idx, child_idx) index pairs.
pub(super) edges: Vec<(usize, usize)>,
}
impl CausalGraph {
/// Create a new causal graph with the given variable names.
pub fn new(nodes: Vec<String>) -> Self {
Self {
nodes,
edges: Vec::new(),
}
}
/// Return the index of a node by name, or `None` if it does not exist.
pub fn node_index(&self, name: &str) -> Option<usize> {
self.nodes.iter().position(|n| n == name)
}
/// Add a directed edge `parent → child`.
///
/// Returns [`CausalError::NodeNotFound`] if either node is absent.
/// Does not check for cycles — call [`CausalGraph::is_acyclic`] separately.
pub fn add_edge(&mut self, parent: &str, child: &str) -> Result<(), CausalError> {
let p = self
.node_index(parent)
.ok_or_else(|| CausalError::NodeNotFound(parent.to_string()))?;
let c = self
.node_index(child)
.ok_or_else(|| CausalError::NodeNotFound(child.to_string()))?;
self.edges.push((p, c));
Ok(())
}
/// Return direct parents of `node`.
pub fn parents_of(&self, node: &str) -> Vec<String> {
match self.node_index(node) {
None => vec![],
Some(idx) => self
.edges
.iter()
.filter(|&&(_, c)| c == idx)
.map(|&(p, _)| self.nodes[p].clone())
.collect(),
}
}
/// Return direct children of `node`.
pub fn children_of(&self, node: &str) -> Vec<String> {
match self.node_index(node) {
None => vec![],
Some(idx) => self
.edges
.iter()
.filter(|&&(p, _)| p == idx)
.map(|&(_, c)| self.nodes[c].clone())
.collect(),
}
}
/// Return all ancestors of `node` (transitive parents), excluding the node itself.
pub fn ancestors_of(&self, node: &str) -> Vec<String> {
let mut visited = HashSet::new();
let mut queue = VecDeque::new();
if let Some(start) = self.node_index(node) {
queue.push_back(start);
}
while let Some(cur) = queue.pop_front() {
for &(p, c) in &self.edges {
if c == cur && !visited.contains(&p) {
visited.insert(p);
queue.push_back(p);
}
}
}
visited.into_iter().map(|i| self.nodes[i].clone()).collect()
}
/// Return all descendants of `node` (transitive children), excluding the node itself.
pub fn descendants_of(&self, node: &str) -> Vec<String> {
let mut visited = HashSet::new();
let mut queue = VecDeque::new();
if let Some(start) = self.node_index(node) {
queue.push_back(start);
}
while let Some(cur) = queue.pop_front() {
for &(p, c) in &self.edges {
if p == cur && !visited.contains(&c) {
visited.insert(c);
queue.push_back(c);
}
}
}
visited.into_iter().map(|i| self.nodes[i].clone()).collect()
}
/// Check whether the graph is acyclic using Kahn's BFS topological sort algorithm.
///
/// Returns `true` if the graph is a valid DAG.
pub fn is_acyclic(&self) -> bool {
let n = self.nodes.len();
let mut in_degree = vec![0usize; n];
for &(_, c) in &self.edges {
in_degree[c] += 1;
}
let mut queue: VecDeque<usize> = (0..n).filter(|&i| in_degree[i] == 0).collect();
let mut processed = 0usize;
while let Some(cur) = queue.pop_front() {
processed += 1;
for &(p, c) in &self.edges {
if p == cur {
in_degree[c] -= 1;
if in_degree[c] == 0 {
queue.push_back(c);
}
}
}
}
processed == n
}
/// Return all node names.
pub fn nodes(&self) -> &[String] {
&self.nodes
}
/// Return the number of nodes.
pub fn node_count(&self) -> usize {
self.nodes.len()
}
/// Return the number of directed edges.
pub fn edge_count(&self) -> usize {
self.edges.len()
}
/// 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`.
pub fn d_separated(&self, x: &str, y: &str, observed: &[&str]) -> bool {
let x_idx = match self.node_index(x) {
Some(i) => i,
None => return true,
};
let y_idx = match self.node_index(y) {
Some(i) => i,
None => return true,
};
if x_idx == y_idx {
return false;
}
let obs_set: HashSet<usize> = observed
.iter()
.filter_map(|&name| self.node_index(name))
.collect();
// Pre-compute descendants of all observed nodes (needed for collider check).
let mut obs_or_desc: HashSet<usize> = obs_set.clone();
for &o in &obs_set {
let node_name = &self.nodes[o].clone();
for desc in self.descendants_of(node_name) {
if let Some(di) = self.node_index(&desc) {
obs_or_desc.insert(di);
}
}
}
// State: (node_idx, arrived_via_child: bool)
// arrived_via_child = true → we arrived at this node from one of its children (going "up")
// arrived_via_child = false → we arrived from a parent (going "down")
let mut visited: HashSet<(usize, bool)> = HashSet::new();
let mut queue: VecDeque<(usize, bool)> = VecDeque::new();
// We can start from x going both up and down.
queue.push_back((x_idx, true));
queue.push_back((x_idx, false));
while let Some((cur, via_child)) = queue.pop_front() {
if !visited.insert((cur, via_child)) {
continue;
}
if cur == y_idx {
return false; // active path found → NOT d-separated
}
if via_child && !obs_set.contains(&cur) {
// Traversing up (non-collider direction): pass through parents and children
// go up to parents
for &(p, c) in &self.edges {
if c == cur {
let state = (p, true);
if !visited.contains(&state) {
queue.push_back(state);
}
}
}
// go down to children
for &(p, c) in &self.edges {
if p == cur {
let state = (c, false);
if !visited.contains(&state) {
queue.push_back(state);
}
}
}
}
if !via_child {
// Arriving from above (going down)
if !obs_set.contains(&cur) {
// Non-collider going down: continue downward
for &(p, c) in &self.edges {
if p == cur {
let state = (c, false);
if !visited.contains(&state) {
queue.push_back(state);
}
}
}
}
// Collider activation: if cur (collider) or descendant is observed, go up
if obs_or_desc.contains(&cur) {
for &(p, c) in &self.edges {
if c == cur {
let state = (p, true);
if !visited.contains(&state) {
queue.push_back(state);
}
}
}
}
}
}
true // no active path found → d-separated
}
/// Internal helper: collect all undirected (bidirectional) adjacency paths from `src` to `dst`
/// that are *backdoor paths* (i.e. paths that enter `src` via a parent of `src`).
/// Returns true if there exists at least one unblocked backdoor path given `adjustment_set`.
pub(super) fn has_unblocked_backdoor_path(
&self,
src: usize,
dst: usize,
adjustment_set: &HashSet<usize>,
) -> bool {
// A backdoor path from src to dst is an undirected path that starts by going
// "upward" from src (i.e. first step is via a parent of src).
// We block a path by conditioning on a non-collider on the path,
// or by NOT conditioning on a collider / its descendant.
//
// We use a simplified reachability check:
// A node Z blocks a path if it is a non-collider on the path AND Z is in adjustment_set,
// or it is a collider not in adjustment_set and none of its descendants are.
//
// State: (current_node, previous_node, direction: true=going_up)
// We only consider paths that leave src going upward (backdoor).
// Compute descendants for collider check
let mut desc_map: HashMap<usize, HashSet<usize>> = HashMap::new();
for i in 0..self.nodes.len() {
let desc_names = self.descendants_of(&self.nodes[i].clone());
let desc_idxs: HashSet<usize> = desc_names
.iter()
.filter_map(|n| self.node_index(n))
.collect();
desc_map.insert(i, desc_idxs);
}
let is_in_adj_or_desc = |node: usize| -> bool {
if adjustment_set.contains(&node) {
return true;
}
if let Some(descs) = desc_map.get(&node) {
return descs.iter().any(|d| adjustment_set.contains(d));
}
false
};
// State: (current_node, prev_node, arrived_via_up: bool)
let mut visited: HashSet<(usize, usize, bool)> = HashSet::new();
let mut queue: VecDeque<(usize, usize, bool)> = VecDeque::new();
// Only start on parents of src (backdoor = entering src from above)
for &(p, c) in &self.edges {
if c == src {
// parent p of src: going up (from src to p)
// The first step is upward. p is a non-collider relative to src→p.
// Block if p is in adjustment_set
if !adjustment_set.contains(&p) {
queue.push_back((p, src, true));
}
}
}
while let Some((cur, prev, going_up)) = queue.pop_front() {
if !visited.insert((cur, prev, going_up)) {
continue;
}
if cur == dst {
return true;
}
// Explore neighbors
// Build set of parents and children of cur
let parents: Vec<usize> = self
.edges
.iter()
.filter(|&&(_, c)| c == cur)
.map(|&(p, _)| p)
.collect();
let children: Vec<usize> = self
.edges
.iter()
.filter(|&&(p, _)| p == cur)
.map(|&(_, c)| c)
.collect();
for &next in parents.iter().chain(children.iter()) {
if next == prev {
continue;
}
// Determine if cur is a collider on the segment prev→cur→next
// cur is a collider iff both prev and next are parents of cur
let prev_is_parent_of_cur = parents.contains(&prev);
let next_is_parent_of_cur = parents.contains(&next);
let is_collider = prev_is_parent_of_cur && next_is_parent_of_cur;
let blocked = if is_collider {
// Collider: blocked unless cur or its descendant is in adjustment set
!is_in_adj_or_desc(cur)
} else {
// Non-collider: blocked if cur is in adjustment set
adjustment_set.contains(&cur)
};
if !blocked {
let next_going_up = parents.contains(&next);
let state = (next, cur, next_going_up);
if !visited.contains(&state) {
queue.push_back(state);
}
}
}
}
false
}
/// Check whether there is a directed path from `src` to `dst`.
pub fn has_directed_path(&self, src: &str, dst: &str) -> bool {
let src_idx = match self.node_index(src) {
Some(i) => i,
None => return false,
};
let dst_idx = match self.node_index(dst) {
Some(i) => i,
None => return false,
};
let mut visited = HashSet::new();
let mut queue = VecDeque::new();
queue.push_back(src_idx);
while let Some(cur) = queue.pop_front() {
if cur == dst_idx {
return true;
}
if !visited.insert(cur) {
continue;
}
for &(p, c) in &self.edges {
if p == cur && !visited.contains(&c) {
queue.push_back(c);
}
}
}
false
}
}