nexus_stats/lib.rs
1#![cfg_attr(not(feature = "std"), no_std)]
2#![warn(missing_docs)]
3
4//! Fixed-memory, zero-allocation streaming statistics for real-time systems.
5//!
6//! 60+ algorithms, all O(1) per update (or O(d) for d-dimensional filters), fixed memory.
7//! Core types are `no_std` compatible; types marked *(std)* require the `std` feature,
8//! *(alloc)* require `alloc`, and *(std|libm)* require either `std` or `libm`.
9//!
10//! # Usage
11//!
12//! Import from the category module you need:
13//!
14//! ```rust
15//! use nexus_stats::smoothing::EmaF64;
16//! use nexus_stats::detection::CusumF64;
17//! use nexus_stats::statistics::WelfordF64;
18//! use nexus_stats::{DataError, ConfigError, Direction};
19//! ```
20//!
21//! With the `full` feature (or individual subcrate features), advanced types
22//! are available through the same module paths:
23//!
24//! ```rust,ignore
25//! // Requires `smoothing` feature
26//! use nexus_stats::smoothing::KamaF64;
27//! // Requires `regression` feature
28//! use nexus_stats::regression::LinearRegressionF64;
29//! // Requires `detection` feature
30//! use nexus_stats::signal::AutocorrelationF64;
31//! ```
32//!
33//! # Data Quality & Error Policy
34//!
35//! nexus-stats distinguishes two failure categories:
36//!
37//! **Data errors** — NaN or Inf values reaching a streaming update.
38//! These indicate upstream data quality problems (broken feeds, failed
39//! computations, missing values). All update methods that accept float
40//! inputs return `Result<_, DataError>`. The library rejects the input
41//! and leaves internal state unchanged. The caller declares the policy:
42//!
43//! - `.unwrap()` to crash on bad data (testing, strict systems)
44//! - Log and continue (monitoring, degraded-mode operation)
45//! - Increment a counter and trigger a circuit breaker (production)
46//!
47//! **Programmer errors** — wrong dimensions, out-of-range indices,
48//! type misuse. These are bugs in the calling code. The library panics
49//! via `assert!`. Fix the code.
50//!
51//! ## Internal State Invariant
52//!
53//! Given only finite (non-NaN, non-Inf) inputs, all internal
54//! accumulators remain finite for typical workloads. Extreme value
55//! ranges (>1e150) or very long-running instances (billions of updates)
56//! can cause internal accumulator overflow through summation. For
57//! long-running systems: call `reset()` periodically, use
58//! exponentially-weighted variants (EW*) which naturally bound growth,
59//! or use `.max_covariance()` on RLS filters to auto-reset when the
60//! covariance matrix diverges.
61//!
62//! # Categories
63//!
64//! ## Core (always available)
65//!
66//! | Module | Contents |
67//! |--------|----------|
68//! | [`smoothing`] | EMA, AsymEma, Slew |
69//! | [`detection`] | CUSUM |
70//! | [`statistics`] | Welford, Moments, EwmaVar, Covariance, HarmonicMean, Percentile, BipowerVariation, RollSpread *(std\|libm)*, TwoScaleRv *(alloc, std\|libm)* |
71//! | [`monitoring`] | Drawdown, Windowed Min/Max, CoDel, Liveness, EventRate, Jitter, ErrorRate, Saturation, HawkesIntensity *(std\|libm)* |
72//! | [`control`] | DeadBand, Hysteresis, Debounce, LevelCrossing, Diff |
73//!
74//! ## Advanced (feature-gated, re-exported from subcrates)
75//!
76//! | Feature | Module | Contents |
77//! |---------|--------|----------|
78//! | `smoothing` | `smoothing` | + Holt, KAMA, Spring, Kalman1d, WindowedMedian |
79//! | `detection` | `detection` | + MOSUM, Shiryaev-Roberts, AdaptiveThreshold, RobustZ, TrendAlert, MultiGate, PageHinkley, ADWIN |
80//! | `detection` | `signal` | Autocorrelation, CrossCorrelation, Entropy, TransferEntropy, PredictiveInfoBound |
81//! | `detection` | `estimation` | + SPRT |
82//! | `regression` | `regression` | Linear, Polynomial, EW variants, Transformed, LogisticRegression |
83//! | `regression` | `learning` | LMS, NLMS, RLS, OnlineKMeans, GD, AdaGrad, Adam, UCB1, ThompsonBeta, ThompsonGamma, EpsilonGreedy, EXP3 |
84//! | `regression` | `estimation` | + Kalman 2d/3d, BetaBinomial, GammaPoisson |
85//! | `control` | `control` | + PeakDetector, BoolWindow |
86//! | `control` | `frequency` | TopK, FlexProportion, DecayAccum |
87//! | `full` | all | Everything above |
88//!
89//! # Features
90//!
91//! | Feature | Default | Enables |
92//! |---------|---------|---------|
93//! | `std` | yes | `WallClock`, `sqrt`/`exp` intrinsics |
94//! | `alloc` | with `std` | MOSUM, KAMA, WindowedMedian, BoolWindow, adaptive filters, optimizers |
95//! | `libm` | no | Pure Rust `sqrt`/`exp` fallback for `no_std` (enables Shiryaev-Roberts, etc.) |
96//! | `smoothing` | no | Advanced smoothing types (Holt, KAMA, Spring, Kalman1d, WindowedMedian) |
97//! | `detection` | no | Advanced detection + signal analysis (implies `smoothing`) |
98//! | `regression` | no | Regression, learning, estimation types |
99//! | `control` | no | Advanced control + frequency types |
100//! | `full` | no | All subcrates |
101
102// Re-export core types at crate root
103pub use nexus_stats_core::{Condition, ConfigError, DataError, Direction};
104
105/// Clock trait and implementations for time-aware stats types.
106pub use nexus_stats_core::clock;
107
108// Re-export the math module (doc-hidden, for subcrate use)
109#[doc(hidden)]
110pub use nexus_stats_core::math;
111
112// Re-export the feature_vector macro
113pub use nexus_stats_core::feature_vector;
114
115// ---- Core modules (always available, from nexus-stats-core) ----
116
117/// Smoothing and filtering primitives.
118///
119/// Core types (EMA, AsymEma, Slew) are always available. Advanced types
120/// (Holt, KAMA, Spring, Kalman1d, WindowedMedian) require the `smoothing`
121/// feature.
122pub mod smoothing {
123 pub use nexus_stats_core::smoothing::*;
124
125 #[cfg(feature = "smoothing")]
126 pub use nexus_stats_smoothing::*;
127}
128
129/// Change detection and anomaly detection.
130///
131/// CUSUM is always available. Advanced types (MOSUM, Shiryaev-Roberts,
132/// AdaptiveThreshold, RobustZ, TrendAlert, MultiGate) require the
133/// `detection` feature.
134pub mod detection {
135 pub use nexus_stats_core::detection::*;
136
137 #[cfg(feature = "detection")]
138 pub use nexus_stats_detection::detection::*;
139}
140
141/// Online feature normalization.
142pub use nexus_stats_core::normalization;
143
144/// Core streaming statistics.
145pub use nexus_stats_core::statistics;
146
147/// Monitoring and health tracking.
148pub use nexus_stats_core::monitoring;
149
150/// Control, thresholding, and differencing.
151///
152/// Core types (DeadBand, Hysteresis, Debounce, LevelCrossing, Diff) are
153/// always available. PeakDetector and BoolWindow require the `control` feature.
154pub mod control {
155 pub use nexus_stats_core::control::*;
156
157 #[cfg(feature = "control")]
158 pub use nexus_stats_control::control::*;
159}
160
161// ---- Advanced modules (from subcrates, feature-gated) ----
162
163/// Signal analysis and information theory.
164///
165/// Requires the `detection` feature.
166#[cfg(feature = "detection")]
167pub mod signal {
168 pub use nexus_stats_detection::signal::*;
169}
170
171/// State estimation, Bayesian inference, and hypothesis testing.
172///
173/// Kalman1d is available via [`smoothing`] with the `smoothing` feature.
174/// Kalman2d/3d, BetaBinomial, GammaPoisson require the `regression` feature.
175/// SPRT requires the `detection` feature.
176#[cfg(any(feature = "regression", feature = "detection"))]
177pub mod estimation {
178 #[cfg(feature = "detection")]
179 pub use nexus_stats_detection::estimation::*;
180 #[cfg(feature = "regression")]
181 pub use nexus_stats_regression::estimation::*;
182}
183
184/// Regression and classification.
185///
186/// Requires the `regression` feature.
187#[cfg(feature = "regression")]
188pub mod regression {
189 pub use nexus_stats_regression::regression::*;
190}
191
192/// Adaptive filters, online learning, and optimization.
193///
194/// Requires the `regression` feature.
195#[cfg(feature = "regression")]
196pub mod learning {
197 pub use nexus_stats_regression::learning::*;
198}
199
200/// Frequency counting and scoring.
201///
202/// Requires the `control` feature.
203#[cfg(feature = "control")]
204pub mod frequency {
205 pub use nexus_stats_control::frequency::*;
206}