Skip to content

Compute time-based exponentially weighted moving average

Description

Given observations x0, x1, …, xn − 1 at times t0, t1, …, tn − 1, the EWMA is calculated as

*y*0 = *x*0

$\alpha_i = 1 - \exp \left\\ \frac{ -\ln(2)(t_i-t\_{i-1}) } { \tau } \right\\$

*y**i* = *α**i**x**i* + (1 − *α**i*)*y**i* − 1;  *i* \> 0 where *τ* is the half_life. ## Usage

expr__ewm_mean_by(by, ..., half_life)
## Arguments
by Times to calculate average by. Should be DateTime, Date, UInt64, UInt32, Int64, or Int32 data type.
These dots are for future extensions and must be empty.
half_life Unit over which observation decays to half its value. Can be created either from a timedelta, or by using the following string language:
  • 1ns (1 nanosecond)
  • 1us (1 microsecond)
  • 1ms (1 millisecond)
  • 1s (1 second)
  • 1m (1 minute)
  • 1h (1 hour)
  • 1d (1 calendar day)
  • 1w (1 calendar week)
  • 1mo (1 calendar month)
  • 1q (1 calendar quarter)
  • 1y (1 calendar year)
Or combine them: “3d12h4m25s” \# 3 days, 12 hours, 4 minutes, and 25 seconds By "calendar day", we mean the corresponding time on the next day (which may not be 24 hours, due to daylight savings). Similarly for "calendar week", "calendar month", "calendar quarter", and "calendar year".
## Value A polars expression ## Examples
library("polars")

df <- pl$DataFrame(
  values = c(0, 1, 2, NA, 4),
  times = as.Date(
    c("2020-01-01", "2020-01-03", "2020-01-10", "2020-01-15", "2020-01-17")
  )
)
df$with_columns(
  result = pl$col("values")$ewm_mean_by("times", half_life = "4d")
)
#> shape: (5, 3) #> ┌────────┬────────────┬──────────┐ #> │ values ┆ times ┆ result │ #> │ --- ┆ --- ┆ --- │ #> │ f64 ┆ date ┆ f64 │ #> ╞════════╪════════════╪══════════╡ #> │ 0.0 ┆ 2020-01-01 ┆ 0.0 │ #> │ 1.0 ┆ 2020-01-03 ┆ 0.292893 │ #> │ 2.0 ┆ 2020-01-10 ┆ 1.492474 │ #> │ null ┆ 2020-01-15 ┆ null │ #> │ 4.0 ┆ 2020-01-17 ┆ 3.254508 │ #> └────────┴────────────┴──────────┘