kaccy-core 0.2.0

Core business logic for Kaccy Protocol - batching, fee optimization, and transaction management
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
//! Anomaly detection for unusual trading patterns and price movements
//!
//! This module provides statistical and ML-based anomaly detection techniques
//! for identifying unusual patterns, outliers, and suspicious activities.

use super::features::PricePoint;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;

/// Anomaly detection result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Anomaly {
    /// Timestamp when the anomaly was detected
    pub timestamp: DateTime<Utc>,
    /// Classification of the anomaly
    pub anomaly_type: AnomalyType,
    /// Severity level of the anomaly
    pub severity: AnomalySeverity,
    /// Numeric anomaly score (higher = more anomalous)
    pub score: f64,
    /// Human-readable description of the anomaly
    pub description: String,
    /// The observed value that triggered the anomaly
    pub value: f64,
    /// The expected range for the observed value
    pub expected_range: (f64, f64),
}

/// Type of anomaly detected
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AnomalyType {
    /// Abnormal upward price movement
    PriceSpike,
    /// Abnormal downward price movement
    PriceDrop,
    /// Abnormal upward volume movement
    VolumeSpike,
    /// Abnormal downward volume movement
    VolumeDrop,
    /// Excessive price volatility
    Volatility,
    /// Suspicious trading pattern
    Pattern,
}

/// Severity level of anomaly
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum AnomalySeverity {
    /// Minor deviation, informational only
    Low,
    /// Noteworthy deviation, warrants monitoring
    Medium,
    /// Significant deviation, warrants investigation
    High,
    /// Extreme deviation, immediate action may be required
    Critical,
}

impl AnomalySeverity {
    /// Derive severity from a numeric anomaly score
    pub fn from_score(score: f64) -> Self {
        if score >= 4.0 {
            Self::Critical
        } else if score >= 3.0 {
            Self::High
        } else if score >= 2.0 {
            Self::Medium
        } else {
            Self::Low
        }
    }
}

/// Anomaly detector trait
pub trait AnomalyDetector: Send + Sync {
    /// Detect anomalies in the data
    fn detect(&mut self, data: &[PricePoint]) -> anyhow::Result<Vec<Anomaly>>;

    /// Real-time anomaly detection for streaming data
    fn detect_realtime(&mut self, point: &PricePoint) -> anyhow::Result<Option<Anomaly>>;

    /// Reset detector state
    fn reset(&mut self);

    /// Detector name
    fn name(&self) -> &str;
}

/// Z-score based anomaly detector
#[derive(Debug, Clone)]
pub struct ZScoreDetector {
    /// Number of past data points used to compute mean and std dev
    window_size: usize,
    /// Z-score threshold above which a point is flagged as anomalous
    threshold: f64,
    /// Sliding window of recent price values
    price_history: VecDeque<f64>,
    /// Sliding window of recent volume values
    volume_history: VecDeque<f64>,
}

impl ZScoreDetector {
    /// Create a new Z-score detector with the given window and threshold
    pub fn new(window_size: usize, threshold: f64) -> Self {
        Self {
            window_size,
            threshold,
            price_history: VecDeque::new(),
            volume_history: VecDeque::new(),
        }
    }

    fn calculate_z_score(&self, value: f64, history: &VecDeque<f64>) -> f64 {
        if history.len() < 2 {
            return 0.0;
        }

        let n = history.len() as f64;
        let mean = history.iter().sum::<f64>() / n;
        let variance = history.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / n;
        let std_dev = variance.sqrt();

        if std_dev < 1e-10 {
            return 0.0;
        }

        (value - mean) / std_dev
    }
}

impl Default for ZScoreDetector {
    fn default() -> Self {
        Self::new(30, 3.0)
    }
}

impl AnomalyDetector for ZScoreDetector {
    fn detect(&mut self, data: &[PricePoint]) -> anyhow::Result<Vec<Anomaly>> {
        let mut anomalies = Vec::new();

        for point in data {
            if let Some(anomaly) = self.detect_realtime(point)? {
                anomalies.push(anomaly);
            }
        }

        Ok(anomalies)
    }

    fn detect_realtime(&mut self, point: &PricePoint) -> anyhow::Result<Option<Anomaly>> {
        let price = point.close.to_string().parse::<f64>().unwrap_or(0.0);
        let volume = point.volume.to_string().parse::<f64>().unwrap_or(0.0);

        let mut anomaly = None;

        // Check price anomaly
        if self.price_history.len() >= self.window_size {
            let z_score = self.calculate_z_score(price, &self.price_history);

            if z_score.abs() > self.threshold {
                let mean = self.price_history.iter().sum::<f64>() / self.price_history.len() as f64;
                let std = {
                    let variance = self
                        .price_history
                        .iter()
                        .map(|&x| (x - mean).powi(2))
                        .sum::<f64>()
                        / self.price_history.len() as f64;
                    variance.sqrt()
                };

                let anomaly_type = if z_score > 0.0 {
                    AnomalyType::PriceSpike
                } else {
                    AnomalyType::PriceDrop
                };

                anomaly = Some(Anomaly {
                    timestamp: point.timestamp,
                    anomaly_type,
                    severity: AnomalySeverity::from_score(z_score.abs()),
                    score: z_score.abs(),
                    description: format!(
                        "{} detected: price {} is {} standard deviations from mean",
                        if z_score > 0.0 { "Spike" } else { "Drop" },
                        price,
                        z_score.abs()
                    ),
                    value: price,
                    expected_range: (mean - self.threshold * std, mean + self.threshold * std),
                });
            }
        }

        // Check volume anomaly
        if anomaly.is_none() && self.volume_history.len() >= self.window_size {
            let z_score = self.calculate_z_score(volume, &self.volume_history);

            if z_score.abs() > self.threshold {
                let mean =
                    self.volume_history.iter().sum::<f64>() / self.volume_history.len() as f64;
                let std = {
                    let variance = self
                        .volume_history
                        .iter()
                        .map(|&x| (x - mean).powi(2))
                        .sum::<f64>()
                        / self.volume_history.len() as f64;
                    variance.sqrt()
                };

                let anomaly_type = if z_score > 0.0 {
                    AnomalyType::VolumeSpike
                } else {
                    AnomalyType::VolumeDrop
                };

                anomaly = Some(Anomaly {
                    timestamp: point.timestamp,
                    anomaly_type,
                    severity: AnomalySeverity::from_score(z_score.abs()),
                    score: z_score.abs(),
                    description: format!(
                        "Volume {} detected: {} is {} standard deviations from mean",
                        if z_score > 0.0 { "spike" } else { "drop" },
                        volume,
                        z_score.abs()
                    ),
                    value: volume,
                    expected_range: (mean - self.threshold * std, mean + self.threshold * std),
                });
            }
        }

        // Update history
        self.price_history.push_back(price);
        self.volume_history.push_back(volume);

        if self.price_history.len() > self.window_size {
            self.price_history.pop_front();
        }
        if self.volume_history.len() > self.window_size {
            self.volume_history.pop_front();
        }

        Ok(anomaly)
    }

    fn reset(&mut self) {
        self.price_history.clear();
        self.volume_history.clear();
    }

    fn name(&self) -> &str {
        "Z-Score Detector"
    }
}

/// Interquartile range (IQR) based anomaly detector
#[derive(Debug, Clone)]
pub struct IQRDetector {
    /// Number of past data points used to compute IQR bounds
    window_size: usize,
    /// IQR fence multiplier (1.5 = standard, 3.0 = extreme outlier)
    multiplier: f64,
    /// Sliding window of recent price values
    price_history: VecDeque<f64>,
}

impl IQRDetector {
    /// Create a new IQR detector with the given window and multiplier
    pub fn new(window_size: usize, multiplier: f64) -> Self {
        Self {
            window_size,
            multiplier,
            price_history: VecDeque::new(),
        }
    }

    fn calculate_quartiles(&self, data: &[f64]) -> (f64, f64, f64) {
        if data.is_empty() {
            return (0.0, 0.0, 0.0);
        }

        let mut sorted = data.to_vec();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());

        let n = sorted.len();
        let q1_idx = n / 4;
        let q2_idx = n / 2;
        let q3_idx = 3 * n / 4;

        (sorted[q1_idx], sorted[q2_idx], sorted[q3_idx])
    }
}

impl Default for IQRDetector {
    fn default() -> Self {
        Self::new(30, 1.5)
    }
}

impl AnomalyDetector for IQRDetector {
    fn detect(&mut self, data: &[PricePoint]) -> anyhow::Result<Vec<Anomaly>> {
        let mut anomalies = Vec::new();

        for point in data {
            if let Some(anomaly) = self.detect_realtime(point)? {
                anomalies.push(anomaly);
            }
        }

        Ok(anomalies)
    }

    fn detect_realtime(&mut self, point: &PricePoint) -> anyhow::Result<Option<Anomaly>> {
        let price = point.close.to_string().parse::<f64>().unwrap_or(0.0);

        let mut anomaly = None;

        if self.price_history.len() >= self.window_size {
            let history: Vec<f64> = self.price_history.iter().copied().collect();
            let (q1, _q2, q3) = self.calculate_quartiles(&history);
            let iqr = q3 - q1;

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

            if price < lower_bound || price > upper_bound {
                let anomaly_type = if price < lower_bound {
                    AnomalyType::PriceDrop
                } else {
                    AnomalyType::PriceSpike
                };

                let deviation = if price < lower_bound {
                    (lower_bound - price) / iqr.max(1.0)
                } else {
                    (price - upper_bound) / iqr.max(1.0)
                };

                anomaly = Some(Anomaly {
                    timestamp: point.timestamp,
                    anomaly_type,
                    severity: AnomalySeverity::from_score(deviation),
                    score: deviation,
                    description: format!(
                        "Price {} is outside IQR bounds [{:.2}, {:.2}]",
                        price, lower_bound, upper_bound
                    ),
                    value: price,
                    expected_range: (lower_bound, upper_bound),
                });
            }
        }

        self.price_history.push_back(price);
        if self.price_history.len() > self.window_size {
            self.price_history.pop_front();
        }

        Ok(anomaly)
    }

    fn reset(&mut self) {
        self.price_history.clear();
    }

    fn name(&self) -> &str {
        "IQR Detector"
    }
}

/// Volatility-based anomaly detector
#[derive(Debug, Clone)]
pub struct VolatilityDetector {
    /// Number of past price points to include in volatility calculation
    window_size: usize,
    /// Volatility threshold above which an anomaly is flagged
    threshold: f64,
    /// Sliding window of recent price values
    price_history: VecDeque<f64>,
}

impl VolatilityDetector {
    /// Create a new volatility detector with the given window and threshold
    pub fn new(window_size: usize, threshold: f64) -> Self {
        Self {
            window_size,
            threshold,
            price_history: VecDeque::new(),
        }
    }

    fn calculate_volatility(&self, prices: &[f64]) -> f64 {
        if prices.len() < 2 {
            return 0.0;
        }

        let returns: Vec<f64> = prices
            .windows(2)
            .map(|w| (w[1] - w[0]) / w[0].max(1e-10))
            .collect();

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

        variance.sqrt()
    }
}

impl Default for VolatilityDetector {
    fn default() -> Self {
        Self::new(20, 0.05)
    }
}

impl AnomalyDetector for VolatilityDetector {
    fn detect(&mut self, data: &[PricePoint]) -> anyhow::Result<Vec<Anomaly>> {
        let mut anomalies = Vec::new();

        for point in data {
            if let Some(anomaly) = self.detect_realtime(point)? {
                anomalies.push(anomaly);
            }
        }

        Ok(anomalies)
    }

    fn detect_realtime(&mut self, point: &PricePoint) -> anyhow::Result<Option<Anomaly>> {
        let price = point.close.to_string().parse::<f64>().unwrap_or(0.0);
        self.price_history.push_back(price);

        if self.price_history.len() > self.window_size {
            self.price_history.pop_front();
        }

        let mut anomaly = None;

        if self.price_history.len() >= self.window_size {
            let prices: Vec<f64> = self.price_history.iter().copied().collect();
            let volatility = self.calculate_volatility(&prices);

            if volatility > self.threshold {
                let severity_score = (volatility / self.threshold) * 2.0;

                anomaly = Some(Anomaly {
                    timestamp: point.timestamp,
                    anomaly_type: AnomalyType::Volatility,
                    severity: AnomalySeverity::from_score(severity_score),
                    score: severity_score,
                    description: format!(
                        "High volatility detected: {:.4} (threshold: {:.4})",
                        volatility, self.threshold
                    ),
                    value: volatility,
                    expected_range: (0.0, self.threshold),
                });
            }
        }

        Ok(anomaly)
    }

    fn reset(&mut self) {
        self.price_history.clear();
    }

    fn name(&self) -> &str {
        "Volatility Detector"
    }
}

/// Composite anomaly detector that combines multiple detectors
pub struct CompositeAnomalyDetector {
    /// Individual detectors to run in parallel
    detectors: Vec<Box<dyn AnomalyDetector>>,
}

impl CompositeAnomalyDetector {
    /// Create a new empty composite detector
    pub fn new() -> Self {
        Self {
            detectors: Vec::new(),
        }
    }

    /// Add a detector to this composite
    pub fn add_detector(mut self, detector: Box<dyn AnomalyDetector>) -> Self {
        self.detectors.push(detector);
        self
    }

    /// Add a default Z-score detector
    pub fn with_zscore(self) -> Self {
        self.add_detector(Box::new(ZScoreDetector::default()))
    }

    /// Add a default IQR detector
    pub fn with_iqr(self) -> Self {
        self.add_detector(Box::new(IQRDetector::default()))
    }

    /// Add a default volatility detector
    pub fn with_volatility(self) -> Self {
        self.add_detector(Box::new(VolatilityDetector::default()))
    }
}

impl Default for CompositeAnomalyDetector {
    fn default() -> Self {
        Self::new()
    }
}

impl AnomalyDetector for CompositeAnomalyDetector {
    fn detect(&mut self, data: &[PricePoint]) -> anyhow::Result<Vec<Anomaly>> {
        let mut all_anomalies = Vec::new();

        for detector in &mut self.detectors {
            all_anomalies.extend(detector.detect(data)?);
        }

        // Sort by timestamp and severity
        all_anomalies.sort_by(|a, b| match a.timestamp.cmp(&b.timestamp) {
            std::cmp::Ordering::Equal => b.severity.cmp(&a.severity),
            other => other,
        });

        Ok(all_anomalies)
    }

    fn detect_realtime(&mut self, point: &PricePoint) -> anyhow::Result<Option<Anomaly>> {
        let mut highest_severity_anomaly: Option<Anomaly> = None;

        for detector in &mut self.detectors {
            if let Some(anomaly) = detector.detect_realtime(point)? {
                if let Some(ref current) = highest_severity_anomaly {
                    if anomaly.severity > current.severity {
                        highest_severity_anomaly = Some(anomaly);
                    }
                } else {
                    highest_severity_anomaly = Some(anomaly);
                }
            }
        }

        Ok(highest_severity_anomaly)
    }

    fn reset(&mut self) {
        for detector in &mut self.detectors {
            detector.reset();
        }
    }

    fn name(&self) -> &str {
        "Composite Detector"
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rust_decimal::Decimal;
    use rust_decimal_macros::dec;

    fn create_normal_data() -> Vec<PricePoint> {
        let now = Utc::now();
        (0..100)
            .map(|i| PricePoint {
                timestamp: now - chrono::Duration::days(100 - i),
                open: dec!(100),
                high: dec!(105),
                low: dec!(95),
                close: dec!(100),
                volume: dec!(1000),
            })
            .collect()
    }

    fn create_anomaly_data() -> Vec<PricePoint> {
        let now = Utc::now();
        let mut data: Vec<_> = (0..100)
            .map(|i| PricePoint {
                timestamp: now - chrono::Duration::days(100 - i),
                open: dec!(100),
                high: dec!(105),
                low: dec!(95),
                // Add small variation to prices to avoid zero std_dev
                close: dec!(100) + Decimal::from(i % 10) - dec!(5),
                volume: dec!(1000),
            })
            .collect();

        // Add significant anomaly
        data[50].close = dec!(500);

        data
    }

    #[test]
    fn test_zscore_detector() {
        let mut detector = ZScoreDetector::default();
        let data = create_anomaly_data();

        let anomalies = detector.detect(&data).unwrap();
        assert!(!anomalies.is_empty());
    }

    #[test]
    fn test_iqr_detector() {
        let mut detector = IQRDetector::default();
        let data = create_anomaly_data();

        let anomalies = detector.detect(&data).unwrap();
        assert!(!anomalies.is_empty());
    }

    #[test]
    fn test_volatility_detector() {
        let mut detector = VolatilityDetector::new(20, 0.01);
        let data = create_anomaly_data();

        let anomalies = detector.detect(&data).unwrap();
        // May or may not detect depending on volatility threshold
        // Just verify it doesn't error
        let _ = anomalies.len();
    }

    #[test]
    fn test_composite_detector() {
        let mut detector = CompositeAnomalyDetector::new().with_zscore().with_iqr();

        let data = create_anomaly_data();
        let anomalies = detector.detect(&data).unwrap();
        assert!(!anomalies.is_empty());
    }

    #[test]
    fn test_no_anomalies_in_normal_data() {
        let mut detector = ZScoreDetector::default();
        let data = create_normal_data();

        let anomalies = detector.detect(&data).unwrap();
        assert_eq!(anomalies.len(), 0);
    }

    #[test]
    fn test_anomaly_severity() {
        assert_eq!(AnomalySeverity::from_score(1.5), AnomalySeverity::Low);
        assert_eq!(AnomalySeverity::from_score(2.5), AnomalySeverity::Medium);
        assert_eq!(AnomalySeverity::from_score(3.5), AnomalySeverity::High);
        assert_eq!(AnomalySeverity::from_score(4.5), AnomalySeverity::Critical);
    }
}