Skip to content

Avoid carrying rounding errors into later move_sum windows - #629

Open
CAOShurong wants to merge 2 commits into
pydata:masterfrom
CAOShurong:fix-move-sum-large-window
Open

CAOShurong wants to merge 2 commits into
pydata:masterfrom
CAOShurong:fix-move-sum-large-window

Conversation

@CAOShurong

@CAOShurong CAOShurong commented Nov 5, 2026 •

Copy link
Copy Markdown

Compute floating-point window sums from the suffix of one block and the prefix of the next. Both parts contain only values still in the window. This takes linear time and uses the output array as scratch space. Integer sums keep their existing path.

On the 350 values in #480, the last sum is 42374.54780862338, matching math.fsum. The shorter example in review now returns 101.0.

There is a speed trade-off. On this Windows machine, a million contiguous float64 values with a window of 100 took 2.60 ms, compared with 2.13 ms on master. The installed wheel passed 212 tests with one expected failure.

Fixes #480.

The running sum subtracts the outgoing value in the input dtype. After a value near 1e19 leaves, the remaining positive window can come back as 0. That window is added directly when the outgoing value dominates the result.
@yucheng-du

yucheng-du commented Dec 2, 2026 •

Copy link
Copy Markdown

I built f6e40d7 and ran the 350 values from #480. With the reporter’s window=100, min_count=20, the last window is still -2595.7239120573686. Summing those 100 binary floats exactly with Fraction.from_float and rounding once gives 42374.54780862338 (also math.fsum). The PR’s new five-element regression does pass on the same build.

The recomputation condition never fires on the reported series. Among outgoing values above 1e6, the largest abs(aold) / (abs(asum) + 1) is about 61.73, below 1e3. Several successive decreases can carry an earlier rounding error through to the last window. Here is a shorter test for that case:

x = 100.0 ** np.arange(9, -1, -1)
x[1] += 4
bn.move_sum(x, 2)[-1]  # 97.0 on the PR
x[-2:].sum()           # 101.0

I also tried splitting each slice into blocks of length window. Each output adds the suffix of the previous block to the prefix of the current block. The output array can hold the suffix sums, so the two passes are linear and need constant extra space. Here is a tested commit on top of this PR if you want to try it. The last window now matches the exact sum above. The worst relative error across the reporter's windows of length 2, 30 and 100 was 8.9e-16; 211 package tests passed (one existing xfail). There is a speed tradeoff: for one million contiguous Float64 values, window 100, I measured 1.01 ms on main, 1.67 ms on this PR, and 2.44 ms with the block sums.

@CAOShurong CAOShurong changed the title Recompute move_sum after a large value leaves Avoid carrying rounding errors into later move_sum windows Dec 2, 2026
@CAOShurong

Copy link
Copy Markdown
Author

Thanks for catching this. I reproduced both failures on the previous head. With the block sums, the final window from #480 gives 42374.54780862338, and your shorter example gives 101.0. I added the short case as a regression test.

The code and tests from #633 still apply on top. The two patches touch the same paragraph about infinity in the docstring.

This branch has not been deployed

No deployments
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.

[BUG] move_sum returns a negative value for a sum over a pandas Series with all non-negative values

2 participants