polars.Series.ewm_sum_by#

Series.ewm_sum_by(by: IntoExpr, *, half_life: str_ | timedelta) Series[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.

Parameters:
by

Times to calculate the sum by. Should be DateTime, Date, UInt64, UInt32, Int64, or Int32 data type.

half_life

Unit over which observation decays to half its value.

Examples

>>> df = pl.DataFrame(
...     {
...         "values": [1, 2, 3, 4, 5],
...         "times": [0, 1, 2, 5, 6],
...     }
... )
>>> df["values"].ewm_sum_by(df["times"], half_life="1i")
shape: (5,)
Series: 'values' [f64]
[
    1.0
    2.5
    4.25
    4.53125
    7.265625
]