Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PERF/COMPAT: use kahan summation in .rolling(..).sum() #13254

Closed
jreback opened this issue May 22, 2016 · 1 comment · Fixed by #36348
Closed

PERF/COMPAT: use kahan summation in .rolling(..).sum() #13254

jreback opened this issue May 22, 2016 · 1 comment · Fixed by #36348
Assignees
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Numeric Operations Arithmetic, Comparison, and Logical operations Performance Memory or execution speed performance Window rolling, ewma, expanding
Milestone

Comments

@jreback
Copy link
Contributor

jreback commented May 22, 2016

from mailing list

to avoid imprecision errors as the rolling computations are evaluated marginally (sliding the window and adding new / subtracting old). We tend to accumulate floating point errors.

I looks like just an extra sum and subtract so I think the perf impact would be minimal.

This might be able to be applied to .rolling(..).mean() as well. Note that .rolling(..).std/var already use Welford's algo for better precision.

https://en.wikipedia.org/wiki/Kahan_summation_algorithm

implementation from numpy

cdef double kahan_sum(double *darr, npy_intp n):
    cdef double c, y, t, sum
    cdef npy_intp i
    sum = darr[0]
    c = 0.0
    for i from 1 <= i < n:
        y = darr[i] - c
        t = sum + y
        c = (t-sum) - y
        sum = t
    return sum

@jreback jreback added Performance Memory or execution speed performance Reshaping Concat, Merge/Join, Stack/Unstack, Explode Numeric Operations Arithmetic, Comparison, and Logical operations Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Difficulty Intermediate labels May 22, 2016
@jreback jreback added this to the Next Major Release milestone May 22, 2016
@jreback
Copy link
Contributor Author

jreback commented May 22, 2016

cc @jaimefrio

@jreback jreback changed the title PERF/COMPAT: use kahan summation in .rolling_sum PERF/COMPAT: use kahan summation in .rolling(..).sum() May 22, 2016
@mroeschke mroeschke added the Window rolling, ewma, expanding label Oct 20, 2019
@mroeschke mroeschke removed the Reshaping Concat, Merge/Join, Stack/Unstack, Explode label May 8, 2020
@phofl phofl self-assigned this Sep 14, 2020
@jreback jreback modified the milestones: Contributions Welcome, 1.2 Sep 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Numeric Operations Arithmetic, Comparison, and Logical operations Performance Memory or execution speed performance Window rolling, ewma, expanding
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants