Skip to content

Rolling correlation

Source code

Description

Calculates the rolling correlation between two columns

Usage

pl$rolling_corr(a, b, window_size, min_periods = NULL, ddof = 1)

Arguments

a One column name or Expr or anything convertible Into via pl$col().
b Another column name or Expr or anything convertible Into via pl$col().
window_size int The length of the window
min_periods NULL or int The number of values in the window that should be non-null before computing a result. If NULL, it will be set equal to window size.
ddof integer Delta Degrees of Freedom: the divisor used in the calculation is N - ddof, where N represents the number of elements. By default ddof is 1.

Value

Expr for the computed rolling correlation

Examples

library("polars")

lf = as_polars_lf(data.frame(a = c(1, 8, 3), b = c(4, 5, 2)))
lf$select(pl$rolling_corr("a", "b", window_size = 2))$collect()
#> shape: (3, 1)
#> ┌──────┐
#> │ a    │
#> │ ---  │
#> │ f64  │
#> ╞══════╡
#> │ null │
#> │ 1.0  │
#> │ 1.0  │
#> └──────┘