Files
foxhunt/ml/examples/feature_importance_analysis.rs
jgrusewski 35feadf55e 🚀 Wave 160 Phase 6: CUDA Mandatory + TDD Testing + TFT Complete (21 Agents)
## Major Achievements

### 1. CUDA Made Default & Mandatory (Agent 143)
- CUDA now default feature in ml/Cargo.toml
- All training requires GPU (no silent CPU fallback)
- Added get_training_device() helper with fail-fast errors
- Removed --use-gpu flags (GPU mandatory)
- **Impact**: No more wasting time on accidental CPU training

### 2. TFT Training COMPLETE (Agent 144)
-  Training completed successfully in 7.6 minutes
-  Early stopping at epoch 100/200 (best val loss: 0.097318)
-  11 checkpoints saved to ml/trained_models/production/tft/
-  GPU Performance: 99% utilization, 367MB VRAM, 4.4s/epoch
-  10x speedup vs CPU (4.4s vs 43-55s per epoch)
- **Status**: PRODUCTION READY

### 3. TFT CUDA Tensor Contiguity Fix (Agent 142)
- Fixed "matmul not supported for non-contiguous tensors" error
- Added .contiguous() call after narrow() operation in QuantileLayer
- Enabled CUDA-accelerated TFT training
- **Files**: ml/src/tft/quantile_outputs.rs

### 4. MAMBA-2 CUDA Layer Normalization (Agent 145)
- Created CudaLayerNorm wrapper for missing CUDA kernel
- Implemented manual layer norm: γ * (x - μ) / sqrt(σ² + ε) + β
- MAMBA-2 now runs on CUDA (no more "no cuda implementation" error)
- **Files**: ml/src/mamba/mod.rs

### 5. TDD E2E Test Suite (Agent 146) 
- Created comprehensive MAMBA-2 test suite (297 lines)
- 7 tests: shapes, batches, CUDA, gradients, configs
- **16x faster debugging**: 5s per iteration vs 80s
- Already caught dtype mismatch bug (F32 vs F64)
- **Files**: ml/tests/e2e_mamba2_training.rs

## Agent Summary (Agents 126-146)

### Code Fixes (Parallel - Agents 137-141)
- **Agent 137**: MAMBA-2 batch dimension fix (streaming + batch loaders)
- **Agent 138**: Liquid NN API fix (mutable loader, iterator fix)
- **Agent 139**: PPO CheckpointMetadata fix (signature fields)
- **Agent 140**: Paper trading executor (498 lines, 100ms polling)
- **Agent 141**: Real model loading (RealDQNModel, RealPPOModel)

### Infrastructure (Agents 143-146)
- **Agent 143**: CUDA mandatory (Cargo.toml, device helpers)
- **Agent 144**: TFT verification (completion monitoring)
- **Agent 145**: MAMBA-2 CUDA layer norm wrapper
- **Agent 146**: TDD E2E test suite (16x faster debugging)

## Files Modified

### Core ML Infrastructure
- ml/Cargo.toml: Added default = ["minimal-inference", "cuda"]
- ml/src/lib.rs: Added get_training_device() helper (+109 lines)
- ml/src/tft/quantile_outputs.rs: Fixed tensor contiguity
- ml/src/mamba/mod.rs: Added CudaLayerNorm wrapper (+41 lines)

### Training Scripts
- ml/examples/train_tft_dbn.rs: Removed --use-gpu flag
- ml/examples/train_ppo.rs: Removed --use-gpu flag
- ml/examples/train_mamba2_dbn.rs: Forced CUDA-only mode
- ml/examples/train_liquid_dbn.rs: Fixed API usage

### Data Loaders
- ml/src/data_loaders/dbn_sequence_loader.rs: Fixed batch dimensions
- ml/src/data_loaders/streaming_dbn_loader.rs: Fixed batch dimensions

### Trading Service
- services/trading_service/src/paper_trading_executor.rs: New executor (+498 lines)
- services/trading_service/src/services/enhanced_ml.rs: Real model loading
- services/trading_service/src/ensemble_coordinator.rs: Integration

### Tests
- ml/tests/e2e_mamba2_training.rs: New TDD test suite (+297 lines)

### Trainers
- ml/src/trainers/tft.rs: Fixed CheckpointMetadata signature fields

## Performance Metrics

### TFT Training
- Duration: 7.6 minutes (100 epochs with early stopping)
- GPU Utilization: 99%
- GPU Memory: 367MB / 4GB (9%)
- Epoch Time: 4.4 seconds (vs 43-55s on CPU)
- Speedup: 10x vs CPU
- Status:  PRODUCTION READY

### TDD Testing
- Test Execution: 5-10 seconds per test
- Debugging Iteration: 5 seconds (vs 80 seconds before)
- Speedup: 16x faster debugging
- First Bug Found: <1 minute (dtype mismatch)

## Documentation
- 21 comprehensive agent reports
- TDD quick start guide
- CUDA troubleshooting guide
- Training verification procedures

## Next Steps
1. Fix MAMBA-2 dtype mismatch (F32→F64) - 2 minutes
2. Run MAMBA-2 tests until passing - 5-10 minutes
3. Launch full MAMBA-2 training - 200 epochs
4. Launch Liquid NN training

## System Status
- TFT:  COMPLETE (production ready)
- MAMBA-2: 🧪 IN TESTING (TDD suite ready)
- CUDA:  DEFAULT (mandatory for training)
- Tests:  16x faster debugging

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-14 23:13:34 +02:00

284 lines
10 KiB
Rust

//! Feature Importance Analysis for Enhanced Feature Engineering
//!
//! Analyzes the correlation between each of the 36 features and future returns
//! to determine which features have the most predictive power.
//!
//! # Usage
//!
//! ```bash
//! cargo run -p ml --example feature_importance_analysis --release
//! ```
use anyhow::{Context, Result};
use ml::real_data_loader::{OHLCVBar, RealDataLoader};
use std::collections::HashMap;
use tracing::{info, warn};
use tracing_subscriber::FmtSubscriber;
// Import the enhanced technical indicators
use ml_training_service::technical_indicators::{IndicatorConfig, TechnicalIndicatorCalculator};
#[tokio::main]
async fn main() -> Result<()> {
// Setup logging
let subscriber = FmtSubscriber::builder()
.with_max_level(tracing::Level::INFO)
.finish();
tracing::subscriber::set_global_default(subscriber)
.context("Failed to set tracing subscriber")?;
info!("🔍 Feature Importance Analysis - Enhanced Feature Engineering");
info!("Analyzing 36 features vs baseline 16 features");
// Load real market data
let data_loader = RealDataLoader::new();
let mut file_mapping = HashMap::new();
file_mapping.insert(
"6E.FUT".to_string(),
"test_data/real/databento/ml_training/6E_FUT_20240101_20240131.dbn".to_string(),
);
info!("📊 Loading market data for 6E.FUT...");
let bars = data_loader
.load_ohlcv_data(&file_mapping)
.await
.context("Failed to load OHLCV data")?;
let total_bars = bars.values().map(|v| v.len()).sum::<usize>();
info!(" Loaded {} bars across {} symbols", total_bars, bars.len());
// Calculate features for each symbol
for (symbol, bar_data) in bars.iter() {
info!("\n📈 Analyzing {} ({} bars)", symbol, bar_data.len());
if bar_data.len() < 50 {
warn!(" Skipping {}: insufficient data", symbol);
continue;
}
// Initialize enhanced indicator calculator
let config = IndicatorConfig::default();
let mut calculator = TechnicalIndicatorCalculator::new(symbol.clone(), config);
// Collect all features and returns
let mut feature_matrix = Vec::new();
let mut returns = Vec::new();
info!(" Computing features and returns...");
for (i, bar) in bar_data.iter().enumerate() {
// Update calculator with OHLC data
calculator.update(
bar.close,
bar.volume,
Some(bar.high),
Some(bar.low),
);
// Skip warmup period
if !calculator.is_warmed_up() {
continue;
}
// Get all current indicators (36 features)
let indicators = calculator.current_indicators();
// Calculate forward return (1-bar ahead)
if i < bar_data.len() - 1 {
let forward_return = (bar_data[i + 1].close / bar.close).ln();
feature_matrix.push(indicators);
returns.push(forward_return);
}
}
info!(" Collected {} feature vectors", feature_matrix.len());
if feature_matrix.is_empty() {
warn!(" No features collected after warmup");
continue;
}
// Calculate feature importance (correlation with returns)
info!("\n📊 Feature Importance Analysis:");
info!(" (Pearson correlation with 1-bar forward returns)\n");
let mut correlations = Vec::new();
// Get all unique feature names
let feature_names: Vec<String> = feature_matrix[0].keys().cloned().collect();
for feature_name in &feature_names {
let mut feature_values = Vec::new();
let mut valid_returns = Vec::new();
// Collect feature values and corresponding returns
for (features, ret) in feature_matrix.iter().zip(returns.iter()) {
if let Some(&value) = features.get(feature_name) {
if value.is_finite() {
feature_values.push(value);
valid_returns.push(*ret);
}
}
}
if feature_values.len() < 10 {
continue;
}
// Calculate Pearson correlation
let correlation = calculate_correlation(&feature_values, &valid_returns);
correlations.push((feature_name.clone(), correlation, feature_values.len()));
}
// Sort by absolute correlation (strongest predictive power first)
correlations.sort_by(|a, b| b.1.abs().partial_cmp(&a.1.abs()).unwrap());
// Print top 20 features
info!(" Top 20 Most Predictive Features:");
info!(" {:<30} {:>12} {:>10}", "Feature", "Correlation", "N");
info!(" {}", "-".repeat(55));
for (i, (name, corr, n)) in correlations.iter().take(20).enumerate() {
let emoji = if i < 10 { "🟢" } else { "🟡" };
info!(" {:<30} {:>12.6} {:>10} {}", name, corr, n, emoji);
}
// Categorize features
info!("\n📋 Feature Categories:");
let momentum_features: Vec<_> = correlations
.iter()
.filter(|(name, _, _)| {
name.contains("rsi")
|| name.contains("mfi")
|| name.contains("cmf")
|| name.contains("chaikin")
|| name.contains("macd")
})
.collect();
let volatility_features: Vec<_> = correlations
.iter()
.filter(|(name, _, _)| {
name.contains("bollinger")
|| name.contains("keltner")
|| name.contains("donchian")
|| name.contains("atr")
})
.collect();
let volume_features: Vec<_> = correlations
.iter()
.filter(|(name, _, _)| {
name.contains("obv")
|| name.contains("vwap")
|| name.contains("volume")
})
.collect();
info!(" Momentum indicators: {} features", momentum_features.len());
if !momentum_features.is_empty() {
let avg_corr: f64 = momentum_features.iter().map(|(_, c, _)| c.abs()).sum::<f64>()
/ momentum_features.len() as f64;
info!(" Average |correlation|: {:.6}", avg_corr);
}
info!(" Volatility indicators: {} features", volatility_features.len());
if !volatility_features.is_empty() {
let avg_corr: f64 = volatility_features.iter().map(|(_, c, _)| c.abs()).sum::<f64>()
/ volatility_features.len() as f64;
info!(" Average |correlation|: {:.6}", avg_corr);
}
info!(" Volume indicators: {} features", volume_features.len());
if !volume_features.is_empty() {
let avg_corr: f64 = volume_features.iter().map(|(_, c, _)| c.abs()).sum::<f64>()
/ volume_features.len() as f64;
info!(" Average |correlation|: {:.6}", avg_corr);
}
// Summary statistics
info!("\n📈 Summary Statistics:");
let all_corrs: Vec<f64> = correlations.iter().map(|(_, c, _)| c.abs()).collect();
let mean_corr = all_corrs.iter().sum::<f64>() / all_corrs.len() as f64;
let max_corr = all_corrs.iter().cloned().fold(0.0, f64::max);
let min_corr = all_corrs.iter().cloned().fold(f64::INFINITY, f64::min);
info!(" Total features: {}", correlations.len());
info!(" Mean |correlation|: {:.6}", mean_corr);
info!(" Max |correlation|: {:.6}", max_corr);
info!(" Min |correlation|: {:.6}", min_corr);
// Identify new features (enhanced set)
let new_features: Vec<_> = correlations
.iter()
.filter(|(name, _, _)| {
name.contains("mfi")
|| name.contains("cmf")
|| name.contains("chaikin")
|| name.contains("keltner")
|| name.contains("donchian")
|| name.contains("obv")
|| name.contains("vwap")
|| name.contains("volume_oscillator")
})
.collect();
info!("\n✨ NEW Features (20 added):");
info!(" {} new features active", new_features.len());
if !new_features.is_empty() {
let new_avg_corr = new_features.iter().map(|(_, c, _)| c.abs()).sum::<f64>()
/ new_features.len() as f64;
info!(" Average |correlation| of new features: {:.6}", new_avg_corr);
info!("\n Top 10 New Features:");
let mut sorted_new = new_features.clone();
sorted_new.sort_by(|a, b| b.1.abs().partial_cmp(&a.1.abs()).unwrap());
for (name, corr, n) in sorted_new.iter().take(10) {
info!(" {:<30} {:>12.6} {:>10}", name, corr, n);
}
}
}
info!("\n✅ Feature importance analysis complete!");
info!("Next steps:");
info!(" 1. Review top predictive features");
info!(" 2. Retrain DQN with enhanced 36-feature set");
info!(" 3. Compare Sharpe ratios (baseline vs enhanced)");
Ok(())
}
/// Calculate Pearson correlation coefficient between two vectors
fn calculate_correlation(x: &[f64], y: &[f64]) -> f64 {
if x.len() != y.len() || x.is_empty() {
return 0.0;
}
let n = x.len() as f64;
// Calculate means
let mean_x = x.iter().sum::<f64>() / n;
let mean_y = y.iter().sum::<f64>() / n;
// Calculate covariance and standard deviations
let mut cov = 0.0;
let mut var_x = 0.0;
let var_y = 0.0;
for (xi, yi) in x.iter().zip(y.iter()) {
let dx = xi - mean_x;
let dy = yi - mean_y;
cov += dx * dy;
var_x += dx * dx;
let var_y = var_y + dy * dy;
}
// Avoid division by zero
if var_x == 0.0 || var_y == 0.0 {
return 0.0;
}
cov / (var_x * var_y).sqrt()
}