AnomalyGridConfig

Struct AnomalyGridConfig 

Source
pub struct AnomalyGridConfig {
    pub max_order: usize,
    pub smoothing_alpha: f64,
    pub memory_limit: Option<usize>,
    pub min_probability: f64,
    pub likelihood_weight: f64,
    pub information_weight: f64,
    pub normalization_factor: f64,
    pub min_sequence_length: usize,
}
Expand description

Configuration parameters for Anomaly Grid components

Fields§

§max_order: usize

Maximum context order for Markov model

§smoothing_alpha: f64

Laplace smoothing parameter (alpha)

§memory_limit: Option<usize>

Maximum number of contexts to store (None = unlimited)

§min_probability: f64

Minimum probability for numerical stability

§likelihood_weight: f64

Weight for likelihood component in anomaly strength calculation

§information_weight: f64

Weight for information component in anomaly strength calculation

§normalization_factor: f64

Normalization factor for tanh scaling in anomaly strength

§min_sequence_length: usize

Minimum sequence length for training

Implementations§

Source§

impl AnomalyGridConfig

Source

pub fn new() -> Self

Create a new configuration with default values

Source

pub fn for_small_alphabet() -> Self

Create a configuration optimized for small alphabets (≤ 10 states)

Source

pub fn for_large_alphabet() -> Self

Create a configuration optimized for large alphabets (> 20 states)

Source

pub fn for_low_memory() -> Self

Create a configuration optimized for memory-constrained environments

Source

pub fn for_high_accuracy() -> Self

Create a configuration optimized for high accuracy

Source

pub fn validate(&self) -> AnomalyGridResult<()>

Validate the configuration parameters

Source

pub fn with_max_order(self, max_order: usize) -> AnomalyGridResult<Self>

Set max_order with validation

Examples found in repository?
examples/multi_modal_anomaly_detection.rs (line 71)
65fn create_multi_modal_detectors(
66) -> Result<HashMap<String, AnomalyDetector>, Box<dyn std::error::Error>> {
67    let mut detectors = HashMap::new();
68
69    // Sequential pattern detector (high order)
70    let sequential_config = AnomalyGridConfig::default()
71        .with_max_order(6)?
72        .with_smoothing_alpha(0.1)?
73        .with_weights(0.9, 0.1)?;
74    let sequential_detector = AnomalyDetector::with_config(sequential_config)?;
75    detectors.insert("sequential".to_string(), sequential_detector);
76
77    // Behavioral pattern detector (medium order)
78    let behavioral_config = AnomalyGridConfig::default()
79        .with_max_order(4)?
80        .with_smoothing_alpha(0.3)?
81        .with_weights(0.7, 0.3)?;
82    let behavioral_detector = AnomalyDetector::with_config(behavioral_config)?;
83    detectors.insert("behavioral".to_string(), behavioral_detector);
84
85    // Temporal pattern detector (high order)
86    let temporal_config = AnomalyGridConfig::default()
87        .with_max_order(8)?
88        .with_smoothing_alpha(0.2)?
89        .with_weights(0.8, 0.2)?;
90    let temporal_detector = AnomalyDetector::with_config(temporal_config)?;
91    detectors.insert("temporal".to_string(), temporal_detector);
92
93    // Network pattern detector (medium order)
94    let network_config = AnomalyGridConfig::default()
95        .with_max_order(5)?
96        .with_smoothing_alpha(0.4)?
97        .with_weights(0.6, 0.4)?;
98    let network_detector = AnomalyDetector::with_config(network_config)?;
99    detectors.insert("network".to_string(), network_detector);
100
101    println!("🔧 Sequential detector: order 6, α=0.1, weights=(0.9,0.1)");
102    println!("🧠 Behavioral detector: order 4, α=0.3, weights=(0.7,0.3)");
103    println!("⏰ Temporal detector: order 8, α=0.2, weights=(0.8,0.2)");
104    println!("🌐 Network detector: order 5, α=0.4, weights=(0.6,0.4)");
105
106    Ok(detectors)
107}
More examples
Hide additional examples
examples/quick_start.rs (line 18)
12fn main() -> Result<(), Box<dyn std::error::Error>> {
13    println!("🚀 Anomaly Grid - Quick Start Example");
14    println!("Demonstrating accurate anomaly detection with real patterns\n");
15
16    // Create detector with optimal configuration for this example
17    let config = AnomalyGridConfig::default()
18        .with_max_order(3)? // Good balance of context and performance
19        .with_smoothing_alpha(1.0)? // Standard Laplace smoothing
20        .with_weights(0.7, 0.3)?; // Emphasize likelihood over information
21
22    let mut detector = AnomalyDetector::with_config(config)?;
23    println!("✅ Created detector with order 3 and optimized configuration");
24
25    // Generate realistic training data with clear patterns
26    let normal_patterns = generate_realistic_training_data();
27    println!(
28        "📚 Generated {} training sequences with realistic patterns",
29        normal_patterns.len()
30    );
31
32    // Train the detector
33    let train_start = Instant::now();
34    detector.train(&normal_patterns)?;
35    let train_time = train_start.elapsed();
36
37    // Get training metrics
38    let metrics = detector.performance_metrics();
39    println!("🎯 Training completed in {:?}", train_time);
40    println!("   - Contexts learned: {}", metrics.context_count);
41    println!(
42        "   - Memory usage: {:.1} KB",
43        metrics.estimated_memory_bytes as f64 / 1024.0
44    );
45
46    // Test different scenarios with proper threshold tuning
47    println!("\n🔍 Testing Anomaly Detection Scenarios");
48
49    let test_scenarios = vec![
50        ("Normal Pattern", generate_normal_sequence(), 0.1),
51        ("Slight Deviation", generate_slight_deviation(), 0.05),
52        ("Clear Anomaly", generate_clear_anomaly(), 0.01),
53        ("Severe Anomaly", generate_severe_anomaly(), 0.001),
54    ];
55
56    for (scenario_name, test_sequence, threshold) in test_scenarios {
57        println!("\n--- {} (threshold: {}) ---", scenario_name, threshold);
58
59        let detect_start = Instant::now();
60        let anomalies = detector.detect_anomalies(&test_sequence, threshold)?;
61        let detect_time = detect_start.elapsed();
62
63        println!("Sequence: {:?}", test_sequence);
64        println!("Detection time: {:?}", detect_time);
65
66        if anomalies.is_empty() {
67            println!("✅ No anomalies detected (normal behavior)");
68        } else {
69            println!("⚠️  {} anomalies detected:", anomalies.len());
70
71            for (i, anomaly) in anomalies.iter().enumerate() {
72                println!("   Anomaly {}: {:?}", i + 1, anomaly.sequence);
73                println!("     - Likelihood: {:.6}", anomaly.likelihood);
74                println!("     - Anomaly Strength: {:.3}", anomaly.anomaly_strength);
75                println!("     - Information Score: {:.3}", anomaly.information_score);
76
77                // Classify anomaly severity
78                let severity = if anomaly.anomaly_strength > 0.8 {
79                    "🚨 CRITICAL"
80                } else if anomaly.anomaly_strength > 0.6 {
81                    "⚠️  HIGH"
82                } else if anomaly.anomaly_strength > 0.4 {
83                    "⚡ MEDIUM"
84                } else {
85                    "📝 LOW"
86                };
87
88                println!("     - Severity: {}", severity);
89            }
90        }
91    }
92
93    // Demonstrate threshold sensitivity analysis
94    println!("\n📊 Threshold Sensitivity Analysis");
95    demonstrate_threshold_sensitivity(&detector)?;
96
97    // Show mathematical properties
98    println!("\n🔬 Mathematical Properties Validation");
99    validate_mathematical_properties(&detector)?;
100
101    println!("\n🎉 Quick start example completed successfully!");
102    println!("💡 Key takeaways:");
103    println!("   - Higher thresholds detect fewer, stronger anomalies");
104    println!("   - Lower thresholds detect more, including weaker anomalies");
105    println!("   - Anomaly strength combines likelihood and information content");
106    println!("   - Detection is fast and mathematically sound");
107
108    Ok(())
109}
examples/industrial_iot_monitoring.rs (line 122)
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11    println!("🏭 Industrial IoT Monitoring with Anomaly Grid");
12    println!("Predictive maintenance and equipment failure detection\n");
13
14    // Generate 1 month of normal sensor data (reduced for performance)
15    let normal_readings = generate_industrial_data(30);
16    println!(
17        "Generated {} sensor readings (1 month)",
18        normal_readings.len()
19    );
20
21    // Initialize anomaly detection for IoT sensor data
22    let mut detector = AnomalyDetector::new(4)?; // Order 4 for sensor patterns
23
24    // Train on normal operational data
25    // Train on normal operational patterns
26    detector.train(&normal_readings)?;
27
28    // Equipment monitoring scenarios
29    println!("\n🔧 Equipment Health Monitoring");
30
31    let equipment_scenarios = vec![
32        ("Bearing Wear", generate_bearing_wear_pattern()),
33        ("Vibration Anomaly", generate_vibration_anomaly()),
34        ("Temperature Drift", generate_temperature_drift()),
35        ("Pressure Fluctuation", generate_pressure_fluctuation()),
36        ("Lubrication Failure", generate_lubrication_failure()),
37        ("Motor Imbalance", generate_motor_imbalance()),
38        ("Seal Degradation", generate_seal_degradation()),
39    ];
40
41    let mut maintenance_alerts = 0;
42    let mut critical_failures_prevented = 0;
43
44    for (equipment_issue, sensor_sequence) in equipment_scenarios {
45        println!("\nMonitoring: {equipment_issue}");
46
47        let detect_start = Instant::now();
48        let anomalies = detector.detect_anomalies(&sensor_sequence, 0.005)?; // Sensitive for safety
49        let detect_time = detect_start.elapsed();
50
51        if !anomalies.is_empty() {
52            maintenance_alerts += 1;
53
54            let severity = calculate_severity(&anomalies);
55            let time_to_failure = estimate_time_to_failure(&sensor_sequence);
56            let maintenance_cost = estimate_maintenance_cost(equipment_issue);
57
58            println!("  ⚠️ MAINTENANCE ALERT");
59            println!("  📊 Anomalies detected: {}", anomalies.len());
60            println!("  Severity: {severity}");
61            println!("  Time to failure: {time_to_failure} hours");
62            println!("  Estimated maintenance cost: ${maintenance_cost:.0}");
63            println!("  Detection time: {detect_time:?}");
64
65            if severity == "CRITICAL" {
66                critical_failures_prevented += 1;
67                println!("  🚨 IMMEDIATE SHUTDOWN RECOMMENDED");
68            }
69
70            generate_maintenance_recommendation(equipment_issue, &severity, time_to_failure);
71        } else {
72            println!("  ✅ Equipment operating normally");
73        }
74    }
75
76    // Production line monitoring
77    println!("\n🏭 Production Line Monitoring");
78    let production_data = generate_production_line_data();
79
80    let line_start = Instant::now();
81    let line_anomalies = detector.detect_anomalies(&production_data, 0.01)?;
82    let line_time = line_start.elapsed();
83
84    if !line_anomalies.is_empty() {
85        let efficiency_impact = calculate_efficiency_impact(&line_anomalies);
86        println!("Production anomalies detected: {}", line_anomalies.len());
87        println!("Efficiency impact: {efficiency_impact:.1}%");
88        println!("Detection time: {line_time:?}");
89    } else {
90        println!("Production line operating normally");
91    }
92
93    // Energy consumption monitoring
94    println!("\n⚡ Energy Consumption Monitoring");
95    let energy_data = generate_energy_consumption_data();
96
97    let energy_start = Instant::now();
98    let energy_anomalies = detector.detect_anomalies(&energy_data, 0.02)?;
99    let energy_time = energy_start.elapsed();
100
101    if !energy_anomalies.is_empty() {
102        let energy_waste = calculate_energy_waste(&energy_anomalies);
103        println!("Energy anomalies detected: {}", energy_anomalies.len());
104        println!("Estimated energy waste: {energy_waste:.0} kWh");
105        println!("Cost impact: ${:.2}", energy_waste * 0.12); // $0.12/kWh
106        println!("Detection time: {energy_time:?}");
107    } else {
108        println!("Energy consumption within normal parameters");
109    }
110
111    // Batch processing for multiple machines
112    println!("\n📦 Multi-Machine Batch Processing");
113    let machine_data = vec![
114        generate_machine_data("CNC_001"),
115        generate_machine_data("PRESS_002"),
116        generate_machine_data("ROBOT_003"),
117        generate_machine_data("CONVEYOR_004"),
118        generate_machine_data("WELDER_005"),
119    ];
120
121    let batch_start = Instant::now();
122    let config = AnomalyGridConfig::default().with_max_order(6)?;
123    let batch_results = batch_process_sequences(&machine_data, &config, 0.01)?;
124    let batch_time = batch_start.elapsed();
125
126    println!(
127        "Processed {} machines in {:?}",
128        machine_data.len(),
129        batch_time
130    );
131    for (i, results) in batch_results.iter().enumerate() {
132        let machine_names = [
133            "CNC_001",
134            "PRESS_002",
135            "ROBOT_003",
136            "CONVEYOR_004",
137            "WELDER_005",
138        ];
139        println!(
140            "  {}: {} anomalies detected",
141            machine_names[i],
142            results.len()
143        );
144    }
145
146    // Summary and ROI calculation
147    println!("\n📊 Predictive Maintenance Summary");
148    println!("Maintenance alerts generated: {maintenance_alerts}");
149    println!("Critical failures prevented: {critical_failures_prevented}");
150
151    let downtime_prevented = critical_failures_prevented * 8; // 8 hours per failure
152    let cost_savings = downtime_prevented as f64 * 5000.0; // $5000/hour downtime cost
153    let maintenance_costs = maintenance_alerts as f64 * 2000.0; // $2000 per maintenance
154    let net_savings = cost_savings - maintenance_costs;
155
156    println!("Downtime prevented: {downtime_prevented} hours");
157    println!("Cost savings: ${cost_savings:.0}");
158    println!("Maintenance costs: ${maintenance_costs:.0}");
159    println!("Net savings: ${net_savings:.0}");
160    println!("ROI: {:.1}%", (net_savings / maintenance_costs) * 100.0);
161
162    Ok(())
163}
examples/behavioral_anomaly_detection.rs (line 22)
16fn main() -> Result<(), Box<dyn std::error::Error>> {
17    println!("🧠 Behavioral Anomaly Detection with Mathematical Validation");
18    println!("Learning user behavior patterns and detecting sophisticated anomalies\n");
19
20    // Configure detector for behavioral pattern analysis
21    let config = AnomalyGridConfig::default()
22        .with_max_order(6)? // High order for complex behavioral sequences
23        .with_smoothing_alpha(0.3)? // Low smoothing for precise pattern learning
24        .with_weights(0.8, 0.2)?; // Emphasize likelihood for behavioral patterns
25
26    let mut detector = AnomalyDetector::with_config(config)?;
27    println!("✅ Configured behavioral detector with order 6 for complex patterns");
28
29    // Phase 1: Generate and validate training data
30    println!("\n📚 Phase 1: Training Data Generation and Validation");
31    let (training_data, ground_truth) = generate_validated_behavioral_data();
32
33    // Validate training data quality
34    validate_training_data_quality(&training_data)?;
35
36    println!("📊 Generated {} behavioral sequences", training_data.len());
37    println!("🎯 Training data validation: PASSED");
38
39    // Phase 2: Train and validate model convergence
40    println!("\n🎯 Phase 2: Model Training and Convergence Validation");
41    let train_start = Instant::now();
42    detector.train(&training_data)?;
43    let train_time = train_start.elapsed();
44
45    let metrics = detector.performance_metrics();
46    println!("⏱️ Training completed in {:?}", train_time);
47    println!("🧮 Behavioral patterns learned: {}", metrics.context_count);
48    println!(
49        "💾 Memory usage: {:.1} KB",
50        metrics.estimated_memory_bytes as f64 / 1024.0
51    );
52
53    // Validate model convergence
54    validate_model_convergence(&detector, &training_data)?;
55
56    // Phase 3: Rigorous anomaly detection testing
57    println!("\n🔬 Phase 3: Rigorous Anomaly Detection Testing");
58
59    let test_scenarios = vec![
60        (
61            "Normal User Behavior",
62            generate_normal_user_session(),
63            false,
64            0.1,
65        ),
66        (
67            "Account Compromise",
68            generate_account_compromise(),
69            true,
70            0.05,
71        ),
72        (
73            "Insider Threat",
74            generate_insider_threat_behavior(),
75            true,
76            0.02,
77        ),
78        ("Bot/Automation", generate_bot_behavior(), true, 0.01),
79        (
80            "Social Engineering",
81            generate_social_engineering(),
82            true,
83            0.03,
84        ),
85        (
86            "Privilege Escalation",
87            generate_privilege_escalation_behavior(),
88            true,
89            0.02,
90        ),
91        (
92            "Data Exfiltration",
93            generate_data_exfiltration_behavior(),
94            true,
95            0.01,
96        ),
97    ];
98
99    let mut detection_results = Vec::new();
100    let mut total_detection_time = std::time::Duration::new(0, 0);
101
102    for (scenario_name, test_sequence, is_anomalous, threshold) in test_scenarios {
103        println!("\n--- Testing: {} ---", scenario_name);
104        println!(
105            "Expected: {}",
106            if is_anomalous { "ANOMALOUS" } else { "NORMAL" }
107        );
108        println!("Sequence length: {}", test_sequence.len());
109        println!("Threshold: {}", threshold);
110
111        let detect_start = Instant::now();
112        let anomalies = detector.detect_anomalies(&test_sequence, threshold)?;
113        let detect_time = detect_start.elapsed();
114        total_detection_time += detect_time;
115
116        let detected = !anomalies.is_empty();
117        let is_correct = detected == is_anomalous;
118
119        println!(
120            "🔍 Detection result: {}",
121            if detected { "ANOMALOUS" } else { "NORMAL" }
122        );
123        println!(
124            "✅ Correctness: {}",
125            if is_correct { "CORRECT" } else { "INCORRECT" }
126        );
127        println!("⏱️ Detection time: {:?}", detect_time);
128
129        if detected {
130            let max_strength = anomalies
131                .iter()
132                .map(|a| a.anomaly_strength)
133                .fold(0.0f64, f64::max);
134            let avg_information =
135                anomalies.iter().map(|a| a.information_score).sum::<f64>() / anomalies.len() as f64;
136
137            println!("📊 Anomalies detected: {}", anomalies.len());
138            println!("🎯 Max anomaly strength: {:.4}", max_strength);
139            println!("📈 Avg information score: {:.4}", avg_information);
140
141            // Validate mathematical properties
142            validate_anomaly_mathematical_properties(&anomalies)?;
143        }
144
145        detection_results.push((
146            scenario_name.to_string(),
147            is_anomalous,
148            detected,
149            is_correct,
150        ));
151    }
152
153    // Phase 4: Comprehensive validation and metrics
154    println!("\n📊 Phase 4: Comprehensive Validation and Metrics");
155
156    let accuracy = calculate_accuracy(&detection_results);
157    let (precision, recall, f1_score) = calculate_precision_recall_f1(&detection_results);
158
159    println!("🎯 Detection Accuracy: {:.1}%", accuracy * 100.0);
160    println!("🎯 Precision: {:.3}", precision);
161    println!("🎯 Recall: {:.3}", recall);
162    println!("🎯 F1 Score: {:.3}", f1_score);
163    println!(
164        "⏱️ Average detection time: {:?}",
165        total_detection_time / detection_results.len() as u32
166    );
167
168    // Phase 5: Advanced behavioral analysis
169    println!("\n🧬 Phase 5: Advanced Behavioral Analysis");
170    perform_behavioral_pattern_analysis(&detector)?;
171
172    // Phase 6: Threshold optimization
173    println!("\n⚙️ Phase 6: Threshold Optimization");
174    let optimal_threshold = optimize_detection_threshold(&detector)?;
175    println!("🎯 Optimal threshold: {:.4}", optimal_threshold);
176
177    // Phase 7: Robustness testing
178    println!("\n🛡️ Phase 7: Robustness Testing");
179    test_detection_robustness(&detector)?;
180
181    // Phase 8: Performance benchmarking
182    println!("\n⚡ Phase 8: Performance Benchmarking");
183    benchmark_detection_performance(&detector)?;
184
185    // Final validation summary
186    println!("\n✅ VALIDATION SUMMARY");
187    println!("═══════════════════════════════════");
188    println!("✅ Training data quality: VALIDATED");
189    println!("✅ Model convergence: VALIDATED");
190    println!("✅ Mathematical properties: VALIDATED");
191    println!("✅ Detection accuracy: {:.1}%", accuracy * 100.0);
192    println!("✅ F1 Score: {:.3}", f1_score);
193    println!(
194        "✅ Performance: {} detections/sec",
195        (1000.0 / total_detection_time.as_millis() as f64 * detection_results.len() as f64) as u32
196    );
197
198    if accuracy >= 0.85 && f1_score >= 0.8 {
199        println!("🎉 ALL VALIDATIONS PASSED - LIBRARY PERFORMANCE VERIFIED");
200    } else {
201        println!("⚠️ VALIDATION CONCERNS - REVIEW REQUIRED");
202    }
203
204    Ok(())
205}
examples/network_security_monitoring.rs (line 19)
13fn main() -> Result<(), Box<dyn std::error::Error>> {
14    println!("🛡️ Network Security Monitoring with Anomaly Grid");
15    println!("Detecting APTs, DDoS, and network intrusions with high accuracy\n");
16
17    // Configure detector for network security patterns
18    let config = AnomalyGridConfig::default()
19        .with_max_order(4)? // Higher order for complex attack patterns
20        .with_smoothing_alpha(0.5)? // Lower smoothing for better discrimination
21        .with_weights(0.6, 0.4)?; // Balance likelihood and information
22
23    let mut detector = AnomalyDetector::with_config(config)?;
24    println!("✅ Configured security detector with order 4 for complex patterns");
25
26    // Generate comprehensive normal network traffic
27    let normal_events = generate_enterprise_traffic(5);
28    println!(
29        "📊 Generated {} normal network events (5 days)",
30        normal_events.len()
31    );
32
33    // Train on normal network patterns
34    let train_start = Instant::now();
35    detector.train(&normal_events)?;
36    let train_time = train_start.elapsed();
37
38    let metrics = detector.performance_metrics();
39    println!("🎯 Training completed in {:?}", train_time);
40    println!("   - Security contexts learned: {}", metrics.context_count);
41    println!(
42        "   - Memory usage: {:.1} KB",
43        metrics.estimated_memory_bytes as f64 / 1024.0
44    );
45
46    // Real-time monitoring simulation with tuned thresholds
47    println!("\n🔍 Real-time Network Security Monitoring");
48
49    let attack_scenarios = vec![
50        ("Advanced Persistent Threat", generate_apt_campaign(), 0.001),
51        ("DDoS Attack", generate_ddos_attack(), 0.005),
52        ("Port Scan", generate_port_scan(), 0.01),
53        ("SQL Injection", generate_sql_injection(), 0.005),
54        ("Lateral Movement", generate_lateral_movement(), 0.001),
55        ("Data Exfiltration", generate_data_exfiltration(), 0.001),
56        (
57            "Malware C2 Communication",
58            generate_malware_communication(),
59            0.002,
60        ),
61    ];
62
63    let mut total_threats_detected = 0;
64    let mut critical_threats = 0;
65    let mut total_detection_time = std::time::Duration::new(0, 0);
66
67    for (attack_name, attack_sequence, threshold) in attack_scenarios {
68        println!("\n--- Analyzing: {} ---", attack_name);
69        println!("Sequence length: {} events", attack_sequence.len());
70        println!("Detection threshold: {}", threshold);
71
72        let detect_start = Instant::now();
73        let anomalies = detector.detect_anomalies(&attack_sequence, threshold)?;
74        let detect_time = detect_start.elapsed();
75        total_detection_time += detect_time;
76
77        if !anomalies.is_empty() {
78            total_threats_detected += 1;
79
80            let max_strength = anomalies
81                .iter()
82                .map(|a| a.anomaly_strength)
83                .fold(0.0, f64::max);
84
85            let avg_information =
86                anomalies.iter().map(|a| a.information_score).sum::<f64>() / anomalies.len() as f64;
87
88            let min_likelihood = anomalies
89                .iter()
90                .map(|a| a.likelihood)
91                .fold(f64::INFINITY, f64::min);
92
93            // Enhanced threat classification
94            let (threat_level, confidence) =
95                classify_threat(max_strength, avg_information, anomalies.len());
96
97            if threat_level == "CRITICAL" {
98                critical_threats += 1;
99            }
100
101            println!("  🚨 THREAT DETECTED");
102            println!("  📊 Anomalies found: {}", anomalies.len());
103            println!("  🎯 Max anomaly strength: {:.3}", max_strength);
104            println!("  📈 Avg information score: {:.3}", avg_information);
105            println!("  📉 Min likelihood: {:.2e}", min_likelihood);
106            println!("  ⚡ Detection time: {:?}", detect_time);
107            println!("  🔥 Threat Level: {}", threat_level);
108            println!("  🎲 Confidence: {:.1}%", confidence);
109
110            // Generate detailed security alert
111            generate_security_alert(attack_name, &threat_level, confidence, &anomalies);
112
113            // Show most suspicious patterns
114            if anomalies.len() > 0 {
115                let most_suspicious = anomalies
116                    .iter()
117                    .max_by(|a, b| a.anomaly_strength.partial_cmp(&b.anomaly_strength).unwrap())
118                    .unwrap();
119                println!(
120                    "  🔍 Most suspicious pattern: {:?}",
121                    most_suspicious.sequence
122                );
123                println!(
124                    "     Strength: {:.3}, Info: {:.3}",
125                    most_suspicious.anomaly_strength, most_suspicious.information_score
126                );
127            }
128        } else {
129            println!("  ✅ No threats detected (normal traffic)");
130        }
131    }
132
133    // Advanced analytics and ROC analysis
134    println!("\n📈 Advanced Security Analytics");
135    perform_roc_analysis(&detector)?;
136
137    // Batch processing demonstration with performance metrics
138    println!("\n📦 High-Volume Batch Processing");
139    let batch_sequences = vec![
140        generate_normal_session(),
141        generate_malware_communication(),
142        generate_data_exfiltration(),
143        generate_insider_threat(),
144        generate_zero_day_exploit(),
145    ];
146
147    let batch_start = Instant::now();
148    let config = AnomalyGridConfig::default().with_max_order(5)?;
149    let batch_results = batch_process_sequences(&batch_sequences, &config, 0.01)?;
150    let batch_time = batch_start.elapsed();
151
152    println!(
153        "Processed {} sequences in {:?}",
154        batch_sequences.len(),
155        batch_time
156    );
157    let throughput = batch_sequences.len() as f64 / batch_time.as_secs_f64();
158    println!("Throughput: {:.1} sequences/second", throughput);
159
160    for (i, results) in batch_results.iter().enumerate() {
161        let sequence_types = [
162            "Normal Session",
163            "Malware C2",
164            "Data Exfiltration",
165            "Insider Threat",
166            "Zero-day Exploit",
167        ];
168        let risk_score = calculate_risk_score(results);
169        println!(
170            "  {}: {} anomalies, risk score: {:.2}",
171            sequence_types[i],
172            results.len(),
173            risk_score
174        );
175    }
176
177    // Performance and accuracy summary
178    println!("\n📊 Security Monitoring Summary");
179    println!("═══════════════════════════════════");
180    println!(
181        "Threats detected: {}/7 ({:.1}%)",
182        total_threats_detected,
183        (total_threats_detected as f64 / 7.0) * 100.0
184    );
185    println!("Critical threats: {}", critical_threats);
186    println!(
187        "Average detection time: {:?}",
188        total_detection_time / total_threats_detected.max(1) as u32
189    );
190
191    let false_positive_rate = calculate_false_positive_rate(&detector)?;
192    println!("Estimated false positive rate: {:.2}%", false_positive_rate);
193
194    // Calculate estimated cost savings
195    let cost_savings = calculate_security_cost_savings(critical_threats, total_threats_detected);
196    println!("Estimated cost savings: ${:.0}", cost_savings);
197
198    println!("\n💡 Security Insights:");
199    println!("   - APT campaigns show complex multi-stage patterns");
200    println!("   - DDoS attacks have high-volume repetitive signatures");
201    println!("   - Lateral movement creates subtle context anomalies");
202    println!("   - Data exfiltration shows unusual data flow patterns");
203    println!("   - Real-time detection enables rapid incident response");
204
205    Ok(())
206}
examples/time_series_anomaly_detection.rs (line 20)
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15    println!("📈 Time Series Anomaly Detection with Mathematical Validation");
16    println!("Converting continuous data to categorical sequences for anomaly detection\n");
17
18    // Configure detector for time series pattern analysis
19    let config = AnomalyGridConfig::default()
20        .with_max_order(8)? // High order for temporal dependencies
21        .with_smoothing_alpha(0.2)? // Low smoothing for precise pattern learning
22        .with_weights(0.9, 0.1)?; // Emphasize likelihood for temporal patterns
23
24    let mut detector = AnomalyDetector::with_config(config)?;
25    println!("✅ Configured time series detector with order 8 for temporal patterns");
26
27    // Phase 1: Generate and validate time series training data
28    println!("\n📊 Phase 1: Time Series Data Generation and Discretization");
29    let (raw_time_series, discretized_series) = generate_time_series_data(1000)?;
30
31    // Validate discretization quality
32    validate_discretization_quality(&raw_time_series, &discretized_series)?;
33
34    println!("📈 Generated {} time series points", raw_time_series.len());
35    println!(
36        "🔢 Discretized to {} categorical states",
37        discretized_series.len()
38    );
39    println!("🎯 Discretization validation: PASSED");
40
41    // Phase 2: Train and validate temporal model
42    println!("\n🎯 Phase 2: Temporal Model Training and Validation");
43    let train_start = Instant::now();
44    detector.train(&discretized_series)?;
45    let train_time = train_start.elapsed();
46
47    let metrics = detector.performance_metrics();
48    println!("⏱️ Training completed in {:?}", train_time);
49    println!("🧮 Temporal patterns learned: {}", metrics.context_count);
50    println!(
51        "💾 Memory usage: {:.1} KB",
52        metrics.estimated_memory_bytes as f64 / 1024.0
53    );
54
55    // Validate temporal consistency
56    validate_temporal_consistency(&detector, &discretized_series)?;
57
58    // Phase 3: Comprehensive anomaly detection testing
59    println!("\n🔬 Phase 3: Time Series Anomaly Detection Testing");
60
61    let test_scenarios = vec![
62        ("Normal Trend", generate_normal_trend(), false, 0.1),
63        ("Sudden Spike", generate_sudden_spike(), true, 0.05),
64        ("Gradual Drift", generate_gradual_drift(), true, 0.08),
65        ("Periodic Anomaly", generate_periodic_anomaly(), true, 0.03),
66        ("Noise Burst", generate_noise_burst(), true, 0.02),
67        ("Level Shift", generate_level_shift(), true, 0.05),
68        ("Trend Change", generate_trend_change(), true, 0.06),
69        ("Seasonal Break", generate_seasonal_break(), true, 0.04),
70    ];
71
72    let mut detection_results = Vec::new();
73    let mut total_detection_time = std::time::Duration::new(0, 0);
74
75    for (scenario_name, (raw_data, test_sequence), is_anomalous, threshold) in test_scenarios {
76        println!("\n--- Testing: {} ---", scenario_name);
77        println!(
78            "Expected: {}",
79            if is_anomalous { "ANOMALOUS" } else { "NORMAL" }
80        );
81        println!("Raw data points: {}", raw_data.len());
82        println!("Discretized sequence length: {}", test_sequence.len());
83        println!("Threshold: {}", threshold);
84
85        let detect_start = Instant::now();
86        let anomalies = detector.detect_anomalies(&test_sequence, threshold)?;
87        let detect_time = detect_start.elapsed();
88        total_detection_time += detect_time;
89
90        let detected = !anomalies.is_empty();
91        let is_correct = detected == is_anomalous;
92
93        println!(
94            "🔍 Detection result: {}",
95            if detected { "ANOMALOUS" } else { "NORMAL" }
96        );
97        println!(
98            "✅ Correctness: {}",
99            if is_correct { "CORRECT" } else { "INCORRECT" }
100        );
101        println!("⏱️ Detection time: {:?}", detect_time);
102
103        if detected {
104            let max_strength = anomalies
105                .iter()
106                .map(|a| a.anomaly_strength)
107                .fold(0.0f64, f64::max);
108            let avg_information =
109                anomalies.iter().map(|a| a.information_score).sum::<f64>() / anomalies.len() as f64;
110
111            println!("📊 Anomalies detected: {}", anomalies.len());
112            println!("🎯 Max anomaly strength: {:.4}", max_strength);
113            println!("📈 Avg information score: {:.4}", avg_information);
114
115            // Validate mathematical properties
116            validate_anomaly_mathematical_properties(&anomalies)?;
117
118            // Show temporal context of strongest anomaly
119            if let Some(strongest) = anomalies
120                .iter()
121                .max_by(|a, b| a.anomaly_strength.partial_cmp(&b.anomaly_strength).unwrap())
122            {
123                println!("🔍 Strongest anomaly pattern: {:?}", strongest.sequence);
124            }
125        }
126
127        detection_results.push((
128            scenario_name.to_string(),
129            is_anomalous,
130            detected,
131            is_correct,
132        ));
133    }
134
135    // Phase 4: Statistical validation and metrics
136    println!("\n📊 Phase 4: Statistical Validation and Performance Metrics");
137
138    let accuracy = calculate_accuracy(&detection_results);
139    let (precision, recall, f1_score) = calculate_precision_recall_f1(&detection_results);
140
141    println!("🎯 Detection Accuracy: {:.1}%", accuracy * 100.0);
142    println!("🎯 Precision: {:.3}", precision);
143    println!("🎯 Recall: {:.3}", recall);
144    println!("🎯 F1 Score: {:.3}", f1_score);
145    println!(
146        "⏱️ Average detection time: {:?}",
147        total_detection_time / detection_results.len() as u32
148    );
149
150    // Phase 5: Temporal pattern analysis
151    println!("\n⏰ Phase 5: Temporal Pattern Analysis");
152    analyze_temporal_patterns(&detector)?;
153
154    // Phase 6: Sensitivity analysis
155    println!("\n🎚️ Phase 6: Sensitivity Analysis");
156    perform_sensitivity_analysis(&detector)?;
157
158    // Phase 7: Robustness testing with edge cases
159    println!("\n🛡️ Phase 7: Robustness Testing");
160    test_temporal_robustness(&detector)?;
161
162    // Phase 8: Performance benchmarking
163    println!("\n⚡ Phase 8: Performance Benchmarking");
164    benchmark_temporal_performance(&detector)?;
165
166    // Phase 9: Mathematical validation
167    println!("\n🔬 Phase 9: Mathematical Property Validation");
168    validate_mathematical_properties(&detector)?;
169
170    // Final validation summary
171    println!("\n✅ COMPREHENSIVE VALIDATION SUMMARY");
172    println!("═══════════════════════════════════════");
173    println!("✅ Time series discretization: VALIDATED");
174    println!("✅ Temporal consistency: VALIDATED");
175    println!("✅ Mathematical properties: VALIDATED");
176    println!("✅ Detection accuracy: {:.1}%", accuracy * 100.0);
177    println!("✅ F1 Score: {:.3}", f1_score);
178    println!(
179        "✅ Performance: {} detections/sec",
180        (1000.0 / total_detection_time.as_millis() as f64 * detection_results.len() as f64) as u32
181    );
182
183    // Determine overall validation status
184    let validation_passed = accuracy >= 0.8
185        && f1_score >= 0.75
186        && total_detection_time.as_millis() < 100 * detection_results.len() as u128;
187
188    if validation_passed {
189        println!("🎉 ALL VALIDATIONS PASSED - TIME SERIES DETECTION VERIFIED");
190        println!("📈 Library successfully handles temporal anomaly detection");
191    } else {
192        println!("⚠️ VALIDATION CONCERNS - REVIEW REQUIRED");
193        if accuracy < 0.8 {
194            println!("   - Accuracy below threshold");
195        }
196        if f1_score < 0.75 {
197            println!("   - F1 score below threshold");
198        }
199    }
200
201    Ok(())
202}
Source

pub fn with_smoothing_alpha(self, alpha: f64) -> AnomalyGridResult<Self>

Set smoothing_alpha with validation

Examples found in repository?
examples/multi_modal_anomaly_detection.rs (line 72)
65fn create_multi_modal_detectors(
66) -> Result<HashMap<String, AnomalyDetector>, Box<dyn std::error::Error>> {
67    let mut detectors = HashMap::new();
68
69    // Sequential pattern detector (high order)
70    let sequential_config = AnomalyGridConfig::default()
71        .with_max_order(6)?
72        .with_smoothing_alpha(0.1)?
73        .with_weights(0.9, 0.1)?;
74    let sequential_detector = AnomalyDetector::with_config(sequential_config)?;
75    detectors.insert("sequential".to_string(), sequential_detector);
76
77    // Behavioral pattern detector (medium order)
78    let behavioral_config = AnomalyGridConfig::default()
79        .with_max_order(4)?
80        .with_smoothing_alpha(0.3)?
81        .with_weights(0.7, 0.3)?;
82    let behavioral_detector = AnomalyDetector::with_config(behavioral_config)?;
83    detectors.insert("behavioral".to_string(), behavioral_detector);
84
85    // Temporal pattern detector (high order)
86    let temporal_config = AnomalyGridConfig::default()
87        .with_max_order(8)?
88        .with_smoothing_alpha(0.2)?
89        .with_weights(0.8, 0.2)?;
90    let temporal_detector = AnomalyDetector::with_config(temporal_config)?;
91    detectors.insert("temporal".to_string(), temporal_detector);
92
93    // Network pattern detector (medium order)
94    let network_config = AnomalyGridConfig::default()
95        .with_max_order(5)?
96        .with_smoothing_alpha(0.4)?
97        .with_weights(0.6, 0.4)?;
98    let network_detector = AnomalyDetector::with_config(network_config)?;
99    detectors.insert("network".to_string(), network_detector);
100
101    println!("🔧 Sequential detector: order 6, α=0.1, weights=(0.9,0.1)");
102    println!("🧠 Behavioral detector: order 4, α=0.3, weights=(0.7,0.3)");
103    println!("⏰ Temporal detector: order 8, α=0.2, weights=(0.8,0.2)");
104    println!("🌐 Network detector: order 5, α=0.4, weights=(0.6,0.4)");
105
106    Ok(detectors)
107}
More examples
Hide additional examples
examples/quick_start.rs (line 19)
12fn main() -> Result<(), Box<dyn std::error::Error>> {
13    println!("🚀 Anomaly Grid - Quick Start Example");
14    println!("Demonstrating accurate anomaly detection with real patterns\n");
15
16    // Create detector with optimal configuration for this example
17    let config = AnomalyGridConfig::default()
18        .with_max_order(3)? // Good balance of context and performance
19        .with_smoothing_alpha(1.0)? // Standard Laplace smoothing
20        .with_weights(0.7, 0.3)?; // Emphasize likelihood over information
21
22    let mut detector = AnomalyDetector::with_config(config)?;
23    println!("✅ Created detector with order 3 and optimized configuration");
24
25    // Generate realistic training data with clear patterns
26    let normal_patterns = generate_realistic_training_data();
27    println!(
28        "📚 Generated {} training sequences with realistic patterns",
29        normal_patterns.len()
30    );
31
32    // Train the detector
33    let train_start = Instant::now();
34    detector.train(&normal_patterns)?;
35    let train_time = train_start.elapsed();
36
37    // Get training metrics
38    let metrics = detector.performance_metrics();
39    println!("🎯 Training completed in {:?}", train_time);
40    println!("   - Contexts learned: {}", metrics.context_count);
41    println!(
42        "   - Memory usage: {:.1} KB",
43        metrics.estimated_memory_bytes as f64 / 1024.0
44    );
45
46    // Test different scenarios with proper threshold tuning
47    println!("\n🔍 Testing Anomaly Detection Scenarios");
48
49    let test_scenarios = vec![
50        ("Normal Pattern", generate_normal_sequence(), 0.1),
51        ("Slight Deviation", generate_slight_deviation(), 0.05),
52        ("Clear Anomaly", generate_clear_anomaly(), 0.01),
53        ("Severe Anomaly", generate_severe_anomaly(), 0.001),
54    ];
55
56    for (scenario_name, test_sequence, threshold) in test_scenarios {
57        println!("\n--- {} (threshold: {}) ---", scenario_name, threshold);
58
59        let detect_start = Instant::now();
60        let anomalies = detector.detect_anomalies(&test_sequence, threshold)?;
61        let detect_time = detect_start.elapsed();
62
63        println!("Sequence: {:?}", test_sequence);
64        println!("Detection time: {:?}", detect_time);
65
66        if anomalies.is_empty() {
67            println!("✅ No anomalies detected (normal behavior)");
68        } else {
69            println!("⚠️  {} anomalies detected:", anomalies.len());
70
71            for (i, anomaly) in anomalies.iter().enumerate() {
72                println!("   Anomaly {}: {:?}", i + 1, anomaly.sequence);
73                println!("     - Likelihood: {:.6}", anomaly.likelihood);
74                println!("     - Anomaly Strength: {:.3}", anomaly.anomaly_strength);
75                println!("     - Information Score: {:.3}", anomaly.information_score);
76
77                // Classify anomaly severity
78                let severity = if anomaly.anomaly_strength > 0.8 {
79                    "🚨 CRITICAL"
80                } else if anomaly.anomaly_strength > 0.6 {
81                    "⚠️  HIGH"
82                } else if anomaly.anomaly_strength > 0.4 {
83                    "⚡ MEDIUM"
84                } else {
85                    "📝 LOW"
86                };
87
88                println!("     - Severity: {}", severity);
89            }
90        }
91    }
92
93    // Demonstrate threshold sensitivity analysis
94    println!("\n📊 Threshold Sensitivity Analysis");
95    demonstrate_threshold_sensitivity(&detector)?;
96
97    // Show mathematical properties
98    println!("\n🔬 Mathematical Properties Validation");
99    validate_mathematical_properties(&detector)?;
100
101    println!("\n🎉 Quick start example completed successfully!");
102    println!("💡 Key takeaways:");
103    println!("   - Higher thresholds detect fewer, stronger anomalies");
104    println!("   - Lower thresholds detect more, including weaker anomalies");
105    println!("   - Anomaly strength combines likelihood and information content");
106    println!("   - Detection is fast and mathematically sound");
107
108    Ok(())
109}
examples/behavioral_anomaly_detection.rs (line 23)
16fn main() -> Result<(), Box<dyn std::error::Error>> {
17    println!("🧠 Behavioral Anomaly Detection with Mathematical Validation");
18    println!("Learning user behavior patterns and detecting sophisticated anomalies\n");
19
20    // Configure detector for behavioral pattern analysis
21    let config = AnomalyGridConfig::default()
22        .with_max_order(6)? // High order for complex behavioral sequences
23        .with_smoothing_alpha(0.3)? // Low smoothing for precise pattern learning
24        .with_weights(0.8, 0.2)?; // Emphasize likelihood for behavioral patterns
25
26    let mut detector = AnomalyDetector::with_config(config)?;
27    println!("✅ Configured behavioral detector with order 6 for complex patterns");
28
29    // Phase 1: Generate and validate training data
30    println!("\n📚 Phase 1: Training Data Generation and Validation");
31    let (training_data, ground_truth) = generate_validated_behavioral_data();
32
33    // Validate training data quality
34    validate_training_data_quality(&training_data)?;
35
36    println!("📊 Generated {} behavioral sequences", training_data.len());
37    println!("🎯 Training data validation: PASSED");
38
39    // Phase 2: Train and validate model convergence
40    println!("\n🎯 Phase 2: Model Training and Convergence Validation");
41    let train_start = Instant::now();
42    detector.train(&training_data)?;
43    let train_time = train_start.elapsed();
44
45    let metrics = detector.performance_metrics();
46    println!("⏱️ Training completed in {:?}", train_time);
47    println!("🧮 Behavioral patterns learned: {}", metrics.context_count);
48    println!(
49        "💾 Memory usage: {:.1} KB",
50        metrics.estimated_memory_bytes as f64 / 1024.0
51    );
52
53    // Validate model convergence
54    validate_model_convergence(&detector, &training_data)?;
55
56    // Phase 3: Rigorous anomaly detection testing
57    println!("\n🔬 Phase 3: Rigorous Anomaly Detection Testing");
58
59    let test_scenarios = vec![
60        (
61            "Normal User Behavior",
62            generate_normal_user_session(),
63            false,
64            0.1,
65        ),
66        (
67            "Account Compromise",
68            generate_account_compromise(),
69            true,
70            0.05,
71        ),
72        (
73            "Insider Threat",
74            generate_insider_threat_behavior(),
75            true,
76            0.02,
77        ),
78        ("Bot/Automation", generate_bot_behavior(), true, 0.01),
79        (
80            "Social Engineering",
81            generate_social_engineering(),
82            true,
83            0.03,
84        ),
85        (
86            "Privilege Escalation",
87            generate_privilege_escalation_behavior(),
88            true,
89            0.02,
90        ),
91        (
92            "Data Exfiltration",
93            generate_data_exfiltration_behavior(),
94            true,
95            0.01,
96        ),
97    ];
98
99    let mut detection_results = Vec::new();
100    let mut total_detection_time = std::time::Duration::new(0, 0);
101
102    for (scenario_name, test_sequence, is_anomalous, threshold) in test_scenarios {
103        println!("\n--- Testing: {} ---", scenario_name);
104        println!(
105            "Expected: {}",
106            if is_anomalous { "ANOMALOUS" } else { "NORMAL" }
107        );
108        println!("Sequence length: {}", test_sequence.len());
109        println!("Threshold: {}", threshold);
110
111        let detect_start = Instant::now();
112        let anomalies = detector.detect_anomalies(&test_sequence, threshold)?;
113        let detect_time = detect_start.elapsed();
114        total_detection_time += detect_time;
115
116        let detected = !anomalies.is_empty();
117        let is_correct = detected == is_anomalous;
118
119        println!(
120            "🔍 Detection result: {}",
121            if detected { "ANOMALOUS" } else { "NORMAL" }
122        );
123        println!(
124            "✅ Correctness: {}",
125            if is_correct { "CORRECT" } else { "INCORRECT" }
126        );
127        println!("⏱️ Detection time: {:?}", detect_time);
128
129        if detected {
130            let max_strength = anomalies
131                .iter()
132                .map(|a| a.anomaly_strength)
133                .fold(0.0f64, f64::max);
134            let avg_information =
135                anomalies.iter().map(|a| a.information_score).sum::<f64>() / anomalies.len() as f64;
136
137            println!("📊 Anomalies detected: {}", anomalies.len());
138            println!("🎯 Max anomaly strength: {:.4}", max_strength);
139            println!("📈 Avg information score: {:.4}", avg_information);
140
141            // Validate mathematical properties
142            validate_anomaly_mathematical_properties(&anomalies)?;
143        }
144
145        detection_results.push((
146            scenario_name.to_string(),
147            is_anomalous,
148            detected,
149            is_correct,
150        ));
151    }
152
153    // Phase 4: Comprehensive validation and metrics
154    println!("\n📊 Phase 4: Comprehensive Validation and Metrics");
155
156    let accuracy = calculate_accuracy(&detection_results);
157    let (precision, recall, f1_score) = calculate_precision_recall_f1(&detection_results);
158
159    println!("🎯 Detection Accuracy: {:.1}%", accuracy * 100.0);
160    println!("🎯 Precision: {:.3}", precision);
161    println!("🎯 Recall: {:.3}", recall);
162    println!("🎯 F1 Score: {:.3}", f1_score);
163    println!(
164        "⏱️ Average detection time: {:?}",
165        total_detection_time / detection_results.len() as u32
166    );
167
168    // Phase 5: Advanced behavioral analysis
169    println!("\n🧬 Phase 5: Advanced Behavioral Analysis");
170    perform_behavioral_pattern_analysis(&detector)?;
171
172    // Phase 6: Threshold optimization
173    println!("\n⚙️ Phase 6: Threshold Optimization");
174    let optimal_threshold = optimize_detection_threshold(&detector)?;
175    println!("🎯 Optimal threshold: {:.4}", optimal_threshold);
176
177    // Phase 7: Robustness testing
178    println!("\n🛡️ Phase 7: Robustness Testing");
179    test_detection_robustness(&detector)?;
180
181    // Phase 8: Performance benchmarking
182    println!("\n⚡ Phase 8: Performance Benchmarking");
183    benchmark_detection_performance(&detector)?;
184
185    // Final validation summary
186    println!("\n✅ VALIDATION SUMMARY");
187    println!("═══════════════════════════════════");
188    println!("✅ Training data quality: VALIDATED");
189    println!("✅ Model convergence: VALIDATED");
190    println!("✅ Mathematical properties: VALIDATED");
191    println!("✅ Detection accuracy: {:.1}%", accuracy * 100.0);
192    println!("✅ F1 Score: {:.3}", f1_score);
193    println!(
194        "✅ Performance: {} detections/sec",
195        (1000.0 / total_detection_time.as_millis() as f64 * detection_results.len() as f64) as u32
196    );
197
198    if accuracy >= 0.85 && f1_score >= 0.8 {
199        println!("🎉 ALL VALIDATIONS PASSED - LIBRARY PERFORMANCE VERIFIED");
200    } else {
201        println!("⚠️ VALIDATION CONCERNS - REVIEW REQUIRED");
202    }
203
204    Ok(())
205}
examples/network_security_monitoring.rs (line 20)
13fn main() -> Result<(), Box<dyn std::error::Error>> {
14    println!("🛡️ Network Security Monitoring with Anomaly Grid");
15    println!("Detecting APTs, DDoS, and network intrusions with high accuracy\n");
16
17    // Configure detector for network security patterns
18    let config = AnomalyGridConfig::default()
19        .with_max_order(4)? // Higher order for complex attack patterns
20        .with_smoothing_alpha(0.5)? // Lower smoothing for better discrimination
21        .with_weights(0.6, 0.4)?; // Balance likelihood and information
22
23    let mut detector = AnomalyDetector::with_config(config)?;
24    println!("✅ Configured security detector with order 4 for complex patterns");
25
26    // Generate comprehensive normal network traffic
27    let normal_events = generate_enterprise_traffic(5);
28    println!(
29        "📊 Generated {} normal network events (5 days)",
30        normal_events.len()
31    );
32
33    // Train on normal network patterns
34    let train_start = Instant::now();
35    detector.train(&normal_events)?;
36    let train_time = train_start.elapsed();
37
38    let metrics = detector.performance_metrics();
39    println!("🎯 Training completed in {:?}", train_time);
40    println!("   - Security contexts learned: {}", metrics.context_count);
41    println!(
42        "   - Memory usage: {:.1} KB",
43        metrics.estimated_memory_bytes as f64 / 1024.0
44    );
45
46    // Real-time monitoring simulation with tuned thresholds
47    println!("\n🔍 Real-time Network Security Monitoring");
48
49    let attack_scenarios = vec![
50        ("Advanced Persistent Threat", generate_apt_campaign(), 0.001),
51        ("DDoS Attack", generate_ddos_attack(), 0.005),
52        ("Port Scan", generate_port_scan(), 0.01),
53        ("SQL Injection", generate_sql_injection(), 0.005),
54        ("Lateral Movement", generate_lateral_movement(), 0.001),
55        ("Data Exfiltration", generate_data_exfiltration(), 0.001),
56        (
57            "Malware C2 Communication",
58            generate_malware_communication(),
59            0.002,
60        ),
61    ];
62
63    let mut total_threats_detected = 0;
64    let mut critical_threats = 0;
65    let mut total_detection_time = std::time::Duration::new(0, 0);
66
67    for (attack_name, attack_sequence, threshold) in attack_scenarios {
68        println!("\n--- Analyzing: {} ---", attack_name);
69        println!("Sequence length: {} events", attack_sequence.len());
70        println!("Detection threshold: {}", threshold);
71
72        let detect_start = Instant::now();
73        let anomalies = detector.detect_anomalies(&attack_sequence, threshold)?;
74        let detect_time = detect_start.elapsed();
75        total_detection_time += detect_time;
76
77        if !anomalies.is_empty() {
78            total_threats_detected += 1;
79
80            let max_strength = anomalies
81                .iter()
82                .map(|a| a.anomaly_strength)
83                .fold(0.0, f64::max);
84
85            let avg_information =
86                anomalies.iter().map(|a| a.information_score).sum::<f64>() / anomalies.len() as f64;
87
88            let min_likelihood = anomalies
89                .iter()
90                .map(|a| a.likelihood)
91                .fold(f64::INFINITY, f64::min);
92
93            // Enhanced threat classification
94            let (threat_level, confidence) =
95                classify_threat(max_strength, avg_information, anomalies.len());
96
97            if threat_level == "CRITICAL" {
98                critical_threats += 1;
99            }
100
101            println!("  🚨 THREAT DETECTED");
102            println!("  📊 Anomalies found: {}", anomalies.len());
103            println!("  🎯 Max anomaly strength: {:.3}", max_strength);
104            println!("  📈 Avg information score: {:.3}", avg_information);
105            println!("  📉 Min likelihood: {:.2e}", min_likelihood);
106            println!("  ⚡ Detection time: {:?}", detect_time);
107            println!("  🔥 Threat Level: {}", threat_level);
108            println!("  🎲 Confidence: {:.1}%", confidence);
109
110            // Generate detailed security alert
111            generate_security_alert(attack_name, &threat_level, confidence, &anomalies);
112
113            // Show most suspicious patterns
114            if anomalies.len() > 0 {
115                let most_suspicious = anomalies
116                    .iter()
117                    .max_by(|a, b| a.anomaly_strength.partial_cmp(&b.anomaly_strength).unwrap())
118                    .unwrap();
119                println!(
120                    "  🔍 Most suspicious pattern: {:?}",
121                    most_suspicious.sequence
122                );
123                println!(
124                    "     Strength: {:.3}, Info: {:.3}",
125                    most_suspicious.anomaly_strength, most_suspicious.information_score
126                );
127            }
128        } else {
129            println!("  ✅ No threats detected (normal traffic)");
130        }
131    }
132
133    // Advanced analytics and ROC analysis
134    println!("\n📈 Advanced Security Analytics");
135    perform_roc_analysis(&detector)?;
136
137    // Batch processing demonstration with performance metrics
138    println!("\n📦 High-Volume Batch Processing");
139    let batch_sequences = vec![
140        generate_normal_session(),
141        generate_malware_communication(),
142        generate_data_exfiltration(),
143        generate_insider_threat(),
144        generate_zero_day_exploit(),
145    ];
146
147    let batch_start = Instant::now();
148    let config = AnomalyGridConfig::default().with_max_order(5)?;
149    let batch_results = batch_process_sequences(&batch_sequences, &config, 0.01)?;
150    let batch_time = batch_start.elapsed();
151
152    println!(
153        "Processed {} sequences in {:?}",
154        batch_sequences.len(),
155        batch_time
156    );
157    let throughput = batch_sequences.len() as f64 / batch_time.as_secs_f64();
158    println!("Throughput: {:.1} sequences/second", throughput);
159
160    for (i, results) in batch_results.iter().enumerate() {
161        let sequence_types = [
162            "Normal Session",
163            "Malware C2",
164            "Data Exfiltration",
165            "Insider Threat",
166            "Zero-day Exploit",
167        ];
168        let risk_score = calculate_risk_score(results);
169        println!(
170            "  {}: {} anomalies, risk score: {:.2}",
171            sequence_types[i],
172            results.len(),
173            risk_score
174        );
175    }
176
177    // Performance and accuracy summary
178    println!("\n📊 Security Monitoring Summary");
179    println!("═══════════════════════════════════");
180    println!(
181        "Threats detected: {}/7 ({:.1}%)",
182        total_threats_detected,
183        (total_threats_detected as f64 / 7.0) * 100.0
184    );
185    println!("Critical threats: {}", critical_threats);
186    println!(
187        "Average detection time: {:?}",
188        total_detection_time / total_threats_detected.max(1) as u32
189    );
190
191    let false_positive_rate = calculate_false_positive_rate(&detector)?;
192    println!("Estimated false positive rate: {:.2}%", false_positive_rate);
193
194    // Calculate estimated cost savings
195    let cost_savings = calculate_security_cost_savings(critical_threats, total_threats_detected);
196    println!("Estimated cost savings: ${:.0}", cost_savings);
197
198    println!("\n💡 Security Insights:");
199    println!("   - APT campaigns show complex multi-stage patterns");
200    println!("   - DDoS attacks have high-volume repetitive signatures");
201    println!("   - Lateral movement creates subtle context anomalies");
202    println!("   - Data exfiltration shows unusual data flow patterns");
203    println!("   - Real-time detection enables rapid incident response");
204
205    Ok(())
206}
examples/time_series_anomaly_detection.rs (line 21)
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15    println!("📈 Time Series Anomaly Detection with Mathematical Validation");
16    println!("Converting continuous data to categorical sequences for anomaly detection\n");
17
18    // Configure detector for time series pattern analysis
19    let config = AnomalyGridConfig::default()
20        .with_max_order(8)? // High order for temporal dependencies
21        .with_smoothing_alpha(0.2)? // Low smoothing for precise pattern learning
22        .with_weights(0.9, 0.1)?; // Emphasize likelihood for temporal patterns
23
24    let mut detector = AnomalyDetector::with_config(config)?;
25    println!("✅ Configured time series detector with order 8 for temporal patterns");
26
27    // Phase 1: Generate and validate time series training data
28    println!("\n📊 Phase 1: Time Series Data Generation and Discretization");
29    let (raw_time_series, discretized_series) = generate_time_series_data(1000)?;
30
31    // Validate discretization quality
32    validate_discretization_quality(&raw_time_series, &discretized_series)?;
33
34    println!("📈 Generated {} time series points", raw_time_series.len());
35    println!(
36        "🔢 Discretized to {} categorical states",
37        discretized_series.len()
38    );
39    println!("🎯 Discretization validation: PASSED");
40
41    // Phase 2: Train and validate temporal model
42    println!("\n🎯 Phase 2: Temporal Model Training and Validation");
43    let train_start = Instant::now();
44    detector.train(&discretized_series)?;
45    let train_time = train_start.elapsed();
46
47    let metrics = detector.performance_metrics();
48    println!("⏱️ Training completed in {:?}", train_time);
49    println!("🧮 Temporal patterns learned: {}", metrics.context_count);
50    println!(
51        "💾 Memory usage: {:.1} KB",
52        metrics.estimated_memory_bytes as f64 / 1024.0
53    );
54
55    // Validate temporal consistency
56    validate_temporal_consistency(&detector, &discretized_series)?;
57
58    // Phase 3: Comprehensive anomaly detection testing
59    println!("\n🔬 Phase 3: Time Series Anomaly Detection Testing");
60
61    let test_scenarios = vec![
62        ("Normal Trend", generate_normal_trend(), false, 0.1),
63        ("Sudden Spike", generate_sudden_spike(), true, 0.05),
64        ("Gradual Drift", generate_gradual_drift(), true, 0.08),
65        ("Periodic Anomaly", generate_periodic_anomaly(), true, 0.03),
66        ("Noise Burst", generate_noise_burst(), true, 0.02),
67        ("Level Shift", generate_level_shift(), true, 0.05),
68        ("Trend Change", generate_trend_change(), true, 0.06),
69        ("Seasonal Break", generate_seasonal_break(), true, 0.04),
70    ];
71
72    let mut detection_results = Vec::new();
73    let mut total_detection_time = std::time::Duration::new(0, 0);
74
75    for (scenario_name, (raw_data, test_sequence), is_anomalous, threshold) in test_scenarios {
76        println!("\n--- Testing: {} ---", scenario_name);
77        println!(
78            "Expected: {}",
79            if is_anomalous { "ANOMALOUS" } else { "NORMAL" }
80        );
81        println!("Raw data points: {}", raw_data.len());
82        println!("Discretized sequence length: {}", test_sequence.len());
83        println!("Threshold: {}", threshold);
84
85        let detect_start = Instant::now();
86        let anomalies = detector.detect_anomalies(&test_sequence, threshold)?;
87        let detect_time = detect_start.elapsed();
88        total_detection_time += detect_time;
89
90        let detected = !anomalies.is_empty();
91        let is_correct = detected == is_anomalous;
92
93        println!(
94            "🔍 Detection result: {}",
95            if detected { "ANOMALOUS" } else { "NORMAL" }
96        );
97        println!(
98            "✅ Correctness: {}",
99            if is_correct { "CORRECT" } else { "INCORRECT" }
100        );
101        println!("⏱️ Detection time: {:?}", detect_time);
102
103        if detected {
104            let max_strength = anomalies
105                .iter()
106                .map(|a| a.anomaly_strength)
107                .fold(0.0f64, f64::max);
108            let avg_information =
109                anomalies.iter().map(|a| a.information_score).sum::<f64>() / anomalies.len() as f64;
110
111            println!("📊 Anomalies detected: {}", anomalies.len());
112            println!("🎯 Max anomaly strength: {:.4}", max_strength);
113            println!("📈 Avg information score: {:.4}", avg_information);
114
115            // Validate mathematical properties
116            validate_anomaly_mathematical_properties(&anomalies)?;
117
118            // Show temporal context of strongest anomaly
119            if let Some(strongest) = anomalies
120                .iter()
121                .max_by(|a, b| a.anomaly_strength.partial_cmp(&b.anomaly_strength).unwrap())
122            {
123                println!("🔍 Strongest anomaly pattern: {:?}", strongest.sequence);
124            }
125        }
126
127        detection_results.push((
128            scenario_name.to_string(),
129            is_anomalous,
130            detected,
131            is_correct,
132        ));
133    }
134
135    // Phase 4: Statistical validation and metrics
136    println!("\n📊 Phase 4: Statistical Validation and Performance Metrics");
137
138    let accuracy = calculate_accuracy(&detection_results);
139    let (precision, recall, f1_score) = calculate_precision_recall_f1(&detection_results);
140
141    println!("🎯 Detection Accuracy: {:.1}%", accuracy * 100.0);
142    println!("🎯 Precision: {:.3}", precision);
143    println!("🎯 Recall: {:.3}", recall);
144    println!("🎯 F1 Score: {:.3}", f1_score);
145    println!(
146        "⏱️ Average detection time: {:?}",
147        total_detection_time / detection_results.len() as u32
148    );
149
150    // Phase 5: Temporal pattern analysis
151    println!("\n⏰ Phase 5: Temporal Pattern Analysis");
152    analyze_temporal_patterns(&detector)?;
153
154    // Phase 6: Sensitivity analysis
155    println!("\n🎚️ Phase 6: Sensitivity Analysis");
156    perform_sensitivity_analysis(&detector)?;
157
158    // Phase 7: Robustness testing with edge cases
159    println!("\n🛡️ Phase 7: Robustness Testing");
160    test_temporal_robustness(&detector)?;
161
162    // Phase 8: Performance benchmarking
163    println!("\n⚡ Phase 8: Performance Benchmarking");
164    benchmark_temporal_performance(&detector)?;
165
166    // Phase 9: Mathematical validation
167    println!("\n🔬 Phase 9: Mathematical Property Validation");
168    validate_mathematical_properties(&detector)?;
169
170    // Final validation summary
171    println!("\n✅ COMPREHENSIVE VALIDATION SUMMARY");
172    println!("═══════════════════════════════════════");
173    println!("✅ Time series discretization: VALIDATED");
174    println!("✅ Temporal consistency: VALIDATED");
175    println!("✅ Mathematical properties: VALIDATED");
176    println!("✅ Detection accuracy: {:.1}%", accuracy * 100.0);
177    println!("✅ F1 Score: {:.3}", f1_score);
178    println!(
179        "✅ Performance: {} detections/sec",
180        (1000.0 / total_detection_time.as_millis() as f64 * detection_results.len() as f64) as u32
181    );
182
183    // Determine overall validation status
184    let validation_passed = accuracy >= 0.8
185        && f1_score >= 0.75
186        && total_detection_time.as_millis() < 100 * detection_results.len() as u128;
187
188    if validation_passed {
189        println!("🎉 ALL VALIDATIONS PASSED - TIME SERIES DETECTION VERIFIED");
190        println!("📈 Library successfully handles temporal anomaly detection");
191    } else {
192        println!("⚠️ VALIDATION CONCERNS - REVIEW REQUIRED");
193        if accuracy < 0.8 {
194            println!("   - Accuracy below threshold");
195        }
196        if f1_score < 0.75 {
197            println!("   - F1 score below threshold");
198        }
199    }
200
201    Ok(())
202}
examples/financial_fraud_detection.rs (line 19)
12fn main() -> Result<(), Box<dyn std::error::Error>> {
13    println!("💳 Financial Fraud Detection with Anomaly Grid");
14    println!("Detecting credit card fraud, velocity attacks, and suspicious patterns\n");
15
16    // Configure detector for financial transaction patterns
17    let config = AnomalyGridConfig::default()
18        .with_max_order(5)? // Higher order for complex fraud patterns
19        .with_smoothing_alpha(0.8)? // Moderate smoothing for financial data
20        .with_weights(0.5, 0.5)?; // Balance likelihood and information equally
21
22    let mut detector = AnomalyDetector::with_config(config)?;
23    println!("✅ Configured fraud detector with order 5 for complex patterns");
24
25    // Generate comprehensive normal transaction data
26    let normal_transactions = generate_normal_transactions(30); // 1 month
27    println!(
28        "📊 Generated {} normal transactions (1 month)",
29        normal_transactions.len()
30    );
31
32    // Train on normal transaction patterns
33    let train_start = Instant::now();
34    detector.train(&normal_transactions)?;
35    let train_time = train_start.elapsed();
36
37    let metrics = detector.performance_metrics();
38    println!("🎯 Training completed in {:?}", train_time);
39    println!(
40        "   - Transaction patterns learned: {}",
41        metrics.context_count
42    );
43    println!(
44        "   - Memory usage: {:.1} KB",
45        metrics.estimated_memory_bytes as f64 / 1024.0
46    );
47
48    // Real-time fraud detection simulation with optimized thresholds
49    println!("\n🔍 Real-time Fraud Detection Simulation");
50
51    let fraud_scenarios = vec![
52        ("Card Testing", generate_card_testing(), 0.01),
53        ("Velocity Attack", generate_velocity_attack(), 0.005),
54        ("Geographic Anomaly", generate_geographic_anomaly(), 0.02),
55        ("Amount Anomaly", generate_amount_anomaly(), 0.01),
56        ("Account Takeover", generate_account_takeover(), 0.001),
57        ("Synthetic Identity", generate_synthetic_identity(), 0.001),
58        ("Merchant Fraud", generate_merchant_fraud(), 0.005),
59        ("Money Laundering", generate_money_laundering(), 0.002),
60    ];
61
62    let mut total_fraud_detected = 0;
63    let mut total_fraud_amount = 0.0;
64    let mut critical_fraud_cases = 0;
65    let mut total_detection_time = std::time::Duration::new(0, 0);
66
67    for (fraud_type, fraud_sequence, threshold) in fraud_scenarios {
68        println!("\n--- Analyzing: {} ---", fraud_type);
69        println!("Transaction sequence length: {}", fraud_sequence.len());
70        println!("Detection threshold: {}", threshold);
71
72        let detect_start = Instant::now();
73        let anomalies = detector.detect_anomalies(&fraud_sequence, threshold)?;
74        let detect_time = detect_start.elapsed();
75        total_detection_time += detect_time;
76
77        if !anomalies.is_empty() {
78            total_fraud_detected += 1;
79
80            let fraud_score = calculate_fraud_score(&anomalies);
81            let (risk_level, confidence) = classify_fraud_risk(fraud_score, anomalies.len());
82            let estimated_amount = estimate_fraud_amount(&fraud_sequence);
83            total_fraud_amount += estimated_amount;
84
85            if risk_level == "CRITICAL" {
86                critical_fraud_cases += 1;
87            }
88
89            println!("  🚨 FRAUD DETECTED");
90            println!("  📊 Anomalies: {}", anomalies.len());
91            println!("  🎯 Fraud Score: {:.2}", fraud_score);
92            println!("  🔥 Risk Level: {}", risk_level);
93            println!("  🎲 Confidence: {:.1}%", confidence);
94            println!("  💰 Estimated Amount: ${:.2}", estimated_amount);
95            println!("  ⚡ Detection Time: {:?}", detect_time);
96
97            // Generate detailed fraud alert
98            generate_fraud_alert(
99                fraud_type,
100                &risk_level,
101                estimated_amount,
102                fraud_score,
103                confidence,
104            );
105
106            // Show most suspicious transaction pattern
107            if let Some(most_suspicious) = anomalies
108                .iter()
109                .max_by(|a, b| a.anomaly_strength.partial_cmp(&b.anomaly_strength).unwrap())
110            {
111                println!(
112                    "  🔍 Most suspicious pattern: {:?}",
113                    most_suspicious.sequence
114                );
115                println!(
116                    "     Strength: {:.3}, Info: {:.3}",
117                    most_suspicious.anomaly_strength, most_suspicious.information_score
118                );
119            }
120        } else {
121            println!("  ✅ No fraud detected (legitimate transactions)");
122        }
123    }
124
125    // Advanced fraud analytics
126    println!("\n📈 Advanced Fraud Analytics");
127    perform_fraud_roc_analysis(&detector)?;
128
129    // High-volume transaction processing simulation
130    println!("\n📦 High-Volume Transaction Processing");
131    let batch_transactions = generate_mixed_transaction_batch(1000);
132
133    let batch_start = Instant::now();
134    let mut fraud_count = 0;
135    let mut legitimate_count = 0;
136    let mut total_risk_score = 0.0;
137
138    // Process in chunks for memory efficiency
139    for chunk in batch_transactions.chunks(100) {
140        let anomalies = detector.detect_anomalies(chunk, 0.05)?;
141        if !anomalies.is_empty() {
142            fraud_count += 1;
143            total_risk_score += calculate_fraud_score(&anomalies);
144        } else {
145            legitimate_count += 1;
146        }
147    }
148
149    let batch_time = batch_start.elapsed();
150    let throughput = batch_transactions.len() as f64 / batch_time.as_secs_f64();
151
152    println!(
153        "Processed {} transactions in {:?}",
154        batch_transactions.len(),
155        batch_time
156    );
157    println!("Throughput: {:.0} transactions/second", throughput);
158    println!(
159        "Fraud detected: {} batches ({:.1}%)",
160        fraud_count,
161        (fraud_count as f64 / (fraud_count + legitimate_count) as f64) * 100.0
162    );
163    println!("Legitimate: {} batches", legitimate_count);
164    println!(
165        "Average risk score: {:.2}",
166        total_risk_score / fraud_count.max(1) as f64
167    );
168
169    // Performance and accuracy summary
170    println!("\n📊 Fraud Detection Summary");
171    println!("═══════════════════════════════════");
172    println!(
173        "Fraud scenarios detected: {}/8 ({:.1}%)",
174        total_fraud_detected,
175        (total_fraud_detected as f64 / 8.0) * 100.0
176    );
177    println!("Critical fraud cases: {}", critical_fraud_cases);
178    println!("Total fraud amount prevented: ${:.2}", total_fraud_amount);
179    println!(
180        "Average detection time: {:?}",
181        total_detection_time / total_fraud_detected.max(1) as u32
182    );
183
184    // Calculate ROI and cost savings
185    let investigation_cost = total_fraud_detected as f64 * 500.0; // $500 per investigation
186    let net_savings = total_fraud_amount - investigation_cost;
187    let roi = if investigation_cost > 0.0 {
188        (net_savings / investigation_cost) * 100.0
189    } else {
190        0.0
191    };
192
193    println!("Investigation costs: ${:.2}", investigation_cost);
194    println!("Net savings: ${:.2}", net_savings);
195    println!("ROI: {:.1}%", roi);
196
197    // Calculate precision and recall estimates
198    let precision = calculate_fraud_precision(&detector)?;
199    let recall = calculate_fraud_recall(&detector)?;
200    let f1_score = 2.0 * (precision * recall) / (precision + recall);
201
202    println!("Estimated precision: {:.1}%", precision);
203    println!("Estimated recall: {:.1}%", recall);
204    println!("F1 score: {:.3}", f1_score);
205
206    println!("\n💡 Fraud Detection Insights:");
207    println!("   - Card testing shows rapid small-amount probing patterns");
208    println!("   - Velocity attacks have high-frequency transaction bursts");
209    println!("   - Geographic anomalies show impossible travel patterns");
210    println!("   - Account takeovers exhibit sudden behavior changes");
211    println!("   - Synthetic identities show artificial credit building");
212    println!("   - Real-time detection enables immediate card blocking");
213
214    Ok(())
215}
Source

pub fn with_memory_limit(self, limit: Option<usize>) -> AnomalyGridResult<Self>

Set memory_limit with validation

Source

pub fn with_weights( self, likelihood_weight: f64, information_weight: f64, ) -> AnomalyGridResult<Self>

Set anomaly strength weights with validation

Examples found in repository?
examples/multi_modal_anomaly_detection.rs (line 73)
65fn create_multi_modal_detectors(
66) -> Result<HashMap<String, AnomalyDetector>, Box<dyn std::error::Error>> {
67    let mut detectors = HashMap::new();
68
69    // Sequential pattern detector (high order)
70    let sequential_config = AnomalyGridConfig::default()
71        .with_max_order(6)?
72        .with_smoothing_alpha(0.1)?
73        .with_weights(0.9, 0.1)?;
74    let sequential_detector = AnomalyDetector::with_config(sequential_config)?;
75    detectors.insert("sequential".to_string(), sequential_detector);
76
77    // Behavioral pattern detector (medium order)
78    let behavioral_config = AnomalyGridConfig::default()
79        .with_max_order(4)?
80        .with_smoothing_alpha(0.3)?
81        .with_weights(0.7, 0.3)?;
82    let behavioral_detector = AnomalyDetector::with_config(behavioral_config)?;
83    detectors.insert("behavioral".to_string(), behavioral_detector);
84
85    // Temporal pattern detector (high order)
86    let temporal_config = AnomalyGridConfig::default()
87        .with_max_order(8)?
88        .with_smoothing_alpha(0.2)?
89        .with_weights(0.8, 0.2)?;
90    let temporal_detector = AnomalyDetector::with_config(temporal_config)?;
91    detectors.insert("temporal".to_string(), temporal_detector);
92
93    // Network pattern detector (medium order)
94    let network_config = AnomalyGridConfig::default()
95        .with_max_order(5)?
96        .with_smoothing_alpha(0.4)?
97        .with_weights(0.6, 0.4)?;
98    let network_detector = AnomalyDetector::with_config(network_config)?;
99    detectors.insert("network".to_string(), network_detector);
100
101    println!("🔧 Sequential detector: order 6, α=0.1, weights=(0.9,0.1)");
102    println!("🧠 Behavioral detector: order 4, α=0.3, weights=(0.7,0.3)");
103    println!("⏰ Temporal detector: order 8, α=0.2, weights=(0.8,0.2)");
104    println!("🌐 Network detector: order 5, α=0.4, weights=(0.6,0.4)");
105
106    Ok(detectors)
107}
More examples
Hide additional examples
examples/quick_start.rs (line 20)
12fn main() -> Result<(), Box<dyn std::error::Error>> {
13    println!("🚀 Anomaly Grid - Quick Start Example");
14    println!("Demonstrating accurate anomaly detection with real patterns\n");
15
16    // Create detector with optimal configuration for this example
17    let config = AnomalyGridConfig::default()
18        .with_max_order(3)? // Good balance of context and performance
19        .with_smoothing_alpha(1.0)? // Standard Laplace smoothing
20        .with_weights(0.7, 0.3)?; // Emphasize likelihood over information
21
22    let mut detector = AnomalyDetector::with_config(config)?;
23    println!("✅ Created detector with order 3 and optimized configuration");
24
25    // Generate realistic training data with clear patterns
26    let normal_patterns = generate_realistic_training_data();
27    println!(
28        "📚 Generated {} training sequences with realistic patterns",
29        normal_patterns.len()
30    );
31
32    // Train the detector
33    let train_start = Instant::now();
34    detector.train(&normal_patterns)?;
35    let train_time = train_start.elapsed();
36
37    // Get training metrics
38    let metrics = detector.performance_metrics();
39    println!("🎯 Training completed in {:?}", train_time);
40    println!("   - Contexts learned: {}", metrics.context_count);
41    println!(
42        "   - Memory usage: {:.1} KB",
43        metrics.estimated_memory_bytes as f64 / 1024.0
44    );
45
46    // Test different scenarios with proper threshold tuning
47    println!("\n🔍 Testing Anomaly Detection Scenarios");
48
49    let test_scenarios = vec![
50        ("Normal Pattern", generate_normal_sequence(), 0.1),
51        ("Slight Deviation", generate_slight_deviation(), 0.05),
52        ("Clear Anomaly", generate_clear_anomaly(), 0.01),
53        ("Severe Anomaly", generate_severe_anomaly(), 0.001),
54    ];
55
56    for (scenario_name, test_sequence, threshold) in test_scenarios {
57        println!("\n--- {} (threshold: {}) ---", scenario_name, threshold);
58
59        let detect_start = Instant::now();
60        let anomalies = detector.detect_anomalies(&test_sequence, threshold)?;
61        let detect_time = detect_start.elapsed();
62
63        println!("Sequence: {:?}", test_sequence);
64        println!("Detection time: {:?}", detect_time);
65
66        if anomalies.is_empty() {
67            println!("✅ No anomalies detected (normal behavior)");
68        } else {
69            println!("⚠️  {} anomalies detected:", anomalies.len());
70
71            for (i, anomaly) in anomalies.iter().enumerate() {
72                println!("   Anomaly {}: {:?}", i + 1, anomaly.sequence);
73                println!("     - Likelihood: {:.6}", anomaly.likelihood);
74                println!("     - Anomaly Strength: {:.3}", anomaly.anomaly_strength);
75                println!("     - Information Score: {:.3}", anomaly.information_score);
76
77                // Classify anomaly severity
78                let severity = if anomaly.anomaly_strength > 0.8 {
79                    "🚨 CRITICAL"
80                } else if anomaly.anomaly_strength > 0.6 {
81                    "⚠️  HIGH"
82                } else if anomaly.anomaly_strength > 0.4 {
83                    "⚡ MEDIUM"
84                } else {
85                    "📝 LOW"
86                };
87
88                println!("     - Severity: {}", severity);
89            }
90        }
91    }
92
93    // Demonstrate threshold sensitivity analysis
94    println!("\n📊 Threshold Sensitivity Analysis");
95    demonstrate_threshold_sensitivity(&detector)?;
96
97    // Show mathematical properties
98    println!("\n🔬 Mathematical Properties Validation");
99    validate_mathematical_properties(&detector)?;
100
101    println!("\n🎉 Quick start example completed successfully!");
102    println!("💡 Key takeaways:");
103    println!("   - Higher thresholds detect fewer, stronger anomalies");
104    println!("   - Lower thresholds detect more, including weaker anomalies");
105    println!("   - Anomaly strength combines likelihood and information content");
106    println!("   - Detection is fast and mathematically sound");
107
108    Ok(())
109}
examples/behavioral_anomaly_detection.rs (line 24)
16fn main() -> Result<(), Box<dyn std::error::Error>> {
17    println!("🧠 Behavioral Anomaly Detection with Mathematical Validation");
18    println!("Learning user behavior patterns and detecting sophisticated anomalies\n");
19
20    // Configure detector for behavioral pattern analysis
21    let config = AnomalyGridConfig::default()
22        .with_max_order(6)? // High order for complex behavioral sequences
23        .with_smoothing_alpha(0.3)? // Low smoothing for precise pattern learning
24        .with_weights(0.8, 0.2)?; // Emphasize likelihood for behavioral patterns
25
26    let mut detector = AnomalyDetector::with_config(config)?;
27    println!("✅ Configured behavioral detector with order 6 for complex patterns");
28
29    // Phase 1: Generate and validate training data
30    println!("\n📚 Phase 1: Training Data Generation and Validation");
31    let (training_data, ground_truth) = generate_validated_behavioral_data();
32
33    // Validate training data quality
34    validate_training_data_quality(&training_data)?;
35
36    println!("📊 Generated {} behavioral sequences", training_data.len());
37    println!("🎯 Training data validation: PASSED");
38
39    // Phase 2: Train and validate model convergence
40    println!("\n🎯 Phase 2: Model Training and Convergence Validation");
41    let train_start = Instant::now();
42    detector.train(&training_data)?;
43    let train_time = train_start.elapsed();
44
45    let metrics = detector.performance_metrics();
46    println!("⏱️ Training completed in {:?}", train_time);
47    println!("🧮 Behavioral patterns learned: {}", metrics.context_count);
48    println!(
49        "💾 Memory usage: {:.1} KB",
50        metrics.estimated_memory_bytes as f64 / 1024.0
51    );
52
53    // Validate model convergence
54    validate_model_convergence(&detector, &training_data)?;
55
56    // Phase 3: Rigorous anomaly detection testing
57    println!("\n🔬 Phase 3: Rigorous Anomaly Detection Testing");
58
59    let test_scenarios = vec![
60        (
61            "Normal User Behavior",
62            generate_normal_user_session(),
63            false,
64            0.1,
65        ),
66        (
67            "Account Compromise",
68            generate_account_compromise(),
69            true,
70            0.05,
71        ),
72        (
73            "Insider Threat",
74            generate_insider_threat_behavior(),
75            true,
76            0.02,
77        ),
78        ("Bot/Automation", generate_bot_behavior(), true, 0.01),
79        (
80            "Social Engineering",
81            generate_social_engineering(),
82            true,
83            0.03,
84        ),
85        (
86            "Privilege Escalation",
87            generate_privilege_escalation_behavior(),
88            true,
89            0.02,
90        ),
91        (
92            "Data Exfiltration",
93            generate_data_exfiltration_behavior(),
94            true,
95            0.01,
96        ),
97    ];
98
99    let mut detection_results = Vec::new();
100    let mut total_detection_time = std::time::Duration::new(0, 0);
101
102    for (scenario_name, test_sequence, is_anomalous, threshold) in test_scenarios {
103        println!("\n--- Testing: {} ---", scenario_name);
104        println!(
105            "Expected: {}",
106            if is_anomalous { "ANOMALOUS" } else { "NORMAL" }
107        );
108        println!("Sequence length: {}", test_sequence.len());
109        println!("Threshold: {}", threshold);
110
111        let detect_start = Instant::now();
112        let anomalies = detector.detect_anomalies(&test_sequence, threshold)?;
113        let detect_time = detect_start.elapsed();
114        total_detection_time += detect_time;
115
116        let detected = !anomalies.is_empty();
117        let is_correct = detected == is_anomalous;
118
119        println!(
120            "🔍 Detection result: {}",
121            if detected { "ANOMALOUS" } else { "NORMAL" }
122        );
123        println!(
124            "✅ Correctness: {}",
125            if is_correct { "CORRECT" } else { "INCORRECT" }
126        );
127        println!("⏱️ Detection time: {:?}", detect_time);
128
129        if detected {
130            let max_strength = anomalies
131                .iter()
132                .map(|a| a.anomaly_strength)
133                .fold(0.0f64, f64::max);
134            let avg_information =
135                anomalies.iter().map(|a| a.information_score).sum::<f64>() / anomalies.len() as f64;
136
137            println!("📊 Anomalies detected: {}", anomalies.len());
138            println!("🎯 Max anomaly strength: {:.4}", max_strength);
139            println!("📈 Avg information score: {:.4}", avg_information);
140
141            // Validate mathematical properties
142            validate_anomaly_mathematical_properties(&anomalies)?;
143        }
144
145        detection_results.push((
146            scenario_name.to_string(),
147            is_anomalous,
148            detected,
149            is_correct,
150        ));
151    }
152
153    // Phase 4: Comprehensive validation and metrics
154    println!("\n📊 Phase 4: Comprehensive Validation and Metrics");
155
156    let accuracy = calculate_accuracy(&detection_results);
157    let (precision, recall, f1_score) = calculate_precision_recall_f1(&detection_results);
158
159    println!("🎯 Detection Accuracy: {:.1}%", accuracy * 100.0);
160    println!("🎯 Precision: {:.3}", precision);
161    println!("🎯 Recall: {:.3}", recall);
162    println!("🎯 F1 Score: {:.3}", f1_score);
163    println!(
164        "⏱️ Average detection time: {:?}",
165        total_detection_time / detection_results.len() as u32
166    );
167
168    // Phase 5: Advanced behavioral analysis
169    println!("\n🧬 Phase 5: Advanced Behavioral Analysis");
170    perform_behavioral_pattern_analysis(&detector)?;
171
172    // Phase 6: Threshold optimization
173    println!("\n⚙️ Phase 6: Threshold Optimization");
174    let optimal_threshold = optimize_detection_threshold(&detector)?;
175    println!("🎯 Optimal threshold: {:.4}", optimal_threshold);
176
177    // Phase 7: Robustness testing
178    println!("\n🛡️ Phase 7: Robustness Testing");
179    test_detection_robustness(&detector)?;
180
181    // Phase 8: Performance benchmarking
182    println!("\n⚡ Phase 8: Performance Benchmarking");
183    benchmark_detection_performance(&detector)?;
184
185    // Final validation summary
186    println!("\n✅ VALIDATION SUMMARY");
187    println!("═══════════════════════════════════");
188    println!("✅ Training data quality: VALIDATED");
189    println!("✅ Model convergence: VALIDATED");
190    println!("✅ Mathematical properties: VALIDATED");
191    println!("✅ Detection accuracy: {:.1}%", accuracy * 100.0);
192    println!("✅ F1 Score: {:.3}", f1_score);
193    println!(
194        "✅ Performance: {} detections/sec",
195        (1000.0 / total_detection_time.as_millis() as f64 * detection_results.len() as f64) as u32
196    );
197
198    if accuracy >= 0.85 && f1_score >= 0.8 {
199        println!("🎉 ALL VALIDATIONS PASSED - LIBRARY PERFORMANCE VERIFIED");
200    } else {
201        println!("⚠️ VALIDATION CONCERNS - REVIEW REQUIRED");
202    }
203
204    Ok(())
205}
examples/network_security_monitoring.rs (line 21)
13fn main() -> Result<(), Box<dyn std::error::Error>> {
14    println!("🛡️ Network Security Monitoring with Anomaly Grid");
15    println!("Detecting APTs, DDoS, and network intrusions with high accuracy\n");
16
17    // Configure detector for network security patterns
18    let config = AnomalyGridConfig::default()
19        .with_max_order(4)? // Higher order for complex attack patterns
20        .with_smoothing_alpha(0.5)? // Lower smoothing for better discrimination
21        .with_weights(0.6, 0.4)?; // Balance likelihood and information
22
23    let mut detector = AnomalyDetector::with_config(config)?;
24    println!("✅ Configured security detector with order 4 for complex patterns");
25
26    // Generate comprehensive normal network traffic
27    let normal_events = generate_enterprise_traffic(5);
28    println!(
29        "📊 Generated {} normal network events (5 days)",
30        normal_events.len()
31    );
32
33    // Train on normal network patterns
34    let train_start = Instant::now();
35    detector.train(&normal_events)?;
36    let train_time = train_start.elapsed();
37
38    let metrics = detector.performance_metrics();
39    println!("🎯 Training completed in {:?}", train_time);
40    println!("   - Security contexts learned: {}", metrics.context_count);
41    println!(
42        "   - Memory usage: {:.1} KB",
43        metrics.estimated_memory_bytes as f64 / 1024.0
44    );
45
46    // Real-time monitoring simulation with tuned thresholds
47    println!("\n🔍 Real-time Network Security Monitoring");
48
49    let attack_scenarios = vec![
50        ("Advanced Persistent Threat", generate_apt_campaign(), 0.001),
51        ("DDoS Attack", generate_ddos_attack(), 0.005),
52        ("Port Scan", generate_port_scan(), 0.01),
53        ("SQL Injection", generate_sql_injection(), 0.005),
54        ("Lateral Movement", generate_lateral_movement(), 0.001),
55        ("Data Exfiltration", generate_data_exfiltration(), 0.001),
56        (
57            "Malware C2 Communication",
58            generate_malware_communication(),
59            0.002,
60        ),
61    ];
62
63    let mut total_threats_detected = 0;
64    let mut critical_threats = 0;
65    let mut total_detection_time = std::time::Duration::new(0, 0);
66
67    for (attack_name, attack_sequence, threshold) in attack_scenarios {
68        println!("\n--- Analyzing: {} ---", attack_name);
69        println!("Sequence length: {} events", attack_sequence.len());
70        println!("Detection threshold: {}", threshold);
71
72        let detect_start = Instant::now();
73        let anomalies = detector.detect_anomalies(&attack_sequence, threshold)?;
74        let detect_time = detect_start.elapsed();
75        total_detection_time += detect_time;
76
77        if !anomalies.is_empty() {
78            total_threats_detected += 1;
79
80            let max_strength = anomalies
81                .iter()
82                .map(|a| a.anomaly_strength)
83                .fold(0.0, f64::max);
84
85            let avg_information =
86                anomalies.iter().map(|a| a.information_score).sum::<f64>() / anomalies.len() as f64;
87
88            let min_likelihood = anomalies
89                .iter()
90                .map(|a| a.likelihood)
91                .fold(f64::INFINITY, f64::min);
92
93            // Enhanced threat classification
94            let (threat_level, confidence) =
95                classify_threat(max_strength, avg_information, anomalies.len());
96
97            if threat_level == "CRITICAL" {
98                critical_threats += 1;
99            }
100
101            println!("  🚨 THREAT DETECTED");
102            println!("  📊 Anomalies found: {}", anomalies.len());
103            println!("  🎯 Max anomaly strength: {:.3}", max_strength);
104            println!("  📈 Avg information score: {:.3}", avg_information);
105            println!("  📉 Min likelihood: {:.2e}", min_likelihood);
106            println!("  ⚡ Detection time: {:?}", detect_time);
107            println!("  🔥 Threat Level: {}", threat_level);
108            println!("  🎲 Confidence: {:.1}%", confidence);
109
110            // Generate detailed security alert
111            generate_security_alert(attack_name, &threat_level, confidence, &anomalies);
112
113            // Show most suspicious patterns
114            if anomalies.len() > 0 {
115                let most_suspicious = anomalies
116                    .iter()
117                    .max_by(|a, b| a.anomaly_strength.partial_cmp(&b.anomaly_strength).unwrap())
118                    .unwrap();
119                println!(
120                    "  🔍 Most suspicious pattern: {:?}",
121                    most_suspicious.sequence
122                );
123                println!(
124                    "     Strength: {:.3}, Info: {:.3}",
125                    most_suspicious.anomaly_strength, most_suspicious.information_score
126                );
127            }
128        } else {
129            println!("  ✅ No threats detected (normal traffic)");
130        }
131    }
132
133    // Advanced analytics and ROC analysis
134    println!("\n📈 Advanced Security Analytics");
135    perform_roc_analysis(&detector)?;
136
137    // Batch processing demonstration with performance metrics
138    println!("\n📦 High-Volume Batch Processing");
139    let batch_sequences = vec![
140        generate_normal_session(),
141        generate_malware_communication(),
142        generate_data_exfiltration(),
143        generate_insider_threat(),
144        generate_zero_day_exploit(),
145    ];
146
147    let batch_start = Instant::now();
148    let config = AnomalyGridConfig::default().with_max_order(5)?;
149    let batch_results = batch_process_sequences(&batch_sequences, &config, 0.01)?;
150    let batch_time = batch_start.elapsed();
151
152    println!(
153        "Processed {} sequences in {:?}",
154        batch_sequences.len(),
155        batch_time
156    );
157    let throughput = batch_sequences.len() as f64 / batch_time.as_secs_f64();
158    println!("Throughput: {:.1} sequences/second", throughput);
159
160    for (i, results) in batch_results.iter().enumerate() {
161        let sequence_types = [
162            "Normal Session",
163            "Malware C2",
164            "Data Exfiltration",
165            "Insider Threat",
166            "Zero-day Exploit",
167        ];
168        let risk_score = calculate_risk_score(results);
169        println!(
170            "  {}: {} anomalies, risk score: {:.2}",
171            sequence_types[i],
172            results.len(),
173            risk_score
174        );
175    }
176
177    // Performance and accuracy summary
178    println!("\n📊 Security Monitoring Summary");
179    println!("═══════════════════════════════════");
180    println!(
181        "Threats detected: {}/7 ({:.1}%)",
182        total_threats_detected,
183        (total_threats_detected as f64 / 7.0) * 100.0
184    );
185    println!("Critical threats: {}", critical_threats);
186    println!(
187        "Average detection time: {:?}",
188        total_detection_time / total_threats_detected.max(1) as u32
189    );
190
191    let false_positive_rate = calculate_false_positive_rate(&detector)?;
192    println!("Estimated false positive rate: {:.2}%", false_positive_rate);
193
194    // Calculate estimated cost savings
195    let cost_savings = calculate_security_cost_savings(critical_threats, total_threats_detected);
196    println!("Estimated cost savings: ${:.0}", cost_savings);
197
198    println!("\n💡 Security Insights:");
199    println!("   - APT campaigns show complex multi-stage patterns");
200    println!("   - DDoS attacks have high-volume repetitive signatures");
201    println!("   - Lateral movement creates subtle context anomalies");
202    println!("   - Data exfiltration shows unusual data flow patterns");
203    println!("   - Real-time detection enables rapid incident response");
204
205    Ok(())
206}
examples/time_series_anomaly_detection.rs (line 22)
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15    println!("📈 Time Series Anomaly Detection with Mathematical Validation");
16    println!("Converting continuous data to categorical sequences for anomaly detection\n");
17
18    // Configure detector for time series pattern analysis
19    let config = AnomalyGridConfig::default()
20        .with_max_order(8)? // High order for temporal dependencies
21        .with_smoothing_alpha(0.2)? // Low smoothing for precise pattern learning
22        .with_weights(0.9, 0.1)?; // Emphasize likelihood for temporal patterns
23
24    let mut detector = AnomalyDetector::with_config(config)?;
25    println!("✅ Configured time series detector with order 8 for temporal patterns");
26
27    // Phase 1: Generate and validate time series training data
28    println!("\n📊 Phase 1: Time Series Data Generation and Discretization");
29    let (raw_time_series, discretized_series) = generate_time_series_data(1000)?;
30
31    // Validate discretization quality
32    validate_discretization_quality(&raw_time_series, &discretized_series)?;
33
34    println!("📈 Generated {} time series points", raw_time_series.len());
35    println!(
36        "🔢 Discretized to {} categorical states",
37        discretized_series.len()
38    );
39    println!("🎯 Discretization validation: PASSED");
40
41    // Phase 2: Train and validate temporal model
42    println!("\n🎯 Phase 2: Temporal Model Training and Validation");
43    let train_start = Instant::now();
44    detector.train(&discretized_series)?;
45    let train_time = train_start.elapsed();
46
47    let metrics = detector.performance_metrics();
48    println!("⏱️ Training completed in {:?}", train_time);
49    println!("🧮 Temporal patterns learned: {}", metrics.context_count);
50    println!(
51        "💾 Memory usage: {:.1} KB",
52        metrics.estimated_memory_bytes as f64 / 1024.0
53    );
54
55    // Validate temporal consistency
56    validate_temporal_consistency(&detector, &discretized_series)?;
57
58    // Phase 3: Comprehensive anomaly detection testing
59    println!("\n🔬 Phase 3: Time Series Anomaly Detection Testing");
60
61    let test_scenarios = vec![
62        ("Normal Trend", generate_normal_trend(), false, 0.1),
63        ("Sudden Spike", generate_sudden_spike(), true, 0.05),
64        ("Gradual Drift", generate_gradual_drift(), true, 0.08),
65        ("Periodic Anomaly", generate_periodic_anomaly(), true, 0.03),
66        ("Noise Burst", generate_noise_burst(), true, 0.02),
67        ("Level Shift", generate_level_shift(), true, 0.05),
68        ("Trend Change", generate_trend_change(), true, 0.06),
69        ("Seasonal Break", generate_seasonal_break(), true, 0.04),
70    ];
71
72    let mut detection_results = Vec::new();
73    let mut total_detection_time = std::time::Duration::new(0, 0);
74
75    for (scenario_name, (raw_data, test_sequence), is_anomalous, threshold) in test_scenarios {
76        println!("\n--- Testing: {} ---", scenario_name);
77        println!(
78            "Expected: {}",
79            if is_anomalous { "ANOMALOUS" } else { "NORMAL" }
80        );
81        println!("Raw data points: {}", raw_data.len());
82        println!("Discretized sequence length: {}", test_sequence.len());
83        println!("Threshold: {}", threshold);
84
85        let detect_start = Instant::now();
86        let anomalies = detector.detect_anomalies(&test_sequence, threshold)?;
87        let detect_time = detect_start.elapsed();
88        total_detection_time += detect_time;
89
90        let detected = !anomalies.is_empty();
91        let is_correct = detected == is_anomalous;
92
93        println!(
94            "🔍 Detection result: {}",
95            if detected { "ANOMALOUS" } else { "NORMAL" }
96        );
97        println!(
98            "✅ Correctness: {}",
99            if is_correct { "CORRECT" } else { "INCORRECT" }
100        );
101        println!("⏱️ Detection time: {:?}", detect_time);
102
103        if detected {
104            let max_strength = anomalies
105                .iter()
106                .map(|a| a.anomaly_strength)
107                .fold(0.0f64, f64::max);
108            let avg_information =
109                anomalies.iter().map(|a| a.information_score).sum::<f64>() / anomalies.len() as f64;
110
111            println!("📊 Anomalies detected: {}", anomalies.len());
112            println!("🎯 Max anomaly strength: {:.4}", max_strength);
113            println!("📈 Avg information score: {:.4}", avg_information);
114
115            // Validate mathematical properties
116            validate_anomaly_mathematical_properties(&anomalies)?;
117
118            // Show temporal context of strongest anomaly
119            if let Some(strongest) = anomalies
120                .iter()
121                .max_by(|a, b| a.anomaly_strength.partial_cmp(&b.anomaly_strength).unwrap())
122            {
123                println!("🔍 Strongest anomaly pattern: {:?}", strongest.sequence);
124            }
125        }
126
127        detection_results.push((
128            scenario_name.to_string(),
129            is_anomalous,
130            detected,
131            is_correct,
132        ));
133    }
134
135    // Phase 4: Statistical validation and metrics
136    println!("\n📊 Phase 4: Statistical Validation and Performance Metrics");
137
138    let accuracy = calculate_accuracy(&detection_results);
139    let (precision, recall, f1_score) = calculate_precision_recall_f1(&detection_results);
140
141    println!("🎯 Detection Accuracy: {:.1}%", accuracy * 100.0);
142    println!("🎯 Precision: {:.3}", precision);
143    println!("🎯 Recall: {:.3}", recall);
144    println!("🎯 F1 Score: {:.3}", f1_score);
145    println!(
146        "⏱️ Average detection time: {:?}",
147        total_detection_time / detection_results.len() as u32
148    );
149
150    // Phase 5: Temporal pattern analysis
151    println!("\n⏰ Phase 5: Temporal Pattern Analysis");
152    analyze_temporal_patterns(&detector)?;
153
154    // Phase 6: Sensitivity analysis
155    println!("\n🎚️ Phase 6: Sensitivity Analysis");
156    perform_sensitivity_analysis(&detector)?;
157
158    // Phase 7: Robustness testing with edge cases
159    println!("\n🛡️ Phase 7: Robustness Testing");
160    test_temporal_robustness(&detector)?;
161
162    // Phase 8: Performance benchmarking
163    println!("\n⚡ Phase 8: Performance Benchmarking");
164    benchmark_temporal_performance(&detector)?;
165
166    // Phase 9: Mathematical validation
167    println!("\n🔬 Phase 9: Mathematical Property Validation");
168    validate_mathematical_properties(&detector)?;
169
170    // Final validation summary
171    println!("\n✅ COMPREHENSIVE VALIDATION SUMMARY");
172    println!("═══════════════════════════════════════");
173    println!("✅ Time series discretization: VALIDATED");
174    println!("✅ Temporal consistency: VALIDATED");
175    println!("✅ Mathematical properties: VALIDATED");
176    println!("✅ Detection accuracy: {:.1}%", accuracy * 100.0);
177    println!("✅ F1 Score: {:.3}", f1_score);
178    println!(
179        "✅ Performance: {} detections/sec",
180        (1000.0 / total_detection_time.as_millis() as f64 * detection_results.len() as f64) as u32
181    );
182
183    // Determine overall validation status
184    let validation_passed = accuracy >= 0.8
185        && f1_score >= 0.75
186        && total_detection_time.as_millis() < 100 * detection_results.len() as u128;
187
188    if validation_passed {
189        println!("🎉 ALL VALIDATIONS PASSED - TIME SERIES DETECTION VERIFIED");
190        println!("📈 Library successfully handles temporal anomaly detection");
191    } else {
192        println!("⚠️ VALIDATION CONCERNS - REVIEW REQUIRED");
193        if accuracy < 0.8 {
194            println!("   - Accuracy below threshold");
195        }
196        if f1_score < 0.75 {
197            println!("   - F1 score below threshold");
198        }
199    }
200
201    Ok(())
202}
examples/financial_fraud_detection.rs (line 20)
12fn main() -> Result<(), Box<dyn std::error::Error>> {
13    println!("💳 Financial Fraud Detection with Anomaly Grid");
14    println!("Detecting credit card fraud, velocity attacks, and suspicious patterns\n");
15
16    // Configure detector for financial transaction patterns
17    let config = AnomalyGridConfig::default()
18        .with_max_order(5)? // Higher order for complex fraud patterns
19        .with_smoothing_alpha(0.8)? // Moderate smoothing for financial data
20        .with_weights(0.5, 0.5)?; // Balance likelihood and information equally
21
22    let mut detector = AnomalyDetector::with_config(config)?;
23    println!("✅ Configured fraud detector with order 5 for complex patterns");
24
25    // Generate comprehensive normal transaction data
26    let normal_transactions = generate_normal_transactions(30); // 1 month
27    println!(
28        "📊 Generated {} normal transactions (1 month)",
29        normal_transactions.len()
30    );
31
32    // Train on normal transaction patterns
33    let train_start = Instant::now();
34    detector.train(&normal_transactions)?;
35    let train_time = train_start.elapsed();
36
37    let metrics = detector.performance_metrics();
38    println!("🎯 Training completed in {:?}", train_time);
39    println!(
40        "   - Transaction patterns learned: {}",
41        metrics.context_count
42    );
43    println!(
44        "   - Memory usage: {:.1} KB",
45        metrics.estimated_memory_bytes as f64 / 1024.0
46    );
47
48    // Real-time fraud detection simulation with optimized thresholds
49    println!("\n🔍 Real-time Fraud Detection Simulation");
50
51    let fraud_scenarios = vec![
52        ("Card Testing", generate_card_testing(), 0.01),
53        ("Velocity Attack", generate_velocity_attack(), 0.005),
54        ("Geographic Anomaly", generate_geographic_anomaly(), 0.02),
55        ("Amount Anomaly", generate_amount_anomaly(), 0.01),
56        ("Account Takeover", generate_account_takeover(), 0.001),
57        ("Synthetic Identity", generate_synthetic_identity(), 0.001),
58        ("Merchant Fraud", generate_merchant_fraud(), 0.005),
59        ("Money Laundering", generate_money_laundering(), 0.002),
60    ];
61
62    let mut total_fraud_detected = 0;
63    let mut total_fraud_amount = 0.0;
64    let mut critical_fraud_cases = 0;
65    let mut total_detection_time = std::time::Duration::new(0, 0);
66
67    for (fraud_type, fraud_sequence, threshold) in fraud_scenarios {
68        println!("\n--- Analyzing: {} ---", fraud_type);
69        println!("Transaction sequence length: {}", fraud_sequence.len());
70        println!("Detection threshold: {}", threshold);
71
72        let detect_start = Instant::now();
73        let anomalies = detector.detect_anomalies(&fraud_sequence, threshold)?;
74        let detect_time = detect_start.elapsed();
75        total_detection_time += detect_time;
76
77        if !anomalies.is_empty() {
78            total_fraud_detected += 1;
79
80            let fraud_score = calculate_fraud_score(&anomalies);
81            let (risk_level, confidence) = classify_fraud_risk(fraud_score, anomalies.len());
82            let estimated_amount = estimate_fraud_amount(&fraud_sequence);
83            total_fraud_amount += estimated_amount;
84
85            if risk_level == "CRITICAL" {
86                critical_fraud_cases += 1;
87            }
88
89            println!("  🚨 FRAUD DETECTED");
90            println!("  📊 Anomalies: {}", anomalies.len());
91            println!("  🎯 Fraud Score: {:.2}", fraud_score);
92            println!("  🔥 Risk Level: {}", risk_level);
93            println!("  🎲 Confidence: {:.1}%", confidence);
94            println!("  💰 Estimated Amount: ${:.2}", estimated_amount);
95            println!("  ⚡ Detection Time: {:?}", detect_time);
96
97            // Generate detailed fraud alert
98            generate_fraud_alert(
99                fraud_type,
100                &risk_level,
101                estimated_amount,
102                fraud_score,
103                confidence,
104            );
105
106            // Show most suspicious transaction pattern
107            if let Some(most_suspicious) = anomalies
108                .iter()
109                .max_by(|a, b| a.anomaly_strength.partial_cmp(&b.anomaly_strength).unwrap())
110            {
111                println!(
112                    "  🔍 Most suspicious pattern: {:?}",
113                    most_suspicious.sequence
114                );
115                println!(
116                    "     Strength: {:.3}, Info: {:.3}",
117                    most_suspicious.anomaly_strength, most_suspicious.information_score
118                );
119            }
120        } else {
121            println!("  ✅ No fraud detected (legitimate transactions)");
122        }
123    }
124
125    // Advanced fraud analytics
126    println!("\n📈 Advanced Fraud Analytics");
127    perform_fraud_roc_analysis(&detector)?;
128
129    // High-volume transaction processing simulation
130    println!("\n📦 High-Volume Transaction Processing");
131    let batch_transactions = generate_mixed_transaction_batch(1000);
132
133    let batch_start = Instant::now();
134    let mut fraud_count = 0;
135    let mut legitimate_count = 0;
136    let mut total_risk_score = 0.0;
137
138    // Process in chunks for memory efficiency
139    for chunk in batch_transactions.chunks(100) {
140        let anomalies = detector.detect_anomalies(chunk, 0.05)?;
141        if !anomalies.is_empty() {
142            fraud_count += 1;
143            total_risk_score += calculate_fraud_score(&anomalies);
144        } else {
145            legitimate_count += 1;
146        }
147    }
148
149    let batch_time = batch_start.elapsed();
150    let throughput = batch_transactions.len() as f64 / batch_time.as_secs_f64();
151
152    println!(
153        "Processed {} transactions in {:?}",
154        batch_transactions.len(),
155        batch_time
156    );
157    println!("Throughput: {:.0} transactions/second", throughput);
158    println!(
159        "Fraud detected: {} batches ({:.1}%)",
160        fraud_count,
161        (fraud_count as f64 / (fraud_count + legitimate_count) as f64) * 100.0
162    );
163    println!("Legitimate: {} batches", legitimate_count);
164    println!(
165        "Average risk score: {:.2}",
166        total_risk_score / fraud_count.max(1) as f64
167    );
168
169    // Performance and accuracy summary
170    println!("\n📊 Fraud Detection Summary");
171    println!("═══════════════════════════════════");
172    println!(
173        "Fraud scenarios detected: {}/8 ({:.1}%)",
174        total_fraud_detected,
175        (total_fraud_detected as f64 / 8.0) * 100.0
176    );
177    println!("Critical fraud cases: {}", critical_fraud_cases);
178    println!("Total fraud amount prevented: ${:.2}", total_fraud_amount);
179    println!(
180        "Average detection time: {:?}",
181        total_detection_time / total_fraud_detected.max(1) as u32
182    );
183
184    // Calculate ROI and cost savings
185    let investigation_cost = total_fraud_detected as f64 * 500.0; // $500 per investigation
186    let net_savings = total_fraud_amount - investigation_cost;
187    let roi = if investigation_cost > 0.0 {
188        (net_savings / investigation_cost) * 100.0
189    } else {
190        0.0
191    };
192
193    println!("Investigation costs: ${:.2}", investigation_cost);
194    println!("Net savings: ${:.2}", net_savings);
195    println!("ROI: {:.1}%", roi);
196
197    // Calculate precision and recall estimates
198    let precision = calculate_fraud_precision(&detector)?;
199    let recall = calculate_fraud_recall(&detector)?;
200    let f1_score = 2.0 * (precision * recall) / (precision + recall);
201
202    println!("Estimated precision: {:.1}%", precision);
203    println!("Estimated recall: {:.1}%", recall);
204    println!("F1 score: {:.3}", f1_score);
205
206    println!("\n💡 Fraud Detection Insights:");
207    println!("   - Card testing shows rapid small-amount probing patterns");
208    println!("   - Velocity attacks have high-frequency transaction bursts");
209    println!("   - Geographic anomalies show impossible travel patterns");
210    println!("   - Account takeovers exhibit sudden behavior changes");
211    println!("   - Synthetic identities show artificial credit building");
212    println!("   - Real-time detection enables immediate card blocking");
213
214    Ok(())
215}
Source

pub fn estimate_memory_usage(&self, alphabet_size: usize) -> usize

Get estimated memory usage for given alphabet size

Source

pub fn is_suitable_for_alphabet(&self, alphabet_size: usize) -> bool

Check if configuration is suitable for given alphabet size

Trait Implementations§

Source§

impl Clone for AnomalyGridConfig

Source§

fn clone(&self) -> AnomalyGridConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AnomalyGridConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for AnomalyGridConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for AnomalyGridConfig

Source§

fn eq(&self, other: &AnomalyGridConfig) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for AnomalyGridConfig

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.