|
4 | 4 | )
|
5 | 5 |
|
6 | 6 | import numpy as np
|
| 7 | +from numpy.lib.stride_tricks import sliding_window_view |
| 8 | + |
| 9 | + |
7 | 10 | import pytest
|
8 | 11 |
|
9 | 12 | from pandas.compat import (
|
@@ -1081,30 +1084,47 @@ def test_rolling_sem(frame_or_series):
|
1081 | 1084 | expected = Series([np.nan] + [0.7071067811865476] * 2)
|
1082 | 1085 | tm.assert_series_equal(result, expected)
|
1083 | 1086 |
|
1084 |
| - |
1085 |
| -@pytest.mark.xfail( |
1086 |
| - is_platform_arm() or is_platform_power() or is_platform_riscv64(), |
1087 |
| - reason="GH 38921", |
1088 |
| -) |
1089 | 1087 | @pytest.mark.parametrize(
|
1090 |
| - ("func", "third_value", "values"), |
1091 |
| - [ |
1092 |
| - ("var", 1, [5e33, 0, 0.5, 0.5, 2, 0]), |
1093 |
| - ("std", 1, [7.071068e16, 0, 0.7071068, 0.7071068, 1.414214, 0]), |
1094 |
| - ("var", 2, [5e33, 0.5, 0, 0.5, 2, 0]), |
1095 |
| - ("std", 2, [7.071068e16, 0.7071068, 0, 0.7071068, 1.414214, 0]), |
1096 |
| - ], |
| 1088 | + ("func", "values", "window", "ddof", "exp_value"), |
| 1089 | + [ |
| 1090 | + ("var", [2.72993945, 1.58444294, 4.14371708, 4.92961687, 2.7138744 ,3.48168586, 0.69505519, 1.87511994, 4.20167276, 0.04797675], 3, 1, "numpy_compute"), |
| 1091 | + ("std", [2.72993945, 1.58444294, 4.14371708, 4.92961687, 2.7138744 ,3.48168586, 0.69505519, 1.87511994, 4.20167276, 0.04797675], 3, 1, "numpy_compute"), |
| 1092 | + ("var", [2.72993945, 1.58444294, 4.14371708, 4.92961687, 2.7138744 ,3.48168586, 0.69505519, 1.87511994, 4.20167276, 0.04797675], 2, 1, "numpy_compute"), |
| 1093 | + ("std", [2.72993945, 1.58444294, 4.14371708, 4.92961687, 2.7138744 ,3.48168586, 0.69505519, 1.87511994, 4.20167276, 0.04797675], 2, 1, "numpy_compute"), |
| 1094 | + ("var", [99999999999999999, 1, 1, 2, 3, 1, 1], 2, 1, 0), |
| 1095 | + ("std", [99999999999999999, 1, 1, 2, 3, 1, 1], 2, 1, 0), |
| 1096 | + ("var", [99999999999999999, 1, 2, 2, 3, 1, 1], 2, 1, 0), |
| 1097 | + ("var", [99999999999999999, 1, 2, 2, 3, 1, 1], 2, 1, 0), |
| 1098 | + ("var", [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 5, 0, "numpy_compute"), |
| 1099 | + ], |
1097 | 1100 | )
|
1098 |
| -def test_rolling_var_numerical_issues(func, third_value, values): |
1099 |
| - # GH: 37051 |
1100 |
| - ds = Series([99999999999999999, 1, third_value, 2, 3, 1, 1]) |
1101 |
| - result = getattr(ds.rolling(2), func)() |
1102 |
| - expected = Series([np.nan] + values) |
1103 |
| - tm.assert_series_equal(result, expected) |
| 1101 | +def test_rolling_var_correctness(func, values, window, ddof, exp_value): |
| 1102 | + # This tests subsume the previous tests under test_rolling_var_numerical_issues |
| 1103 | + # GH: 37051, 42064, 54333 |
| 1104 | + ts = Series(values) |
| 1105 | + result = getattr(ts.rolling(window=window, center=True), func)(ddof=ddof) |
| 1106 | + if result.last_valid_index(): |
| 1107 | + result = result[result.first_valid_index() : result.last_valid_index()+1].reset_index(drop=True) |
| 1108 | + expected = Series(getattr(sliding_window_view(values, window_shape=window), func)(axis=-1, ddof=ddof)) #.var(axis=-1, ddof=ddof)) |
| 1109 | + tm.assert_series_equal(result, expected, atol=1e-55) |
1104 | 1110 | # GH 42064
|
1105 |
| - # new `roll_var` will output 0.0 correctly |
1106 |
| - tm.assert_series_equal(result == 0, expected == 0) |
1107 |
| - |
| 1111 | + if exp_value == 0: |
| 1112 | + # new `roll_var` will output 0.0 correctly |
| 1113 | + tm.assert_series_equal(result==0, expected==0) |
| 1114 | + |
| 1115 | +def test_rolling_var_numerical_stability(): |
| 1116 | + # GH 52407 |
| 1117 | + A = [0.00000000e+00, 0.00000000e+00, 3.16188252e-18, 2.95781651e-16, |
| 1118 | + 2.23153542e-51, 0.00000000e+00, 0.00000000e+00, 5.39943432e-48, |
| 1119 | + 1.38206260e-73, 0.00000000e+00] |
| 1120 | + ts = Series(A) |
| 1121 | + |
| 1122 | + result = ts.rolling(window=3, center=True).var(ddof=1) |
| 1123 | + result = result[result.first_valid_index() : result.last_valid_index()+1].reset_index(drop=True) |
| 1124 | + |
| 1125 | + # numpy implementation |
| 1126 | + expected = Series(sliding_window_view(A, window_shape=3).var(axis=-1, ddof=1)) |
| 1127 | + tm.assert_series_equal(result, expected, atol=1e-55) |
1108 | 1128 |
|
1109 | 1129 | def test_timeoffset_as_window_parameter_for_corr(unit):
|
1110 | 1130 | # GH: 28266
|
|
0 commit comments