Skip to content

Analysis ToolPak: moving-average standard errors use a running sum (#1043) - #1735

Merged
aiongg merged 1 commit into
storytold:mainfrom
TheRenegadeDev:fix/1043-moving-average-stderr
Oct 22, 2026
Merged

aiongg merged 1 commit into
storytold:mainfrom
TheRenegadeDev:fix/1043-moving-average-stderr

Conversation

@TheRenegadeDev

Copy link
Copy Markdown
Contributor

Fixes #1043

Problem

With Standard errors on, forecast_grid (crates/engine/src/cmd/analysis.rs) re-added every squared residual in each window (for j in s0..=i). That's O(n × window) work, so a long series with a wide interval could freeze the app.

Fix

Keep a running sum of squared residuals over the window: add the term that enters it and subtract the one that leaves it. Missing forecasts (the leading points, or any None) reset the sum and the run length, so every window that touches one is still #N/A, as before. The sum is clamped at 0 before sqrt so rounding in the subtraction can't produce a NaN.

Tests

  • New regression test forecast_standard_errors_match_the_full_window_rescan. It compares the new output with the old rescan code on an 11-point series with a gap in the middle, for windows 1–5 (tolerance 1e-12), and checks that the #N/As around the gap are unchanged.
  • cargo test --workspace, cargo fmt --check, cargo xtask assets, cargo xtask layers and cargo xtask wasm all pass.

Note on cargo xtask ci

The clippy step (-D warnings) fails on two existing warnings in crates/xlsx, which this PR doesn't touch:

  • crates/xlsx/src/xls/mod.rs:273: if can be collapsed into the outer match
  • crates/xlsx/src/tests/passthrough_tests.rs:480: boolean expression can be simplified

Those two are the only warnings cargo clippy --workspace --all-targets reports; there are none in crates/engine.

🤖 Generated with Claude Code

…ixes storytold#1043)

forecast_grid re-added every squared residual in each window, O(n×window).
Keep a running sum instead: add the entering term, subtract the leaving one,
and restart at a point without a forecast so windows touching it stay #N/A.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MichaelMuClaude

Copy link
Copy Markdown

Independent verification, since this had no review.

This one's a bit different from the usual pattern — it's a performance fix (O(n·window) → O(n)), not a behavior bug, so there's no "fails without the fix" angle; the new test's own job is to prove the fast running-sum rewrite gives identical output to a full rescan, not to catch wrong results. Applied cleanly on current main (74 lines, no conflicts):

  • cargo test -p gridcraft-engine --lib forecast_standard_errors_match_the_full_window_rescan: pass — matches an independent brute-force rescan to 1e-12 across windows 1–5 on an 11-point series with a gap, and separately confirms the #N/A pattern around the gap is unchanged (row 5 stays #N/A, row 6 recovers)
  • cargo test -p gridcraft-engine --lib analysis::: 6/6 pass, including hostile_inputs_are_refused_without_panicking — no regression to the rest of the Analysis ToolPak
  • cargo clippy -p gridcraft-engine --lib -- -D warnings: clean (the PR correctly notes the two cargo xtask ci clippy failures are pre-existing, in crates/xlsx, unrelated to this change — confirmed, scoping my check to gridcraft-engine alone also comes back clean)
  • cargo fmt --check: clean

The running-sum math itself (add the entering term, subtract the leaving one, reset on a gap, clamp before sqrt to guard against a tiny negative from subtraction rounding) is straightforward and the equivalence test is a reasonable way to prove a performance rewrite didn't change behavior. Looks ready to merge.

@aiongg
aiongg merged commit ad490a1 into storytold:main Oct 22, 2026
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.

fix(analysis): moving-average standard errors rescan every overlapping window

3 participants