//! Unit Tests for Microstructure Features (Roll Measure & Amihud Illiquidity) //! //! TDD Implementation: Tests written FIRST, then implementation //! //! ## Test Coverage //! - Roll Measure: Serial correlation, zero covariance, negative handling //! - Amihud Illiquidity: Normal case, high volume, zero volume //! - Performance: <5μs latency, 72 bytes memory per symbol //! - Integration: 256-feature pipeline compatibility use ml::features::microstructure::{AmihudIlliquidity, RollMeasure}; // ============================================================================ // Roll Measure Tests (Agent A9) // ============================================================================ #[test] fn test_roll_measure_positive_serial_correlation() { // Roll spread = 2 * sqrt(-cov(Δp_t, Δp_{t-1})) // With positive serial correlation, cov < 0, so sqrt should work let mut roll = RollMeasure::new(); // Simulate mean-reverting prices (negative serial correlation) let prices = vec![100.0, 101.0, 100.0, 101.0, 100.0, 101.0]; for price in prices { roll.update(price); } let spread = roll.compute(); // Should produce positive spread estimate assert!(spread > 0.0, "Roll spread should be positive: {}", spread); assert!( spread < 10.0, "Roll spread should be reasonable: {}", spread ); } #[test] fn test_roll_measure_negative_serial_correlation() { // With negative serial correlation (mean reversion), cov > 0 // Formula: 2 * sqrt(-cov) requires taking sqrt of negative value // Implementation should handle this by taking sqrt(abs(cov)) let mut roll = RollMeasure::new(); // Simulate trending prices (positive serial correlation) let prices = vec![100.0, 100.5, 101.0, 101.5, 102.0, 102.5]; for price in prices { roll.update(price); } let spread = roll.compute(); // Should still produce valid spread estimate (non-negative) assert!( spread >= 0.0, "Roll spread should be non-negative: {}", spread ); } #[test] fn test_roll_measure_zero_covariance() { // Random walk (no serial correlation) => cov ≈ 0 // Roll spread should be close to zero let mut roll = RollMeasure::new(); // Simulate random walk with alternating changes let prices = vec![100.0, 100.1, 100.0, 100.2, 100.1, 100.3]; for price in prices { roll.update(price); } let spread = roll.compute(); // Should be small (close to zero) assert!(spread >= 0.0, "Roll spread should be non-negative"); assert!( spread < 1.0, "Roll spread should be small for random walk: {}", spread ); } #[test] fn test_roll_measure_insufficient_data() { let mut roll = RollMeasure::new(); // Need at least 2 price changes (3 prices) for covariance roll.update(100.0); roll.update(101.0); let spread = roll.compute(); // Should return 0.0 or handle gracefully assert!( spread >= 0.0, "Roll spread should be non-negative with insufficient data" ); } #[test] fn test_roll_measure_latency_requirement() { use std::time::Instant; let mut roll = RollMeasure::new(); // Warm up with 20 prices for i in 0..20 { roll.update(100.0 + (i as f64) * 0.1); } // Measure update + compute latency let start = Instant::now(); for _ in 0..100 { roll.update(105.0); let _ = roll.compute(); } let elapsed = start.elapsed(); let avg_latency_us = elapsed.as_micros() / 100; // Requirement: <5μs per update+compute assert!( avg_latency_us < 5, "Roll measure latency {}μs exceeds 5μs requirement", avg_latency_us ); } #[test] fn test_roll_measure_memory_footprint() { use std::mem::size_of; let roll = RollMeasure::new(); let size = size_of::(); // Requirement: 72 bytes per symbol assert!( size <= 72, "Roll measure memory {}B exceeds 72B requirement", size ); } #[test] fn test_roll_measure_real_market_data() { // Test with ES.FUT-like price movements let mut roll = RollMeasure::new(); let prices = vec![ 4500.25, 4500.50, 4500.25, 4500.75, 4500.50, 4500.25, 4501.00, 4500.75, 4500.50, 4501.25, ]; for price in prices { roll.update(price); } let spread = roll.compute(); // Typical bid-ask spread for ES futures: 0.25-1.0 points assert!(spread >= 0.0, "Roll spread should be non-negative"); assert!( spread < 5.0, "Roll spread should be realistic for ES.FUT: {}", spread ); } #[test] fn test_roll_measure_extreme_volatility() { let mut roll = RollMeasure::new(); // Simulate flash crash scenario let prices = vec![100.0, 100.5, 101.0, 95.0, 90.0, 92.0, 95.0, 98.0, 100.0]; for price in prices { roll.update(price); } let spread = roll.compute(); // Should handle extreme volatility without panicking assert!(spread.is_finite(), "Roll spread should be finite"); assert!(spread >= 0.0, "Roll spread should be non-negative"); } // ============================================================================ // Amihud Illiquidity Tests (Agent A8) // ============================================================================ #[test] fn test_amihud_normal_case() { // Amihud = |return| / dollar_volume let mut amihud = AmihudIlliquidity::new(0.05); amihud.update(100.0, 1_000_000.0); // price, volume amihud.update(101.0, 1_000_000.0); let illiquidity = amihud.compute(); // Expected: abs(log(101/100)) / 1_000_000 ≈ 0.00995 / 1M ≈ 1e-8 assert!(illiquidity > 0.0, "Amihud should be positive"); assert!( illiquidity < 1e-5, "Amihud should be small for liquid market: {}", illiquidity ); } #[test] fn test_amihud_high_volume_low_illiquidity() { let mut amihud = AmihudIlliquidity::new(0.05); // High volume => low illiquidity amihud.update(100.0, 10_000_000.0); amihud.update(101.0, 10_000_000.0); let high_vol_illiquidity = amihud.compute(); // Compare with low volume let mut amihud2 = AmihudIlliquidity::new(0.05); amihud2.update(100.0, 1_000_000.0); amihud2.update(101.0, 1_000_000.0); let low_vol_illiquidity = amihud2.compute(); assert!( high_vol_illiquidity < low_vol_illiquidity, "High volume should have lower illiquidity" ); } #[test] fn test_amihud_zero_volume() { let mut amihud = AmihudIlliquidity::new(0.05); // Zero volume should be handled gracefully amihud.update(100.0, 0.0); amihud.update(101.0, 0.0); let illiquidity = amihud.compute(); // Should return max illiquidity or capped value assert!(illiquidity.is_finite(), "Amihud should handle zero volume"); } #[test] fn test_amihud_latency_requirement() { use std::time::Instant; let mut amihud = AmihudIlliquidity::new(0.05); // Warm up for i in 0..20 { amihud.update(100.0 + (i as f64) * 0.1, 1_000_000.0); } // Measure latency let start = Instant::now(); for _ in 0..100 { amihud.update(105.0, 1_000_000.0); let _ = amihud.compute(); } let elapsed = start.elapsed(); let avg_latency_us = elapsed.as_micros() / 100; // Requirement: <5μs assert!( avg_latency_us < 5, "Amihud latency {}μs exceeds 5μs requirement", avg_latency_us ); } #[test] fn test_amihud_memory_footprint() { use std::mem::size_of; let amihud = AmihudIlliquidity::new(0.05); let size = size_of::(); // Requirement: 72 bytes per symbol assert!( size <= 72, "Amihud memory {}B exceeds 72B requirement", size ); } // ============================================================================ // Integration Tests // ============================================================================ #[test] fn test_microstructure_integration_256_features() { // Verify microstructure features fit within 256-dim feature vector // Features 115-164 are allocated for microstructure (50 features) use chrono::Utc; use ml::features::extraction::{extract_ml_features, OHLCVBar}; let bars: Vec = (0..100) .map(|i| OHLCVBar { timestamp: Utc::now() + chrono::Duration::hours(i), open: 100.0 + (i as f64) * 0.1, high: 101.0 + (i as f64) * 0.1, low: 99.0 + (i as f64) * 0.1, close: 100.5 + (i as f64) * 0.1, volume: 1_000_000.0 + (i as f64) * 10_000.0, }) .collect(); let features = extract_ml_features(&bars).unwrap(); // Should extract 225-dim features assert_eq!(features.len(), 50); // 100 bars - 50 warmup assert_eq!(features[0].len(), 225); // Verify all features are finite for feature_vec in &features { for (i, &val) in feature_vec.iter().enumerate() { assert!(val.is_finite(), "Feature {} is not finite: {}", i, val); } } } #[test] fn test_microstructure_features_non_negative() { // Roll and Amihud should produce non-negative values let mut roll = RollMeasure::new(); let mut amihud = AmihudIlliquidity::new(0.05); // Feed price/volume data for i in 0..20 { let price = 100.0 + (i as f64) * 0.1; let volume = 1_000_000.0 + (i as f64) * 10_000.0; roll.update(price); amihud.update(price, volume); } let roll_spread = roll.compute(); let amihud_illiq = amihud.compute(); assert!(roll_spread >= 0.0, "Roll spread should be non-negative"); assert!( amihud_illiq >= 0.0, "Amihud illiquidity should be non-negative" ); } #[test] fn test_microstructure_features_normalization() { // Features should be normalized for ML training use chrono::Utc; use ml::features::extraction::{extract_ml_features, OHLCVBar}; let bars: Vec = (0..100) .map(|i| OHLCVBar { timestamp: Utc::now() + chrono::Duration::hours(i), open: 100.0, high: 101.0, low: 99.0, close: 100.5, volume: 1_000_000.0, }) .collect(); let features = extract_ml_features(&bars).unwrap(); // Microstructure features (115-164) should be normalized for feature_vec in &features { for i in 115..165 { let val = feature_vec[i]; // Check if normalized (0-1 range or standardized) // Most features should be in reasonable range assert!( val.abs() < 10.0, "Feature {} has unreasonable value: {}", i, val ); } } }