Repository navigation
Analysis ToolPak: moving-average standard errors use a running sum (#1043) - #1735
Merged
aiongg merged 1 commit intoOct 22, 2026
Merged
Conversation
…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>
|
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
The running-sum math itself (add the entering term, subtract the leaving one, reset on a gap, clamp before |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 beforesqrtso rounding in the subtraction can't produce a NaN.Tests
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 layersandcargo xtask wasmall pass.Note on
cargo xtask ciThe clippy step (
-D warnings) fails on two existing warnings incrates/xlsx, which this PR doesn't touch:crates/xlsx/src/xls/mod.rs:273:ifcan be collapsed into the outermatchcrates/xlsx/src/tests/passthrough_tests.rs:480: boolean expression can be simplifiedThose two are the only warnings
cargo clippy --workspace --all-targetsreports; there are none incrates/engine.🤖 Generated with Claude Code