Skip to content
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

fix: Compute joint null mask before calling rolling corr/cov stats #18246

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 14 additions & 6 deletions crates/polars-plan/src/dsl/functions/correlation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,15 @@ pub fn rolling_corr(x: Expr, y: Expr, options: RollingCovOptions) -> Expr {
..Default::default()
};

let non_null_mask = when(x.clone().is_not_null().and(y.clone().is_not_null()))
.then(lit(1.0))
.otherwise(lit(Null {}));

let mean_x_y = (x.clone() * y.clone()).rolling_mean(rolling_options.clone());
let mean_x = x.clone().rolling_mean(rolling_options.clone());
let mean_y = y.clone().rolling_mean(rolling_options.clone());
let var_x = x.clone().rolling_var(rolling_options.clone());
let var_y = y.clone().rolling_var(rolling_options);
let mean_x = (x.clone() * non_null_mask.clone()).rolling_mean(rolling_options.clone());
let mean_y = (y.clone() * non_null_mask.clone()).rolling_mean(rolling_options.clone());
let var_x = (x.clone() * non_null_mask.clone()).rolling_var(rolling_options.clone());
let var_y = (y.clone() * non_null_mask.clone()).rolling_var(rolling_options);

let rolling_options_count = RollingOptionsFixedWindow {
window_size: options.window_size as usize,
Expand All @@ -110,9 +114,13 @@ pub fn rolling_cov(x: Expr, y: Expr, options: RollingCovOptions) -> Expr {
..Default::default()
};

let non_null_mask = when(x.clone().is_not_null().and(y.clone().is_not_null()))
.then(lit(1.0))
.otherwise(lit(Null {}));

let mean_x_y = (x.clone() * y.clone()).rolling_mean(rolling_options.clone());
let mean_x = x.clone().rolling_mean(rolling_options.clone());
let mean_y = y.clone().rolling_mean(rolling_options);
let mean_x = (x.clone() * non_null_mask.clone()).rolling_mean(rolling_options.clone());
let mean_y = (y.clone() * non_null_mask.clone()).rolling_mean(rolling_options);
let rolling_options_count = RollingOptionsFixedWindow {
window_size: options.window_size as usize,
min_periods: 0,
Expand Down
30 changes: 30 additions & 0 deletions py-polars/tests/unit/operations/rolling/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,36 @@ def test_rolling_cov_corr() -> None:
assert res["corr"][:2] == [None] * 2


def test_rolling_cov_corr_nulls() -> None:
df1 = pl.DataFrame(
{"a": [1.06, 1.07, 0.93, 0.78, 0.85], "lag_a": [1.0, 1.06, 1.07, 0.93, 0.78]}
)
df2 = pl.DataFrame(
{
"a": [1.0, 1.06, 1.07, 0.93, 0.78, 0.85],
"lag_a": [None, 1.0, 1.06, 1.07, 0.93, 0.78],
}
)

val_1 = df1.select(
pl.rolling_corr("a", "lag_a", window_size=10, min_periods=5, ddof=1).tail(1)
).item()
val_2 = df2.select(
pl.rolling_corr("a", "lag_a", window_size=10, min_periods=5, ddof=1).tail(1)
).item()

assert val_1 == val_2
Copy link
Member

Choose a reason for hiding this comment

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

Can you also test the actual value here?


val_1 = df1.select(
pl.rolling_cov("a", "lag_a", window_size=10, min_periods=5, ddof=1).tail(1)
).item()
val_2 = df2.select(
pl.rolling_cov("a", "lag_a", window_size=10, min_periods=5, ddof=1).tail(1)
).item()

assert val_1 == val_2
Copy link
Member

Choose a reason for hiding this comment

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

Can you also test the actual value here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the test as suggested and pushed. However, I also spent some time trying to put together a hypothesis test that would cross check these corr and cov functions against numpy. I could not get it to pass, and have an example frame which yields correlation > 1.0 :-/

df = pl.DataFrame(
{
"a": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0],
"b": [101.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.000061, 0.0],
}
)

df_corr = df.select(
pl.rolling_corr("a", "b", window_size=7, min_periods=5, ddof=1)
)

I don't have time to push more on this right now (or even this week maybe). But I will log a separate issue.



@pytest.mark.parametrize("time_unit", ["ms", "us", "ns"])
def test_rolling_empty_window_9406(time_unit: TimeUnit) -> None:
datecol = pl.Series(
Expand Down
Loading