WAVE 19: Risk-optimized hyperparameter tuning with Kelly position sizing Background: - Wave 18 investigation found Kelly parameters were HARDCODED in trainer - Missing opportunity for +10-30% Sharpe improvement from Kelly optimization - DQN trainer already has full Kelly sizing infrastructure (get_kelly_fraction) Implementation (3 Parallel Test-Driven Agents): **Agent 1**: Search Space Expansion (18D → 22D) - Added 4 Kelly fields to DQNParams struct (lines 253-256): * kelly_fractional: [0.25, 1.0] - Fractional Kelly bet sizing * kelly_max_fraction: [0.1, 0.5] - Maximum position cap * kelly_min_trades: [10, 50] - Minimum sample size for Kelly * volatility_window: [10, 30] - Rolling volatility lookback - Updated continuous_bounds() with Kelly parameter ranges (lines 329-331) - Updated from_continuous() to parse 22D vectors (lines 434-437) - Wired Kelly params to DQNHyperparameters construction (line 1786) - Fixed duplicate field initialization bugs - Created 7 comprehensive tests (76 lines) **Agent 2**: Struct Compatibility Validation - Verified DQNHyperparameters has all 4 Kelly fields (trainers/dqn.rs:484-490) - Confirmed fields actively used in get_kelly_fraction() method - Fixed duplicate Kelly field assignments in existing tests - Created 8 validation tests (119 lines) **Agent 3**: Integration Testing - Created 4 end-to-end 22D parameter conversion tests (122 lines) - Verified round-trip parameter conversion - Validated Kelly parameter extraction and clamping Files Modified: - ml/src/hyperopt/adapters/dqn.rs: +106 lines (search space expansion) - ml/tests/hyperopt_kelly_params_test.rs: +76 lines (NEW) - ml/tests/dqn_hyperparams_kelly_fields_test.rs: +119 lines (NEW) - ml/tests/hyperopt_kelly_integration_test.rs: +122 lines (NEW) Test Results: - New tests: 19 (7 + 8 + 4) - All tests: 1,718/1,718 passing (100%) Search Space Evolution: - Wave 1-10: 18D (Core DQN + Rainbow + Bug Fixes) - Wave 19: 22D (+ Kelly Risk Parameters) Expected Impact: - +10-30% Sharpe improvement from optimized Kelly position sizing - Adaptive risk management tuned per market regime - Better drawdown control via kelly_max_fraction optimization Next: 5-trial hyperopt validation with 22D search space (Wave 17) 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
121 lines
4.6 KiB
Rust
121 lines
4.6 KiB
Rust
//! Test Suite: DQN Hyperparameters Kelly Fields Validation
|
|
//!
|
|
//! Purpose: Verify that DQNHyperparameters struct has all 4 Kelly sizing fields
|
|
//! and that they are correctly configured with sensible defaults.
|
|
//!
|
|
//! This test validates WAVE 19 Agent 2's requirement that Kelly parameters
|
|
//! are properly integrated into the hyperparameters struct.
|
|
|
|
#[cfg(test)]
|
|
mod dqn_hyperparams_kelly_tests {
|
|
use ml::trainers::dqn::DQNHyperparameters;
|
|
|
|
#[test]
|
|
fn test_hyperparams_has_kelly_fractional() {
|
|
let hyperparams = DQNHyperparameters::conservative();
|
|
// Should compile if field exists
|
|
let _kelly = hyperparams.kelly_fractional;
|
|
|
|
// Verify it's accessible
|
|
assert!(hyperparams.kelly_fractional > 0.0);
|
|
}
|
|
|
|
#[test]
|
|
fn test_hyperparams_has_kelly_max_fraction() {
|
|
let hyperparams = DQNHyperparameters::conservative();
|
|
let _kelly_max = hyperparams.kelly_max_fraction;
|
|
|
|
// Verify it's accessible
|
|
assert!(hyperparams.kelly_max_fraction > 0.0);
|
|
}
|
|
|
|
#[test]
|
|
fn test_hyperparams_has_kelly_min_trades() {
|
|
let hyperparams = DQNHyperparameters::conservative();
|
|
let _min_trades = hyperparams.kelly_min_trades;
|
|
|
|
// Verify it's accessible
|
|
assert!(hyperparams.kelly_min_trades > 0);
|
|
}
|
|
|
|
#[test]
|
|
fn test_hyperparams_has_volatility_window() {
|
|
let hyperparams = DQNHyperparameters::conservative();
|
|
let _vol_window = hyperparams.volatility_window;
|
|
|
|
// Verify it's accessible
|
|
assert!(hyperparams.volatility_window > 0);
|
|
}
|
|
|
|
#[test]
|
|
fn test_custom_kelly_params_can_be_set() {
|
|
let mut hyperparams = DQNHyperparameters::conservative();
|
|
|
|
// Set custom values
|
|
hyperparams.kelly_fractional = 0.75;
|
|
hyperparams.kelly_max_fraction = 0.4;
|
|
hyperparams.kelly_min_trades = 35;
|
|
hyperparams.volatility_window = 25;
|
|
|
|
// Verify they were set correctly
|
|
assert_eq!(hyperparams.kelly_fractional, 0.75);
|
|
assert_eq!(hyperparams.kelly_max_fraction, 0.4);
|
|
assert_eq!(hyperparams.kelly_min_trades, 35);
|
|
assert_eq!(hyperparams.volatility_window, 25);
|
|
}
|
|
|
|
#[test]
|
|
fn test_default_kelly_params_are_reasonable() {
|
|
let hyperparams = DQNHyperparameters::conservative();
|
|
|
|
// Check defaults are within hyperopt bounds (from Agent 1 specification)
|
|
// kelly_fractional: [0.25, 1.0]
|
|
assert!(hyperparams.kelly_fractional >= 0.25 && hyperparams.kelly_fractional <= 1.0,
|
|
"Default kelly_fractional ({}) should be in [0.25, 1.0]", hyperparams.kelly_fractional);
|
|
|
|
// kelly_max_fraction: [0.1, 0.5]
|
|
assert!(hyperparams.kelly_max_fraction >= 0.1 && hyperparams.kelly_max_fraction <= 0.5,
|
|
"Default kelly_max_fraction ({}) should be in [0.1, 0.5]", hyperparams.kelly_max_fraction);
|
|
|
|
// kelly_min_trades: [10, 50]
|
|
assert!(hyperparams.kelly_min_trades >= 10 && hyperparams.kelly_min_trades <= 50,
|
|
"Default kelly_min_trades ({}) should be in [10, 50]", hyperparams.kelly_min_trades);
|
|
|
|
// volatility_window: [10, 30]
|
|
assert!(hyperparams.volatility_window >= 10 && hyperparams.volatility_window <= 30,
|
|
"Default volatility_window ({}) should be in [10, 30]", hyperparams.volatility_window);
|
|
}
|
|
|
|
#[test]
|
|
fn test_kelly_fields_are_public() {
|
|
// This test verifies that all Kelly fields are pub (accessible to hyperopt adapter)
|
|
let hyperparams = DQNHyperparameters::conservative();
|
|
|
|
// If these compile, the fields are public
|
|
let _: f64 = hyperparams.kelly_fractional;
|
|
let _: f64 = hyperparams.kelly_max_fraction;
|
|
let _: usize = hyperparams.kelly_min_trades;
|
|
let _: usize = hyperparams.volatility_window;
|
|
}
|
|
|
|
#[test]
|
|
fn test_expected_default_values() {
|
|
let hyperparams = DQNHyperparameters::conservative();
|
|
|
|
// From trainer code (lines 629-632):
|
|
// kelly_fractional: 0.5 (half-Kelly, conservative)
|
|
// kelly_max_fraction: 0.25 (max 25% of portfolio)
|
|
// kelly_min_trades: 20 (20 trades minimum)
|
|
// volatility_window: 20 (20-period rolling window)
|
|
|
|
assert_eq!(hyperparams.kelly_fractional, 0.5,
|
|
"Expected default kelly_fractional=0.5 (half-Kelly)");
|
|
assert_eq!(hyperparams.kelly_max_fraction, 0.25,
|
|
"Expected default kelly_max_fraction=0.25 (25% max)");
|
|
assert_eq!(hyperparams.kelly_min_trades, 20,
|
|
"Expected default kelly_min_trades=20");
|
|
assert_eq!(hyperparams.volatility_window, 20,
|
|
"Expected default volatility_window=20");
|
|
}
|
|
}
|