Skip to content

Compute exponentially-weighted moving variance

Description

Compute exponentially-weighted moving variance

Usage

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

Arguments

These dots are for future extensions and must be empty.
com Specify decay in terms of center of mass, *γ*, with

$\alpha = \frac{1}{1 + \gamma} \\ \forall \\ \gamma \geq 0$ .

span Specify decay in terms of span, *θ*, with

$\alpha = \frac{2}{\theta + 1} \\ \forall \\ \theta \geq 1$

half_life Specify decay in terms of half-life, *λ*, with

= 1 - { } ; ; \> 0

alpha Specify smoothing factor alpha directly, 0 \< .
adjust Divide by decaying adjustment factor in beginning periods to account for imbalance in relative weightings:
  • when TRUE (default), the EW function is calculated using weights *w**i* = (1 − *α*)*i*;
  • when FALSE, the EW function is calculated recursively by

    *y*0 = *x*0

    *y**t* = (1 − *α*)*y**t* − 1 + *α**x**t*

bias If FALSE (default), apply a correction to make the estimate statistically unbiased.
min_periods The number of values in the window that should be non-null before computing a result. If NULL (default), it will be set equal to window_size.
ignore_nulls Ignore missing values when calculating weights.
  • when FALSE (default), weights are based on absolute positions. For example, the weights of *x*0 and *x*2 used in calculating the final weighted average of (*x*0, null, *x*2) are (1 − *α*)2 and 1 if adjust = TRUE, and (1 − *α*)2 and *α* if adjust = FALSE.
  • when TRUE, 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, null, *x*2) are 1 − *α* and 1 if adjust = TRUE, and 1 − *α* and *α* if adjust = FALSE.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(a = 1:3)
df$select(pl$col("a")$ewm_var(com = 1, ignore_nulls = FALSE))
#> shape: (3, 1)
#> ┌──────────┐
#> │ a        │
#> │ ---      │
#> │ f64      │
#> ╞══════════╡
#> │ 0.0      │
#> │ 0.5      │
#> │ 0.928571 │
#> └──────────┘