Files
foxhunt/MAMBA2_HYPERPARAMETER_TUNING_REPORT.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

18 KiB

MAMBA-2 Hyperparameter Tuning Configuration & Analysis

Mission: Configure and execute Optuna hyperparameter tuning for MAMBA-2 state-space model Status: READY TO EXECUTE Created: 2025-10-14 GPU: RTX 3050 Ti (4GB VRAM) Expected Duration: 6-10 hours (40 trials)


🎯 Executive Summary

MAMBA-2 is a state-space sequence model that uses structured state-space dynamics for efficient long-range dependency modeling in HFT market data. This tuning configuration optimizes 14 hyperparameters across 40 trials to maximize Sharpe ratio while respecting 4GB VRAM constraints.

Key Objectives:

  1. Find optimal state-space architecture (state size, hidden dim, layers)
  2. Tune state dynamics (dt_min, dt_max, expansion factor)
  3. Optimize training parameters (learning rate, batch size, regularization)
  4. Analyze memory vs accuracy tradeoff
  5. Enable advanced features (SSD, selective state, hardware-aware optimizations)

📊 Search Space Configuration

Critical Parameters (State-Space Dynamics)

1. State Size (d_state)

state_size: [8, 16, 32]  # 3 choices

Role: Controls the dimensionality of the latent state vector in the state-space model

Trade-offs:

  • Small (8):
    • Memory: ~150MB per batch
    • Capacity: Limited expressiveness, may miss complex patterns
    • Speed: Fast inference (<50μs)
  • Medium (16):
    • Memory: ~250MB per batch
    • Capacity: Balanced representation
    • Speed: 50-100μs inference
  • Large (32):
    • Memory: ~400MB per batch
    • Capacity: Rich state representation for complex dynamics
    • Speed: 100-200μs inference

Expected Outcome: State size 16-32 likely optimal for HFT microstructure patterns


2. Hidden Dimension (d_model)

hidden_dim: [128, 256, 512]  # 3 choices

Role: Model dimension for embeddings and hidden layers

Memory Impact:

  • 128: ~500MB total VRAM (safe, conservative)
  • 256: ~1.2GB total VRAM (balanced)
  • 512: ~3.0GB total VRAM (aggressive, requires batch_size=16)

Expected Outcome: 256 likely optimal (balanced capacity vs memory)


3. Number of Layers

num_layers: [2, 4, 8]  # 3 choices

Role: Depth of state-space model stack

Trade-offs:

  • 2 layers: Fast, simple, may underfit
  • 4 layers: Balanced depth, standard for sequence modeling
  • 8 layers: Deep representation, risk of overfitting, memory-intensive

Expected Outcome: 4-6 layers optimal for temporal market dynamics


4. Expansion Factor

expansion_factor: [2, 4]  # 2 choices

Role: Controls inner dimension size (d_inner = expand * d_model)

Impact:

  • expand=2: d_inner = 2 * d_model (standard, memory-efficient)
  • expand=4: d_inner = 4 * d_model (more capacity, 2x memory usage)

Expected Outcome: expand=2 likely sufficient for HFT patterns


Training Parameters

5. Learning Rate

learning_rate: [0.00001, 0.0001, 0.001]  # 3 choices

Conservative range for state-space models (sensitive to instability)

Expected Outcome: 0.0001 likely optimal (state-space models require careful tuning)


6. Batch Size

batch_size: [16, 32, 64]  # 3 choices

Memory-constrained due to state propagation across sequences

VRAM Usage (estimated for d_model=256, state_size=16, num_layers=4):

  • batch_size=16: ~1.8GB (safe)
  • batch_size=32: ~2.8GB (balanced)
  • batch_size=64: ~4.2GB (risky, may OOM with large state_size)

Expected Outcome: batch_size=32 likely optimal (balanced throughput vs memory)


State-Space Dynamics

7. Time-Step Range (dt_min, dt_max)

dt_min: [0.0001, 0.01]  # Log scale
dt_max: [0.01, 1.0]     # Log scale

Role: Controls discrete-time state transition dynamics

Impact on Market Modeling:

  • Small dt (0.0001-0.001): Captures high-frequency tick-level dynamics
  • Large dt (0.01-1.0): Captures longer-term trends

Expected Outcome: dt_min ~0.001, dt_max ~0.1 (millisecond to 100ms range)


Architecture Features

8. Structured State Duality (use_ssd)

use_ssd: [true, false]

Purpose: Advanced state-space technique for efficient computation

Expected Outcome: use_ssd=true likely improves both speed and accuracy


9. Selective State Mechanism

use_selective_state: [true, false]

Purpose: Input-dependent state transitions (context-aware dynamics)

Expected Outcome: use_selective_state=true likely critical for market regime changes


10. Hardware-Aware Optimizations

hardware_aware: [true, false]

Purpose: GPU-specific optimizations for RTX 3050 Ti

Expected Outcome: hardware_aware=true should be enabled (10-30% speedup)


Regularization

11. Dropout

dropout: [0.0, 0.3]  # Step 0.05

Expected Outcome: 0.1-0.15 likely optimal (prevent overfitting without underfitting)


12. Gradient Clipping

grad_clip: [0.5, 2.0]  # Step 0.25

Critical for state-space stability (exploding gradients common in SSMs)

Expected Outcome: 1.0-1.5 likely optimal


13. Weight Decay

weight_decay: [0.0001, 0.01]  # Log scale

Expected Outcome: 0.0001-0.001 likely optimal (L2 regularization)


14. Warmup Steps

warmup_steps: [100, 2000]  # Step 100

Critical for state-space model training stability

Expected Outcome: 500-1000 steps likely optimal (gradual learning rate ramp)


🧮 Search Space Statistics

Theoretical Search Space Size

Categorical parameters:

  • learning_rate: 3 choices
  • batch_size: 3 choices
  • hidden_dim: 3 choices
  • state_size: 3 choices
  • num_layers: 3 choices
  • expansion_factor: 2 choices
  • use_ssd: 2 choices
  • use_selective_state: 2 choices
  • hardware_aware: 2 choices

Continuous parameters:

  • dropout: 7 values (0.0 to 0.3, step 0.05)
  • grad_clip: 7 values (0.5 to 2.0, step 0.25)
  • dt_min: Continuous (log scale)
  • dt_max: Continuous (log scale)
  • weight_decay: Continuous (log scale)
  • warmup_steps: 20 values (100 to 2000, step 100)

Categorical-only combinations: 3^5 * 2^4 = 243 * 16 = 3,888 configurations

Including continuous: ~50,000+ unique configurations

With 40 trials: Optuna TPE sampler explores 0.08% of discrete space (smart sampling critical)


🎯 Tuning Strategy

Objective Function

objective = maximize(sharpe_ratio)

Sharpe Ratio = (Mean Return - Risk-Free Rate) / StdDev(Returns)

Why Sharpe?

  • Risk-adjusted performance metric
  • Balances returns vs volatility
  • Industry-standard for HFT evaluation

Pruning Strategy

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

MedianPruner Logic:

  • If trial's loss at epoch N > median loss at epoch N across all trials: PRUNE
  • Saves 30-50% compute time by stopping unpromising trials early

Expected Pruning:

  • Trials with poor learning_rate: Pruned at epoch 10-20
  • Trials with OOM risk (batch_size too large): Pruned immediately
  • Trials with unstable state dynamics (poor dt range): Pruned at epoch 15-30

Sampler

sampler: TPE  # Tree-structured Parzen Estimator

TPE Algorithm:

  1. Build probabilistic models of "good" vs "bad" hyperparameter regions
  2. Sample from regions likely to improve objective
  3. Exploit promising areas while exploring new regions

vs Random Search: 2-5x more efficient at finding optimal configurations


🔬 State-Space Dynamics Analysis

Research Questions

  1. State Size vs Performance:

    • Does larger state size (32) improve Sharpe ratio significantly?
    • At what state size do diminishing returns occur?
    • Memory cost per unit of Sharpe improvement?
  2. Expansion Factor Impact:

    • Does expand=4 justify 2x memory cost?
    • Interaction with state_size?
  3. Time-Step Dynamics:

    • Optimal dt_min for tick-level capture?
    • Optimal dt_max for trend capture?
    • Correlation between (dt_min, dt_max) and Sharpe?
  4. Feature Importance:

    • Which hyperparameters most impact Sharpe ratio?
    • Are architecture features (SSD, selective state) worth complexity?

Expected Findings

Hypothesis 1: State Size Sweet Spot

  • State size 16-32 likely optimal
  • State size 8 likely insufficient for HFT complexity
  • Sharpe improvement 8→16 > Sharpe improvement 16→32 (diminishing returns)

Hypothesis 2: Memory-Accuracy Tradeoff

  • hidden_dim=256, state_size=16, batch_size=32 likely best balance
  • hidden_dim=512 may not justify 2x memory cost

Hypothesis 3: Advanced Features Critical

  • use_selective_state=true likely provides 5-15% Sharpe boost
  • use_ssd=true likely improves both speed (10-30%) and accuracy (2-5%)

Hypothesis 4: Time-Step Optimization

  • dt_min ~0.001 (1ms tick-level capture)
  • dt_max ~0.05-0.1 (50-100ms trend capture)
  • Narrow dt range may underperform (insufficient multi-scale modeling)

📈 Expected Performance Outcomes

DQN/PPO Baseline (from prior tuning)

DQN:
  Sharpe Ratio: 1.50
  Win Rate: 52%
  Max Drawdown: -15%

PPO:
  Sharpe Ratio: 1.30
  Win Rate: 50%
  Max Drawdown: -18%

MAMBA-2 Expected Performance (40 trials)

Conservative Estimate:

Sharpe Ratio: 1.60-1.80  (10-20% improvement over DQN)
Win Rate: 53-56%
Max Drawdown: -12-14%
Inference Latency: <100μs

Optimistic Estimate (if state-space dynamics excel at HFT patterns):

Sharpe Ratio: 1.90-2.20  (30-50% improvement over DQN)
Win Rate: 57-62%
Max Drawdown: -10-12%
Inference Latency: <80μs

Why MAMBA-2 May Outperform DQN/PPO:

  1. Efficient long-range dependencies: State-space models excel at temporal patterns
  2. Sub-quadratic complexity: O(N) vs Transformer O(N²) for sequence length N
  3. Continuous-time dynamics: Better aligned with market microstructure
  4. Hardware efficiency: Optimized for GPU inference (RTX 3050 Ti)

⏱️ Time & Resource Estimates

Per-Trial Performance

Estimated per-trial time (50 epochs, RTX 3050 Ti):

  • hidden_dim=128, state_size=8, batch_size=64: ~6 minutes/trial
  • hidden_dim=256, state_size=16, batch_size=32: ~10 minutes/trial
  • hidden_dim=512, state_size=32, batch_size=16: ~18 minutes/trial

Average trial duration: ~10-12 minutes

MedianPruner savings: 30-50% time reduction

  • Effective trials: 40 trials * 0.6 = 24 full trials + 16 pruned trials
  • Pruned trial avg duration: ~4 minutes (stopped early)

Total time estimate:

  • Without pruning: 40 trials * 10 min = 400 minutes = 6.7 hours
  • With pruning: (24 * 10 min) + (16 * 4 min) = 240 + 64 = 304 minutes = 5.1 hours
  • Best case: 4.5 hours (aggressive pruning)
  • Worst case: 8 hours (many high-memory trials)

Expected range: 6-10 hours


VRAM Usage Monitoring

Critical Configurations (risk of OOM):

  • hidden_dim=512, state_size=32, batch_size=64: 4.8GB (OOM risk)
  • hidden_dim=512, state_size=32, batch_size=32: 3.8GB (safe)

Safe Configurations:

  • hidden_dim=256, state_size=16, batch_size=32: 2.2GB (optimal)
  • hidden_dim=128, state_size=8, batch_size=64: 1.4GB (conservative)

VRAM Monitoring Strategy:

  • Optuna trial executor checks GPU memory before each trial
  • Auto-skip configurations exceeding 3.5GB estimated usage
  • Fallback to smaller batch_size if OOM detected

🚀 Execution Commands

1. Start MAMBA-2 Tuning (TLI)

# Login to API Gateway
tli login

# Start 40-trial tuning job with live monitoring
tli tune start \
  --model MAMBA_2 \
  --trials 40 \
  --watch

# Expected output:
# Job ID: 8a7b9c3d-4e5f-6a1b-2c3d-4e5f6a7b8c9d
# Model: MAMBA_2
# Trials: 40
# Estimated time: 6-10 hours
# [Trial 1/40] learning_rate=0.0001, batch_size=32, state_size=16, sharpe=1.42
# [Trial 2/40] learning_rate=0.001, batch_size=16, state_size=32, sharpe=1.38 (PRUNED at epoch 25)
# ...

2. Monitor Progress

# Check tuning job status
tli tune status --job-id 8a7b9c3d-4e5f-6a1b-2c3d-4e5f6a7b8c9d

# Expected output:
# Job ID: 8a7b9c3d-4e5f-6a1b-2c3d-4e5f6a7b8c9d
# Status: Running
# Progress: 15/40 trials (37.5%)
# Best Sharpe: 1.68
# Elapsed: 2h 15m
# Estimated remaining: 4h 30m

3. Get Best Hyperparameters

# Retrieve best configuration (after completion)
tli tune best --job-id 8a7b9c3d-4e5f-6a1b-2c3d-4e5f6a7b8c9d

# Expected output (example):
# Best Trial: 23
# Sharpe Ratio: 1.78
# Hyperparameters:
#   learning_rate: 0.0001
#   batch_size: 32
#   hidden_dim: 256
#   state_size: 16
#   num_layers: 4
#   expansion_factor: 2
#   dropout: 0.15
#   dt_min: 0.001
#   dt_max: 0.08
#   use_ssd: true
#   use_selective_state: true
#   hardware_aware: true
#   grad_clip: 1.25
#   weight_decay: 0.0005
#   warmup_steps: 800

4. Stop Tuning (if needed)

# Cancel running tuning job
tli tune stop --job-id 8a7b9c3d-4e5f-6a1b-2c3d-4e5f6a7b8c9d

# Partial results still available via MinIO persistence

📊 Analysis & Comparison

Post-Tuning Analysis Tasks

  1. State-Space Dynamics Analysis:

    • Plot state_size vs Sharpe ratio
    • Analyze memory vs accuracy tradeoff
    • Visualize dt_min/dt_max impact on performance
  2. Feature Importance:

    • Correlation matrix of hyperparameters vs Sharpe
    • Identify most impactful parameters
    • Test interaction effects (e.g., state_size * expansion_factor)
  3. Performance Comparison:

    Model      | Sharpe | Win Rate | Max DD | Inference | VRAM
    -----------|--------|----------|--------|-----------|------
    DQN        | 1.50   | 52%      | -15%   | 120μs     | 1.2GB
    PPO        | 1.30   | 50%      | -18%   | 180μs     | 1.8GB
    MAMBA-2    | 1.78   | 56%      | -12%   | 85μs      | 2.2GB
    
  4. Production Deployment Decision:

    • If MAMBA-2 Sharpe > 1.70: Deploy to production ensemble
    • If MAMBA-2 Sharpe 1.50-1.70: Use as diversification model
    • If MAMBA-2 Sharpe < 1.50: Investigate failure modes

📁 Output Artifacts

Tuning Results

  • MinIO Path: s3://foxhunt-ml-models/mamba2/tuning_jobs/{job_id}/
  • Optuna Study: optuna_study_mamba2.db (JournalStorage for crash recovery)
  • Best Checkpoint: s3://foxhunt-ml-models/mamba2/checkpoints/trial_{best_trial}/

Analysis Reports

  • Trial History: JSON with all 40 trial results
  • Visualization: Sharpe vs hyperparameters plots
  • Feature Importance: Correlation heatmap
  • Memory Profile: VRAM usage vs configuration

🔍 Success Criteria

Must-Have (Critical)

  • Complete 40 trials without crashes
  • Sharpe ratio > 1.50 (at least match DQN baseline)
  • Inference latency < 200μs (HFT-ready)
  • VRAM usage < 3.5GB (fit on RTX 3050 Ti)
  • No training instability (exploding gradients, NaN losses)

Should-Have (Important)

  • Sharpe ratio > 1.60 (10%+ improvement over DQN)
  • MedianPruner saves 30%+ time (efficient search)
  • State-space features provide lift (use_ssd, use_selective_state impact)
  • Clear hyperparameter trends (interpretable results)

Nice-to-Have (Aspirational)

  • Sharpe ratio > 1.80 (20%+ improvement)
  • Inference latency < 100μs (ultra-low latency)
  • Win rate > 55% (robust edge)
  • Max drawdown < -12% (controlled risk)

🚧 Risk Mitigation

Risk 1: OOM Errors (High Probability)

Mitigation:

  • Conservative batch_size choices [16, 32, 64]
  • Pre-trial VRAM estimation
  • Auto-skip configurations exceeding 3.5GB
  • Fallback to CPU if GPU OOM (graceful degradation)

Risk 2: Training Instability (Medium Probability)

Mitigation:

  • Gradient clipping [0.5, 2.0]
  • Conservative learning rates [0.00001, 0.0001, 0.001]
  • Warmup steps [100, 2000]
  • Early stopping on NaN loss

Risk 3: Poor Hyperparameter Exploration (Low Probability)

Mitigation:

  • TPE sampler (smarter than random search)
  • 5 startup trials for baseline
  • Broad search space (50,000+ configurations)
  • 40 trials sufficient for major trends

Risk 4: Long Tuning Duration (Medium Probability)

Mitigation:

  • MedianPruner (30-50% time savings)
  • Batch jobs overnight (6-10 hours)
  • Checkpointing every trial (resume on crash)
  • tli tune stop for early termination

📞 Next Steps

1. Execute Tuning (6-10 hours)

tli tune start --model MAMBA_2 --trials 40 --watch

2. Extract Best Configuration

tli tune best --job-id <uuid> > mamba2_best_hyperparams.yaml

3. Train Final Model (2-3 days)

tli train \
  --model MAMBA_2 \
  --config mamba2_best_hyperparams.yaml \
  --epochs 500 \
  --symbols ES.FUT,NQ.FUT,ZN.FUT,6E.FUT \
  --checkpoint-interval 50

4. Backtest & Validate (1 day)

tli backtest \
  --model MAMBA_2 \
  --checkpoint checkpoints/mamba2_final.safetensors \
  --start-date 2024-10-01 \
  --end-date 2024-11-01 \
  --symbols ES.FUT,NQ.FUT

5. Production Deployment Decision

  • Compare MAMBA-2 vs DQN/PPO on holdout data
  • Ensemble weighting optimization
  • Deploy to paper trading (1 week validation)
  • Gradual capital allocation (1% → 10% → 50% → 100%)

📚 References

MAMBA-2 Architecture

Optuna Hyperparameter Tuning

HFT ML Best Practices

  • /home/jgrusewski/Work/foxhunt/OPTUNA_QUICKSTART.md (DQN/PPO tuning results)
  • /home/jgrusewski/Work/foxhunt/ml/src/trainers/mamba2.rs (MAMBA-2 trainer implementation)
  • /home/jgrusewski/Work/foxhunt/services/ml_training_service/tuning_config.yaml (Full search space)

Document Version: 1.0 Last Updated: 2025-10-14 Status: READY TO EXECUTE Estimated Completion: 2025-10-15 (assuming overnight run)