Skip to content

Exponentially-weighted moving variance

Source code

Description

Exponentially-weighted moving variance

Usage

<Expr>$ewm_var(
  com = NULL,
  span = NULL,
  half_life = NULL,
  alpha = NULL,
  adjust = TRUE,
  bias = FALSE,
  min_periods = 1L,
  ignore_nulls = TRUE
)

Arguments

com Specify decay in terms of center of mass, *γ*, with = ; ;
span Specify decay in terms of span, *θ*, with $= ; ; $
half_life Specify decay in terms of half-life, :math:, with $ = 1 - { } $ $ ; \> 0$
alpha Specify smoothing factor alpha directly, 0 \< *α* ≤ 1.
adjust Divide by decaying adjustment factor in beginning periods to account for imbalance in relative weightings:
  • When adjust=TRUE the EW function is calculatedusing weights $w_i = (1 - )^i $
  • When adjust=FALSE the EW function is calculated recursively by y_0 = x_0 \\ y_t = (1 - )y\_{t - 1} + x_t
bias If FALSE, the calculations are corrected for statistical bias.
min_periods Minimum number of observations in window required to have a value (otherwise result is null).
ignore_nulls Ignore missing values when calculating weights:
  • When TRUE (default), weights are based on relative positions. For example, the weights of *x*0 and *x*2 used in calculating the final weighted average of \[ *x*0, None, *x*2\] are 1 − *α* and 1 if adjust=TRUE, and 1 − *α* and *α* if adjust=FALSE.
  • When FALSE, weights are based on absolute positions. For example, the weights of :math:x_0 and :math:x_2 used in calculating the final weighted average of \[ *x*0, None, *x*2\\] are 1 − *α*)2 and 1 if adjust=TRUE, and (1 − *α*)2 and *α* if adjust=FALSE.

Value

Expr

Examples

library("polars")

pl$DataFrame(a = 1:3)$
  with_columns(ewm_var = pl$col("a")$ewm_var(com = 1))
#> shape: (3, 2)
#> ┌─────┬──────────┐
#> │ a   ┆ ewm_var  │
#> │ --- ┆ ---      │
#> │ i32 ┆ f64      │
#> ╞═════╪══════════╡
#> │ 1   ┆ 0.0      │
#> │ 2   ┆ 0.5      │
#> │ 3   ┆ 0.928571 │
#> └─────┴──────────┘