pub struct BaselineTracker { /* private fields */ }Expand description
Sliding window statistics tracker for anomaly detection.
Tracks multiple metrics, each with its own sliding window of values. Computes mean and standard deviation for z-score based anomaly detection.
Implementations§
Source§impl BaselineTracker
impl BaselineTracker
Sourcepub fn new(window_size: usize) -> Self
pub fn new(window_size: usize) -> Self
Create a new baseline tracker with specified window size.
Sourcepub fn update(&mut self, metric: &str, value: f64)
pub fn update(&mut self, metric: &str, value: f64)
Update a metric with a new value.
Adds the value to the sliding window, evicting the oldest value if the window is full.
Sourcepub fn update_batch(&mut self, metric: &str, values: &[f64])
pub fn update_batch(&mut self, metric: &str, values: &[f64])
Update a metric with multiple values at once.
Sourcepub fn get_baseline(&self, metric: &str) -> Baseline
pub fn get_baseline(&self, metric: &str) -> Baseline
Get baseline statistics for a metric.
Returns mean=0, std=0, n=0 if the metric has no data.
Sourcepub fn z_score(&self, metric: &str, value: f64) -> f64
pub fn z_score(&self, metric: &str, value: f64) -> f64
Calculate z-score for a value given the metric’s baseline.
Returns 0.0 if there’s insufficient data or std=0.
Sourcepub fn is_anomaly(
&self,
metric: &str,
value: f64,
sigma_threshold: f64,
min_samples: usize,
) -> bool
pub fn is_anomaly( &self, metric: &str, value: f64, sigma_threshold: f64, min_samples: usize, ) -> bool
Check if a value is an anomaly for the given metric.
§Arguments
metric- The metric namevalue- The value to checksigma_threshold- Number of standard deviations for anomaly (e.g., 2.0)min_samples- Minimum number of samples required to detect anomalies
Returns false if there’s insufficient data to make a determination.
Sourcepub fn is_high_anomaly(
&self,
metric: &str,
value: f64,
sigma_threshold: f64,
min_samples: usize,
) -> bool
pub fn is_high_anomaly( &self, metric: &str, value: f64, sigma_threshold: f64, min_samples: usize, ) -> bool
Check if a value is anomalously high.
Sourcepub fn is_low_anomaly(
&self,
metric: &str,
value: f64,
sigma_threshold: f64,
min_samples: usize,
) -> bool
pub fn is_low_anomaly( &self, metric: &str, value: f64, sigma_threshold: f64, min_samples: usize, ) -> bool
Check if a value is anomalously low.
Sourcepub fn sample_count(&self, metric: &str) -> usize
pub fn sample_count(&self, metric: &str) -> usize
Get the number of samples for a metric.
Sourcepub fn clear_metric(&mut self, metric: &str)
pub fn clear_metric(&mut self, metric: &str)
Clear all data for a metric.
Sourcepub fn window_size(&self) -> usize
pub fn window_size(&self) -> usize
Get the window size.
Sourcepub fn get_values(&self, metric: &str) -> Vec<f64>
pub fn get_values(&self, metric: &str) -> Vec<f64>
Get recent values for a metric (returns a copy).
Sourcepub fn last_value(&self, metric: &str) -> Option<f64>
pub fn last_value(&self, metric: &str) -> Option<f64>
Get the most recent value for a metric.
Sourcepub fn percentile(&self, metric: &str, percentile: f64) -> Option<f64>
pub fn percentile(&self, metric: &str, percentile: f64) -> Option<f64>
Calculate percentile for a metric.
§Arguments
metric- The metric namepercentile- The percentile (0.0 to 1.0, e.g., 0.95 for 95th percentile)
Sourcepub fn to_signal(
&self,
metric: &str,
value: f64,
sigma_threshold: f64,
) -> Option<InteroceptiveSignal>
pub fn to_signal( &self, metric: &str, value: f64, sigma_threshold: f64, ) -> Option<InteroceptiveSignal>
Convert the current state of a metric into an [InteroceptiveSignal].
valence: negated normalized z-score (deviation = negative feeling).arousal: absolute z-score divided by threshold, clamped to [0, 1].
Returns None if the metric has no data.
Trait Implementations§
Source§impl Clone for BaselineTracker
impl Clone for BaselineTracker
Source§fn clone(&self) -> BaselineTracker
fn clone(&self) -> BaselineTracker
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more