-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
ENH: Use Kahan summation and Welfords method to calculate rolling var and std #37055
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5066d54
Implement bedfords algorithm
phofl 9f83a4c
Use kahan summation for var
phofl 214d563
Add whatsnew and adjust comments to reflect new behavior
phofl 602443c
Add test
phofl a953f46
Delete old comment
phofl 3546935
Fix flake problems
phofl d29e7c5
Adjust kahan summation and add new tests
phofl 411ba68
Run black
phofl 95d652c
Add old comment
phofl cfca750
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37051
phofl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -311,37 +311,46 @@ cdef inline float64_t calc_var(int64_t minp, int ddof, float64_t nobs, | |
|
||
|
||
cdef inline void add_var(float64_t val, float64_t *nobs, float64_t *mean_x, | ||
float64_t *ssqdm_x) nogil: | ||
float64_t *ssqdm_x, float64_t *compensation) nogil: | ||
""" add a value from the var calc """ | ||
cdef: | ||
float64_t delta | ||
float64_t delta, prev_mean, y, t | ||
|
||
# `isnan` instead of equality as fix for GH-21813, msvc 2017 bug | ||
if isnan(val): | ||
return | ||
|
||
nobs[0] = nobs[0] + 1 | ||
# a part of Welford's method for the online variance-calculation | ||
# Welford's method for the online variance-calculation | ||
# using Kahan summation | ||
# https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance | ||
delta = val - mean_x[0] | ||
prev_mean = mean_x[0] - compensation[0] | ||
y = val - compensation[0] | ||
t = y - mean_x[0] | ||
compensation[0] = t + mean_x[0] - y | ||
delta = t | ||
mean_x[0] = mean_x[0] + delta / nobs[0] | ||
ssqdm_x[0] = ssqdm_x[0] + ((nobs[0] - 1) * delta ** 2) / nobs[0] | ||
ssqdm_x[0] = ssqdm_x[0] + (val - prev_mean) * (val - mean_x[0]) | ||
|
||
|
||
cdef inline void remove_var(float64_t val, float64_t *nobs, float64_t *mean_x, | ||
float64_t *ssqdm_x) nogil: | ||
float64_t *ssqdm_x, float64_t *compensation) nogil: | ||
""" remove a value from the var calc """ | ||
cdef: | ||
float64_t delta | ||
|
||
float64_t delta, prev_mean, y, t | ||
if notnan(val): | ||
nobs[0] = nobs[0] - 1 | ||
if nobs[0]: | ||
# a part of Welford's method for the online variance-calculation | ||
# Welford's method for the online variance-calculation | ||
# using Kahan summation | ||
# https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance | ||
delta = val - mean_x[0] | ||
prev_mean = mean_x[0] - compensation[0] | ||
y = val - compensation[0] | ||
t = y - mean_x[0] | ||
compensation[0] = t + mean_x[0] - y | ||
delta = t | ||
mean_x[0] = mean_x[0] - delta / nobs[0] | ||
ssqdm_x[0] = ssqdm_x[0] - ((nobs[0] + 1) * delta ** 2) / nobs[0] | ||
ssqdm_x[0] = ssqdm_x[0] - (val - prev_mean) * (val - mean_x[0]) | ||
else: | ||
mean_x[0] = 0 | ||
ssqdm_x[0] = 0 | ||
|
@@ -353,7 +362,8 @@ def roll_var(ndarray[float64_t] values, ndarray[int64_t] start, | |
Numerically stable implementation using Welford's method. | ||
""" | ||
cdef: | ||
float64_t mean_x = 0, ssqdm_x = 0, nobs = 0, | ||
float64_t mean_x = 0, ssqdm_x = 0, nobs = 0, compensation_add = 0, | ||
float64_t compensation_remove = 0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't there a line in this func that you need to remove eg the < 1e-14 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See below |
||
float64_t val, prev, delta, mean_x_old | ||
int64_t s, e | ||
Py_ssize_t i, j, N = len(values) | ||
|
@@ -375,26 +385,28 @@ def roll_var(ndarray[float64_t] values, ndarray[int64_t] start, | |
if i == 0 or not is_monotonic_bounds: | ||
|
||
for j in range(s, e): | ||
add_var(values[j], &nobs, &mean_x, &ssqdm_x) | ||
add_var(values[j], &nobs, &mean_x, &ssqdm_x, &compensation_add) | ||
|
||
else: | ||
|
||
# After the first window, observations can both be added | ||
# and removed | ||
|
||
# calculate adds | ||
for j in range(end[i - 1], e): | ||
add_var(values[j], &nobs, &mean_x, &ssqdm_x) | ||
|
||
# calculate deletes | ||
for j in range(start[i - 1], s): | ||
remove_var(values[j], &nobs, &mean_x, &ssqdm_x) | ||
remove_var(values[j], &nobs, &mean_x, &ssqdm_x, | ||
&compensation_remove) | ||
|
||
# calculate adds | ||
for j in range(end[i - 1], e): | ||
add_var(values[j], &nobs, &mean_x, &ssqdm_x, &compensation_add) | ||
|
||
output[i] = calc_var(minp, ddof, nobs, ssqdm_x) | ||
|
||
if not is_monotonic_bounds: | ||
for j in range(s, e): | ||
remove_var(values[j], &nobs, &mean_x, &ssqdm_x) | ||
remove_var(values[j], &nobs, &mean_x, &ssqdm_x, | ||
&compensation_remove) | ||
|
||
return output | ||
|
||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not fully true, but its better so ok.