Skip to content

BUG: {expanding,rolling}_{cov,corr} don't handle arguments with different index sets properly #7604

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 1 commit into from
Jul 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,5 @@ Bug Fixes

- Bug in ``Float64Index`` assignment with a non scalar indexer (:issue:`7586`)
- Bug in ``pandas.core.strings.str_contains`` does not properly match in a case insensitive fashion when ``regex=False`` and ``case=False`` (:issue:`7505`)

- Bug in ``expanding_cov``, ``expanding_corr``, ``rolling_cov``, and ``rolling_corr`` for two arguments with mismatched index (:issue:`7512`)
25 changes: 13 additions & 12 deletions pandas/stats/moments.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ def rolling_cov(arg1, arg2=None, window=None, min_periods=None, freq=None,
pairwise = True if pairwise is None else pairwise # only default unset
arg1 = _conv_timerule(arg1, freq, how)
arg2 = _conv_timerule(arg2, freq, how)
window = min(window, len(arg1), len(arg2))

def _get_cov(X, Y):
mean = lambda x: rolling_mean(x, window, min_periods, center=center)
count = rolling_count(X + Y, window, center=center)
adj_window = min(window, len(X), len(Y))
mean = lambda x: rolling_mean(x, adj_window, min_periods, center=center)
count = rolling_count(X + Y, adj_window, center=center)
bias_adj = count / (count - 1)
return (mean(X * Y) - mean(X) * mean(Y)) * bias_adj
rs = _flex_binary_moment(arg1, arg2, _get_cov, pairwise=bool(pairwise))
Expand All @@ -234,16 +234,17 @@ def rolling_corr(arg1, arg2=None, window=None, min_periods=None, freq=None,
pairwise = True if pairwise is None else pairwise # only default unset
arg1 = _conv_timerule(arg1, freq, how)
arg2 = _conv_timerule(arg2, freq, how)
window = min(window, len(arg1), len(arg2))

def _get_corr(a, b):
num = rolling_cov(a, b, window, min_periods, freq=freq,
adj_window = min(window, len(a), len(b))
num = rolling_cov(a, b, adj_window, min_periods, freq=freq,
center=center)
den = (rolling_std(a, window, min_periods, freq=freq,
den = (rolling_std(a, adj_window, min_periods, freq=freq,
center=center) *
rolling_std(b, window, min_periods, freq=freq,
rolling_std(b, adj_window, min_periods, freq=freq,
center=center))
return num / den

return _flex_binary_moment(arg1, arg2, _get_corr, pairwise=bool(pairwise))


Expand All @@ -261,9 +262,9 @@ def _flex_binary_moment(arg1, arg2, f, pairwise=False):
results = {}
if isinstance(arg2, DataFrame):
X, Y = arg1.align(arg2, join='outer')
X = X + 0 * Y
Y = Y + 0 * X
if pairwise is False:
X = X + 0 * Y
Y = Y + 0 * X
res_columns = arg1.columns.union(arg2.columns)
for col in res_columns:
if col in X and col in Y:
Expand All @@ -276,7 +277,7 @@ def _flex_binary_moment(arg1, arg2, f, pairwise=False):
# Symmetric case
results[k1][k2] = results[k2][k1]
else:
results[k1][k2] = f(arg1[k1], arg2[k2])
results[k1][k2] = f(*_prep_binary(arg1[k1], arg2[k2]))
return Panel.from_dict(results).swapaxes('items', 'major')
else:
raise ValueError("'pairwise' is not True/False")
Expand Down Expand Up @@ -917,7 +918,7 @@ def expanding_cov(arg1, arg2=None, min_periods=1, freq=None, center=False,
min_periods = arg2
arg2 = arg1
pairwise = True if pairwise is None else pairwise
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expanding_xxx() functions need window = len(arg1) + len(arg2) rather than = max(len(arg1), len(arg2)) because once arg1 and arg2 are merged the result could be of length up to len(arg1) + len(arg2) if there is no overlap between the index (or major_axis) of arg1 and arg2. If the index of one is a subset of the index of the other, then max() will be fine; but in general when neither is a subset of the other max() will be too small.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, without this fix, see the following; whereas the final value should be 1.

In [2]: s1 = Series([7,8,10], index=[0,1,3])

In [3]: s2 = Series([7,9,10], index=[0,2,3])

In [4]: expanding_corr(s1, s2)
Out[4]:
0   NaN
1   NaN
2   NaN
3   NaN
dtype: float64

There isn't quite a test for this. I think I should add one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why would u not align the 2 objects before you start, then all this is moot

eg try doing

df1, df2 = df1.align(df2)

and see what that results

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea. This is the way it was written, and I didn't want to modify things any more than necessary.
At the very least I will add a test, and will see if the .align() thing works. Probably won't get it done until tomorrow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacing:

    window = len(arg1) + len(arg2)

with:

    arg1, arg2 = arg1.align(arg2)
    window = len(arg1)

in expanding_cov/corr() works for series, but not for data frames: it not only merges the index, but also the columns, thus producing the wrong result for pairwise=True calls -- e.g. if arg1.columns=['A','B'] and arg2.columns=['X','Y'], then while the result should have major/minor axes ['A','B'] and ['X','Y'], instead it ends up with both being ['A','B','X','Y'].

At any rate, since the two objects end up being aligned anyway in _flex_binary_moment(), aligning them in expanding_cov/corr is just redundant.

I updated the code to include several additional tests, but left window = len(arg1) + len(arg2) unchanged.

window = max(len(arg1), len(arg2))
window = len(arg1) + len(arg2)
return rolling_cov(arg1, arg2, window,
min_periods=min_periods, freq=freq,
center=center, pairwise=pairwise)
Expand All @@ -935,7 +936,7 @@ def expanding_corr(arg1, arg2=None, min_periods=1, freq=None, center=False,
min_periods = arg2
arg2 = arg1
pairwise = True if pairwise is None else pairwise
window = max(len(arg1), len(arg2))
window = len(arg1) + len(arg2)
return rolling_corr(arg1, arg2, window,
min_periods=min_periods,
freq=freq, center=center, pairwise=pairwise)
Expand Down
92 changes: 92 additions & 0 deletions pandas/stats/tests/test_moments.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,98 @@ def test_expanding_corr_pairwise(self):
for i in result.items:
assert_almost_equal(result[i], rolling_result[i])

def test_expanding_cov_diff_index(self):
# GH 7512
s1 = Series([1, 2, 3], index=[0, 1, 2])
s2 = Series([1, 3], index=[0, 2])
result = mom.expanding_cov(s1, s2)
expected = Series([None, None, 2.0])
assert_series_equal(result, expected)

s2a = Series([1, None, 3], index=[0, 1, 2])
result = mom.expanding_cov(s1, s2a)
assert_series_equal(result, expected)

s1 = Series([7, 8, 10], index=[0, 1, 3])
s2 = Series([7, 9, 10], index=[0, 2, 3])
result = mom.expanding_cov(s1, s2)
expected = Series([None, None, None, 4.5])
assert_series_equal(result, expected)

def test_expanding_corr_diff_index(self):
# GH 7512
s1 = Series([1, 2, 3], index=[0, 1, 2])
s2 = Series([1, 3], index=[0, 2])
result = mom.expanding_corr(s1, s2)
expected = Series([None, None, 1.0])
assert_series_equal(result, expected)

s2a = Series([1, None, 3], index=[0, 1, 2])
result = mom.expanding_corr(s1, s2a)
assert_series_equal(result, expected)

s1 = Series([7, 8, 10], index=[0, 1, 3])
s2 = Series([7, 9, 10], index=[0, 2, 3])
result = mom.expanding_corr(s1, s2)
expected = Series([None, None, None, 1.])
assert_series_equal(result, expected)

def test_rolling_cov_diff_length(self):
# GH 7512
s1 = Series([1, 2, 3], index=[0, 1, 2])
s2 = Series([1, 3], index=[0, 2])
result = mom.rolling_cov(s1, s2, window=3, min_periods=2)
expected = Series([None, None, 2.0])
assert_series_equal(result, expected)

s2a = Series([1, None, 3], index=[0, 1, 2])
result = mom.rolling_cov(s1, s2a, window=3, min_periods=2)
assert_series_equal(result, expected)

def test_rolling_corr_diff_length(self):
# GH 7512
s1 = Series([1, 2, 3], index=[0, 1, 2])
s2 = Series([1, 3], index=[0, 2])
result = mom.rolling_corr(s1, s2, window=3, min_periods=2)
expected = Series([None, None, 1.0])
assert_series_equal(result, expected)

s2a = Series([1, None, 3], index=[0, 1, 2])
result = mom.rolling_corr(s1, s2a, window=3, min_periods=2)
assert_series_equal(result, expected)

def test_expanding_cov_pairwise_diff_length(self):
# GH 7512
df1 = DataFrame([[1,5], [3, 2], [3,9]], columns=['A','B'])
df1a = DataFrame([[1,5], [3,9]], index=[0,2], columns=['A','B'])
df2 = DataFrame([[5,6], [None,None], [2,1]], columns=['X','Y'])
df2a = DataFrame([[5,6], [2,1]], index=[0,2], columns=['X','Y'])
result1 = mom.expanding_cov(df1, df2, pairwise=True)[2]
result2 = mom.expanding_cov(df1, df2a, pairwise=True)[2]
result3 = mom.expanding_cov(df1a, df2, pairwise=True)[2]
result4 = mom.expanding_cov(df1a, df2a, pairwise=True)[2]
expected = DataFrame([[-3., -5.], [-6., -10.]], index=['A','B'], columns=['X','Y'])
assert_frame_equal(result1, expected)
assert_frame_equal(result2, expected)
assert_frame_equal(result3, expected)
assert_frame_equal(result4, expected)

def test_expanding_corr_pairwise_diff_length(self):
# GH 7512
df1 = DataFrame([[1,2], [3, 2], [3,4]], columns=['A','B'])
df1a = DataFrame([[1,2], [3,4]], index=[0,2], columns=['A','B'])
df2 = DataFrame([[5,6], [None,None], [2,1]], columns=['X','Y'])
df2a = DataFrame([[5,6], [2,1]], index=[0,2], columns=['X','Y'])
result1 = mom.expanding_corr(df1, df2, pairwise=True)[2]
result2 = mom.expanding_corr(df1, df2a, pairwise=True)[2]
result3 = mom.expanding_corr(df1a, df2, pairwise=True)[2]
result4 = mom.expanding_corr(df1a, df2a, pairwise=True)[2]
expected = DataFrame([[-1.0, -1.0], [-1.0, -1.0]], index=['A','B'], columns=['X','Y'])
assert_frame_equal(result1, expected)
assert_frame_equal(result2, expected)
assert_frame_equal(result3, expected)
assert_frame_equal(result4, expected)

def test_rolling_skew_edge_cases(self):

all_nan = Series([np.NaN] * 5)
Expand Down