Skip to main content

fdars_core/streaming_depth/
mod.rs

1//! Streaming / online depth computation for functional data.
2//!
3//! This module decouples reference-set construction from query evaluation,
4//! enabling efficient depth computation in streaming scenarios:
5//!
6//! - [`SortedReferenceState`] pre-sorts reference values per time point for O(log N) rank queries.
7//! - [`StreamingMbd`] uses a rank-based combinatorial identity to compute Modified Band Depth
8//!   in O(T log N) per query instead of O(N^2 T).
9//! - [`StreamingFraimanMuniz`] computes Fraiman-Muniz depth via binary search on sorted columns.
10//! - [`StreamingBd`] computes Band Depth with decoupled reference and early-exit optimisation.
11//! - [`RollingReference`] maintains a sliding window of reference curves with incremental
12//!   sorted-column updates.
13
14use crate::matrix::FdMatrix;
15
16pub mod bd;
17pub mod fraiman_muniz;
18pub mod mbd;
19pub mod rolling;
20pub mod sorted_ref;
21
22#[cfg(test)]
23mod tests;
24
25// Re-export all public types and traits
26pub use bd::{FullReferenceState, StreamingBd};
27pub use fraiman_muniz::StreamingFraimanMuniz;
28pub use mbd::StreamingMbd;
29pub use rolling::RollingReference;
30pub use sorted_ref::SortedReferenceState;
31
32// ---------------------------------------------------------------------------
33// Shared trait and helper
34// ---------------------------------------------------------------------------
35
36/// Trait for streaming depth estimators backed by a pre-built reference state.
37pub trait StreamingDepth {
38    /// Depth of a single curve given as a contiguous `&[f64]` of length `n_points`.
39    fn depth_one(&self, curve: &[f64]) -> f64;
40
41    /// Batch depth for a matrix of query curves (`nobj x n_points`).
42    fn depth_batch(&self, data_obj: &FdMatrix) -> Vec<f64>;
43
44    /// Number of evaluation points.
45    fn n_points(&self) -> usize;
46
47    /// Number of reference observations backing this estimator.
48    fn n_reference(&self) -> usize;
49}
50
51/// Helper: choose-2 combinator
52#[inline]
53pub(super) fn c2(k: usize) -> usize {
54    k * k.wrapping_sub(1) / 2
55}