polars.Expr.ewm_sum_by#

Expr.ewm_sum_by(by: str_ | IntoExpr, *, half_life: str_ | timedelta) Expr[source]#

Compute time-based exponentially-weighted moving sum.

Warning

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Given observations \(x_0, x_1, \ldots, x_{n-1}\) at times \(t_0, t_1, \ldots, t_{n-1}\), the EWMS is calculated as

\[ \begin{align}\begin{aligned}y_0 &= x_0\\\lambda_i &= \exp \left\{ \frac{ -\ln(2)(t_i-t_{i-1}) } { \tau } \right\}\\y_i &= x_i + \lambda_i y_{i-1}; \quad i > 0\end{aligned}\end{align} \]

where \(\tau\) is the half_life.

engine:In Memory
Parameters:
by

Column to use as reference for the time decay.

half_life

Half-life of the exponential decay.

Examples

>>> df = pl.DataFrame(
...     {
...         "values": [1, 2, 3, 4, 5],
...         "times": [0, 1, 2, 5, 6],
...     }
... )
>>> df.select(
...     pl.col("values").ewm_sum_by("times", half_life="1i"),
... )
shape: (5, 1)
┌──────────┐
│ values   │
│ ---      │
│ f64      │
╞══════════╡
│ 1.0      │
│ 2.5      │
│ 4.25     │
│ 4.53125  │
│ 7.265625 │
└──────────┘