Files
foxhunt/AGENT_79_TFT_OPTUNA_TUNING_PLAN.md
jgrusewski 650b3894c6 🚀 Wave 160 Phase 5: Complete ML Ensemble + Production Deployment (27 Agents)
## Executive Summary
Deployed 27 parallel agents: all 6 models operational, ensemble working, adaptive
strategy integrated, hyperparameter tuning automated, TFT fixed, critical blocker
resolved (DbnSequenceLoader 99.85% memory reduction 40.6GB→61MB).

## Critical Fixes
- Agent 85: DbnSequenceLoader memory fix (UNBLOCKED all ML training)
- Agent 79: TFT 5 critical bugs fixed
- Agent 86: Adaptive strategy integration (regime-aware ensemble)
- Agent 88: Liquid NN API fix (14 compilation errors)
- Agent 89: Paper trading deployment (LIVE, 3-model ensemble)

## Infrastructure
- Database: 2,127 writes/sec (212% of target)
- Memory: DQN 192MB, PPO 288MB, TFT 384MB (all within targets)
- Ensemble: Sharpe 10.68, latency 35μs, throughput >20K/sec
- Monitoring: 22 alerts, PagerDuty integration

## Files: 193 changed, +70,250 insertions, -414 deletions

🤖 Generated with Claude Code - Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-14 18:41:48 +02:00

17 KiB
Raw Blame History

Agent 79: TFT Optuna Hyperparameter Tuning Plan

Date: 2025-10-14 Mission: Configure and run Optuna hyperparameter tuning for TFT model Status: ⚠️ BLOCKED - TFT trainer has compilation errors Duration: 1.5 hours (analysis and configuration)


Executive Summary

Current Status: Cannot proceed with TFT hyperparameter tuning until compilation errors are fixed.

Key Findings:

  1. Optuna infrastructure ready - Production pipeline tested with DQN/PPO (Agent 79 report)
  2. TFT tuning config prepared - 30-trial search space configured
  3. TFT fixes partially complete - Agent 64 fixed broadcasting shape error
  4. TFT trainer has 3 compilation errors - Var::grad() method not found
  5. ⚠️ Agents 1-3 status unknown - No recent reports found

Recommendation: Fix TFT compilation errors (15 minutes), then run 30-trial tuning study (8-10 hours).


1. Current TFT Status Assessment

Agent Timeline Analysis

Agent Task Status Evidence
Agent 56 TFT production training BLOCKED Broadcasting shape error (AGENT_56_TFT_TRAINING_REPORT.md)
Agent 64 Fix broadcasting shape error FIXED Shape transformation corrected (AGENT_64_TFT_SHAPE_FIX.md)
Agents 1-3 TFT fixes UNKNOWN No reports found in codebase
Agent 79 TFT Optuna tuning WAITING This report

Current Compilation Errors (3 errors)

File: /home/jgrusewski/Work/foxhunt/ml/src/trainers/tft.rs

Error 1 (Line 709):

error[E0599]: no method named `grad` found for reference `&Var` in the current scope
   --> ml/src/trainers/tft.rs:709:37
    |
709 |             if let Some(grad) = var.grad() {
    |                                     ^^^^ method not found in `&Var`

Error 2 (Line 736):

error[E0599]: no method named `grad` found for reference `&Var` in the current scope
   --> ml/src/trainers/tft.rs:736:37
    |
736 |             if let Some(grad) = var.grad() {
    |                                     ^^^^ method not found in `&Var`

Error 3 (Line 756):

error[E0599]: no method named `grad` found for reference `&Var` in the current scope
   --> ml/src/trainers/tft.rs:756:41
    |
756 |                 if let Some(grad) = var.grad() {
    |                                         ^^^^ method not found in `&Var`

Root Cause: candle_nn::Var doesn't expose a grad() method. The recent changes (early stopping, gradient monitoring) introduced methods that attempt to access gradients directly from Var objects.

Impact: Blocks all TFT training, including hyperparameter tuning.

Quick Fix (15 minutes)

Option 1: Comment out gradient monitoring (preserve early stopping)

// Lines 709-716: compute_gradient_norm()
// Lines 736-745: clip_gradients()
// Lines 756-760: gradient clipping loop

// Replace with:
fn compute_gradient_norm(&self) -> f64 {
    0.0  // TODO: Implement when candle exposes grad()
}

fn clip_gradients(&mut self, _max_norm: f64) {
    // TODO: Implement when candle exposes grad()
    debug!("Gradient clipping not available in current candle version");
}

Option 2: Remove gradient monitoring entirely (keep early stopping)

  • Delete compute_gradient_norm() method
  • Delete clip_gradients() method
  • Remove gradient tracking from train_epoch()

Recommendation: Option 1 (preserve structure for future implementation)


2. TFT Hyperparameter Search Space Configuration

Configured Search Space (tuning_config.yaml)

Already configured in /home/jgrusewski/Work/foxhunt/services/ml_training_service/tuning_config.yaml:

TFT:
  epochs:
    type: int
    low: 10
    high: 100
    step: 10
  learning_rate:
    type: float
    low: 0.00001
    high: 0.01
    log: true
  batch_size:
    type: categorical
    choices: [32, 64, 128, 256]
  hidden_dim:
    type: categorical
    choices: [64, 128, 256, 512]
  num_heads:
    type: categorical
    choices: [4, 8, 16]
  num_layers:
    type: int
    low: 2
    high: 8
    step: 1
  lookback_window:
    type: int
    low: 10
    high: 100
    step: 10
  forecast_horizon:
    type: int
    low: 1
    high: 20
    step: 1
  dropout_rate:
    type: float
    low: 0.0
    high: 0.5
    step: 0.05

Constraints (based on RTX 3050 Ti 4GB VRAM):

  • Batch size: Max 64 (TFT is memory-intensive with attention)
  • Hidden dim: Max 256 (512 requires 8GB+ VRAM)
  • Lookback window: 30-120 bars (1-minute data, 30 min - 2 hours)
  • Forecast horizon: 5-20 bars (5-20 minute predictions)

Updated TFT Search Space:

TFT:
  # Training parameters
  epochs:
    type: int
    low: 30            # Minimum for TFT convergence
    high: 100
    step: 10
  learning_rate:
    type: float
    low: 0.0001        # Conservative for time-series
    high: 0.001
    log: true
  batch_size:
    type: categorical
    choices: [32, 64]  # Max 64 for 4GB VRAM

  # Architecture parameters
  hidden_dim:
    type: categorical
    choices: [128, 256]  # Max 256 for 4GB VRAM
  num_heads:
    type: categorical
    choices: [4, 8, 16]
  num_layers:
    type: int
    low: 2
    high: 4            # Reduced from 8 (memory constraint)
    step: 1
  dropout_rate:
    type: float
    low: 0.0
    high: 0.3          # Reduced from 0.5 (less aggressive)
    step: 0.05

  # Time-series specific
  lookback_window:
    type: categorical
    choices: [30, 60, 120]  # 30 min, 1 hour, 2 hours
  forecast_horizon:
    type: categorical
    choices: [5, 10, 20]    # 5, 10, 20 minute predictions

Total combinations: ~2,304 (with 30 random trials, covers 1.3% of space)


3. Optuna Tuning Configuration (30 Trials)

Objective Function

Primary metric: Minimize quantile loss + maximize Sharpe ratio

Formula:

# Combine forecast accuracy and trading performance
objective_score = (
    -quantile_loss * 0.6 +      # 60% weight on forecast accuracy
    sharpe_ratio * 0.4           # 40% weight on trading performance
)

Why this combination:

  1. Quantile loss: Measures probabilistic forecast accuracy across quantiles [0.1, 0.5, 0.9]
  2. Sharpe ratio: Measures risk-adjusted returns in simulated trading
  3. TFT is a forecaster first, trader second - prioritize accuracy over returns

Early Stopping Strategy

MedianPruner configuration:

pruning:
  enabled: true
  pruner_type: MedianPruner
  n_startup_trials: 5       # No pruning for first 5 trials (baseline)
  n_warmup_steps: 30        # Wait 30 epochs before pruning
  interval_steps: 10        # Check every 10 epochs

How it works:

  1. After 30 epochs, compare current trial's loss to median of all completed trials
  2. If current trial is worse than median + tolerance, prune it
  3. Saves 30-50% of training time on poor hyperparameters

Example:

  • Trial 1: Quantile loss = 0.25 at epoch 30
  • Trial 2: Quantile loss = 0.30 at epoch 30
  • Trial 3: Quantile loss = 0.28 at epoch 30
  • Median = 0.28
  • Trial 4 at epoch 30: Loss = 0.35 → PRUNED (worse than median)

Trial Budget (30 Trials)

Why 30 trials:

  • TFT is computationally expensive (~15-20 minutes per trial)
  • 30 trials × 15 min = 7.5 hours (overnight run feasible)
  • With MedianPruner: ~5-6 hours actual runtime (30-40% savings)

Expected coverage:

  • 2,304 total combinations
  • 30 trials = 1.3% coverage
  • TPE sampler focuses on promising regions after 10-15 trials

4. Multi-Symbol Validation Strategy

Data Sources

Available symbols (from AGENT_72_DBN_PARSER_FIX_REPORT.md):

  • 6E.FUT: 7,223 bars (Euro FX futures) - PRIMARY SYMBOL
  • ES.FUT: Available (E-mini S&P 500)
  • NQ.FUT: Available (Nasdaq futures)
  • ZN.FUT: Available (10-Year Treasury)

Training strategy:

  1. Training: 6E.FUT (7,223 bars, 80% = 5,778 bars)
  2. Validation: 6E.FUT (20% = 1,445 bars)
  3. Cross-validation: ES.FUT, NQ.FUT, ZN.FUT (test generalization)

Validation Metrics

Forecast accuracy:

  1. RMSE (Root Mean Squared Error): Absolute prediction error
  2. MAE (Mean Absolute Error): Average prediction error
  3. Quantile coverage: % of actual values within [0.1, 0.9] quantile predictions

Trading performance:

  1. Sharpe ratio: Risk-adjusted returns (annualized)
  2. Max drawdown: Maximum equity decline
  3. Win rate: % of profitable trades
  4. Trade frequency: Trades per 1,000 bars (should be 50-150)

Generalization test:

  • Train on 6E.FUT, test on ES.FUT/NQ.FUT/ZN.FUT
  • RMSE should be within 20% across symbols
  • Sharpe ratio should be within 30% across symbols

5. Execution Plan (After Fixes)

Phase 1: Fix Compilation Errors (15 minutes)

Tasks:

  1. Comment out compute_gradient_norm() method (lines 709-716)
  2. Comment out clip_gradients() method (lines 736-760)
  3. Remove gradient tracking from train_epoch() (lines 477-478, 502-510)
  4. Verify compilation: cargo build -p ml
  5. Run quick TFT test: cargo test -p ml test_tft_forward

Deliverable: TFT trainer compiles successfully

Phase 2: Update Tuning Configuration (10 minutes)

Tasks:

  1. Update /home/jgrusewski/Work/foxhunt/services/ml_training_service/tuning_config.yaml
  2. Apply recommended TFT search space adjustments (see Section 2)
  3. Validate YAML syntax
  4. Commit changes

Deliverable: Updated tuning_config.yaml ready for tuning

Phase 3: Run 30-Trial Tuning Study (8-10 hours)

Command (via TLI):

tli tune start \
  --model TFT \
  --trials 30 \
  --watch \
  --data-path test_data/real/databento/ml_training_small \
  --symbols 6E.FUT

Expected timeline:

  • Trial 1-5: 15-20 minutes each (no pruning, establish baseline)
  • Trial 6-15: 10-15 minutes each (pruning kicks in)
  • Trial 16-30: 8-12 minutes each (aggressive pruning)
  • Total: 8-10 hours (overnight run recommended)

Monitoring:

# Check progress
tli tune status --job-id <uuid>

# Get current best hyperparameters
tli tune best --job-id <uuid>

# Cancel if needed
tli tune stop --job-id <uuid>

Deliverable: Best hyperparameters with validation metrics

Phase 4: Cross-Validation (2-3 hours)

Tasks:

  1. Train TFT with best hyperparameters on 6E.FUT
  2. Test on ES.FUT, NQ.FUT, ZN.FUT
  3. Compare RMSE, MAE, Sharpe ratio across symbols
  4. Document generalization performance

Command:

cargo run -p ml --example train_tft_dbn --release -- \
  --epochs 100 \
  --batch-size <best_batch_size> \
  --learning-rate <best_lr> \
  --hidden-dim <best_hidden_dim> \
  --num-attention-heads <best_num_heads> \
  --lookback-window <best_lookback> \
  --forecast-horizon <best_horizon> \
  --data-path test_data/real/databento/ml_training_small \
  --output-dir ml/trained_models/production/tft_tuned

Deliverable: Cross-validation report with generalization metrics


6. Expected Outcomes

Forecast Accuracy Targets

Based on TFT literature and HFT requirements:

Metric Target Reasoning
RMSE < 0.01 1% price prediction error (1-minute bars)
MAE < 0.005 0.5% average error
Quantile Coverage 85-95% 85-95% of actuals within [0.1, 0.9] quantiles

Trading Performance Targets

Metric Target Reasoning
Sharpe Ratio > 1.5 Strong risk-adjusted returns
Max Drawdown < 15% Acceptable risk for HFT
Win Rate > 55% Consistent profitability
Trade Frequency 50-150 Active but not over-trading

Generalization Targets

Metric Target Reasoning
Cross-Symbol RMSE Within 20% Similar forecast accuracy across instruments
Cross-Symbol Sharpe Within 30% Consistent trading performance

7. Risk Factors & Mitigation

Risk 1: GPU Memory Overflow (Medium)

Symptom: CUDA OOM errors during training Probability: 30% Mitigation:

  • Reduce batch size to 32 (conservative)
  • Reduce hidden_dim to 128 if OOM persists
  • Sequential trials (n_jobs=1) already configured

Risk 2: Poor Convergence (High)

Symptom: All trials achieve similar poor loss (e.g., > 0.5 quantile loss) Probability: 40% Mitigation:

  • Extend trial budget to 50 trials if needed
  • Lower learning rate range (0.00005 - 0.0005)
  • Increase lookback window (60-180 bars)

Risk 3: Overfitting (Medium)

Symptom: Low training loss, high validation loss (gap > 20%) Probability: 35% Mitigation:

  • Increase dropout_rate range (0.1-0.4)
  • Reduce model capacity (hidden_dim 64-128)
  • Add L2 regularization (weight_decay 1e-4 to 1e-3)

Risk 4: Long Runtime (High)

Symptom: 30 trials takes > 12 hours Probability: 50% Mitigation:

  • Reduce epochs to 50 (from 100)
  • Increase MedianPruner aggressiveness (n_warmup_steps=20)
  • Run on weekend for extended runtime

8. Success Criteria

Minimum Viable Hyperparameters

A trial is considered successful if:

  1. Quantile loss < 0.3 (reasonable forecast accuracy)
  2. Sharpe ratio > 1.0 (positive risk-adjusted returns)
  3. Validation loss within 20% of training loss (no overfitting)
  4. Trade frequency 50-150 per 7,223 bars (0.7-2.1% trade rate)

Production-Ready Hyperparameters

A trial is considered production-ready if:

  1. Quantile loss < 0.2 (strong forecast accuracy)
  2. Sharpe ratio > 1.5 (excellent risk-adjusted returns)
  3. RMSE < 0.01 (1% prediction error)
  4. Quantile coverage > 85% (reliable uncertainty estimates)
  5. Cross-symbol Sharpe within 30% (generalizes well)

Study Success

The 30-trial tuning study is successful if:

  1. At least 3 trials meet minimum viable criteria (10% success rate)
  2. At least 1 trial meets production-ready criteria (3% success rate)
  3. Best trial improves over baseline by 20% (significant improvement)

9. Next Steps

Immediate Actions (This Agent)

  1. Assess TFT status - COMPLETE (compilation errors identified)
  2. Configure tuning search space - COMPLETE (recommendations provided)
  3. Document execution plan - COMPLETE (this report)
  4. Wait for compilation fixes - BLOCKED (Agents 1-3 or new agent required)

Next Agent (Fix TFT Compilation)

Tasks:

  1. Fix 3 compilation errors in ml/src/trainers/tft.rs
  2. Comment out gradient monitoring methods (lines 709-760)
  3. Remove gradient tracking from training loop
  4. Verify TFT trainer compiles successfully
  5. Run quick TFT forward pass test

Duration: 15 minutes Deliverable: Compiling TFT trainer

Following Agent (Run Tuning Study)

Tasks:

  1. Update tuning_config.yaml with recommended TFT search space
  2. Start 30-trial Optuna study via TLI
  3. Monitor progress (every 2-3 hours)
  4. Document best hyperparameters
  5. Run cross-validation on ES.FUT, NQ.FUT, ZN.FUT

Duration: 8-10 hours (overnight run) Deliverable: Best TFT hyperparameters with validation metrics


10. References

Agent Reports Reviewed

  1. AGENT_56_TFT_TRAINING_REPORT.md - Original TFT training failure (broadcasting shape error)
  2. AGENT_64_TFT_SHAPE_FIX.md - Broadcasting shape error fixed
  3. AGENT_72_DBN_PARSER_FIX_REPORT.md - DBN data loading fixed (7,223 bars)
  4. AGENT_71_MODEL_VALIDATION_REPORT.md - DQN/PPO validation results
  5. OPTUNA_TUNING_INTEGRATION_REPORT.md - Optuna production infrastructure

Configuration Files

  1. /home/jgrusewski/Work/foxhunt/services/ml_training_service/tuning_config.yaml - Current tuning config
  2. /home/jgrusewski/Work/foxhunt/ml/src/trainers/tft.rs - TFT trainer implementation

Documentation

  1. CLAUDE.md - System architecture and ML training roadmap
  2. ML_TRAINING_ROADMAP.md - 4-6 week ML training plan
  3. HYPERPARAMETER_TUNING.md - Optuna integration guide

Conclusion

Status: ⚠️ BLOCKED - TFT trainer has 3 compilation errors

Readiness:

  • Optuna infrastructure (production-tested)
  • TFT search space configuration (optimized for 4GB VRAM)
  • Execution plan (30 trials, 8-10 hours)
  • TFT trainer compilation (requires 15-minute fix)

Recommendation:

  1. Fix compilation errors first (15 minutes) - Comment out gradient monitoring
  2. Run 30-trial tuning study (8-10 hours) - Overnight run
  3. Cross-validate on 3 symbols (2-3 hours) - Test generalization

Expected Best Hyperparameters (after tuning):

learning_rate: 0.0003
batch_size: 64
hidden_dim: 256
num_heads: 8
num_layers: 3
lookback_window: 60
forecast_horizon: 10
dropout_rate: 0.15

Expected Performance:

  • Quantile loss: 0.15-0.25
  • RMSE: 0.008-0.012
  • Sharpe ratio: 1.3-1.8
  • Trade frequency: 80-120 trades per 7,223 bars

Confidence: 70% (assuming compilation fix is straightforward and tuning study completes successfully)


Agent 79 Status: CONFIGURATION COMPLETE - Ready for execution after TFT compilation fix