From cdfaa5e7da339b05f51b94f54961a35822c5395f Mon Sep 17 00:00:00 2001 From: jgrusewski Date: Sat, 23 May 2026 16:55:41 +0200 Subject: [PATCH] fix(rl): mean_abs_pnl_ema tracks all non-zero rewards, not just closes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cluster smoke `alpha-rl-9cbpj` diag.jsonl revealed that 64% of non-zero reward events (170 of 266 across 1000 steps) occur on non-done steps — mid-trade PnL deltas from trail-stop adjustments, mark-to-market, or partial fills. The reward_scale controller's mean_abs_pnl_ema was done-gated, so these mid-trade reward magnitudes never contributed to the scale calibration. Concretely: closed-trade |PnL| settled around $832-910, controller calibrated `scale = 1/910 ≈ 0.0011`. Mid-trade swings can reach $2,390 (step 590 raw reward); scaled by 0.0011 they produce reward = -$58.8, which V regression must learn to predict. With the C51 V-head atom support of [−1, +1] the −58.8 target generates MSE ≈ 3,456 (canonical incident, prior smoke). The controller is calibrated for the wrong distribution. ## Fix The `ema_update_on_done` kernel's "dones" parameter is really a generic gate tested as `d >= 0.5f`. Passing `reward_abs_d` as the gate (instead of `dones_d`) gives "gate on |reward| ≥ 0.5" which for any practical dollar magnitude means "gate on non-zero reward event". Zero-reward steps still stay excluded so the EMA isn't biased toward zero on idle hold steps. One-line change at the launch site (the `obs` and gate arguments become the same buffer, `reward_abs_d`). No kernel modification needed — the same kernel serves both done-gated EMAs (e.g. `mean_trade_duration`) and reward-event-gated EMAs (this one) just by choice of which buffer is passed as the gate. ## Expected effect on next smoke The new mean_abs_pnl_ema will track the average |reward| across both close events ($832-910) and mid-trade events ($100-2400). With mid-trade magnitudes typically 2-3× larger than close magnitudes, the new mean will be higher → reward_scale lower → all reward magnitudes (close AND mid-trade) get squeezed into the V-head's atom support more consistently. The step-590 spike (l_v=3,456) should drop to O(1). ## Verified gates (local sm_86) All R-phase tests still green — the change is a single-argument swap at the launch site, no kernel logic touched. Tests pass unchanged because they don't exercise the mid-trade reward path (test harness uses synthetic LobSim with simple cross-and-close mechanics, not the trail-stop / partial-fill mid-trade dynamics that surfaced in the cluster smoke). Co-Authored-By: Claude Opus 4.7 --- crates/ml-alpha/src/trainer/integrated.rs | 26 +++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/crates/ml-alpha/src/trainer/integrated.rs b/crates/ml-alpha/src/trainer/integrated.rs index 2b335d02c..1043884da 100644 --- a/crates/ml-alpha/src/trainer/integrated.rs +++ b/crates/ml-alpha/src/trainer/integrated.rs @@ -2379,9 +2379,27 @@ impl IntegratedTrainer { } } - // Update ISV[423] MEAN_ABS_PNL_EMA from |reward| over closed - // trades (dones_d gating). R5 controllers consume this on - // their next per-step launch below. + // Update ISV[423] MEAN_ABS_PNL_EMA from |reward| over ALL + // non-zero reward events (NOT just trade closes). + // + // Originally gated on dones_d so only closed-trade realized + // PnL contributed to the EMA. Cluster smoke `alpha-rl-9cbpj` + // diag revealed that 170 of 266 non-zero reward events occur + // on non-done steps (mid-trade PnL deltas from trail-stop + // adjustments / mark-to-market / partial fills). These + // mid-trade swings can be 2-3× larger than realized close + // PnL — invisible to a done-gated EMA, they end up scaled + // by `reward_scale = 1/mean_abs_pnl_close` which is calibrated + // for the smaller magnitude. Result: V regression target + // spikes ×50-100 on volatile mid-trade steps (l_v=3,456 + // observed at step 590 with raw $2,390 PnL × scale=0.0011). + // + // The kernel's "done" parameter is really a generic gate + // tested as `d >= 0.5`. Passing reward_abs_d as the gate + // gives "gate on |reward| ≥ 0.5" (= "non-zero reward" for + // any practical magnitude in dollars) — exactly the + // semantics we want. Zero-reward steps stay excluded so the + // EMA isn't biased toward zero on idle steps. { let cfg = LaunchConfig { grid_dim: (1, 1, 1), @@ -2396,7 +2414,7 @@ impl IntegratedTrainer { .arg(&slot_i) .arg(&alpha) .arg(&self.reward_abs_d) - .arg(&self.dones_d) + .arg(&self.reward_abs_d) // gate on |reward|>0, not just on dones .arg(&b_size_i); unsafe { launch