Commit Graph

2782 Commits

Author SHA1 Message Date
jgrusewski
fbc7c6d6bb test(ml): add walk-forward validation assertion to smoke test (7/7)
Assertion 7 runs DqnStrategy through ValidationHarness with real
6E.FUT data to verify the full train->validate pipeline works
end-to-end. Produces 15 folds with finite Sharpe ratio.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 20:22:15 +01:00
jgrusewski
4cff7a56a7 test(ml): add DQN training smoke test with 6 core assertions
Assertions verified on real 6E.FUT data:
1. All 20 epochs complete (no premature early stopping)
2. Loss decreases >5% (gradient flow works) — actual: 22.4%
3. All per-epoch losses are finite (no NaN/Inf)
4. Q-value divergence (model develops action preferences)
5. Checkpoint round-trip (save/load weight integrity)
6. Epsilon decayed below 0.5 (exploration schedule ran)

Also fixes divide-by-zero in triple_barrier.rs:103 when
entry_price_cents is zero (guard in both barrier tracker
and trainer caller).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 20:17:34 +01:00
jgrusewski
50a0d98a5a feat(ml): add loss_history and agent accessors to DQNTrainer
Add public accessor methods for smoke test verification:
- loss_history() — per-epoch training loss
- val_loss_history() — per-epoch validation loss
- get_agent_epsilon() — current epsilon from DQN agent

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 20:07:46 +01:00
jgrusewski
ee601149de docs: add DQN training smoke test implementation plan
4-task plan: add trainer accessors, write smoke test with 6 core
assertions (loss convergence, finite losses, Q-value divergence,
checkpoint integrity, epsilon decay), add Sharpe comparison vs
untrained baseline, final verification.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 20:03:10 +01:00
jgrusewski
525355fe73 docs: add DQN training smoke test design
Single integration test that verifies the complete train → checkpoint →
validate pipeline works on real 6E.FUT data. 7 assertions covering
loss convergence, Q-value divergence, checkpoint round-trip, epsilon
decay, and Sharpe improvement vs untrained baseline.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 19:56:20 +01:00
jgrusewski
987e5e6ac2 refactor(ml): remove 797 lines of commented-out code and disabled imports
Removed across 66 files:
- 49 instances of "// use crate::safe_operations; // DISABLED"
- 11 instances of "// use error_handling::{...}; // crate doesn't exist"
- 2 instances of "// use crate::Optimizer; // not available"
- 5 disabled test placeholder blocks (/* ... */) in ensemble/
- 1 disabled From impl in lib.rs (38 lines)
- 1 disabled test module in model.rs (113 lines)
- 1 disabled code block in integration/distillation.rs (41 lines)
- Various other disabled imports with explanation comments

All of this code references modules/crates that were removed during
prior refactoring waves and is preserved in git history. Removing it
reduces noise and makes the codebase easier to navigate.

1922 lib tests passing, compilation clean.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 19:43:47 +01:00
jgrusewski
bcf9ecd07c feat(ml): complete all algorithm gaps — TFT, Mamba2, PPO, CPCV, FDR
TFT training completeness:
- Fix mod.rs::train() stub backward → real AdamW optimizer + gradient flow
- Fix TFTTrainer optimizer init, backward pass, LR scheduling, checkpointing
- Fix temporal_attention weight tracking (RwLock, stores per-head means)

Mamba2 discretization:
- Replace raw continuous-time state transition with proper discretization
- SSD layer now uses softplus(delta) step size with 2nd-order Taylor
  approximation of matrix exponential: A_bar ≈ I + A*dt + (A*dt)²/2
- Correct dtype handling (F64 SSM matrices, F32 output)

PPO entropy fix:
- Fix LSTM training path: was using constant entropy (coeff * 0.5),
  now computes real entropy from log-probabilities

Circuit breaker consolidation:
- Move canonical implementation to ml/src/common/circuit_breaker.rs
- DQN and PPO circuit_breaker.rs now re-export from common

Validation stack additions:
- Add CPCV (Combinatorial Purged Cross-Validation) with purging/embargo
- Add FDR correction (Benjamini-Hochberg + Benjamini-Yekutieli)

1922 lib tests pass, 0 failures.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 19:19:16 +01:00
jgrusewski
342c93e2f6 refactor(ml): deduplicate PPO circuit breaker (re-export from DQN)
PPO's circuit_breaker.rs was a 276-line copy-paste of DQN's version
(only difference: "PPO" vs "DQN" in log messages). Since ppo/ppo.rs
already imports from dqn::circuit_breaker, the PPO copy was dead code.

Replace with a 6-line re-export module. Tests still pass via the
canonical DQN implementation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 18:28:56 +01:00
jgrusewski
bdf5b690b7 cleanup(ml): remove 31 disabled imports and commented-out module blocks
Removes dead code across 28 files:
- 31 commented-out "DISABLED" import lines (mostly safe_operations, error_handling)
- Commented-out module declarations in lib.rs (deployment, model_loader_integration, tests)
- Commented-out re-exports in lib.rs (training_pipeline, deployment::ModelVersion)
- Commented-out adaptive strategy modules in regime/mod.rs

All are in git history if ever needed. Net -74 lines removed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 18:23:41 +01:00
jgrusewski
533459570b refactor(ml): consolidate 13 duplicate OHLCVBar structs into canonical types module
Introduces ml/src/types/ohlcv.rs as the single source of truth for
OHLCVBar (DateTime<Utc>, f64). Replaces 13 identical struct definitions
scattered across features/, regime/, real_data_loader, and evaluation/.

The f32 backtesting variant in evaluation/metrics.rs is renamed to
OHLCVBarF32 to distinguish it from the canonical type. The regime_adx.rs
i64-timestamp variant was safely migrated since its timestamp field was
never accessed. The orchestrator's Bar alias is replaced with OHLCVBar.

39 files changed, -151 net lines removed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 18:14:50 +01:00
jgrusewski
1934367bfa refactor(ml): consolidate 13 duplicate OHLCVBar definitions into single canonical type
Created ml/src/types/ohlcv.rs as the single source of truth for OHLCVBar
(DateTime<Utc> timestamp, f64 OHLCV fields). Replaced all 13 duplicate
definitions across features/, regime/, real_data_loader, and evaluation/
with imports from crate::types::OHLCVBar.

Key changes:
- New: ml/src/types/mod.rs + ohlcv.rs with canonical OHLCVBar
  (derives: Debug, Clone, Copy, PartialEq, Serialize, Deserialize + Default)
- Renamed: evaluation::metrics::OHLCVBar → OHLCVBarF32 (genuinely
  different type: f32 fields, i64 timestamp for compact backtesting)
- Eliminated all import aliases (ExtractionOHLCVBar, RegimeOHLCVBar,
  PriceOHLCVBar, VolumeOHLCVBar) in dbn_sequence_loader.rs and pipeline.rs
- Renamed regime::orchestrator::Bar → OHLCVBar (same fields, just aliased)
- Updated 39 files total (13 definitions removed, imports normalized)

1883 lib tests passing, compilation clean.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 18:14:42 +01:00
jgrusewski
4ad9d45d7e test(ml): add real-data validation integration test for 6E.FUT
Runs full ValidationHarness on actual Databento 6E.FUT 1-minute OHLCV
bars (~30k bars). Extracts 15-dim features (5 OHLCV + 10 technical
indicators), configures DQN with walk-forward validation, and prints
a detailed report including DSR, PBO, permutation test, and per-regime
breakdown.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 17:33:05 +01:00
jgrusewski
49defb0745 fix(validation): correct DSR formula, PBO methodology, and DQN adapter bugs
Critical fixes found during pre-GPU-test code audit:
- DSR SE formula: (kurt-1)/4 → kurt/4 for excess kurtosis input
- PBO CSCV: replace circular fold ranking with IS/OOS mean comparison
- DqnStrategy evaluate: fix off-by-one (loop over returns, not bars)
- DqnStrategy action mapping: handle small num_actions (3→5) directly
  instead of FactoredAction decomposition (which maps all to Short100)
- Walk-forward: document rolling window as deliberate design choice

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 17:20:41 +01:00
jgrusewski
74c605683d test(validation): add end-to-end integration test for full validation pipeline
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 16:56:29 +01:00
jgrusewski
d4382a3c1e feat(validation): add DQN strategy adapter for ValidatableStrategy trait
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 16:50:13 +01:00
jgrusewski
2bd1208db5 feat(validation): add validation harness orchestrator with verdict system
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 16:44:03 +01:00
jgrusewski
5c37db5d00 feat(validation): add per-regime performance analysis
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 16:37:34 +01:00
jgrusewski
d73ab6689f feat(validation): add DSR, PBO, permutation test, and math helpers
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 16:32:02 +01:00
jgrusewski
253afd7efc feat(validation): implement walk-forward splitter with embargo periods
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 16:21:54 +01:00
jgrusewski
1d545aabfb feat(validation): add TimeSeriesData struct and ValidatableStrategy trait
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 16:16:53 +01:00
jgrusewski
d956add21c refactor(validation): convert to directory module for validation stack
Move validation.rs to validation/financial.rs, create validation/mod.rs
with re-exports for backward compatibility, and remove orphan
numerical_tests.rs that referenced nonexistent types.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 16:12:19 +01:00
jgrusewski
7ce7e33115 feat(dqn): replace fixed Polyak with cosine-annealed EMA target updates
Replace fixed τ=0.005 Polyak averaging with cosine-annealed EMA schedule
(BYOL/MoCo v3). τ(t) = τ_final - (τ_final - τ_base)·(cos(πt/T)+1)/2.

Early training: τ ≈ 0.005 (fast adaptation while model is learning)
Late training: τ ≈ 0.0005 (stability to prevent bootstrap error drift)

This is critical for offline RL — fixed τ causes target drift that
accumulates over training since we can't collect corrective data.
CQL handles Q-value overestimation; annealed EMA handles target stability.

New config fields: tau_final, tau_anneal_steps. Set tau_anneal_steps=0
to revert to fixed τ behavior.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 15:49:07 +01:00
jgrusewski
b9fbd86fc7 feat(dqn): IQN+CQL integration test and verified module re-exports
Add 3 integration tests verifying the complete IQN+CQL training pipeline:
full training loop with both features, IQN-only mode, and CVaR risk-aware
action selection. Module re-exports for QuantileConfig/QuantileNetwork
were already present from Wave 26.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 15:40:03 +01:00
jgrusewski
85f100ca44 feat(dqn): wire IQN target network into Polyak/hard update logic
Add copy_weights_from() to QuantileNetwork for target sync. Wire IQN
target updates into both Polyak averaging (soft) and hard copy (legacy)
update paths in train_step(). Also fix agent.rs tests that still
referenced state_dim=52 after consolidation to 51.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 15:36:23 +01:00
jgrusewski
bafe784dbf feat(dqn): add IQN action selection with optional CVaR risk-aware mode
Wire IQN into select_action() greedy branch. When use_iqn is enabled,
computes quantile-based Q-values via uniform quantile sampling. CVaR mode
(use_cvar_action_selection) optimizes for worst-case outcomes instead of
mean, providing risk-averse action selection for conservative trading.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 15:30:15 +01:00
jgrusewski
19f0de509c feat(dqn): add IQN quantile Huber loss path in train_step (replaces C51)
Add 3-way loss branch in train_step(): IQN > C51 > scalar. The IQN path
implements Dabney et al. 2018b using quantile Huber loss — no scatter_add
needed (bypasses Candle BUG #36). Also adds get_state_embedding() helper
to extract base Q-network hidden layer outputs for IQN, stores VarMap in
QuantileNetwork for optimizer access, and includes IQN vars in optimizer.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 15:26:37 +01:00
jgrusewski
33a3ee99ea feat(dqn): add IQN network fields to DQN struct with initialization
Add iqn_network and iqn_target_network as Option<QuantileNetwork> fields
to the DQN struct. When use_iqn is enabled, DQN::new() creates both networks
with QuantileConfig derived from DQNConfig (embed_dim from last hidden layer).
Each network gets its own VarMap for independent parameter tracking.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 15:17:29 +01:00
jgrusewski
02393a4cd1 feat(dqn): add CQL offline RL regularization to train_step (Kumar et al. 2020)
CQL penalty = logsumexp(Q(s, all_a)) - Q(s, a_data)
- Numerically stable logsumexp with max subtraction
- Controlled by use_cql and cql_alpha config fields
- Periodic logging every 100 steps
- test_cql_regularization passes

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 15:09:29 +01:00
jgrusewski
061b68d739 feat(dqn): expand QuantileNetwork to multi-action IQN with random tau sampling
- Add num_actions to QuantileConfig (default: 45)
- Output shape: [batch, num_quantiles] → [batch, num_actions, num_quantiles]
- Add sample_random_quantiles() for IQN training mode
- Rename to_scalar() → to_expected_q() for multi-action
- Update compute_cvar() for multi-action [batch, num_actions] output
- All 10 tests pass including new test_random_quantile_sampling

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 15:05:43 +01:00
jgrusewski
18dabb6671 feat(dqn): add CQL, IQN, and CVaR config fields to DQNConfig
- CQL: use_cql (default: true), cql_alpha (default: 1.0)
- IQN: use_iqn (default: true), iqn_num_quantiles, iqn_kappa, iqn_embedding_dim
- CVaR: use_cvar_action_selection (default: false), cvar_alpha (default: 0.05)
- Updated all constructors: default, aggressive, conservative, emergency
- Updated trainer.rs, config.rs, benchmark configs

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 14:59:33 +01:00
jgrusewski
427a436862 fix(dqn): consolidate state_dim to 51 (45 market + 6 portfolio)
- dqn::DQNConfig default: 54 → 51
- agent::DQNConfig default: 52 → 51
- Updated test_training_step_with_data to match

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 14:54:46 +01:00
jgrusewski
3e5db0b377 fix(dqn): NaN sort panic, Adam eps per Rainbow paper, dedup DistributionalType
- Fix NaN panic at dqn.rs:1716 with unwrap_or(Ordering::Equal)
- Fix Adam epsilon 1e-8 → 1.5e-4 per Hessel et al. 2018
- Remove duplicate DistributionalType enum from quantile_regression.rs

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 14:51:48 +01:00
jgrusewski
6007f98c26 docs: Add DQN algorithm fix implementation plan (11 tasks)
Detailed TDD implementation plan for:
- Task 1: Bug fixes (NaN panic, Adam eps, dedup enum)
- Task 2: State dimension consolidation (→51)
- Task 3-5: CQL offline RL regularization
- Task 4,6-9: IQN distributional RL (replaces broken C51)
- Task 8: CVaR risk-aware action selection
- Task 10-11: Integration test and verification

Each task has exact file paths, code, test commands, and safety gates.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 14:44:44 +01:00
jgrusewski
91af2b9334 docs: Add DQN algorithm fix & 2026 modernization design
Research-validated design for fixing critical DQN issues:
- CQL regularization for offline RL training on historical data
- IQN integration replacing broken C51 (Candle scatter_add bug)
- CVaR risk-aware action selection
- State dimension consolidation (51 is canonical)
- NaN panic fix, Adam epsilon fix per Rainbow paper

Verified against 20+ papers and 2026 SOTA.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 14:39:58 +01:00
jgrusewski
7a02382965 refactor(ml): split DQN trainer.rs into sub-modules
Extract ~1,300 lines from the 4,755-line trainer.rs into four focused
sub-modules to improve maintainability and code navigation:

- monitoring.rs (290 lines): TrainingMonitor for per-epoch reward,
  action, and Q-value tracking with health validation
- data_loading.rs (719 lines): Parquet/DBN data loading, MBP-10 OFI
  integration, feature caching, and preprocessing pipeline
- risk.rs (145 lines): Volatility-adjusted epsilon, risk-adjusted
  rewards, Kelly criterion sizing, and risk tracker updates
- features.rs (211 lines): Full feature extraction (51-dim), feature
  statistics calculation, z-score normalization, synthetic features

All public API paths preserved via mod.rs re-exports. Fields accessed
across module boundaries changed to pub(crate) visibility.

Verified: cargo check --workspace (0 errors), cargo test -p ml --lib
(1,817 passed, 0 failed).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 14:16:38 +01:00
jgrusewski
ac0a83e4f7 refactor(ml): reorganize tests — move integration tests to ml/tests/
Move test files from ml/src/*/tests/ to ml/tests/. Convert
crate-internal imports to public API imports. Rename files to
follow naming conventions (no wave/priority prefixes).

Files moved:
- ml/src/dqn/tests/ -> ml/tests/ (4 files)
- ml/src/trainers/dqn/tests/ -> ml/tests/ (5 files)

Renames:
- target_update_comprehensive_tests.rs -> target_update_tests.rs
- p0_integration_tests.rs -> dqn_trainer_integration_tests.rs
- p1_integration_tests.rs -> dqn_trainer_p1_tests.rs
- ensemble_uncertainty_hyperopt_tests.rs -> ensemble_hyperopt_tests.rs

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 14:07:22 +01:00
jgrusewski
d56a7f41e2 chore: remove deprecated FeatureVector54 type alias
Dimension was reduced from 54 to 51 in WAVE 10. All usages now
use FeatureVector ([f64; 51]) directly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 13:46:56 +01:00
jgrusewski
313d983ae2 chore(ml): remove orphan source files and zero-importer modules
- Delete lib_test.rs (no mod declaration — true orphan)
- Delete dqn/tests/factored_integration_tests.rs (not declared in tests/mod.rs)
- Delete hyperopt/adapters/tests/ (not declared in adapters/mod.rs)
- Delete integration_test.rs, test_common.rs, test_fixtures.rs (declared
  in lib.rs but had zero imports — dead code that compiles but nobody uses)
- Inline test symbol data that was pulled from test_fixtures
- Remove include!("tests/dqn_wave26_params_test.rs") from hyperopt dqn adapter

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 13:38:53 +01:00
jgrusewski
13ff856e92 chore(ml): remove 11 dead code files (~4,267 lines)
Files had zero imports across the entire workspace:
- error_consolidated.rs, operations.rs, operations_safe.rs, ops_production.rs
- dqn/multi_step_new.rs, reward_elite.rs, reward_coordinator.rs
- dqn/intrinsic_rewards.rs, reward_simple_pnl.rs, reward_sharpe_tests.rs
- dqn/reward/tests/ (orphaned duplicate)
Also removed 5 dead pub mod declarations from lib.rs and dqn/mod.rs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 13:30:39 +01:00
jgrusewski
71444f9471 chore: purge all documentation except implementation plans
Remove 1,307 AI-generated markdown files (984 in docs/, 323 in archive/).
No code references these files. Plans directory preserved.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 13:25:14 +01:00
jgrusewski
2b914728c6 docs: Add codebase deep clean implementation plan
16-task bottom-up execution plan with exact file paths, commands,
and safety gates. Covers: doc purge, dead code removal, module
consolidation, test reorganization, file splitting, and folder
restructuring.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 13:21:25 +01:00
jgrusewski
cec36d3072 docs: Add codebase deep clean design document
Comprehensive cleanup plan covering: documentation purge, dead code
removal (~4,846 lines across 11 files), module consolidation,
test reorganization, large file splitting, and folder restructuring.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 13:08:10 +01:00
jgrusewski
7f53baff8f feat(ml): DQN improvements and fix downstream compilation errors
DQN changes: improved attention, ensemble networks, hindsight replay,
mixed precision, noisy layers, prioritized replay, RMSNorm, hyperopt
adapter updates, and trainer enhancements with weight_decay support.

Fix downstream crates broken by DQNConfig changes:
- trading_service: import agent::DQNConfig directly, add weight_decay field
- backtesting_service: update feature vector size 54 -> 51
- ml_training_service: convert compile-time sqlx macro to runtime query_as
- pre-commit hook: add SQLX_OFFLINE=true for DB-free compilation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 13:07:48 +01:00
jgrusewski
49ad0050aa chore: Major documentation cleanup - remove 2,060 obsolete files
BREAKING: Removes 746,569 lines of outdated documentation from root folder

## Summary
- Deleted 2,060 report/documentation files from root folder
- Kept only essential files: README.md, CLAUDE.md
- Updated .gitignore and config/tarpaulin.toml
- Reorganized config files into config/ directory

## Removed Content Categories
- Agent reports (AGENT_*.md, AGENT*.txt)
- Wave reports (WAVE_*.md, DQN_*.md)
- Implementation summaries
- Quick references and summaries
- Test reports and validation docs
- Deployment scripts (obsolete .sh files)
- Legacy config files and logs

## Preserved
- README.md - Main project documentation
- CLAUDE.md - Claude Code configuration
- docs/archive/ - Historical files for reference
- docs/ folder - Current documentation
- All source code unchanged

🐝 Hive Mind Collective Intelligence Cleanup

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 10:29:45 +01:00
jgrusewski
b0a5146885 feat(ml): Add weight_decay L2 regularization to DQN (P0.1 fix)
Implements critical P0.1 gap from DQN 2025 upgrade roadmap to address
overfitting in hyperopt/trainer by enabling proper weight decay in AdamW.

Changes:
- Add weight_decay field to DQNHyperparameters (default: 1e-4)
- Wire weight_decay to AdamW via Decay::DecoupledWeightDecay at 3 optimizer
  initialization points in agent.rs
- Expand hyperopt search space to 40D with weight_decay [1e-5, 1e-3] log scale
- Update from_continuous(), to_continuous(), param_names() for roundtrip

This enables the modern best practice of decoupled weight decay (AdamW paper)
which prevents co-adaptation of network weights and improves generalization.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 09:29:38 +01:00
jgrusewski
4080f73ba4 feat(ml): WAVE 30 - Implement optimized DQN logging module
Add comprehensive logging utilities for DQN training with best practices:

- LoggingConfig: Configurable log levels, intervals, and sampling rates
- MetricsAggregator: Windowed statistics (mean, std_dev) for training metrics
- SampledLogger: Rate-limited logging for high-frequency events

Key features:
- Structured logging with tracing crate (info/debug/trace hierarchy)
- 23 unit tests for full coverage
- Integration with existing DQN training pipeline

Bug fixes:
- Fix u8 overflow in prioritized_replay.rs test (500 > u8::MAX)
- Fix GradStore assertion in residual.rs (no is_empty method)
- Fix Tensor::get() Option/Result handling in quantile_regression.rs
- Fix Device PartialEq comparison in ensemble_network.rs

Documentation:
- Add Rust logging best practices guide for ML training
- Add DQN logging analysis and design summary

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 00:15:23 +01:00
jgrusewski
2df1ea92e1 feat(ml): WAVE 29 DQN Codebase Cleanup & Refactoring Campaign
BREAKING CHANGES:
- Removed orphaned dqn.rs monolithic trainer (4,975 lines)
- Removed orphaned dqn_ensemble.rs module (816 lines)
- Removed orphaned tft.rs and tft_complete_int8_integration_test.rs
- TFT trainer split into modular directory structure

DQN Module Refactoring:
- Split trainers/dqn.rs into modular structure (config.rs, statistics.rs, trainer.rs)
- Fixed hyperopt 39D search space (continuous params only)
- Boolean flags (use_dueling, use_double_dqn, use_per, use_noisy_nets) are now FIXED architectural decisions
- use_distributional defaults to false (Candle BUG #36 - scatter_add gradient issues)

Clean Module Structure:
- ml/src/trainers/dqn/ directory with proper mod.rs exports
- ml/src/trainers/tft/ directory with config.rs, types.rs, model.rs, trainer.rs, tests.rs
- All P0 features validated: TD-error clamping, batch diversity, LR scheduler, priority staleness

Documentation:
- Added comprehensive docs in docs/codebase-cleanup/
- ADR-001 for DQN refactoring decisions
- Rainbow DQN component matrix and quick reference guides

Build Status: Compiles with zero errors

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-27 23:46:13 +01:00
jgrusewski
2c1acda2f3 feat: DQN Rainbow enhancements with hyperopt results and test coverage
- Update DQN trainer with gradient collapse detection warmup
- Add portfolio tracker improvements
- Include hyperopt trial results (multiple Sharpe ratio experiments)
- Add new test files for action/position sign convention, early stopping,
  cash reserve bugs, and portfolio execution
- Update trained model files
- Add Claude Code configuration and skills

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-27 14:45:25 +01:00
jgrusewski
ae64902564 fix(dqn): Add warmup period for gradient collapse detection (BUG #43)
WAVE 24: Gradient collapse threshold too strict for cold start

Problem: 100-epoch runs terminated at step 5 due to false gradient collapse
detection despite using verified hyperparameters (LR=6.6e-5, Sharpe 2.0203)

Root Cause: Threshold (0.006615 = 0.1 × LR × 1000) expects large gradients
from step 1, but cold-start training has naturally small norms (0.0001-0.001)
during replay buffer filling

Solution: Skip gradient collapse check for first 20% of buffer capacity
- Warmup steps = replay_buffer_capacity × 0.2
- Example: 54,255 buffer → 10,851 warmup steps
- Check activates only after sufficient experience collected

Fix Location: ml/src/dqn/dqn.rs line 1691
- Changed: self.config.buffer_size (doesn't exist on WorkingDQNConfig)
- To: self.config.replay_buffer_capacity (correct field, cast to u64)

Validation:
- Quick test (15 epochs):  PASSED - zero gradient collapse warnings
- Full test (100 epochs):  PASSED - no false positive at step 5
- Warmup period correctly deferred check until step 10,851

Impact: Unblocks 100-epoch+ production training runs

Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-24 14:57:49 +01:00
jgrusewski
85de92d85b docs: Update CLAUDE.md with WAVE 23 summary
Added WAVE 23 section to Recent Updates with all three priorities:
- Early Stopping Termination Bug (FIXED)
- 80/20 Train/Test Split (VERIFIED WORKING)
- MBP-10 Feature Caching (COMPLETE)

Updated system status header with Feature Caching and Early Stopping status.

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-24 10:07:40 +01:00