Skip to main content

fdars_core/depth/
random_projection.rs

1//! Random projection depth measures.
2
3use crate::matrix::FdMatrix;
4
5use super::random_depth_core;
6
7/// Compute random projection depth for 1D functional data.
8///
9/// Projects curves to scalars using random projections and computes
10/// average univariate depth.
11///
12/// # Arguments
13/// * `data_obj` - Data to compute depth for
14/// * `data_ori` - Reference data
15/// * `nproj` - Number of random projections
16///
17/// # Examples
18///
19/// ```
20/// use fdars_core::matrix::FdMatrix;
21/// use fdars_core::depth::random_projection_1d;
22///
23/// let data = FdMatrix::from_column_major(
24///     (0..50).map(|i| (i as f64 * 0.1).sin()).collect(),
25///     5, 10,
26/// ).unwrap();
27/// let depths = random_projection_1d(&data, &data, 50);
28/// assert_eq!(depths.len(), 5);
29/// assert!(depths.iter().all(|&d| d >= 0.0));
30/// ```
31#[must_use = "expensive computation whose result should not be discarded"]
32pub fn random_projection_1d(data_obj: &FdMatrix, data_ori: &FdMatrix, nproj: usize) -> Vec<f64> {
33    random_projection_1d_seeded(data_obj, data_ori, nproj, None)
34}
35
36/// Compute random projection depth with optional seed for reproducibility.
37#[must_use = "expensive computation whose result should not be discarded"]
38pub fn random_projection_1d_seeded(
39    data_obj: &FdMatrix,
40    data_ori: &FdMatrix,
41    nproj: usize,
42    seed: Option<u64>,
43) -> Vec<f64> {
44    random_depth_core(
45        data_obj,
46        data_ori,
47        nproj,
48        seed,
49        0.0,
50        |acc, d| acc + d,
51        |acc, n| acc / n as f64,
52    )
53}
54
55/// Compute random projection depth for 2D functional data.
56#[must_use = "expensive computation whose result should not be discarded"]
57pub fn random_projection_2d(data_obj: &FdMatrix, data_ori: &FdMatrix, nproj: usize) -> Vec<f64> {
58    random_projection_1d(data_obj, data_ori, nproj)
59}