Components were fighting a broken signal, not broken themselves.
With correct signal: adapt DSR to trade-level, rank only non-zero
rewards, keep commitment as soft signal, let E1 enrichment handle
Q-drift instead of hardcoded kernel penalty.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Root cause: training environment differs from validation backtest in 3 ways:
1. 100-bar episodes force exits (val runs continuous) → position-gated done
2. Rank normalization destroys sparse trade-level reward → remove it
3. Different Sharpe computation (per-trade vs per-bar, different annualization)
Single execution path:
Phase 1: Episode alignment (100→5000 bars, position-gated done, soft reset)
Phase 2: Reward alignment (remove rank norm, raw trade P&L to replay)
Phase 3: Metrics alignment (un-annualized per-trade Sharpe, both paths)
Expected: training Sharpe within 2× of validation with same weights.
Removes 5 unnecessary shaping components that fought the broken signal.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Q-mean drifted to +4.6 but E1 correction was capped at -2.0.
The model needs full correction range to keep Q-values calibrated.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
segment_pnl depended on raw_pnl (now zero after raw_next removal).
The trade-closing P&L was missing the final bar's unrealized gain/loss.
Fix: compute unrealized_at_exit = position * (raw_close - entry_price)
directly. No future price needed — just current close vs entry.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
E1 enrichment was computing q_corr = mean(predicted_q - pnl) but
predicted_q was set to avg_pnl → bias always ~0. Now uses actual
avg_q_value from the training step, producing meaningful corrections
when Q-values drift away from realized returns.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
portfolio_sim_kernel also used next_close_raw (future price) for
mark-to-market and reward computation. Fixed: use current price only.
Updated stale comments referencing per-bar reward in trade-level section.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The experience collector used raw_next (NEXT bar's close) for P&L and
equity, creating a 1-bar misalignment between actions and rewards.
The model couldn't learn which actions produce which outcomes.
val_Sharpe=24 (backtest uses raw_close correctly) but training Sharpe=0
(experience collector used raw_next, shifting reward by 1 bar).
Fix: ALL portfolio computations use raw_close only. raw_next removed
from reward path entirely. Rewards are per-trade only (position change
= realized P&L, holding = tiny cost, flat = zero).
This is the root cause of training Sharpe being capped at ~0 across
ALL runs regardless of model complexity or component changes.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Post-validation enrichment: extract eval trades from WindowMetrics,
run all 8 enrichments, apply adaptive epsilon + gamma + agreement
threshold. Smoke test verifies enrichment runs at least once.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Every epoch's validation backtest feeds back into the next epoch:
E1: Q-value reality check (bias correction, replaces drift penalty)
E2: Adaptive epsilon (performance-driven, replaces schedule)
E3: Dynamic gamma (from trade duration, replaces manual annealing)
E4: Trade autopsy (per-branch LR scaling from error rates)
E5: Ensemble agreement tuning (auto-tune epistemic gate)
E6: Winner distillation (boost top 10% trades in replay)
E7: Hindsight optimal labels (correct actions for losers)
E8: Curriculum weights (oversample failure regimes)
E9: State confidence (tradability scores from eval)
Zero hardcoded schedules. The model adapts from its own performance.
~215 lines Rust, no new CUDA kernels. ~15s overhead per epoch.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Risk branch weights trained via gentle decay toward initial values
(prevents R collapse to 0 or 1). Full BPTT backward deferred —
the risk branch learns its initial representation from trunk gradients
flowing through shared weights.
compute-sanitizer: 0 errors. Smoke test passes.
NUM_WEIGHT_TENSORS: 68. Total risk params: ~33K.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
c51_loss_kernel: reads cvar_alpha_buf[sample_id] when available (NULL = iqn_readiness fallback).
env_step: reads commit_lambda_buf[i] when available (NULL = 0.01 fallback).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1. Zero-target normalization: scale=max(|target|,|observed|,1.0) instead of
max(|target|,1e-6). Prevents Q-mean (target=0) from producing infinite
error that steals entire budget from other observables.
2. Per-observable budget cap: each observable gets budget/N_OBS instead of
competing for a global pool. One runaway can't starve the others.
3. Readiness-driven alpha: alpha = 0.3*(1-readiness) + 0.01*readiness.
Model readiness drives target adaptation speed, not epoch number.
Exploring → fast targets. Converged → slow targets. Never frozen.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Per-bar reward (next_close - close) has SNR ~0.01 — 99% random walk noise.
Trade-level P&L has SNR ~0.1-0.5 — the atomic unit of trading signal.
Position change + had old trade → reward = realized_pnl (trade outcome)
Holding (no change) → reward = -0.0001 * |position| (holding cost)
Flat → reward = 0
C51 atoms now model distribution of TRADE OUTCOMES instead of
distribution of per-bar noise. 10-50× signal improvement.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Two fundamental fixes for training Sharpe breakthrough:
1. Trade-level rewards: replace per-bar noise (SNR=0.01) with trade
P&L attribution (SNR=0.1-0.5). C51 atoms model trade outcome
distributions, not random walk noise.
2. Exploration risk budget: protection stack (CVaR, epistemic gate,
commitment, DSR) scaled by iqn_readiness². Loose during exploration,
tight when converged. Model can discover edges before being punished.
Also: homeostatic regularization spec (unified adaptive penalties).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Adds G16 homeostatic regularization that penalizes training observables
drifting from calibrated set-points. All 6 scalar signals use pinned
device-mapped memory (zero memcpy). Targets self-calibrate via EMA
during epochs 1-5, then freeze.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Q-mean drift: linear penalty (0.01 * q_mean) → quadratic
(0.01 * q_mean * |q_mean|). Small drift = tiny penalty, large
drift = hard correction. At q_mean=3.5: 12.25× stronger than linear.
Cost curriculum: linear ramp (epoch/20) → sigmoid centered at epoch 10.
Gradual start (find raw edges), steep middle (force cost adaptation),
gradual finish (fine-tune at real costs). Prevents strategy breakage
from sudden cost increases that caused training Sharpe oscillation.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
mamba2_scan_backward kernel reads d_h_enriched [B, SH2] but was passed
self.grad_buf [TOTAL_PARAMS] — wrong buffer, wrong size. At batch_size=4096
the kernel read 1M floats from a 582K buffer → 2749 OOB reads.
Fixed: use self.bw_d_h_s2 [B, SH2] which is the actual trunk activation
gradient from the cuBLAS backward pass.
Also increased smoke test batch_size to 4096 to catch scale-dependent OOB.
compute-sanitizer: 0 errors at batch_size=4096.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
CRITICAL: epsilon_buf memcpy_htod used full max_batch_size buffer but
eps_host was batch_size. Fixed: slice_mut(..batch_size) to match.
HIGH: update_q_mean_ema read pinned memory before GPU finished writing.
Moved after cuStreamSynchronize to ensure kernel completion.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The compute_expected_q and quantile_q_select kernels read per_sample_support[i*3+0/1/2]
(3 floats per sample), but the experience collector was passing eval_v_range_ptr which
is only 2 floats (v_min, v_max). Every sample after sample 0 read out of bounds.
Replace the u64 pointer field with a proper CudaSlice<f32> buffer [alloc_episodes, 3]
that is tiled with [v_min, v_max, delta_z] once per epoch via update_per_sample_support().
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Two tests:
- test_generalization_kernels_load: no data, verifies all CUDA cubins load
- test_generalization_components_smoke: 3 epochs on fxcache, verifies
AdamW, cost_anneal, gamma_anneal, DSR, walk-forward state, Q-gap,
atom utilization, all kernel launches succeed, finite metrics.
Passes in 2.7s on RTX 3050 with 5000 bars.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
HIGH: c51_loss_kernel now uses atom_positions per-branch in shmem_support
(was ignoring adaptive positions → forward/loss atom mismatch).
MEDIUM: adaptive_gamma wired into C51 Bellman projection via
set_adaptive_gamma(). Config gamma replaced with adaptive_gamma in
both launch_c51_loss sites.
MEDIUM: c51_grad z_norm uses adaptive atom positions when available
(was assuming linear grid for spread gradient).
LOW: adaptive_gamma field added to GpuDqnTrainer, initialized from
config, updated via passthrough from training loop.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
G5: Ensemble variance gates magnitude Q-values via sigmoid scale.
High disagreement forces conservative (Small) position. Pinned
var_ema threshold — no cuMemcpy in hot path.
G6: 6-way cosine similarity penalty on branch hidden activations
(lambda=0.01). Informational — gradient integration deferred.
G10: Lipschitz penalty on Q-diffs between consecutive similar states
(lambda=0.005, threshold=0.95). Uses atomicAdd accumulation.
G14: Confidence-weighted PER flagged for follow-up (needs on-GPU
priority modification to avoid memcpy).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
G2: Transaction costs anneal 0%→100% over 20 epochs via pinned
device-mapped cost_anneal_ptr. No memcpy — CPU writes, GPU reads.
G15: Commitment penalty lambda=0.01, tau=5.0 bars. Also scales
with cost_anneal to ramp together.
G11: Per-branch Q-anchoring to neutral actions (Flat/Small/Market/Normal).
Focuses model capacity on alpha signal over doing nothing.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
q_mean_scratch was CudaSlice with memcpy_dtoh readback every step.
Now pinned device-mapped: GPU writes via dev_ptr, CPU reads via
host_ptr directly. Dead inline EMA code removed, wired through
update_q_mean_ema(&self) method (DRY). No hot-path memcpy remains.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
AdamW weight_decay was in kernel but hardcoded to 0.0 in launch.
Now uses config value (1e-4) gated by per-param mask (1.0 for trunk+value
indices 0-7, 0.0 for branches). L1 proximal step on w_s1 (first layer)
induces automatic feature selection from 42-dim input.
Also updates decision_transformer.rs Adam launch to match the new
3-arg kernel signature (uniform mask, L1 disabled).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Reward rank normalization now operates on Sharpe contributions
(return - mean) / std instead of raw returns. Aligns reward signal
with Sharpe ratio goal. High-return trades during volatile periods
get lower rank weight than same return during calm periods.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
9d: Q-mean EMA (alpha=0.01) tracks epoch-level drift. Drift penalty
(lambda=0.01) in c51_grad pushes Q-distribution back toward zero.
9e: 500-step LR warmup for Mamba2 and other new components. Prevents
gradient shock from new modules destabilizing trained trunk.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Allocate rewards_5bar and rewards_20bar buffers [B] for future n-step
return computation. Wire multi_horizon_value_forward into training step
(currently degenerate d2d copy of 1-bar logits, ready for full GEMMs).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
atom_position_gradient kernel computes entropy-based gradient for
spacing_raw parameters. SGD decay toward uniform (lr=1e-3, every 50
steps) prevents atom positions from drifting. Concentrates atoms
where return distribution has mass.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Primary goal: val/OOS Sharpe gap < 15%. If val_Sharpe drops to 25
post-generalization, OOS should be > 21. If val drops to 15, OOS > 13.
OOS Sharpe > 10 sustained as secondary target.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2 extra value heads (W_v1/W_v2 pairs) for 5-bar and 20-bar horizons.
Regime-weighted blend: trend_weight=sigmoid((ADX-25)/5) mixes 1-bar
and 20-bar Q-values. Urgency branch learns temporal opportunity
structure. ~144K extra params. NUM_WEIGHT_TENSORS: 56 -> 64.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Walk-forward: strictly chronological, no future leakage.
Weight decay mask: indices 0-7 (trunk + value head), not just trunk.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>