Skip to content

Exponentially-weighted moving average

Source code

Description

Exponentially-weighted moving average

Usage

<Expr>$ewm_mean(
  com = NULL,
  span = NULL,
  half_life = NULL,
  alpha = NULL,
  adjust = TRUE,
  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
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_mean = pl$col("a")$ewm_mean(com = 1))
#> shape: (3, 2)
#> ┌─────┬──────────┐
#> │ a   ┆ ewm_mean │
#> │ --- ┆ ---      │
#> │ i32 ┆ f64      │
#> ╞═════╪══════════╡
#> │ 1   ┆ 1.0      │
#> │ 2   ┆ 1.666667 │
#> │ 3   ┆ 2.428571 │
#> └─────┴──────────┘