Files
foxhunt/ml/tests/bug20_portfolio_normalization_test.rs
jgrusewski 15496deb1d docs: Fix hyperopt blocker investigation - all systems operational
Investigation revealed all 3 "blockers" were false alarms:

BLOCKER #1 (FALSE): 45-action space already operational
- ml/src/trainers/dqn.rs:573 uses num_actions=45 (production)
- ml/src/hyperopt/adapters/dqn.rs:286 had stale comment (3→45)
- Fix: Updated documentation to reflect reality

BLOCKER #2 (COMPLETE): Action masking params already exposed
- max_position_absolute field exists in DQNHyperparameters
- Search space: 1.0-10.0 contracts (6D hyperopt)
- Thrashing risk constraint implemented

BLOCKER #3 (FALSE): Transaction costs fully implemented
- Order-type specific fees: LimitMaker 0.05%, Market 0.15%, IoC 0.10%
- PortfolioTracker applies costs during trade execution
- Cumulative tracking operational since Wave 9-A3

Files Modified:
- ml/src/hyperopt/adapters/dqn.rs (3 lines - doc corrections)
- CLAUDE.md (hyperopt status updated to READY)

Production Readiness:  CERTIFIED
- 6D parameter space operational
- All Wave 9-16 features integrated
- Ready for 30-100 trial hyperopt campaign

Report: /tmp/HYPEROPT_BLOCKER_INVESTIGATION_COMPLETE.md

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-14 20:22:57 +01:00

125 lines
4.2 KiB
Rust

// Bug #20: Portfolio value normalization test
// Tests that portfolio value is normalized by initial_capital
// to prevent 100,000x feature imbalance
use anyhow::Result;
use ml::dqn::portfolio_tracker::PortfolioTracker;
#[test]
fn test_portfolio_value_normalized_to_baseline() -> Result<()> {
// Bug #20: Portfolio value should be normalized to ~1.0 baseline
let initial_capital = 100_000.0;
let avg_spread = 0.0001;
let cash_reserve_percent = 0.0;
let tracker = PortfolioTracker::new(initial_capital, avg_spread, cash_reserve_percent);
// At start, portfolio value = initial_capital
let features = tracker.get_portfolio_features(4000.0);
// Bug #20: Feature should be 1.0 (normalized), NOT 100,000
assert_eq!(features.len(), 3, "Should have 3 portfolio features");
let portfolio_feature = features[0];
// After fix, this should be ~1.0 (normalized by initial_capital)
// Before fix, this would be 100,000.0 (raw value)
assert!(
(portfolio_feature - 1.0).abs() < 0.01,
"Portfolio value should be normalized to 1.0, got: {}",
portfolio_feature
);
println!("Portfolio value normalized correctly: {}", portfolio_feature);
Ok(())
}
#[test]
fn test_all_portfolio_features_similar_scale() -> Result<()> {
// Bug #20: All portfolio features should be in similar scale
// No 100,000x imbalance
let initial_capital = 100_000.0;
let avg_spread = 0.0001;
let cash_reserve_percent = 0.0;
let tracker = PortfolioTracker::new(initial_capital, avg_spread, cash_reserve_percent);
let features = tracker.get_portfolio_features(4000.0);
// Check all features are in reasonable scale (NOT 100,000x difference)
for (i, &feature) in features.iter().enumerate() {
assert!(
feature.abs() < 10.0,
"Feature {} should be in reasonable scale, got: {}",
i,
feature
);
}
println!("All portfolio features in similar scale: {:?}", features);
Ok(())
}
#[test]
fn test_feature_scale_consistency() -> Result<()> {
// Bug #20: Verify portfolio features don't dominate other features
// The key test is that portfolio value is NOT 100,000 (raw value)
let initial_capital = 100_000.0;
let avg_spread = 0.0001;
let cash_reserve_percent = 0.0;
let tracker = PortfolioTracker::new(initial_capital, avg_spread, cash_reserve_percent);
let features = tracker.get_portfolio_features(4000.0);
// The key fix: Portfolio value should be ~1.0 (normalized), NOT 100,000
let portfolio_value = features[0];
// Before Bug #20 fix: Would be 100,000.0 (raw value)
// After Bug #20 fix: Should be ~1.0 (normalized)
assert!(
portfolio_value.abs() < 10.0,
"Portfolio value should be normalized, got: {}",
portfolio_value
);
// Check that portfolio value doesn't dwarf other features by 100,000x
// (spread is intentionally small, but that's OK - key is portfolio isn't massive)
let max_feature = features.iter().copied().fold(f32::NEG_INFINITY, f32::max);
// Before fix: max_feature would be 100,000 (raw portfolio value)
// After fix: max_feature should be ~1.0 (normalized portfolio value)
assert!(
max_feature < 10.0,
"Max feature should be in normalized scale, got: {}",
max_feature
);
println!("Feature scale check passed: max={:.2}, portfolio={:.2}", max_feature, portfolio_value);
Ok(())
}
#[test]
fn test_portfolio_feature_format() -> Result<()> {
// Test that get_portfolio_features returns 3 features:
// [portfolio_value, position_normalized, spread]
let tracker = PortfolioTracker::new(100_000.0, 0.0001, 0.0);
let features = tracker.get_portfolio_features(4000.0);
assert_eq!(features.len(), 3, "Should return exactly 3 features");
// Feature 0: Portfolio value (should be normalized to ~1.0)
// Feature 1: Position (normalized by max_position)
// Feature 2: Spread
println!("Portfolio features: {:?}", features);
println!("Feature 0 (portfolio value): {}", features[0]);
println!("Feature 1 (position): {}", features[1]);
println!("Feature 2 (spread): {}", features[2]);
Ok(())
}