Skip to content

Commit

Permalink
CLN: Clean _test_moments_consistency in common.py (pandas-dev#30577)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesdong1991 authored and hweecat committed Jan 1, 2020
1 parent 7827a0c commit aed06b2
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 49 deletions.
51 changes: 28 additions & 23 deletions pandas/tests/window/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,40 +212,23 @@ def _create_data(self):
super()._create_data()
self.data = _consistency_data

def _test_moments_consistency(
self,
min_periods,
count,
mean,
mock_mean,
corr,
var_unbiased=None,
std_unbiased=None,
cov_unbiased=None,
var_biased=None,
std_biased=None,
cov_biased=None,
var_debiasing_factors=None,
):
def _non_null_values(x):
values = x.values.ravel()
return set(values[notna(values)].tolist())

def _test_moments_consistency_mock_mean(self, mean, mock_mean):
for (x, is_constant, no_nans) in self.data:
count_x = count(x)
mean_x = mean(x)
# check that correlation of a series with itself is either 1 or NaN

if mock_mean:
# check that mean equals mock_mean
expected = mock_mean(x)
tm.assert_equal(mean_x, expected.astype("float64"))

def _test_moments_consistency_is_constant(self, min_periods, count, mean, corr):
for (x, is_constant, no_nans) in self.data:
count_x = count(x)
mean_x = mean(x)
# check that correlation of a series with itself is either 1 or NaN
corr_x_x = corr(x, x)

# assert _non_null_values(corr_x_x).issubset(set([1.]))
# restore once rolling_cov(x, x) is identically equal to var(x)

if is_constant:
exp = x.max() if isinstance(x, Series) else x.max().max()

Expand All @@ -258,13 +241,35 @@ def _non_null_values(x):
expected[:] = np.nan
tm.assert_equal(corr_x_x, expected)

def _test_moments_consistency_var_debiasing_factors(
self, var_biased=None, var_unbiased=None, var_debiasing_factors=None
):
for (x, is_constant, no_nans) in self.data:
if var_unbiased and var_biased and var_debiasing_factors:
# check variance debiasing factors
var_unbiased_x = var_unbiased(x)
var_biased_x = var_biased(x)
var_debiasing_factors_x = var_debiasing_factors(x)
tm.assert_equal(var_unbiased_x, var_biased_x * var_debiasing_factors_x)

def _test_moments_consistency(
self,
min_periods,
count,
mean,
corr,
var_unbiased=None,
std_unbiased=None,
cov_unbiased=None,
var_biased=None,
std_biased=None,
cov_biased=None,
):

for (x, is_constant, no_nans) in self.data:
count_x = count(x)
mean_x = mean(x)

for (std, var, cov) in [
(std_biased, var_biased, cov_biased),
(std_unbiased, var_unbiased, cov_unbiased),
Expand Down
45 changes: 37 additions & 8 deletions pandas/tests/window/moments/test_moments_ewm.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,16 +379,50 @@ def _ewma(s, com, min_periods, adjust, ignore_na):
return result

com = 3.0
self._test_moments_consistency_mock_mean(
mean=lambda x: x.ewm(
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
).mean(),
mock_mean=lambda x: _ewma(
x, com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
),
)

self._test_moments_consistency_is_constant(
min_periods=min_periods,
count=lambda x: x.expanding().count(),
mean=lambda x: x.ewm(
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
).mean(),
corr=lambda x, y: x.ewm(
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
).corr(y),
)

self._test_moments_consistency_var_debiasing_factors(
var_unbiased=lambda x: (
x.ewm(
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
).var(bias=False)
),
var_biased=lambda x: (
x.ewm(
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
).var(bias=True)
),
var_debiasing_factors=lambda x: (
_variance_debiasing_factors(
x, com=com, adjust=adjust, ignore_na=ignore_na
)
),
)
# test consistency between different ewm* moments
self._test_moments_consistency(
min_periods=min_periods,
count=lambda x: x.expanding().count(),
mean=lambda x: x.ewm(
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
).mean(),
mock_mean=lambda x: _ewma(
x, com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
),
corr=lambda x, y: x.ewm(
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
).corr(y),
Expand Down Expand Up @@ -420,9 +454,4 @@ def _ewma(s, com, min_periods, adjust, ignore_na):
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
).cov(y, bias=True)
),
var_debiasing_factors=lambda x: (
_variance_debiasing_factors(
x, com=com, adjust=adjust, ignore_na=ignore_na
)
),
)
27 changes: 21 additions & 6 deletions pandas/tests/window/moments/test_moments_expanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,31 @@ def test_expanding_consistency(self, min_periods):
)

# test consistency between different expanding_* moments
self._test_moments_consistency_mock_mean(
mean=lambda x: x.expanding(min_periods=min_periods).mean(),
mock_mean=lambda x: x.expanding(min_periods=min_periods).sum()
/ x.expanding().count(),
)

self._test_moments_consistency_is_constant(
min_periods=min_periods,
count=lambda x: x.expanding().count(),
mean=lambda x: x.expanding(min_periods=min_periods).mean(),
corr=lambda x, y: x.expanding(min_periods=min_periods).corr(y),
)

self._test_moments_consistency_var_debiasing_factors(
var_unbiased=lambda x: x.expanding(min_periods=min_periods).var(),
var_biased=lambda x: x.expanding(min_periods=min_periods).var(ddof=0),
var_debiasing_factors=lambda x: (
x.expanding().count()
/ (x.expanding().count() - 1.0).replace(0.0, np.nan)
),
)
self._test_moments_consistency(
min_periods=min_periods,
count=lambda x: x.expanding().count(),
mean=lambda x: x.expanding(min_periods=min_periods).mean(),
mock_mean=lambda x: x.expanding(min_periods=min_periods).sum()
/ x.expanding().count(),
corr=lambda x, y: x.expanding(min_periods=min_periods).corr(y),
var_unbiased=lambda x: x.expanding(min_periods=min_periods).var(),
std_unbiased=lambda x: x.expanding(min_periods=min_periods).std(),
Expand All @@ -343,10 +362,6 @@ def test_expanding_consistency(self, min_periods):
cov_biased=lambda x, y: x.expanding(min_periods=min_periods).cov(
y, ddof=0
),
var_debiasing_factors=lambda x: (
x.expanding().count()
/ (x.expanding().count() - 1.0).replace(0.0, np.nan)
),
)

# test consistency between expanding_xyz() and either (a)
Expand Down
60 changes: 48 additions & 12 deletions pandas/tests/window/moments/test_moments_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,9 +945,7 @@ def test_rolling_consistency(self, window, min_periods, center):
)

# test consistency between different rolling_* moments
self._test_moments_consistency(
min_periods=min_periods,
count=lambda x: (x.rolling(window=window, center=center).count()),
self._test_moments_consistency_mock_mean(
mean=lambda x: (
x.rolling(
window=window, min_periods=min_periods, center=center
Expand All @@ -962,6 +960,53 @@ def test_rolling_consistency(self, window, min_periods, center):
).count()
)
),
)

self._test_moments_consistency_is_constant(
min_periods=min_periods,
count=lambda x: (x.rolling(window=window, center=center).count()),
mean=lambda x: (
x.rolling(
window=window, min_periods=min_periods, center=center
).mean()
),
corr=lambda x, y: (
x.rolling(
window=window, min_periods=min_periods, center=center
).corr(y)
),
)

self._test_moments_consistency_var_debiasing_factors(
var_unbiased=lambda x: (
x.rolling(
window=window, min_periods=min_periods, center=center
).var()
),
var_biased=lambda x: (
x.rolling(
window=window, min_periods=min_periods, center=center
).var(ddof=0)
),
var_debiasing_factors=lambda x: (
x.rolling(window=window, center=center)
.count()
.divide(
(x.rolling(window=window, center=center).count() - 1.0).replace(
0.0, np.nan
)
)
),
)

self._test_moments_consistency(
min_periods=min_periods,
count=lambda x: (x.rolling(window=window, center=center).count()),
mean=lambda x: (
x.rolling(
window=window, min_periods=min_periods, center=center
).mean()
),
corr=lambda x, y: (
x.rolling(
window=window, min_periods=min_periods, center=center
Expand Down Expand Up @@ -997,15 +1042,6 @@ def test_rolling_consistency(self, window, min_periods, center):
window=window, min_periods=min_periods, center=center
).cov(y, ddof=0)
),
var_debiasing_factors=lambda x: (
x.rolling(window=window, center=center)
.count()
.divide(
(x.rolling(window=window, center=center).count() - 1.0).replace(
0.0, np.nan
)
)
),
)

# test consistency between rolling_xyz() and either (a)
Expand Down

0 comments on commit aed06b2

Please sign in to comment.