llm-cost-ops 0.1.1

Core library for cost operations on LLM deployments
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// Anomaly detection for cost forecasting

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use super::{
    types::{DataPoint, TimeSeriesData},
    ForecastError, ForecastResult,
};

/// Anomaly detection method
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AnomalyMethod {
    /// Z-Score method (standard deviations from mean)
    ZScore,

    /// Interquartile Range (IQR) method
    Iqr,

    /// Moving Average method
    MovingAverage,

    /// Modified Z-Score (using median absolute deviation)
    ModifiedZScore,
}

/// Anomaly severity level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AnomalySeverity {
    Low,
    Medium,
    High,
    Critical,
}

/// Detected anomaly
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Anomaly {
    /// Index of the anomalous data point
    pub index: usize,

    /// The anomalous data point
    pub point: DataPoint,

    /// Anomaly score (how anomalous it is)
    pub score: f64,

    /// Severity level
    pub severity: AnomalySeverity,

    /// Method used for detection
    pub method: AnomalyMethod,

    /// Additional context
    pub context: HashMap<String, String>,
}

/// Anomaly detection result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnomalyResult {
    /// Detected anomalies
    pub anomalies: Vec<Anomaly>,

    /// Total number of data points analyzed
    pub total_points: usize,

    /// Anomaly rate (percentage)
    pub anomaly_rate: f64,

    /// Detection method used
    pub method: AnomalyMethod,

    /// Threshold used for detection
    pub threshold: f64,
}

/// Anomaly detector configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnomalyConfig {
    /// Detection method
    pub method: AnomalyMethod,

    /// Sensitivity threshold (higher = more sensitive)
    pub sensitivity: f64,

    /// Minimum number of data points required
    pub min_data_points: usize,

    /// Window size for moving average method
    pub window_size: usize,
}

impl Default for AnomalyConfig {
    fn default() -> Self {
        Self {
            method: AnomalyMethod::ZScore,
            sensitivity: 3.0, // 3 standard deviations
            min_data_points: 10,
            window_size: 7,
        }
    }
}

/// Anomaly detector
pub struct AnomalyDetector {
    config: AnomalyConfig,
}

impl AnomalyDetector {
    /// Create a new anomaly detector
    pub fn new(config: AnomalyConfig) -> Self {
        Self { config }
    }

    /// Create with default configuration
    pub fn with_defaults() -> Self {
        Self {
            config: AnomalyConfig::default(),
        }
    }

    /// Detect anomalies in time series data
    pub fn detect(&self, data: &TimeSeriesData) -> ForecastResult<AnomalyResult> {
        if data.len() < self.config.min_data_points {
            return Err(ForecastError::InsufficientData(format!(
                "Anomaly detection requires at least {} data points",
                self.config.min_data_points
            )));
        }

        let anomalies = match self.config.method {
            AnomalyMethod::ZScore => self.detect_zscore(data)?,
            AnomalyMethod::Iqr => self.detect_iqr(data)?,
            AnomalyMethod::MovingAverage => self.detect_moving_average(data)?,
            AnomalyMethod::ModifiedZScore => self.detect_modified_zscore(data)?,
        };

        let anomaly_rate = if !data.is_empty() {
            (anomalies.len() as f64 / data.len() as f64) * 100.0
        } else {
            0.0
        };

        Ok(AnomalyResult {
            anomalies,
            total_points: data.len(),
            anomaly_rate,
            method: self.config.method,
            threshold: self.config.sensitivity,
        })
    }

    /// Z-Score anomaly detection
    fn detect_zscore(&self, data: &TimeSeriesData) -> ForecastResult<Vec<Anomaly>> {
        let values = data.values_f64();
        let mean = values.iter().sum::<f64>() / values.len() as f64;

        let variance = values
            .iter()
            .map(|v| (v - mean).powi(2))
            .sum::<f64>() / values.len() as f64;

        let std_dev = variance.sqrt();

        if std_dev < f64::EPSILON {
            return Ok(Vec::new()); // No variation, no anomalies
        }

        let mut anomalies = Vec::new();

        for (i, point) in data.points.iter().enumerate() {
            let value = values[i];
            let z_score = ((value - mean) / std_dev).abs();

            if z_score > self.config.sensitivity {
                let severity = self.calculate_severity(z_score, self.config.sensitivity);

                let mut context = HashMap::new();
                context.insert("mean".to_string(), format!("{:.2}", mean));
                context.insert("std_dev".to_string(), format!("{:.2}", std_dev));
                context.insert("z_score".to_string(), format!("{:.2}", z_score));

                anomalies.push(Anomaly {
                    index: i,
                    point: point.clone(),
                    score: z_score,
                    severity,
                    method: AnomalyMethod::ZScore,
                    context,
                });
            }
        }

        Ok(anomalies)
    }

    /// IQR (Interquartile Range) anomaly detection
    fn detect_iqr(&self, data: &TimeSeriesData) -> ForecastResult<Vec<Anomaly>> {
        let mut values = data.values_f64();
        values.sort_by(|a, b| a.partial_cmp(b).unwrap());

        let q1_idx = values.len() / 4;
        let q3_idx = (values.len() * 3) / 4;

        let q1 = values[q1_idx];
        let q3 = values[q3_idx];
        let iqr = q3 - q1;

        if iqr < f64::EPSILON {
            return Ok(Vec::new()); // No variation
        }

        let lower_bound = q1 - self.config.sensitivity * iqr;
        let upper_bound = q3 + self.config.sensitivity * iqr;

        let mut anomalies = Vec::new();
        let original_values = data.values_f64();

        for (i, point) in data.points.iter().enumerate() {
            let value = original_values[i];

            if value < lower_bound || value > upper_bound {
                let distance_from_bound = if value < lower_bound {
                    lower_bound - value
                } else {
                    value - upper_bound
                };

                let score = distance_from_bound / iqr;
                let severity = self.calculate_severity(score, self.config.sensitivity);

                let mut context = HashMap::new();
                context.insert("q1".to_string(), format!("{:.2}", q1));
                context.insert("q3".to_string(), format!("{:.2}", q3));
                context.insert("iqr".to_string(), format!("{:.2}", iqr));
                context.insert("lower_bound".to_string(), format!("{:.2}", lower_bound));
                context.insert("upper_bound".to_string(), format!("{:.2}", upper_bound));

                anomalies.push(Anomaly {
                    index: i,
                    point: point.clone(),
                    score,
                    severity,
                    method: AnomalyMethod::Iqr,
                    context,
                });
            }
        }

        Ok(anomalies)
    }

    /// Moving Average anomaly detection
    fn detect_moving_average(&self, data: &TimeSeriesData) -> ForecastResult<Vec<Anomaly>> {
        let values = data.values_f64();
        let window_size = self.config.window_size.min(data.len() / 2);

        if window_size < 2 {
            return Err(ForecastError::InvalidConfig(
                "Window size too small for moving average".to_string(),
            ));
        }

        let mut anomalies = Vec::new();

        for (i, point) in data.points.iter().enumerate() {
            // Skip first window_size points
            if i < window_size {
                continue;
            }

            // Calculate moving average and std dev for window
            let window_start = i.saturating_sub(window_size);
            let window = &values[window_start..i];

            let window_mean = window.iter().sum::<f64>() / window.len() as f64;
            let window_variance = window
                .iter()
                .map(|v| (v - window_mean).powi(2))
                .sum::<f64>() / window.len() as f64;
            let window_std = window_variance.sqrt();

            if window_std < f64::EPSILON {
                continue; // No variation in window
            }

            let current_value = values[i];
            let deviation = ((current_value - window_mean) / window_std).abs();

            if deviation > self.config.sensitivity {
                let severity = self.calculate_severity(deviation, self.config.sensitivity);

                let mut context = HashMap::new();
                context.insert("window_mean".to_string(), format!("{:.2}", window_mean));
                context.insert("window_std".to_string(), format!("{:.2}", window_std));
                context.insert("deviation".to_string(), format!("{:.2}", deviation));

                anomalies.push(Anomaly {
                    index: i,
                    point: point.clone(),
                    score: deviation,
                    severity,
                    method: AnomalyMethod::MovingAverage,
                    context,
                });
            }
        }

        Ok(anomalies)
    }

    /// Modified Z-Score using Median Absolute Deviation
    fn detect_modified_zscore(&self, data: &TimeSeriesData) -> ForecastResult<Vec<Anomaly>> {
        let mut values = data.values_f64();
        let original_values = values.clone();

        // Calculate median
        values.sort_by(|a, b| a.partial_cmp(b).unwrap());
        let median = if values.len().is_multiple_of(2) {
            (values[values.len() / 2 - 1] + values[values.len() / 2]) / 2.0
        } else {
            values[values.len() / 2]
        };

        // Calculate MAD (Median Absolute Deviation)
        let mut deviations: Vec<f64> = original_values
            .iter()
            .map(|v| (v - median).abs())
            .collect();

        deviations.sort_by(|a, b| a.partial_cmp(b).unwrap());
        let mad = if deviations.len().is_multiple_of(2) {
            (deviations[deviations.len() / 2 - 1] + deviations[deviations.len() / 2]) / 2.0
        } else {
            deviations[deviations.len() / 2]
        };

        if mad < f64::EPSILON {
            return Ok(Vec::new()); // No variation
        }

        let mut anomalies = Vec::new();

        for (i, point) in data.points.iter().enumerate() {
            let value = original_values[i];
            // Modified Z-score = 0.6745 * (x - median) / MAD
            let modified_z = 0.6745 * ((value - median) / mad).abs();

            if modified_z > self.config.sensitivity {
                let severity = self.calculate_severity(modified_z, self.config.sensitivity);

                let mut context = HashMap::new();
                context.insert("median".to_string(), format!("{:.2}", median));
                context.insert("mad".to_string(), format!("{:.2}", mad));
                context.insert("modified_z".to_string(), format!("{:.2}", modified_z));

                anomalies.push(Anomaly {
                    index: i,
                    point: point.clone(),
                    score: modified_z,
                    severity,
                    method: AnomalyMethod::ModifiedZScore,
                    context,
                });
            }
        }

        Ok(anomalies)
    }

    /// Calculate severity based on score
    fn calculate_severity(&self, score: f64, threshold: f64) -> AnomalySeverity {
        let ratio = score / threshold;

        if ratio >= 2.0 {
            AnomalySeverity::Critical
        } else if ratio >= 1.5 {
            AnomalySeverity::High
        } else if ratio >= 1.2 {
            AnomalySeverity::Medium
        } else {
            AnomalySeverity::Low
        }
    }
}

impl Default for AnomalyDetector {
    fn default() -> Self {
        Self::with_defaults()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::{Duration, Utc};
    use rust_decimal::Decimal;

    fn create_test_series(values: Vec<i32>) -> TimeSeriesData {
        let start = Utc::now();
        let points: Vec<DataPoint> = values
            .into_iter()
            .enumerate()
            .map(|(i, v)| {
                DataPoint::new(start + Duration::hours(i as i64), Decimal::from(v))
            })
            .collect();

        TimeSeriesData::with_auto_interval(points)
    }

    #[test]
    fn test_detector_creation() {
        let detector = AnomalyDetector::with_defaults();
        assert_eq!(detector.config.method, AnomalyMethod::ZScore);
        assert_eq!(detector.config.sensitivity, 3.0);
    }

    #[test]
    fn test_zscore_detection() {
        let detector = AnomalyDetector::with_defaults();

        // Data with clear outliers - more data points help z-score detection
        // Normal range around 10-13, then a spike at 100
        let data = create_test_series(vec![
            10, 12, 11, 13, 10, 12, 11, 13, 10, 12,  // 10 normal points
            11, 13, 10, 12, 100, 11, 13, 10, 12, 11, // outlier at index 14
        ]);

        let result = detector.detect(&data).unwrap();

        assert!(result.anomalies.len() > 0);
        assert!(result.anomaly_rate > 0.0);
        assert_eq!(result.method, AnomalyMethod::ZScore);

        // The outlier (100) should be detected
        let outlier_detected = result.anomalies.iter().any(|a| a.point.value == Decimal::from(100));
        assert!(outlier_detected);
    }

    #[test]
    fn test_iqr_detection() {
        let mut config = AnomalyConfig::default();
        config.method = AnomalyMethod::Iqr;
        config.sensitivity = 1.5;

        let detector = AnomalyDetector::new(config);
        let data = create_test_series(vec![10, 12, 11, 13, 12, 150, 11, 10, 12, 11]);

        let result = detector.detect(&data).unwrap();

        assert!(result.anomalies.len() > 0);
        assert_eq!(result.method, AnomalyMethod::Iqr);

        // The outlier (150) should be detected
        let outlier_detected = result.anomalies.iter().any(|a| a.point.value == Decimal::from(150));
        assert!(outlier_detected);
    }

    #[test]
    fn test_moving_average_detection() {
        let mut config = AnomalyConfig::default();
        config.method = AnomalyMethod::MovingAverage;
        config.window_size = 3;
        config.sensitivity = 3.0;

        let detector = AnomalyDetector::new(config);
        let data = create_test_series(vec![10, 12, 11, 13, 12, 11, 100, 10, 12, 11, 13]);

        let result = detector.detect(&data).unwrap();

        assert!(result.anomalies.len() > 0);
        assert_eq!(result.method, AnomalyMethod::MovingAverage);
    }

    #[test]
    fn test_modified_zscore_detection() {
        let mut config = AnomalyConfig::default();
        config.method = AnomalyMethod::ModifiedZScore;
        config.sensitivity = 3.5;

        let detector = AnomalyDetector::new(config);
        let data = create_test_series(vec![10, 12, 11, 13, 12, 200, 11, 10, 12, 11]);

        let result = detector.detect(&data).unwrap();

        assert!(result.anomalies.len() > 0);
        assert_eq!(result.method, AnomalyMethod::ModifiedZScore);

        // The outlier (200) should be detected
        let outlier_detected = result.anomalies.iter().any(|a| a.point.value == Decimal::from(200));
        assert!(outlier_detected);
    }

    #[test]
    fn test_no_anomalies() {
        let detector = AnomalyDetector::with_defaults();

        // Stable data with no outliers
        let data = create_test_series(vec![10, 11, 12, 11, 10, 12, 11, 10, 11, 12]);

        let result = detector.detect(&data).unwrap();

        assert_eq!(result.anomalies.len(), 0);
        assert_eq!(result.anomaly_rate, 0.0);
    }

    #[test]
    fn test_insufficient_data() {
        let detector = AnomalyDetector::with_defaults();
        let data = create_test_series(vec![10, 12, 11]); // Less than min_data_points

        let result = detector.detect(&data);
        assert!(result.is_err());
    }

    #[test]
    fn test_severity_calculation() {
        let detector = AnomalyDetector::with_defaults();

        assert_eq!(
            detector.calculate_severity(6.0, 3.0),
            AnomalySeverity::Critical
        );
        assert_eq!(
            detector.calculate_severity(5.0, 3.0),
            AnomalySeverity::High
        );
        assert_eq!(
            detector.calculate_severity(4.0, 3.0),
            AnomalySeverity::Medium
        );
        assert_eq!(
            detector.calculate_severity(3.5, 3.0),
            AnomalySeverity::Low
        );
    }

    #[test]
    fn test_anomaly_context() {
        let detector = AnomalyDetector::with_defaults();
        let data = create_test_series(vec![10, 12, 11, 13, 100, 12, 11, 10, 12, 11]);

        let result = detector.detect(&data).unwrap();

        if let Some(anomaly) = result.anomalies.first() {
            assert!(anomaly.context.contains_key("mean"));
            assert!(anomaly.context.contains_key("std_dev"));
            assert!(anomaly.context.contains_key("z_score"));
        }
    }

    #[test]
    fn test_different_sensitivities() {
        let mut config = AnomalyConfig::default();
        let data = create_test_series(vec![10, 12, 11, 13, 25, 12, 11, 10, 12, 11]);

        // High sensitivity (lower threshold)
        config.sensitivity = 2.0;
        let detector = AnomalyDetector::new(config.clone());
        let result_high = detector.detect(&data).unwrap();

        // Low sensitivity (higher threshold)
        config.sensitivity = 4.0;
        let detector = AnomalyDetector::new(config);
        let result_low = detector.detect(&data).unwrap();

        // High sensitivity should detect more anomalies
        assert!(result_high.anomalies.len() >= result_low.anomalies.len());
    }
}