-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
ENH: Improve numerical stability for Pearson corr() and cov() #37453
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
061a51f
BUG: Inconsisten result for corr with constant columns
phofl daaaabd
Fix pattern
phofl 168d78d
Use welford to calculate corr
phofl 11e8f25
Delete inline comment
phofl 7b1089d
Fix flake8
phofl 9bef42f
Adress review
phofl 1659881
Delte comma
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 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -268,7 +268,8 @@ def nancorr(const float64_t[:, :] mat, bint cov=False, minp=None): | |
ndarray[float64_t, ndim=2] result | ||
ndarray[uint8_t, ndim=2] mask | ||
int64_t nobs = 0 | ||
float64_t vx, vy, sumx, sumy, sumxx, sumyy, meanx, meany, divisor | ||
float64_t vx, vy, meanx, meany, divisor, prev_meany, prev_meanx, ssqdmx, | ||
float64_t ssqdmy, covxy | ||
|
||
N, K = (<object>mat).shape | ||
|
||
|
@@ -283,37 +284,29 @@ def nancorr(const float64_t[:, :] mat, bint cov=False, minp=None): | |
with nogil: | ||
for xi in range(K): | ||
for yi in range(xi + 1): | ||
nobs = sumxx = sumyy = sumx = sumy = 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. can u add in the welford reference link somewhere 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. Done |
||
# Welford's method for the variance-calculation | ||
# https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance | ||
nobs = ssqdmx = ssqdmy = covxy = meanx = meany = 0 | ||
for i in range(N): | ||
if mask[i, xi] and mask[i, yi]: | ||
vx = mat[i, xi] | ||
vy = mat[i, yi] | ||
nobs += 1 | ||
sumx += vx | ||
sumy += vy | ||
prev_meanx = meanx | ||
prev_meany = meany | ||
meanx = meanx + 1 / nobs * (vx - meanx) | ||
meany = meany + 1 / nobs * (vy - meany) | ||
ssqdmx = ssqdmx + (vx - meanx) * (vx - prev_meanx) | ||
ssqdmy = ssqdmy + (vy - meany) * (vy - prev_meany) | ||
covxy = covxy + (vx - meanx) * (vy - prev_meany) | ||
|
||
if nobs < minpv: | ||
result[xi, yi] = result[yi, xi] = NaN | ||
else: | ||
meanx = sumx / nobs | ||
meany = sumy / nobs | ||
|
||
# now the cov numerator | ||
sumx = 0 | ||
|
||
for i in range(N): | ||
if mask[i, xi] and mask[i, yi]: | ||
vx = mat[i, xi] - meanx | ||
vy = mat[i, yi] - meany | ||
|
||
sumx += vx * vy | ||
sumxx += vx * vx | ||
sumyy += vy * vy | ||
|
||
divisor = (nobs - 1.0) if cov else sqrt(sumxx * sumyy) | ||
divisor = (nobs - 1.0) if cov else sqrt(ssqdmx * ssqdmy) | ||
|
||
if divisor != 0: | ||
result[xi, yi] = result[yi, xi] = sumx / divisor | ||
result[xi, yi] = result[yi, xi] = covxy / divisor | ||
else: | ||
result[xi, yi] = result[yi, xi] = NaN | ||
|
||
|
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
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.
extra comma at the end :-<
can you run a quick asv to make sure that you typed all of the variables (not sure how else to check)
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.
Thanks, missed that somehow. Asv is running, I will report the results when I get them. Result should be similar as mentioned above, because I made no substantial changes in algos after this run.