Files
foxhunt/ml/tests/ppo_reward_normalizer_tests.rs
jgrusewski c645e6222d Wave 11: Rainbow DQN integration + 23/23 tests passing
CRITICAL FINDINGS from 3-trial validation:
- 85,120 gradient clipping warnings (81.6% of logs) - REGRESSION
- Rainbow features DISABLED: use_dueling=false, use_distributional=false, use_noisy_nets=false
- Negative Q-values confirmed: HOLD -1000 to -3250
- Performance: Sharpe 0.29 (target 0.77)

Changes:
- Fixed N-Step compilation (7/7 tests passing)
- Fixed Distributional compilation (6/6 tests passing)
- Fixed Dueling CUDA errors (10/10 tests passing)
- Added TDD validation for state_dim=225
- Total: 23/23 Wave 11 tests passing (100%)

Issues requiring investigation:
1. Why are Dueling/Distributional/Noisy disabled in hyperopt?
2. Why gradient explosion despite previous fixes?
3. Test coverage gaps - unit tests pass but integration fails

🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 13:53:59 +01:00

88 lines
2.6 KiB
Rust

//! Test suite for PPO RewardNormalizer
//!
//! TDD Red Phase: Tests written FIRST before implementation
//! These tests define the expected behavior of the RewardNormalizer module
// Import from PPO module (will fail until implementation exists)
use ml::ppo::reward_normalizer::RewardNormalizer;
#[test]
fn test_reward_normalizer_welford() {
// Test Welford's algorithm correctness with known statistics
let mut normalizer = RewardNormalizer::new();
// Add values: [10, 20, 30, 40, 50]
let values = vec![10.0, 20.0, 30.0, 40.0, 50.0];
for &value in &values {
normalizer.update(value);
}
// Expected mean: 30.0
// Expected variance: 200.0 (population variance)
// Expected std: 14.142
let (mean, std) = normalizer.get_stats();
assert!((mean - 30.0).abs() < 1e-6, "Mean should be 30.0, got {}", mean);
assert!(
(std - 14.142135623730951).abs() < 1e-6,
"Std should be ~14.142, got {}",
std
);
// Test normalization: (40 - 30) / 14.142 ≈ 0.707
let normalized = normalizer.normalize(40.0);
assert!(
(normalized - 0.7071067811865475).abs() < 1e-6,
"Normalized value should be ~0.707, got {}",
normalized
);
// Verify count
assert_eq!(normalizer.count(), 5, "Count should be 5");
}
#[test]
fn test_reward_normalizer_numerical_stability() {
// Test with extreme values to ensure numerical stability
let mut normalizer = RewardNormalizer::new();
// Add extreme positive and negative values
let extreme_values = vec![-1000.0, -500.0, 0.0, 500.0, 1000.0];
for &value in &extreme_values {
normalizer.update(value);
}
let (mean, std) = normalizer.get_stats();
// Mean should be 0.0 (symmetric distribution)
assert!(
mean.abs() < 1e-6,
"Mean should be ~0.0 for symmetric values, got {}",
mean
);
// Std should be ~707.1 (for uniform extreme distribution)
assert!(
std > 700.0 && std < 710.0,
"Std should be ~707.1, got {}",
std
);
// Normalize a new extreme value
let normalized_extreme = normalizer.normalize(1000.0);
// Should be bounded: 1000 is 1 std above mean
assert!(
normalized_extreme > 1.0 && normalized_extreme < 2.0,
"Normalized extreme should be ~1.58, got {}",
normalized_extreme
);
// Test with very small epsilon (no division by zero)
normalizer.update(0.0);
let normalized_zero = normalizer.normalize(0.0);
assert!(
!normalized_zero.is_nan() && !normalized_zero.is_infinite(),
"Normalized value should not be NaN or Inf"
);
}