- Fixed DQN early stopping checkpoint naming bug (Option B)
- Added is_final: bool parameter to checkpoint callback signature
- Trainer now distinguishes final checkpoints from regular epoch checkpoints
- Final checkpoints use 'dqn_final_epoch{N}' naming convention
- Regular checkpoints use 'dqn_epoch_{N}' naming convention
- Completed comprehensive TFT OOM investigation
- Spawned 3 parallel agents for memory analysis
- Identified 16.4GB memory leak (29.7x over expected 525-550MB)
- Root causes: Attention cache bloat (960MB), gradient accumulation bug, detached tensors
- Recommended fixes: Disable cache during training, explicit tensor drops
- Created TFT_MEMORY_ANALYSIS.md, TFT_MEMORY_LEAK_ANALYSIS.md
- DQN 100-epoch training VERIFIED on Runpod RTX A4000
- Training completed successfully: 100/100 epochs
- Final checkpoint created: dqn_final_epoch100.safetensors
- Training speed: 4.8 sec/epoch (3.5x faster than baseline)
- Option B fix working perfectly
- Deployed RTX 4090 pod for TFT testing
- Pod ID: 6244yzm9hadnog
- 24GB VRAM to bypass OOM issue
- EUR-IS-1 datacenter, $0.59/hr
Files modified:
- ml/examples/train_dqn.rs (checkpoint callback signature)
- ml/src/trainers/dqn.rs (callback signature + is_final parameter)
- CLAUDE.md (compacted to ~11k chars)
Generated reports:
- TFT_MEMORY_ANALYSIS.md (15-section memory breakdown)
- TFT_MEMORY_QUICK_SUMMARY.md (executive summary)
- TFT_MEMORY_LEAK_ANALYSIS.md (5 critical leaks identified)
Co-Authored-By: Claude <noreply@anthropic.com>
470 lines
14 KiB
Rust
470 lines
14 KiB
Rust
//! Comprehensive Unit Tests for Transition Probability Features (Wave D Phase 3, Agent D15)
|
|
//!
|
|
//! This test suite validates transition probability features (indices 216-220, 5 features):
|
|
//! 1. **Stability P(i→i)** (216): Probability of staying in current regime
|
|
//! 2. **Most Likely Next Regime** (217): Index of regime with highest transition probability
|
|
//! 3. **Shannon Entropy** (218): H = -Σ P(i→j) log₂ P(i→j), uncertainty measure
|
|
//! 4. **Expected Duration** (219): E[T] = 1 / (1 - P[i][i]), bars until transition
|
|
//! 5. **Change Probability** (220): 1 - P(i→i), probability of regime change
|
|
//!
|
|
//! ## Test Coverage (15 tests across 5 categories)
|
|
//! - ✅ Stability tests (3): P(i→i) calculation, deterministic transitions, random transitions
|
|
//! - ✅ Most likely next tests (3): argmax calculation, tie breaking, index encoding
|
|
//! - ✅ Entropy tests (3): bounds [0, log₂N], deterministic (entropy=0), uniform (max entropy)
|
|
//! - ✅ Expected duration tests (3): duration calculation, integration with TransitionMatrix, edge cases
|
|
//! - ✅ Change probability tests (3): complement of stability, bounds [0, 1], deterministic vs random
|
|
//!
|
|
//! ## TDD Methodology
|
|
//! Tests written to validate full implementation of TransitionProbabilityFeatures.
|
|
|
|
use ml::ensemble::MarketRegime;
|
|
use ml::regime::transition_probability_features::TransitionProbabilityFeatures;
|
|
|
|
// ==================== CATEGORY 1: STABILITY TESTS (3 tests) ====================
|
|
|
|
#[test]
|
|
fn test_stability_self_transition_probability() {
|
|
// Test: Stability feature correctly tracks P(i→i) for current regime
|
|
let regimes = vec![
|
|
MarketRegime::Bull,
|
|
MarketRegime::Bear,
|
|
MarketRegime::Sideways,
|
|
];
|
|
|
|
let mut features = TransitionProbabilityFeatures::new(regimes, 0.2, 1);
|
|
|
|
// Create a strongly persistent sequence: Bull → Bull → Bull
|
|
for _ in 0..10 {
|
|
features.update(MarketRegime::Bull);
|
|
}
|
|
|
|
let result = features.compute_features();
|
|
let stability = result[0];
|
|
|
|
// After 10 self-transitions, stability should be very high (>0.8)
|
|
assert!(
|
|
stability > 0.8 && stability <= 1.0,
|
|
"Stability for persistent regime should be >0.8, got {}",
|
|
stability
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_stability_deterministic_transitions() {
|
|
// Test: Deterministic self-transitions yield stability ≈ 1.0
|
|
let regimes = vec![MarketRegime::Sideways];
|
|
|
|
let mut features = TransitionProbabilityFeatures::new(regimes, 0.1, 1);
|
|
|
|
// Only one regime: all transitions are self-transitions
|
|
for _ in 0..20 {
|
|
features.update(MarketRegime::Sideways);
|
|
}
|
|
|
|
let result = features.compute_features();
|
|
let stability = result[0];
|
|
|
|
// With only self-transitions, stability should approach 1.0
|
|
assert!(
|
|
stability > 0.95,
|
|
"Deterministic self-transitions should yield stability >0.95, got {}",
|
|
stability
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_stability_random_transitions() {
|
|
// Test: Random transitions between regimes yield lower stability
|
|
let regimes = vec![
|
|
MarketRegime::Bull,
|
|
MarketRegime::Bear,
|
|
MarketRegime::Sideways,
|
|
];
|
|
|
|
let mut features = TransitionProbabilityFeatures::new(regimes, 0.2, 1);
|
|
|
|
// Alternate between regimes (low persistence)
|
|
let sequence = vec![
|
|
MarketRegime::Bull,
|
|
MarketRegime::Bear,
|
|
MarketRegime::Sideways,
|
|
MarketRegime::Bull,
|
|
MarketRegime::Bear,
|
|
MarketRegime::Sideways,
|
|
];
|
|
|
|
for regime in sequence {
|
|
features.update(regime);
|
|
}
|
|
|
|
let result = features.compute_features();
|
|
let stability = result[0];
|
|
|
|
// With frequent transitions, stability should be lower (<0.6)
|
|
assert!(
|
|
stability < 0.6,
|
|
"Random transitions should yield stability <0.6, got {}",
|
|
stability
|
|
);
|
|
}
|
|
|
|
// ==================== CATEGORY 2: MOST LIKELY NEXT REGIME TESTS (3 tests) ====================
|
|
|
|
#[test]
|
|
fn test_most_likely_next_argmax_calculation() {
|
|
// Test: Most likely next regime correctly identifies highest transition probability
|
|
let regimes = vec![
|
|
MarketRegime::Bull,
|
|
MarketRegime::Bear,
|
|
MarketRegime::Sideways,
|
|
];
|
|
|
|
let mut features = TransitionProbabilityFeatures::new(regimes, 0.2, 1);
|
|
|
|
// Create pattern: Bull → Sideways (repeated)
|
|
for _ in 0..10 {
|
|
features.update(MarketRegime::Bull);
|
|
features.update(MarketRegime::Sideways);
|
|
}
|
|
|
|
// Ensure current regime is Bull
|
|
features.update(MarketRegime::Bull);
|
|
|
|
let result = features.compute_features();
|
|
let most_likely_idx = result[1] as usize;
|
|
|
|
// Most likely next regime from Bull should be Sideways (index 2)
|
|
assert_eq!(
|
|
most_likely_idx, 2,
|
|
"Most likely next regime after Bull should be Sideways (index 2), got {}",
|
|
most_likely_idx
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_most_likely_next_tie_breaking() {
|
|
// Test: Tie breaking when multiple regimes have equal probability
|
|
let regimes = vec![MarketRegime::Bull, MarketRegime::Bear];
|
|
|
|
let features = TransitionProbabilityFeatures::new(regimes, 0.1, 10);
|
|
|
|
// With no updates, both transitions have equal probability (uniform initialization)
|
|
let result = features.compute_features();
|
|
let most_likely_idx = result[1] as usize;
|
|
|
|
// Should return the first matching index (0 or 1)
|
|
assert!(
|
|
most_likely_idx < 2,
|
|
"Most likely index should be valid (0-1), got {}",
|
|
most_likely_idx
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_most_likely_next_index_encoding() {
|
|
// Test: Index encoding correctly maps regime to 0-based index
|
|
let regimes = vec![
|
|
MarketRegime::Bull, // Index 0
|
|
MarketRegime::Bear, // Index 1
|
|
MarketRegime::Sideways, // Index 2
|
|
];
|
|
|
|
let features = TransitionProbabilityFeatures::new(regimes, 0.1, 1);
|
|
let result = features.compute_features();
|
|
let most_likely_idx = result[1];
|
|
|
|
// Index should be in valid range [0, 2]
|
|
assert!(
|
|
most_likely_idx >= 0.0 && most_likely_idx <= 2.0,
|
|
"Most likely index should be in [0, 2], got {}",
|
|
most_likely_idx
|
|
);
|
|
}
|
|
|
|
// ==================== CATEGORY 3: ENTROPY TESTS (3 tests) ====================
|
|
|
|
#[test]
|
|
fn test_entropy_bounds() {
|
|
// Test: Shannon entropy stays within bounds [0, log₂(N)]
|
|
let regimes = vec![
|
|
MarketRegime::Bull,
|
|
MarketRegime::Bear,
|
|
MarketRegime::Sideways,
|
|
MarketRegime::HighVolatility,
|
|
];
|
|
|
|
let mut features = TransitionProbabilityFeatures::new(regimes.clone(), 0.2, 1);
|
|
|
|
// Create diverse transition pattern
|
|
let sequence = vec![
|
|
MarketRegime::Bull,
|
|
MarketRegime::Bear,
|
|
MarketRegime::Sideways,
|
|
MarketRegime::HighVolatility,
|
|
MarketRegime::Bull,
|
|
];
|
|
|
|
for regime in sequence {
|
|
features.update(regime);
|
|
}
|
|
|
|
let result = features.compute_features();
|
|
let entropy = result[2];
|
|
|
|
let max_entropy = (regimes.len() as f64).log2();
|
|
|
|
assert!(
|
|
entropy >= 0.0 && entropy <= max_entropy,
|
|
"Entropy should be in [0, {:.4}], got {:.4}",
|
|
max_entropy,
|
|
entropy
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_entropy_deterministic_zero() {
|
|
// Test: Deterministic transitions (single outcome) yield entropy ≈ 0
|
|
let regimes = vec![MarketRegime::Sideways];
|
|
|
|
let mut features = TransitionProbabilityFeatures::new(regimes, 0.1, 1);
|
|
|
|
// Only one regime: deterministic transitions
|
|
for _ in 0..20 {
|
|
features.update(MarketRegime::Sideways);
|
|
}
|
|
|
|
let result = features.compute_features();
|
|
let entropy = result[2];
|
|
|
|
// Deterministic case: entropy should be near zero
|
|
assert!(
|
|
entropy < 0.1,
|
|
"Deterministic transitions should yield low entropy (<0.1), got {:.4}",
|
|
entropy
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_entropy_uniform_maximum() {
|
|
// Test: Uniform distribution over regimes yields maximum entropy
|
|
let regimes = vec![
|
|
MarketRegime::Bull,
|
|
MarketRegime::Bear,
|
|
MarketRegime::Sideways,
|
|
MarketRegime::HighVolatility,
|
|
];
|
|
|
|
// With min_obs=100, insufficient data forces uniform Laplace smoothing
|
|
let features = TransitionProbabilityFeatures::new(regimes.clone(), 0.1, 100);
|
|
|
|
let result = features.compute_features();
|
|
let entropy = result[2];
|
|
|
|
let max_entropy = (regimes.len() as f64).log2();
|
|
|
|
// Uniform distribution should yield near-maximum entropy
|
|
assert!(
|
|
(entropy - max_entropy).abs() < 0.5,
|
|
"Uniform distribution should yield entropy ≈ {:.4}, got {:.4}",
|
|
max_entropy,
|
|
entropy
|
|
);
|
|
}
|
|
|
|
// ==================== CATEGORY 4: EXPECTED DURATION TESTS (3 tests) ====================
|
|
|
|
#[test]
|
|
fn test_expected_duration_calculation() {
|
|
// Test: Expected duration correctly calculated as E[T] = 1 / (1 - P[i][i])
|
|
let regimes = vec![MarketRegime::Sideways];
|
|
|
|
let mut features = TransitionProbabilityFeatures::new(regimes, 0.1, 1);
|
|
|
|
// Create high persistence: P(Sideways→Sideways) ≈ 0.9
|
|
for _ in 0..20 {
|
|
features.update(MarketRegime::Sideways);
|
|
}
|
|
|
|
let result = features.compute_features();
|
|
let duration = result[3];
|
|
|
|
// E[T] = 1 / (1 - 0.9) = 10 periods (approximately)
|
|
assert!(
|
|
duration > 5.0,
|
|
"High persistence should yield duration >5 periods, got {:.2}",
|
|
duration
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_expected_duration_integration_with_transition_matrix() {
|
|
// Test: Duration feature integrates correctly with underlying TransitionMatrix
|
|
let regimes = vec![MarketRegime::Bull, MarketRegime::Bear];
|
|
|
|
let mut features = TransitionProbabilityFeatures::new(regimes, 0.2, 1);
|
|
|
|
// Create persistent Bull regime
|
|
for _ in 0..15 {
|
|
features.update(MarketRegime::Bull);
|
|
}
|
|
|
|
let result = features.compute_features();
|
|
let duration = result[3];
|
|
|
|
// Direct validation: duration from TransitionMatrix should match feature
|
|
let matrix_duration = features
|
|
.transition_matrix()
|
|
.get_expected_duration(MarketRegime::Bull);
|
|
|
|
assert!(
|
|
(duration - matrix_duration).abs() < 1e-6,
|
|
"Feature duration should match TransitionMatrix, got feature={:.4}, matrix={:.4}",
|
|
duration,
|
|
matrix_duration
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_expected_duration_edge_cases() {
|
|
// Test: Edge cases - zero persistence, low observations
|
|
let regimes = vec![MarketRegime::Bull, MarketRegime::Bear];
|
|
|
|
let mut features = TransitionProbabilityFeatures::new(regimes, 0.5, 1);
|
|
|
|
// Alternate between regimes (zero persistence in each regime)
|
|
for _ in 0..10 {
|
|
features.update(MarketRegime::Bull);
|
|
features.update(MarketRegime::Bear);
|
|
}
|
|
|
|
// Ensure current regime is Bull
|
|
features.update(MarketRegime::Bull);
|
|
|
|
let result = features.compute_features();
|
|
let duration = result[3];
|
|
|
|
// Low persistence: duration should be near 1.0 (immediate exit)
|
|
assert!(
|
|
duration >= 1.0 && duration < 3.0,
|
|
"Low persistence should yield duration near 1.0, got {:.2}",
|
|
duration
|
|
);
|
|
}
|
|
|
|
// ==================== CATEGORY 5: CHANGE PROBABILITY TESTS (3 tests) ====================
|
|
|
|
#[test]
|
|
fn test_change_probability_complement_of_stability() {
|
|
// Test: Change probability = 1 - stability (exact complement)
|
|
let regimes = vec![
|
|
MarketRegime::Bull,
|
|
MarketRegime::Bear,
|
|
MarketRegime::Sideways,
|
|
];
|
|
|
|
let mut features = TransitionProbabilityFeatures::new(regimes, 0.2, 1);
|
|
|
|
// Create mixed transition pattern
|
|
for _ in 0..5 {
|
|
features.update(MarketRegime::Bull);
|
|
features.update(MarketRegime::Bear);
|
|
}
|
|
|
|
let result = features.compute_features();
|
|
let stability = result[0];
|
|
let change_prob = result[4];
|
|
|
|
// Change probability should be exact complement of stability
|
|
assert!(
|
|
(stability + change_prob - 1.0).abs() < 1e-10,
|
|
"stability + change_prob should equal 1.0, got {:.10} + {:.10} = {:.10}",
|
|
stability,
|
|
change_prob,
|
|
stability + change_prob
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_change_probability_bounds() {
|
|
// Test: Change probability stays within [0, 1]
|
|
let regimes = vec![
|
|
MarketRegime::Bull,
|
|
MarketRegime::Bear,
|
|
MarketRegime::Sideways,
|
|
MarketRegime::HighVolatility,
|
|
];
|
|
|
|
let mut features = TransitionProbabilityFeatures::new(regimes, 0.2, 1);
|
|
|
|
// Create diverse transitions
|
|
let sequence = vec![
|
|
MarketRegime::Bull,
|
|
MarketRegime::Bear,
|
|
MarketRegime::Sideways,
|
|
MarketRegime::HighVolatility,
|
|
];
|
|
|
|
for regime in sequence {
|
|
features.update(regime);
|
|
}
|
|
|
|
let result = features.compute_features();
|
|
let change_prob = result[4];
|
|
|
|
assert!(
|
|
change_prob >= 0.0 && change_prob <= 1.0,
|
|
"Change probability should be in [0, 1], got {:.4}",
|
|
change_prob
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_change_probability_deterministic_vs_random() {
|
|
// Test: Compare change probability for deterministic vs random transitions
|
|
|
|
// Deterministic case: single regime (low change probability)
|
|
let regimes_det = vec![MarketRegime::Sideways];
|
|
let mut features_det = TransitionProbabilityFeatures::new(regimes_det, 0.1, 1);
|
|
|
|
for _ in 0..20 {
|
|
features_det.update(MarketRegime::Sideways);
|
|
}
|
|
|
|
let result_det = features_det.compute_features();
|
|
let change_prob_det = result_det[4];
|
|
|
|
// Random case: alternating regimes (high change probability)
|
|
let regimes_rand = vec![MarketRegime::Bull, MarketRegime::Bear];
|
|
let mut features_rand = TransitionProbabilityFeatures::new(regimes_rand, 0.2, 1);
|
|
|
|
for _ in 0..10 {
|
|
features_rand.update(MarketRegime::Bull);
|
|
features_rand.update(MarketRegime::Bear);
|
|
}
|
|
|
|
let result_rand = features_rand.compute_features();
|
|
let change_prob_rand = result_rand[4];
|
|
|
|
// Random transitions should have higher change probability than deterministic
|
|
assert!(
|
|
change_prob_rand > change_prob_det,
|
|
"Random transitions should have higher change probability than deterministic, got random={:.4}, det={:.4}",
|
|
change_prob_rand,
|
|
change_prob_det
|
|
);
|
|
|
|
// Deterministic case should have low change probability (<0.1)
|
|
assert!(
|
|
change_prob_det < 0.1,
|
|
"Deterministic case should have change probability <0.1, got {:.4}",
|
|
change_prob_det
|
|
);
|
|
|
|
// Random case should have high change probability (>0.5)
|
|
assert!(
|
|
change_prob_rand > 0.5,
|
|
"Random case should have change probability >0.5, got {:.4}",
|
|
change_prob_rand
|
|
);
|
|
}
|