fix(critical): backtest single-step uses 4-branch action encoding

Legacy 3-branch action decode/encode in backtest_env_step was
corrupting actions_history. Now uses correct 4-branch factored
logic matching backtest_env_step_batch. Fixes backtest metrics.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
jgrusewski
2026-04-17 02:03:10 +02:00
parent 58c2bd7ae0
commit 6208ace512

View File

@@ -261,14 +261,22 @@ extern "C" __global__ void backtest_env_step(
// Re-encode position -> exposure_idx -> factored action to ensure metrics
// kernel counts real position changes, not model-requested actions.
{
float f_actual_frac = position / fmaxf(max_position, 0.01f);
int actual_exp_idx = (int)roundf((f_actual_frac + 1.0f) * 0.5f * (float)(b0_size - 1));
if (actual_exp_idx < 0) actual_exp_idx = 0;
if (actual_exp_idx >= b0_size) actual_exp_idx = b0_size - 1;
// Preserve original order/urgency from model action
int orig_order = (b1_size > 0 && b2_size > 0) ? (action_val / b2_size) % b1_size : 0;
int orig_urgency = (b2_size > 0) ? action_val % b2_size : 0;
int actual_action = actual_exp_idx * b1_size * b2_size + orig_order * b2_size + orig_urgency;
/* Re-encode actual position as 4-branch factored action.
* Recover direction and magnitude from position, preserve order/urgency. */
float pos_sign = position;
int actual_dir = (pos_sign > 0.001f) ? 2 : (pos_sign < -0.001f) ? 0 : 1;
float abs_pos = fabsf(position) / fmaxf(max_position, 0.01f);
int actual_mag;
if (abs_pos < 0.375f) actual_mag = 0; /* ~0.25 -> Quarter */
else if (abs_pos < 0.75f) actual_mag = 1; /* ~0.50 -> Half */
else actual_mag = 2; /* ~1.00 -> Full */
if (actual_dir == 1) actual_mag = 0; /* Flat: magnitude irrelevant */
int orig_order = decode_order_4b(action_val, b2_size, b3_size);
int orig_urgency = decode_urgency_4b(action_val, b3_size);
int actual_action = actual_dir * b1_size * b2_size * b3_size
+ actual_mag * b2_size * b3_size
+ orig_order * b3_size
+ orig_urgency;
actions_history[w * max_len + current_step] = actual_action;
}
}