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
use itertools::Itertools;
use log::debug;
use rayon::prelude::*;
use crate::{
estimators::{CITest, PK},
models::{DiGraph, Graph, Labelled},
set,
types::{Error, Result, Set},
};
/// A struct representing a continuous-time Peter-Clark estimator.
#[derive(Clone, Debug)]
pub struct CTPC<'a, T, S> {
initial_graph: &'a DiGraph,
null_time: &'a T,
null_state: &'a S,
prior_knowledge: Option<&'a PK>,
}
impl<'a, T, S> CTPC<'a, T, S>
where
T: CITest + Labelled,
S: CITest + Labelled,
{
/// Creates a new `CTPC` instance.
///
/// # Arguments
///
/// * `initial_graph` - A reference to the initial graph.
/// * `null_time` - A reference to the null time to transition hypothesis test.
/// * `null_state` - A reference to the null state-to-state transition hypothesis test.
///
/// # Returns
///
/// A new `CTPC` instance.
///
#[inline]
pub fn new(initial_graph: &'a DiGraph, null_time: &'a T, null_state: &'a S) -> Result<Self> {
// Check labels of the initial graph and the estimator are the same.
if initial_graph.labels() != null_time.labels() {
return Err(Error::LabelMismatch(
&format!("{:?}", initial_graph.labels()),
&format!("{:?}", null_time.labels()),
));
}
// Check labels of the initial graph and the estimator are the same.
if initial_graph.labels() != null_state.labels() {
return Err(Error::LabelMismatch(
&format!("{:?}", initial_graph.labels()),
&format!("{:?}", null_state.labels()),
));
}
Ok(Self {
initial_graph,
null_time,
null_state,
prior_knowledge: None,
})
}
/// Sets the prior knowledge for the algorithm.
///
/// # Arguments
///
/// * `prior_knowledge` - The prior knowledge to use.
///
/// # Returns
///
/// The modified instance.
///
#[inline]
pub fn with_prior_knowledge(mut self, prior_knowledge: &'a PK) -> Result<Self> {
// Check labels of prior knowledge and initial graph are the same.
if self.initial_graph.labels() != prior_knowledge.labels() {
return Err(Error::LabelMismatch(
&format!("{:?}", self.initial_graph.labels()),
&format!("{:?}", prior_knowledge.labels()),
));
}
// Check prior knowledge is consistent with initial graph.
for edge in self.initial_graph.vertices().into_iter().permutations(2) {
// Get the edge indices.
let (i, j) = (edge[0], edge[1]);
// Check edge must be either present and not forbidden ...
if self.initial_graph.has_edge(i, j)? {
if prior_knowledge.is_forbidden(i, j) {
return Err(Error::PriorKnowledgeConflict(&format!(
"Initial graph contains forbidden edge ({i}, {j})."
)));
}
}
// ... or absent and not required.
else if prior_knowledge.is_required(i, j) {
return Err(Error::PriorKnowledgeConflict(&format!(
"Initial graph does not contain required edge ({i}, {j})."
)));
}
}
// Set prior knowledge.
self.prior_knowledge = Some(prior_knowledge);
Ok(self)
}
/// Execute the CTPC algorithm.
///
/// # Returns
///
/// The fitted graph.
///
pub fn fit(&self) -> Result<DiGraph> {
// Clone the initial graph.
let mut graph = self.initial_graph.clone();
// For each vertex in the graph ...
for i in graph.vertices() {
// Get the parents of the vertex.
let mut pa_i = graph.parents(&set![i])?;
// Initialize the counter.
let mut k = 0;
// While the counter is smaller than the number of parents ...
while k < pa_i.len() {
// Initialize the set of vertices to remove, to ensure stability.
// For each parent, check if it is independent of the child given a subset of size k.
let not_pa_i: Vec<_> = pa_i
.iter()
.filter_map(|&j| {
// Check prior knowledge, if available.
if let Some(pk) = self.prior_knowledge {
// If the edge is required, skip the tests.
// NOTE: Since CTPC only removes edges,
// it is sufficient to check for required edges.
if pk.is_required(j, i) {
// Log the skipped CIT.
debug!("CIT for {j} _||_ {i} | [*] ... SKIPPED");
return None;
}
}
// Filter out the parent.
let pa_i_not_j = pa_i.iter().filter(|&&z| z != j).cloned();
// For any combination of size k of Pa(X_i) \ { X_j } ...
pa_i_not_j
.combinations(k)
.map(Set::from_iter)
.find_map(|s_ij| {
// Log the current combination.
debug!("CIT for {i} _||_ {j} | {s_ij:?} ...");
// If X_i _||_ X_j | S_{X_i, X_j} ...
match self.null_time.call(&set![i], &set![j], &s_ij) {
Ok(true) => {
match self.null_state.call(&set![i], &set![j], &s_ij) {
Ok(true) => {
// Log the result of the CIT.
debug!(
"CIT for {i} _||_ {j} | {s_ij:?} ... PASSED"
);
Some(Ok(j))
}
Ok(false) => None,
Err(e) => Some(Err(e)),
}
}
Ok(false) => None,
Err(e) => Some(Err(e)),
}
})
})
.collect::<Result<_>>()?;
// Remove the vertices from the graph.
for &j in ¬_pa_i {
// Remove the vertex from the parents.
pa_i.retain(|&x| x != j);
// Remove the edge from the graph.
graph.del_edge(j, i)?;
}
// Increment the counter.
k += 1;
}
}
// Return the fitted graph.
Ok(graph)
}
}
impl<'a, T, S> CTPC<'a, T, S>
where
T: CITest + Sync,
S: CITest + Sync,
{
/// Execute the CTPC algorithm and return the fitted graph in parallel.
///
/// # Returns
///
/// The fitted graph.
///
pub fn par_fit(&self) -> Result<DiGraph> {
// For each vertex in the graph ...
let parents: Vec<_> = self
.initial_graph
.vertices()
.into_par_iter()
.map(|i| -> Result<Set<usize>> {
// Get the parents of the vertex.
let mut pa_i = self.initial_graph.parents(&set![i])?;
// Initialize the counter.
let mut k = 0;
// While the counter is smaller than the number of parents ...
while k < pa_i.len() {
// Filter the parents in parallel.
pa_i = pa_i
.par_iter()
.map(|&j| -> Result<Option<usize>> {
// Check prior knowledge, if available.
if let Some(pk) = self.prior_knowledge {
// If the edge is required, skip the tests.
// NOTE: Since CTPC only removes edges,
// it is sufficient to check for required edges.
if pk.is_required(j, i) {
// Log the skipped CIT.
debug!("CIT for {j} _||_ {i} | [*] ... SKIPPED");
return Ok(Some(j));
}
}
// Filter out the parent.
let pa_i_not_j = pa_i.iter().filter(|&&z| z != j).cloned();
// For any combination of size k of Pa(X_i) \ { X_j } ...
for s_ij in pa_i_not_j.combinations(k).map(Set::from_iter) {
// Log the current combination.
debug!("CIT for {i} _||_ {j} | {s_ij:?} ...");
// If X_i _||_ X_j | S_{X_i, X_j} ...
if self.null_time.call(&set![i], &set![j], &s_ij)?
&& self.null_state.call(&set![i], &set![j], &s_ij)?
{
// Log the result of the CIT.
debug!("CIT for {i} _||_ {j} | {s_ij:?} ... PASSED");
// Add the parent to the set of vertices to remove.
return Ok(None);
}
}
// Otherwise, keep the parent.
Ok(Some(j))
})
.filter_map(|x| x.transpose())
.collect::<Result<_>>()?;
// Increment the counter.
k += 1;
}
// Return the parents of the vertex.
Ok(pa_i)
})
.collect::<Result<_>>()?;
// Initialize an empty graph.
let mut graph = DiGraph::empty(self.initial_graph.labels())?;
// Set the parents of each vertex.
parents.into_iter().enumerate().try_for_each(|(i, pa_i)| {
// For each parent ...
pa_i.into_iter().try_for_each(|j| -> Result<_> {
graph.add_edge(j, i)?;
Ok(())
})
})?;
// Return the fitted graph.
Ok(graph)
}
}