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
use itertools::{Either, Itertools};
use ndarray::prelude::*;
use ndarray_stats::CorrelationExt;
use crate::{
models::Labelled,
types::{Labels, Set},
};
/// A struct for missing information in a tabular dataset.
#[derive(Clone, Debug)]
pub struct MissingTable {
labels: Labels,
fully_observed: Set<usize>,
partially_observed: Set<usize>,
missing_mask: Array2<bool>,
missing_mask_by_cols: Array1<bool>,
missing_mask_by_rows: Array1<bool>,
missing_count: usize,
missing_count_by_cols: Array1<usize>,
missing_count_by_rows: Array1<usize>,
missing_rate: f64,
missing_rate_by_cols: Array1<f64>,
missing_rate_by_rows: Array1<f64>,
missing_correlation: Array2<f64>,
missing_covariance: Array2<f64>,
complete_cols_count: usize,
complete_rows_count: usize,
}
impl Labelled for MissingTable {
#[inline]
fn labels(&self) -> &Labels {
&self.labels
}
}
impl MissingTable {
/// Create a new missing information table from the given labels and missing mask.
///
/// # Arguments
///
/// * `labels` - The labels of the dataset.
/// * `missing_mask` - A boolean matrix indicating missing values.
///
/// # Returns
///
/// A new missing information instance.
///
pub fn new(mut labels: Labels, mut missing_mask: Array2<bool>) -> Self {
// Assert dimensions match.
assert_eq!(
labels.len(),
missing_mask.ncols(),
"Number of labels must match the number of columns in the missing mask."
);
// Check if labels are sorted.
if !labels.is_sorted() {
// Allocate indices to sort labels.
let mut indices: Vec<usize> = (0..labels.len()).collect();
// Sort the indices by labels.
indices.sort_by_key(|&i| &labels[i]);
// Sort the labels.
labels.sort();
// Allocate new missing mask.
let mut new_missing_mask = missing_mask.clone();
// Sort the new missing mask according to the sorted indices.
indices.into_iter().enumerate().for_each(|(i, j)| {
new_missing_mask
.column_mut(i)
.assign(&missing_mask.column(j));
});
// Update missing mask.
missing_mask = new_missing_mask;
}
// Compute missing counts.
let missing_count_by_cols = missing_mask.rows().into_iter().fold(
// Map to numeric one at a time to save memory.
Array::zeros(missing_mask.ncols()),
|acc, row| acc + row.mapv(|x| x as usize),
);
let missing_count_by_rows = missing_mask.columns().into_iter().fold(
// Map to numeric one at a time to save memory.
Array::zeros(missing_mask.nrows()),
|acc, col| acc + col.mapv(|x| x as usize),
);
let missing_count = missing_count_by_cols.sum();
// Compute missing mask by cols and rows.
let missing_mask_by_cols = missing_count_by_cols.mapv(|x| x > 0);
let missing_mask_by_rows = missing_count_by_rows.mapv(|x| x > 0);
// Compute fully and partially observed variable sets.
let (fully_observed, partially_observed) = missing_mask_by_cols
.iter()
.enumerate()
.partition_map(|(i, &x)| {
if !x {
Either::Left(i)
} else {
Either::Right(i)
}
});
// Compute complete counts.
let complete_cols_count = missing_mask_by_cols.mapv(|x| (!x) as usize).sum();
let complete_rows_count = missing_mask_by_rows.mapv(|x| (!x) as usize).sum();
// Compute missing rates.
let missing_rate_by_cols =
missing_count_by_cols.mapv(|x| x as f64) / missing_mask.nrows() as f64;
let missing_rate_by_rows =
missing_count_by_rows.mapv(|x| x as f64) / missing_mask.ncols() as f64;
let missing_rate = missing_count as f64 / missing_mask.len() as f64;
// TODO: Make this optional for large datasets.
// Map to numeric (float) mask.
let missing_mask_numeric = missing_mask.mapv(|x| x as u8 as f64);
// Transpose for correlation/covariance computation.
let missing_mask_numeric = missing_mask_numeric.t();
// Compute missing correlation.
let missing_correlation = missing_mask_numeric
.pearson_correlation()
.expect("Failed to compute missing correlation.");
// Compute missing covariance.
let missing_covariance = missing_mask_numeric
.cov(1.)
.expect("Failed to compute missing covariance.");
Self {
labels,
fully_observed,
partially_observed,
missing_mask,
missing_mask_by_cols,
missing_mask_by_rows,
missing_count,
missing_count_by_cols,
missing_count_by_rows,
missing_rate,
missing_rate_by_cols,
missing_rate_by_rows,
missing_correlation,
missing_covariance,
complete_cols_count,
complete_rows_count,
}
}
/// Get the set of fully observed variables.
///
/// # Returns
///
/// A reference to the set of fully observed variables.
///
#[inline]
pub const fn fully_observed(&self) -> &Set<usize> {
&self.fully_observed
}
/// Get the set of partially observed variables.
///
/// # Returns
///
/// A reference to the set of partially observed variables.
///
#[inline]
pub const fn partially_observed(&self) -> &Set<usize> {
&self.partially_observed
}
/// Get the missing mask indicating the presence of missing values in the table.
///
/// # Returns
///
/// A reference to the missing mask.
///
#[inline]
pub const fn missing_mask(&self) -> &Array2<bool> {
&self.missing_mask
}
/// Get the missing mask indicating the presence of missing values in each column.
///
/// # Returns
///
/// A reference to the missing mask by columns.
///
#[inline]
pub const fn missing_mask_by_cols(&self) -> &Array1<bool> {
&self.missing_mask_by_cols
}
/// Get the missing mask indicating the presence of missing values in each row.
///
/// # Returns
///
/// A reference to the missing mask by rows.
///
#[inline]
pub const fn missing_mask_by_rows(&self) -> &Array1<bool> {
&self.missing_mask_by_rows
}
/// Get the total count of missing values in the table.
///
/// # Returns
///
/// The count of missing values.
///
#[inline]
pub const fn missing_count(&self) -> usize {
self.missing_count
}
/// Get the count of missing values in each column.
///
/// # Returns
///
/// A reference to the missing count by columns.
///
#[inline]
pub const fn missing_count_by_cols(&self) -> &Array1<usize> {
&self.missing_count_by_cols
}
/// Get the count of missing values in each row.
///
/// # Returns
///
/// A reference to the missing count by rows.
///
#[inline]
pub const fn missing_count_by_rows(&self) -> &Array1<usize> {
&self.missing_count_by_rows
}
/// Get the overall missing rate in the table.
///
/// # Returns
///
/// The percentage of missing values.
///
#[inline]
pub const fn missing_rate(&self) -> f64 {
self.missing_rate
}
/// Get the missing rate in each column.
///
/// # Returns
///
/// A reference to the missing percentage by columns.
///
#[inline]
pub const fn missing_rate_by_cols(&self) -> &Array1<f64> {
&self.missing_rate_by_cols
}
/// Get the missing rate in each row.
///
/// # Returns
///
/// A reference to the missing percentage by rows.
///
#[inline]
pub const fn missing_rate_by_rows(&self) -> &Array1<f64> {
&self.missing_rate_by_rows
}
/// Get the missing (Pearson) correlation matrix.
///
/// # Returns
///
/// A reference to the missing correlation matrix.
///
#[inline]
pub const fn missing_correlation(&self) -> &Array2<f64> {
&self.missing_correlation
}
/// Get the missing (unbiased) covariance matrix.
///
/// # Returns
///
/// A reference to the missing covariance matrix.
///
#[inline]
pub const fn missing_covariance(&self) -> &Array2<f64> {
&self.missing_covariance
}
/// Get the count of complete columns (without any missing values) in the table.
///
/// # Returns
///
/// The count of complete columns.
///
#[inline]
pub const fn complete_cols_count(&self) -> usize {
self.complete_cols_count
}
/// Get the count of complete rows (without any missing values) in the table.
///
/// # Returns
///
/// The count of complete rows.
///
#[inline]
pub const fn complete_rows_count(&self) -> usize {
self.complete_rows_count
}
}