Files
foxhunt/crates/ml/tests/bug20_portfolio_normalization_test.rs
jgrusewski 9c3d741a08 refactor: restructure repo — crates/, bin/, testing/ layout
Move 17 library crates into crates/, CLI binary into bin/fxt,
consolidate 10 test crates into testing/, split config crate
from deployment config files.

Root directory reduced from 38+ to ~17 directories.
All Cargo.toml paths and build.rs proto refs updated.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 11:56:00 +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(())
}