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
use crate::{
models::{DiGraph, Graph},
set,
types::{Result, Set},
};
/// A trait for backdoor adjustment criterion.
pub trait BackdoorCriterion {
/// Checks if the `Z` is a backdoor adjustment set for `X` and `Y`.
///
/// # Arguments
///
/// * `x` - A set vertices representing set `X`.
/// * `y` - A set vertices representing set `Y`.
/// * `z` - A set vertices representing set `Z`.
///
/// # Errors
///
/// * `IndexOutOfBounds` if any of the vertex in `X`, `Y`, or `Z` are out of bounds.
/// * `SetsNotDisjoint` if `X`, `Y` or `Z` are not disjoint sets.
/// * `EmptySet` if `X` or `Y` are empty sets.
///
/// # Returns
///
/// `true` if `X` and `Y` are separated by `Z`, `false` otherwise.
///
fn is_backdoor_set(&self, x: &Set<usize>, y: &Set<usize>, z: &Set<usize>) -> Result<bool>;
/// Checks if the `Z` is a minimal backdoor adjustment set for `X` and `Y`.
///
/// # Arguments
///
/// * `x` - A set vertices representing set `X`.
/// * `y` - A set vertices representing set `Y`.
/// * `z` - A set vertices representing set `Z`.
/// * `w` - An optional iterable collection of vertices representing set `W`.
/// * `v` - An optional iterable collection of vertices representing set `V`.
///
/// # Errors
///
/// * `IndexOutOfBounds` if any of the vertex in `X`, `Y`, `Z`, `W`, or `V` are out of bounds.
/// * `SetsNotDisjoint` if `X`, `Y` or `Z` are not disjoint sets.
/// * `EmptySet` if `X` or `Y` are empty sets.
/// * `SubsetMismatch` if not `W` <= `Z` <= `V`.
///
/// # Returns
///
/// `true` if `Z` is a minimal backdoor adjustment set for `X` and `Y`, `false` otherwise.
///
fn is_minimal_backdoor_set(
&self,
x: &Set<usize>,
y: &Set<usize>,
z: &Set<usize>,
w: Option<&Set<usize>>,
v: Option<&Set<usize>>,
) -> Result<bool>;
/// Finds a minimal backdoor adjustment set for the vertex sets `X` and `Y`, if any.
///
/// # Arguments
///
/// * `x` - A set vertices representing set `X`.
/// * `y` - A set vertices representing set `Y`.
/// * `w` - An optional iterable collection of vertices representing set `W`.
/// * `v` - An optional iterable collection of vertices representing set `V`.
///
/// # Errors
///
/// * `IndexOutOfBounds` if any of the vertex in `X`, `Y`, `W`, or `V` are out of bounds.
/// * `SetsNotDisjoint` if `X` and `Y` are not disjoint sets.
/// * `EmptySet` if `X` or `Y` are empty sets.
/// * `SubsetMismatch` if not `W` <= `V`.
///
/// # Returns
///
/// `Some(Set)` containing the minimal backdoor adjustment set,
/// or `None` if no backdoor adjustment set exists.
///
fn find_minimal_backdoor_set(
&self,
x: &Set<usize>,
y: &Set<usize>,
w: Option<&Set<usize>>,
v: Option<&Set<usize>>,
) -> Result<Option<Set<usize>>>;
}
mod digraph {
use super::*;
use crate::inference::{GraphicalSeparation, digraph::_assert};
// Returns the set of vertices:
//
// PCP(X, Y) = { W \in (V \ X) | W is on a *proper possible causal path* from X to Y }
//
// where:
//
// * possible causal path in a directed graph is a directed path from X to Y,
// * proper path is a directed path from X to Y that does not contain any vertex in X.
//
fn _proper_causal_path(g: &DiGraph, x: &Set<usize>, y: &Set<usize>) -> Result<Set<usize>> {
// Initialize the PCP set.
let mut pcp = set![];
// Perform a visit starting from each vertex in X.
for &x_i in x {
// Initialize stack and visited set.
let mut stack = vec![x_i];
let mut visited = set![x_i];
// While there are vertices to visit ...
while let Some(z) = stack.pop() {
// For each child of the current node ...
for w in g.children(&set![z])? {
// Skip if W is in X or already visited.
if x.contains(&w) || visited.contains(&w) {
continue;
}
// Set W as visited.
visited.insert(w);
// Skip if W is in PCP, continue search.
if pcp.contains(&w) {
continue;
}
// Add W to the PCP set.
pcp.insert(w);
// Skip if W is in Y, continue search.
if y.contains(&w) {
continue;
}
// Add W to the stack for further exploration.
stack.push(w);
}
}
}
Ok(pcp)
}
// Returns the proper backdoor graph:
//
// G^PDB = G \ { X -> PCP(X, Y) }
//
fn _proper_backdoor_graph(g: &DiGraph, x: &Set<usize>, pcp: &Set<usize>) -> Result<DiGraph> {
// Clone the graph.
let mut g_pdb = g.clone();
// Remove all the edge from X to PCP(X, Y).
x.iter()
.flat_map(|&i| pcp.iter().map(move |&j| (i, j)))
.try_for_each(|(i, j)| -> Result<_> {
g_pdb.del_edge(i, j)?;
Ok(())
})?;
// Return the modified graph.
Ok(g_pdb)
}
impl BackdoorCriterion for DiGraph {
fn is_backdoor_set(&self, x: &Set<usize>, y: &Set<usize>, z: &Set<usize>) -> Result<bool> {
// Perform sanity checks and convert sets.
_assert(self, x, y, Some(z), None::<&Set<_>>, None::<&Set<_>>)?;
// Constructive backdoor criterion:
//
// Z is a backdoor set for X and Y if and only if:
//
// a) Z <= V \ pDe(PCP(X, Y)), and
// b) Z separates X from Y in G^PDB.
//
// Compute the proper causal path.
let pcp = _proper_causal_path(self, x, y)?;
// Compute the descendants of the proper causal path.
let pde = self.descendants(&pcp)?;
// a) Check if Z is a subset of V \ pDe(PCP(X, Y)).
if !z.is_subset(&(&self.vertices() - &pde)) {
return Ok(false);
}
// Compute the proper backdoor graph.
let g_pdb = _proper_backdoor_graph(self, x, &pcp)?;
// b) Check if Z separates X from Y in G^PDB.
if !g_pdb.is_separator_set(x, y, z)? {
return Ok(false);
}
// Otherwise, return true.
Ok(true)
}
fn is_minimal_backdoor_set(
&self,
x: &Set<usize>,
y: &Set<usize>,
z: &Set<usize>,
w: Option<&Set<usize>>,
v: Option<&Set<usize>>,
) -> Result<bool> {
// Perform sanity checks and convert sets.
_assert(self, x, y, Some(z), w, v)?;
// Set default values for W and V if not provided.
let w = match w {
Some(w) => w,
None => &set![],
};
let v = match v {
Some(v) => v,
None => &self.vertices(),
};
// Every minimal backdoor adjustment set is a
// minimal separator in the proper backdoor graph
// G^PDB under the constraint V' = V \ pDe(PCP(X, Y)).
// Compute the proper causal path.
let pcp = _proper_causal_path(self, x, y)?;
// Compute the descendants of the proper causal path.
let pde = self.descendants(&pcp)?;
// Constraint the restricted vertices.
let v_prime = &(v - &pde);
// Compute the proper backdoor graph.
let g_pdb = _proper_backdoor_graph(self, x, &pcp)?;
// Check if Z is a minimal separator in G^PDB under the constraint V'.
g_pdb.is_minimal_separator_set(x, y, z, Some(w), Some(v_prime))
}
fn find_minimal_backdoor_set(
&self,
x: &Set<usize>,
y: &Set<usize>,
w: Option<&Set<usize>>,
v: Option<&Set<usize>>,
) -> Result<Option<Set<usize>>> {
// Perform sanity checks and convert sets.
_assert(self, x, y, None::<&Set<_>>, w, v)?;
// Set default values for W and V if not provided.
let w = match w {
Some(w) => w,
None => &set![],
};
let v = match v {
Some(v) => v,
None => &self.vertices(),
};
// Every minimal backdoor adjustment set is a
// minimal separator in the proper backdoor graph
// G^PDB under the constraint V' = V \ pDe(PCP(X, Y)).
// Compute the proper causal path.
let pcp = _proper_causal_path(self, x, y)?;
// Compute the descendants of the proper causal path.
let pde = self.descendants(&pcp)?;
// Constraint the restricted vertices.
let v_prime = &(v - &pde);
// Compute the proper backdoor graph.
let g_pdb = _proper_backdoor_graph(self, x, &pcp)?;
// Find a minimal separator in G^PDB under the constraint V'.
g_pdb.find_minimal_separator_set(x, y, Some(w), Some(v_prime))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn proper_causal_path() -> Result<()> {
let mut graph = DiGraph::empty(vec!["A", "B", "C", "D", "E"])?;
graph.add_edge(0, 1)?;
graph.add_edge(0, 2)?;
graph.add_edge(1, 2)?;
graph.add_edge(1, 3)?;
graph.add_edge(2, 3)?;
graph.add_edge(3, 4)?;
assert_eq!(
_proper_causal_path(&graph, &set![0], &set![3])?,
set![1, 2, 3]
);
Ok(())
}
}
}