Skip to content

fix(optimizer): make backtest first actions causal - #891

Merged
frahlg merged 2 commits into
masterfrom
agent/optimizer-causal-backtest
Aug 11, 2026
Merged

frahlg merged 2 commits into
masterfrom
agent/optimizer-causal-backtest

Conversation

@frahlg

@frahlg frahlg commented Aug 11, 2026

Copy link
Copy Markdown
Member

What

  • select only the latest diagnostic available by each measured interval's start
  • score at most one decision per unique, non-overlapping interval
  • stop summing overlapping full-horizon objectives
  • rename measured-data output to first_action_counterfactual
  • exclude PV-curtail actions that replay cannot model, including the active-zero sentinel through a balance check
  • report invalid measured rows as exclusions
  • bump the report schema to version 2

Why

The old report could use a plan produced after the measured bucket began and could add the same future hours many times. It also called planned battery power "realized" without command or measured delivery data.

This PR makes the remaining result narrow and explicit: it reprices independent first actions against measured exogenous interval averages. Its scope is grid_boundary_energy_only; it is not delivered optimizer savings.

Part of #885.

Tests

  • optimizer/.venv/bin/python -m pytest optimizer/tests/test_backtest.py -q — 11 passed
  • full optimizer suite — 100 passed
  • make verify
  • pre-push make verify-all, including linux/arm64, linux/amd64 and windows/amd64 builds

Limits

Each comparison starts from its persisted SoC. Closed-loop policy ranking still needs both policies rerun from the same propagated state, plus command results, measured battery delivery, wear and end-energy value.

@frahlg
frahlg marked this pull request as ready for review August 11, 2026 06:57
@frahlg
frahlg merged commit 1682e9a into master Aug 11, 2026
14 checks passed

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c9e7a98100

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +328 to +330
if decision_ms > start_ms:
exclusions["diagnostic after decision cutoff"] += 1
continue

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Select an action that can exist before the cutoff

For normal persisted diagnostics, this condition excludes essentially every record: buildSlots retains the slot containing the replan time, so the first slot has already started, while ComputedAtMs is the plan generation timestamp recorded during that replan. Consequently decision_ms is normally greater than slots[0].slot_start_ms; with --realized-csv, replay_records becomes empty and the report produces no horizon or first-action comparisons. To obtain a causal interval, select the matching future action from a diagnostic generated before that action's slot start rather than requiring the diagnostic's first action to satisfy this cutoff.

Useful? React with 👍 / 👎.

@frahlg
frahlg deleted the agent/optimizer-causal-backtest branch August 17, 2026 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant