polars_xdt.ewma_by_time#
- polars_xdt.ewma_by_time(values: IntoExpr, *, times: IntoExpr, half_life: timedelta) pl.Expr #
Calculate time-based exponentially weighted moving average.
Given observations \(x_1, x_2, \ldots, x_n\) at times \(t_1, t_2, \ldots, t_n\), the EWMA is calculated as
\[ \begin{align}\begin{aligned}y_0 &= x_0\\\alpha_i &= \exp(-\lambda(t_i - t_{i-1}))\\y_i &= \alpha_i x_i + (1 - \alpha_i) y_{i-1}; \quad i > 0\end{aligned}\end{align} \]where \(\lambda\) equals \(\ln(2) / \text{half_life}\).
- Parameters:
- values
Values to calculate EWMA for. Should be signed numeric.
- times
Times corresponding to values. Should be
DateTime
orDate
.- half_life
Unit over which observation decays to half its value.
- Returns:
- pl.Expr
Float64
Examples
>>> import polars as pl >>> import polars_xdt as xdt >>> from datetime import date, timedelta >>> df = pl.DataFrame( ... { ... "values": [0, 1, 2, None, 4], ... "times": [ ... date(2020, 1, 1), ... date(2020, 1, 3), ... date(2020, 1, 10), ... date(2020, 1, 15), ... date(2020, 1, 17), ... ], ... } ... ) >>> df.with_columns( ... ewma=xdt.ewma_by_time( ... "values", times="times", half_life=timedelta(days=4) ... ), ... ) shape: (5, 3) ┌────────┬────────────┬──────────┐ │ values ┆ times ┆ ewma │ │ --- ┆ --- ┆ --- │ │ i64 ┆ date ┆ f64 │ ╞════════╪════════════╪══════════╡ │ 0 ┆ 2020-01-01 ┆ 0.0 │ │ 1 ┆ 2020-01-03 ┆ 0.292893 │ │ 2 ┆ 2020-01-10 ┆ 1.492474 │ │ null ┆ 2020-01-15 ┆ null │ │ 4 ┆ 2020-01-17 ┆ 3.254508 │ └────────┴────────────┴──────────┘