fdars-core 0.23.0

Functional Data Analysis algorithms in Rust
Documentation
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Hypograph, modified-hypograph, and epigraph indices (HI, MHI, EI).
//!
//! These are members of the roahd index-measure family, complementing the
//! already-shipped Modified Epigraph Index (MEI).

use crate::error::FdarError;
use crate::iter_maybe_parallel;
use crate::matrix::FdMatrix;
#[cfg(feature = "parallel")]
use rayon::iter::ParallelIterator;

/// Compute the Hypograph Index (HI) for 1D functional data.
///
/// HI is a **global indicator**: for each object curve `X_i`, count the fraction
/// of reference curves `X_j` that lie **entirely at or below** `X_i` across the
/// whole domain, i.e. `X_j(t) <= X_i(t)` for every evaluation point `t`.
///
/// ```text
/// HI(X_i) = (1/N) · #{j : X_j(t) ≤ X_i(t) for all t}
/// ```
///
/// Tie convention: `<=` (consistent with MEI and roahd). [CITED: roahd::HI]
///
/// Range: [0, 1]. Returned values are always integer multiples of `1/nori`
/// because the indicator is 0 or 1 for each reference curve — one crossing is
/// enough to exclude that reference. The deepest curve in the HI sense is the
/// one that is highest (all others below it); HRD = min(EI, HI) provides the
/// symmetric composite.
///
/// # Errors
///
/// Returns [`FdarError::InvalidDimension`] if `data_obj` or `data_ori` is empty,
/// or if `data_ori` has fewer than 2 rows.
#[must_use = "expensive computation whose result should not be discarded"]
pub fn hypograph_index_1d(data_obj: &FdMatrix, data_ori: &FdMatrix) -> Result<Vec<f64>, FdarError> {
    let (nobj, nori, m) = (data_obj.nrows(), data_ori.nrows(), data_obj.ncols());
    if nobj == 0 || nori == 0 || m == 0 {
        return Err(FdarError::InvalidDimension {
            parameter: "data_obj",
            expected: "non-empty matrix".to_string(),
            actual: format!("{nobj}x{m}"),
        });
    }
    if nori < 2 {
        return Err(FdarError::InvalidDimension {
            parameter: "data_ori",
            expected: "at least 2 reference curves for hypograph index".to_string(),
            actual: format!("{nori}"),
        });
    }

    let depths: Vec<f64> = iter_maybe_parallel!(0..nobj)
        .map(|i| {
            let mut count = 0.0_f64;
            'outer: for j in 0..nori {
                for t in 0..m {
                    // If X_j crosses above X_i, this reference is not in the hypograph.
                    if data_ori[(j, t)] > data_obj[(i, t)] {
                        continue 'outer;
                    }
                }
                count += 1.0;
            }
            count / nori as f64
        })
        .collect();

    Ok(depths)
}

/// Compute the Epigraph Index (EI) for 1D functional data.
///
/// EI is a **global indicator** and the complement of HI: for each object curve
/// `X_i`, count the fraction of reference curves `X_j` that lie **entirely at
/// or above** `X_i` across the whole domain, i.e. `X_j(t) >= X_i(t)` for every
/// evaluation point `t`.
///
/// ```text
/// EI(X_i) = (1/N) · #{j : X_j(t) ≥ X_i(t) for all t}
/// ```
///
/// Tie convention: `>=` (i.e., `<=` on the reversed side, consistent with MEI).
/// [CITED: roahd::EI]
///
/// Range: [0, 1]. Returned values are always integer multiples of `1/nori`.
/// Curves at the bottom of the sample score highest in EI; HRD = min(EI, HI)
/// resolves this asymmetry.
///
/// # Errors
///
/// Returns [`FdarError::InvalidDimension`] if `data_obj` or `data_ori` is empty,
/// or if `data_ori` has fewer than 2 rows.
#[must_use = "expensive computation whose result should not be discarded"]
pub fn epigraph_index_1d(data_obj: &FdMatrix, data_ori: &FdMatrix) -> Result<Vec<f64>, FdarError> {
    let (nobj, nori, m) = (data_obj.nrows(), data_ori.nrows(), data_obj.ncols());
    if nobj == 0 || nori == 0 || m == 0 {
        return Err(FdarError::InvalidDimension {
            parameter: "data_obj",
            expected: "non-empty matrix".to_string(),
            actual: format!("{nobj}x{m}"),
        });
    }
    if nori < 2 {
        return Err(FdarError::InvalidDimension {
            parameter: "data_ori",
            expected: "at least 2 reference curves for epigraph index".to_string(),
            actual: format!("{nori}"),
        });
    }

    let depths: Vec<f64> = iter_maybe_parallel!(0..nobj)
        .map(|i| {
            let mut count = 0.0_f64;
            'outer: for j in 0..nori {
                for t in 0..m {
                    // If X_j drops below X_i, this reference is not in the epigraph.
                    if data_ori[(j, t)] < data_obj[(i, t)] {
                        continue 'outer;
                    }
                }
                count += 1.0;
            }
            count / nori as f64
        })
        .collect();

    Ok(depths)
}

/// Compute the Modified Hypograph Index (MHI) for 1D functional data.
///
/// MHI is a **pointwise average**: for each object curve `X_i` and each reference
/// curve `X_j`, measure the fraction of evaluation points where `X_i(t) >= X_j(t)`,
/// then average over all reference curves.
///
/// ```text
/// MHI(X_i) = (1/N) · Σ_j (1/m) · #{t : X_i(t) ≥ X_j(t)}
/// ```
///
/// This is the exact mirror of MEI (`modified_epigraph_index_1d`) with the
/// comparison direction reversed. Without ties: MHI(X) + MEI(X) = 1.
/// [CITED: roahd::MHI]
///
/// Tie convention: `>=` for the MHI condition (consistent with roahd). Range: [0, 1].
/// The central curve of a sample gets MHI ≈ 0.5.
///
/// # Errors
///
/// Returns [`FdarError::InvalidDimension`] if `data_obj` or `data_ori` is empty.
/// Unlike HI/EI, a single reference curve (nori = 1) is mathematically valid for MHI.
#[must_use = "expensive computation whose result should not be discarded"]
pub fn modified_hypograph_index_1d(
    data_obj: &FdMatrix,
    data_ori: &FdMatrix,
) -> Result<Vec<f64>, FdarError> {
    let (nobj, nori, m) = (data_obj.nrows(), data_ori.nrows(), data_obj.ncols());
    if nobj == 0 || nori == 0 || m == 0 {
        return Err(FdarError::InvalidDimension {
            parameter: "data_obj",
            expected: "non-empty matrix".to_string(),
            actual: format!("{nobj}x{m}"),
        });
    }

    let depths: Vec<f64> = iter_maybe_parallel!(0..nobj)
        .map(|i| {
            let mut total = 0.0_f64;
            for j in 0..nori {
                let mut count = 0.0_f64;
                for t in 0..m {
                    // MHI: fraction of t where X_i(t) dominates X_j(t) from above.
                    if data_obj[(i, t)] >= data_ori[(j, t)] {
                        count += 1.0;
                    }
                }
                total += count / m as f64;
            }
            total / nori as f64
        })
        .collect();

    Ok(depths)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::depth::dispatch::{functional_depth, DepthMethod};
    use crate::matrix::FdMatrix;

    /// Build a sample of `n` sinusoidal curves on an `m`-point grid with a
    /// uniform vertical offset of 0.05 per curve index. Row 0 is the lowest
    /// curve, row n-1 is the highest.
    fn sample(n: usize, m: usize) -> FdMatrix {
        let mut col_major = vec![0.0; n * m];
        for i in 0..n {
            for t in 0..m {
                let x = t as f64 / (m as f64 - 1.0);
                // column-major: element (i, t) at index i + t*n
                col_major[i + t * n] = (x * std::f64::consts::PI).sin() + 0.05 * i as f64;
            }
        }
        FdMatrix::from_column_major(col_major, n, m).unwrap()
    }

    // --- HI tests ---

    #[test]
    fn hi_values_are_multiples_of_1_over_nori() {
        // HI is a global indicator — values must be k/nori for some non-negative integer k.
        let n = 8usize;
        let data = sample(n, 20);
        let hi = hypograph_index_1d(&data, &data).unwrap();
        assert_eq!(hi.len(), n);
        let inv = 1.0 / n as f64;
        for &v in &hi {
            assert!((0.0..=1.0 + 1e-12).contains(&v), "HI out of range: {v}");
            // v * n must be an integer within floating-point rounding.
            let k = v / inv;
            assert!(
                (k - k.round()).abs() < 1e-9,
                "HI value {v} is not a multiple of 1/{n}"
            );
        }
    }

    #[test]
    fn hi_highest_curve_scores_deepest() {
        // In the sample fixture, the curve with the largest offset (row n-1) has
        // all others at or below it, so it should get the maximum HI.
        let n = 8usize;
        let data = sample(n, 20);
        let hi = hypograph_index_1d(&data, &data).unwrap();
        let max_idx = hi
            .iter()
            .enumerate()
            .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
            .map(|(i, _)| i)
            .unwrap();
        assert_eq!(
            max_idx,
            n - 1,
            "Expected the highest curve (row {}) to have max HI",
            n - 1
        );
    }

    #[test]
    fn hi_empty_matrix_returns_err() {
        let empty = FdMatrix::from_column_major(vec![], 0, 0).unwrap();
        assert!(hypograph_index_1d(&empty, &empty).is_err());
    }

    #[test]
    fn hi_single_curve_returns_err() {
        let one = sample(1, 10);
        assert!(hypograph_index_1d(&one, &one).is_err());
    }

    // --- EI tests ---

    #[test]
    fn ei_values_are_multiples_of_1_over_nori() {
        let n = 8usize;
        let data = sample(n, 20);
        let ei = epigraph_index_1d(&data, &data).unwrap();
        assert_eq!(ei.len(), n);
        let inv = 1.0 / n as f64;
        for &v in &ei {
            assert!((0.0..=1.0 + 1e-12).contains(&v), "EI out of range: {v}");
            let k = v / inv;
            assert!(
                (k - k.round()).abs() < 1e-9,
                "EI value {v} is not a multiple of 1/{n}"
            );
        }
    }

    #[test]
    fn ei_lowest_curve_scores_deepest() {
        // In the sample fixture, row 0 is the lowest curve — all others are above it.
        let n = 8usize;
        let data = sample(n, 20);
        let ei = epigraph_index_1d(&data, &data).unwrap();
        let max_idx = ei
            .iter()
            .enumerate()
            .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
            .map(|(i, _)| i)
            .unwrap();
        assert_eq!(
            max_idx, 0,
            "Expected the lowest curve (row 0) to have max EI"
        );
    }

    #[test]
    fn ei_empty_matrix_returns_err() {
        let empty = FdMatrix::from_column_major(vec![], 0, 0).unwrap();
        assert!(epigraph_index_1d(&empty, &empty).is_err());
    }

    #[test]
    fn ei_single_curve_returns_err() {
        let one = sample(1, 10);
        assert!(epigraph_index_1d(&one, &one).is_err());
    }

    // --- MHI tests ---

    #[test]
    fn mhi_is_monotone_in_curve_height_and_central_near_half() {
        // MHI is a ONE-SIDED index, not a depth: it measures how much a curve
        // dominates the others from above. With n=9 vertically-stacked curves
        // (offsets 0..8), MHI increases monotonically with row height, so the
        // TOP curve (row 8) is the maximum and the central curve (row 4) ≈ 0.5.
        let n = 9usize;
        let data = sample(n, 30);
        let mhi = modified_hypograph_index_1d(&data, &data).unwrap();
        assert_eq!(mhi.len(), n);

        // Monotone non-decreasing in row index (higher curve dominates more).
        for i in 1..n {
            assert!(
                mhi[i] >= mhi[i - 1] - 1e-12,
                "MHI should be non-decreasing in curve height: row {i} = {}, row {} = {}",
                mhi[i],
                i - 1,
                mhi[i - 1]
            );
        }

        // The top curve is the maximum; the central curve sits near 0.5.
        let max_idx = mhi
            .iter()
            .enumerate()
            .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
            .map(|(i, _)| i)
            .unwrap();
        assert_eq!(
            max_idx,
            n - 1,
            "top curve should maximize MHI, got row {max_idx}"
        );
        let center = n / 2;
        assert!(
            (mhi[center] - 0.5).abs() < 0.1,
            "central curve MHI should be near 0.5, got {}",
            mhi[center]
        );
    }

    #[test]
    fn mhi_central_value_approx_half() {
        let n = 9usize;
        let data = sample(n, 30);
        let mhi = modified_hypograph_index_1d(&data, &data).unwrap();
        let center = n / 2;
        assert!(
            (mhi[center] - 0.5).abs() < 0.1,
            "MHI of central curve should be near 0.5, got {}",
            mhi[center]
        );
    }

    #[test]
    fn mhi_values_in_unit_interval() {
        let data = sample(8, 20);
        let mhi = modified_hypograph_index_1d(&data, &data).unwrap();
        for &v in &mhi {
            assert!((0.0..=1.0 + 1e-12).contains(&v), "MHI out of range: {v}");
        }
    }

    #[test]
    fn mhi_empty_matrix_returns_err() {
        let empty = FdMatrix::from_column_major(vec![], 0, 0).unwrap();
        assert!(modified_hypograph_index_1d(&empty, &empty).is_err());
    }

    // MHI allows nori=1 (no n<2 guard) — single curve is valid.
    #[test]
    fn mhi_single_curve_is_ok() {
        let one = sample(1, 10);
        let result = modified_hypograph_index_1d(&one, &one);
        assert!(
            result.is_ok(),
            "MHI with a single curve should be Ok (no n<2 guard)"
        );
        let depths = result.unwrap();
        assert_eq!(depths.len(), 1);
        // A single curve compared against itself: all t satisfy xi >= xi, so MHI = 1.0.
        assert!((depths[0] - 1.0).abs() < 1e-12);
    }

    // --- Dispatcher round-trip tests ---

    #[test]
    fn dispatcher_hypograph_index_round_trips() {
        let data = sample(6, 12);
        let got = functional_depth(&data, DepthMethod::HypographIndex).unwrap();
        let want = hypograph_index_1d(&data, &data).unwrap();
        assert_eq!(got, want);
        assert_eq!(got.len(), data.nrows());
    }

    #[test]
    fn dispatcher_epigraph_index_round_trips() {
        let data = sample(6, 12);
        let got = functional_depth(&data, DepthMethod::EpigraphIndex).unwrap();
        let want = epigraph_index_1d(&data, &data).unwrap();
        assert_eq!(got, want);
        assert_eq!(got.len(), data.nrows());
    }

    #[test]
    fn dispatcher_modified_hypograph_index_round_trips() {
        let data = sample(6, 12);
        let got = functional_depth(&data, DepthMethod::ModifiedHypographIndex).unwrap();
        let want = modified_hypograph_index_1d(&data, &data).unwrap();
        assert_eq!(got, want);
        assert_eq!(got.len(), data.nrows());
    }

    #[test]
    fn dispatcher_band_still_works_after_extension() {
        // Regression check: existing DepthMethod variants must still work.
        let data = sample(6, 12);
        let got = functional_depth(&data, DepthMethod::Band).unwrap();
        assert_eq!(got.len(), data.nrows());
        assert!(got.iter().all(|&d| (0.0..=1.0 + 1e-12).contains(&d)));
    }
}