Skip to content

Rolling standard deviation

Source code

Description

Compute the rolling (= moving) standard deviation over the values in this array. A window of length window_size will traverse the array. The values that fill this window will (optionally) be multiplied with the weights given by the weight vector.

Usage

<Expr>$rolling_std(
  window_size,
  weights = NULL,
  min_periods = NULL,
  center = FALSE,
  by = NULL,
  closed = NULL,
  warn_if_unsorted = TRUE
)

Arguments

window_size The length of the window. Can be a fixed integer size, or a dynamic temporal size indicated by the following string language:
  • 1ns (1 nanosecond)
  • 1us (1 microsecond)
  • 1ms (1 millisecond)
  • 1s (1 second)
  • 1m (1 minute)
  • 1h (1 hour)
  • 1d (1 day)
  • 1w (1 week)
  • 1mo (1 calendar month)
  • 1y (1 calendar year)
  • 1i (1 index count) If the dynamic string language is used, the by and closed arguments must also be set.
weights An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.
min_periods 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.
center Set the labels at the center of the window
by If the window_size is temporal for instance “5h” or “3s”, you must set the column that will be used to determine the windows. This column must be of DataType Date or DateTime.
closed Defines whether the temporal window interval is closed or not. Only applicable if by is not NULL (in which case, its possible values are “left”, “right” (default), “both”, “none”).
warn_if_unsorted Warn if data is not known to be sorted by by column (if passed).

Details

If you want to compute multiple aggregation statistics over the same dynamic window, consider using $rolling() this method can cache the window size computation.

Value

Expr

Examples

library(polars)

pl$DataFrame(a = c(1, 3, 2, 4, 5, 6))$
  with_columns(roll_std = pl$col("a")$rolling_std(window_size = 2))
#> shape: (6, 2)
#> ┌─────┬──────────┐
#> │ a   ┆ roll_std │
#> │ --- ┆ ---      │
#> │ f64 ┆ f64      │
#> ╞═════╪══════════╡
#> │ 1.0 ┆ null     │
#> │ 3.0 ┆ 1.414214 │
#> │ 2.0 ┆ 0.707107 │
#> │ 4.0 ┆ 1.414214 │
#> │ 5.0 ┆ 0.707107 │
#> │ 6.0 ┆ 0.707107 │
#> └─────┴──────────┘