Rolling Sample Variance

Computes the rolling sample variance over a numeric vector.

Usage

rolling_variance(x, window_size, min_periods = window_size, method = "stable")

Parameters

Parameter

Description

x

A numeric vector of type double.

window_size

Positive integer window length.

min_periods

Minimum number of non-NA observations required in a window to return a result. Defaults to window_size (pandas semantics). Positions with fewer non-NA values yield NA.

method

"stable" (default) uses the Welford online algorithm. "fast" uses a prefix-sum approach (faster, but susceptible to catastrophic cancellation when values are large and variance is small).

Returns

A numeric vector with rolling sample variance values. Entries are NA when fewer than min_periods non-NA observations are present in the window, and NaN when variance is undefined (fewer than two values).

Examples

x <- as.double(c(1, 2, 3, 4))
rolling_variance(x, 3L)