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
use dry::macro_for;
use crate::{
inference::{BNInference, BackdoorCriterion, Modelled, ParBNInference},
models::{BN, CatBN, GaussBN, Labelled, Phi},
set,
types::Set,
};
/// A causal inference engine.
#[derive(Clone, Debug)]
pub struct CausalInference<'a, E> {
engine: &'a E,
}
impl<'a, E> CausalInference<'a, E> {
/// Create a new causal inference engine.
///
/// # Arguments
///
/// * `engine` - The underlying inference engine.
///
/// # Returns
///
/// The causal inference engine.
///
pub fn new(engine: &'a E) -> Self {
Self { engine }
}
}
/// A trait for causal inference with Bayesian Networks.
pub trait BNCausalInference<T>
where
T: BN,
{
/// Estimate the average causal effect of `X` on `Y` as E(Y | do(X)).
///
/// # Arguments
///
/// * `x` - The cause variables.
/// * `y` - The effect variables.
///
/// # Panics
///
/// * If `X` is empty.
/// * If `Y` is empty.
/// * If `X` and `Y` are not disjoint.
///
/// # Returns
///
/// The estimated average causal effect of `X` on `Y`.
///
fn ace_estimate(&self, x: &Set<usize>, y: &Set<usize>) -> Option<T::CPD> {
self.cace_estimate(x, y, &set![])
}
/// Estimate the conditional average causal effect of `X` on `Y` given `Z` as E(Y | do(X), Z).
///
/// # Arguments
///
/// * `x` - The cause variables.
/// * `y` - The effect variables.
/// * `z` - The conditioning variables.
///
/// # Panics
///
/// * If `X` is empty.
/// * If `Y` is empty.
/// * If `X` and `Y` are not disjoint.
/// * If `X` and `Z` are not disjoint.
/// * If `Y` and `Z` are not disjoint.
///
/// # Returns
///
/// The estimated conditional average causal effect of `X` on `Y` given `Z`.
///
fn cace_estimate(&self, x: &Set<usize>, y: &Set<usize>, z: &Set<usize>) -> Option<T::CPD>;
}
macro_for!($type in [CatBN, GaussBN] {
impl<E> BNCausalInference<$type> for CausalInference<'_, E>
where
E: Modelled<$type> + BNInference<$type>,
{
fn cace_estimate(&self, x: &Set<usize>, y: &Set<usize>, z: &Set<usize>) -> Option<<$type as BN>::CPD> {
// Assert X is not empty.
assert!(!x.is_empty(), "Variables X must not be empty.");
// Assert Y is not empty.
assert!(!y.is_empty(), "Variables Y must not be empty.");
// Assert X and Y are disjoint.
assert!(x.is_disjoint(y), "Variables X and Y must be disjoint.");
// Assert X and Z are disjoint.
assert!(x.is_disjoint(z), "Variables X and Z must be disjoint.");
// Assert Y and Z are disjoint.
assert!(y.is_disjoint(z), "Variables Y and Z must be disjoint.");
/* Effect Identification */
// Get the model.
let m = self.engine.model();
// Find a minimal backdoor adjustment set Z \cup S, if any.
let z_s = m.graph().find_minimal_backdoor_set(x, y, Some(z), None);
/* Effect Estimation */
// Match on the backdoor adjustment set.
match z_s {
// If no backdoor adjustment set exists, return None.
None => None,
// If the backdoor adjustment set is empty ...
Some(z_s) if z_s.is_empty() => {
// ... estimate P(Y | do(X)) as P(Y | X).
Some(self.engine.estimate(y, x))
}
// If the backdoor adjustment set is equal to Z ...
Some(z_s) if z_s.eq(z) => {
// ... estimate P(Y | do(X), Z) as P(Y | X, Z).
Some(self.engine.estimate(y, &(x | z)))
}
// If the backdoor adjustment set is not equal to Z ...
Some(z_s) => {
// Get the S part.
let s = &(&z_s - z);
// Estimate P(Y | X, Z, S) and P(S).
let p_y_x_z_s = self.engine.estimate(y, &(x | &z_s));
let p_s = self.engine.estimate(s, &set![]);
// Convert to potentials for aligned multiplication.
let p_y_x_z_s = p_y_x_z_s.into_phi();
let p_s = p_s.into_phi();
// Compute P(Y | X, Z, S) * P(S) using potentials.
let p_y_s_do_x_z = &p_y_x_z_s * &p_s;
// Map BN indices to the potential indices.
let s = p_y_s_do_x_z.indices_from(s, m.labels());
// Marginalize over S.
let p_y_do_x_z = p_y_s_do_x_z.marginalize(&s);
// Map BN indices to the potential indices.
let x = p_y_do_x_z.indices_from(x, m.labels());
let y = p_y_do_x_z.indices_from(y, m.labels());
let z = p_y_do_x_z.indices_from(z, m.labels());
// Convert back to CPD.
let p_y_do_x_z = p_y_do_x_z.into_cpd(&y, &(&x | &z));
// Return the result.
Some(p_y_do_x_z)
}
}
}
}
});
/// A trait for causal inference with Bayesian Networks in parallel.
pub trait ParBNCausalInference<T>
where
T: BN,
{
/// Estimate the average causal effect of `X` on `Y` as E(Y | do(X)) in parallel.
///
/// # Arguments
///
/// * `x` - The cause variables.
/// * `y` - The effect variables.
///
/// # Panics
///
/// * If `X` is empty.
/// * If `Y` is empty.
/// * If `X` and `Y` are not disjoint.
///
/// # Returns
///
/// The estimated average causal effect of `X` on `Y`.
///
fn par_ace_estimate(&self, x: &Set<usize>, y: &Set<usize>) -> Option<T::CPD> {
self.par_cace_estimate(x, y, &set![])
}
/// Estimate the conditional average causal effect of `X` on `Y` given `Z` as E(Y | do(X), Z) in parallel.
///
/// # Arguments
///
/// * `x` - The cause variables.
/// * `y` - The effect variables.
/// * `z` - The conditioning variables.
///
/// # Panics
///
/// * If `X` is empty.
/// * If `Y` is empty.
/// * If `X` and `Y` are not disjoint.
/// * If `X` and `Z` are not disjoint.
/// * If `Y` and `Z` are not disjoint.
///
/// # Returns
///
/// The estimated conditional average causal effect of `X` on `Y` given `Z`.
///
fn par_cace_estimate(&self, x: &Set<usize>, y: &Set<usize>, z: &Set<usize>) -> Option<T::CPD>;
}
macro_for!($type in [CatBN, GaussBN] {
impl<E> ParBNCausalInference<$type> for CausalInference<'_, E>
where
E: Modelled<$type> + ParBNInference<$type>,
{
fn par_cace_estimate(&self, x: &Set<usize>, y: &Set<usize>, z: &Set<usize>) -> Option<<$type as BN>::CPD> {
// Assert X is not empty.
assert!(!x.is_empty(), "Variables X must not be empty.");
// Assert Y is not empty.
assert!(!y.is_empty(), "Variables Y must not be empty.");
// Assert X and Y are disjoint.
assert!(x.is_disjoint(y), "Variables X and Y must be disjoint.");
// Assert X and Z are disjoint.
assert!(x.is_disjoint(z), "Variables X and Z must be disjoint.");
// Assert Y and Z are disjoint.
assert!(y.is_disjoint(z), "Variables Y and Z must be disjoint.");
/* Effect Identification */
// Get the model.
let m = self.engine.model();
// Find a minimal backdoor adjustment set Z \cup S, if any.
let z_s = m.graph().find_minimal_backdoor_set(x, y, Some(z), None);
/* Effect Estimation */
// Match on the backdoor adjustment set.
match z_s {
// If no backdoor adjustment set exists, return None.
None => None,
// If the backdoor adjustment set is empty ...
Some(z_s) if z_s.is_empty() => {
// ... estimate P(Y | do(X)) as P(Y | X).
Some(self.engine.par_estimate(y, x))
}
// If the backdoor adjustment set is equal to Z ...
Some(z_s) if z_s.eq(z) => {
// ... estimate P(Y | do(X), Z) as P(Y | X, Z).
Some(self.engine.par_estimate(y, &(x | z)))
}
// If the backdoor adjustment set is not equal to Z ...
Some(z_s) => {
// Get the S part.
let s = &(&z_s - z);
// Estimate P(Y | X, Z, S) and P(S).
let p_y_x_z_s = self.engine.par_estimate(y, &(x | &z_s));
let p_s = self.engine.par_estimate(s, &set![]);
// Convert to potentials for aligned multiplication.
let p_y_x_z_s = p_y_x_z_s.into_phi();
let p_s = p_s.into_phi();
// Compute P(Y | X, Z, S) * P(S) using potentials.
let p_y_s_do_x_z = &p_y_x_z_s * &p_s;
// Map BN indices to the potential indices.
let s = p_y_s_do_x_z.indices_from(s, m.labels());
// Marginalize over S.
let p_y_do_x_z = p_y_s_do_x_z.marginalize(&s);
// Map BN indices to the potential indices.
let x = p_y_do_x_z.indices_from(x, m.labels());
let y = p_y_do_x_z.indices_from(y, m.labels());
let z = p_y_do_x_z.indices_from(z, m.labels());
// Convert back to CPD.
let p_y_do_x_z = p_y_do_x_z.into_cpd(&y, &(&x | &z));
// Return the result.
Some(p_y_do_x_z)
}
}
}
}
});