rheon.polars.ops.ewm_sum¶
- rheon.polars.ops.ewm_sum(input: str | pl.Expr, *, half_life: float | Duration, window_size: float | Duration | None = None, closed: Literal['right', 'left', 'both', 'none'] = 'right', min_samples: int = 0, partial: Literal['keep', 'null', 'drop'] | None = None, reverse: bool = False) pl.Expr[source]¶
The EW sum of a column over each row’s window.
Use it for a decayed total, such as recent notional, and for a weighted mean as a ratio of two sums. For a mean weighted by time, use
ewm_mean(), and for a total per unit time,ewm_rate().Parameters
inputThe values the window reads: a column name, or an element-wise expression of the row, such as
pl.col("price") * pl.col("quantity")orincrement(). It may hold no other window operator.half_lifeHow fast a row’s weight fades with its distance in clock time from the row the window is read at: a row
half_lifeaway weighs half as much. A number of clock units on a numeric clock, or a duration ("10s", atimedelta,pl.duration(seconds=10)) on a temporal one. Required.float("inf")weighs every row of the window evenly.window_sizeThe span of clock time a window covers, in the same units as
half_life. The default,None, covers every row since the last clock break (a gap pastgap_capor a session change), decayed. Required underreverse=True, since the rows after a row have no end without it.closedWhich ends of the window are in:
"right"(the default, as in Polars’rolling_*_by),"left","both"or"none". The module’s table ofclosed(rheon.polars.ops) gives each end, looking back and ahead.min_samplesThe fewest rows with a value a window must hold to give a value, as in Polars’ rolling functions. Default 0, as in Polars’
rolling_sum_by: a window with no row with a value sums to 0.0. With 1 or more, such a window is null. An integer from 0 to2**32 - 1.partialWhat a window cut short gives: one that a gap past
gap_cap, a session change or the stream’s start cuts before its full span."keep"gives the value over what it saw,"null"gives null, and"drop"removes the row from the output.None(the default) means"keep"looking back and"null"looking ahead. It needswindow_size, since without one no window is cut short.reverseTruereads the rows after each row, at mostwindow_sizelater, in place of the rows at or before it, as Polars’cum_sum(reverse=True)runs from the end. As a spec’s target, a window looking ahead is known only once it has closed, so the bank learns it under anembargoof at leastwindow_size. DefaultFalse.
Returns
A
pl.Exprthatrheon.polars.stream.with_windows()computes as a column, or that a spec takes as its target. Its value is 0.0 where the window holds no row with a value, under the defaultmin_samples=0, and null where it holds fewer thanmin_samplesrows with a value. It is null too wherepartial="null"meets a cut window, where a forward window is still open at the input’s end, and after a reset (the module’spartialtable). At a row whose value is missing, it gives the window as it stands.Raises
TypeErrorWhen:
inputis neither a column name nor apl.Expr;half_lifeorwindow_sizeis neither a number nor a duration (text such as"10s", atimedeltaorpl.duration);min_samplesis not an integer (aWrongTypeError, also aValueError);reverseis notTrueorFalse.
ValueErrorWhen:
half_lifeis NaN or not above 0, orwindow_sizeis NaN, infinite or not above 0;half_lifeorwindow_sizeis text that is not a duration, such as"abc";closedorpartialis a word it does not know, orpartialis given withoutwindow_size;min_samplesis below 1 or above2**32 - 1;inputholds another window operator, a part that is not element-wise, or a column whose name starts with@po:, the prefix the operators’ own columns carry.
RefusedValueErrorreverse=Truewithoutwindow_size. It is both aValueErrorand aTypeError.
rheon.polars.stream.with_windows()refuses the rest when it builds its query, such as a window looking ahead undergroupwith no clock column.Examples
rows = pl.DataFrame({"t": [0.0, 1.0, 2.0, 3.0, 4.0], "x": [1.0, 2.0, 3.0, 4.0, 5.0]}) out = rp.stream.with_windows(rows, s=rp.ewm_sum("x", half_life=1.0), clock="t", gap_cap=10) polars = rows.select(pl.col("x").ewm_sum_by(pl.col("t").cast(pl.Int64), half_life="1i")) print(out["s"].round(4).to_list(), polars["x"].round(4).to_list()) # the same as Polars'
[1.0, 2.5, 4.25, 6.125, 8.0625] [1.0, 2.5, 4.25, 6.125, 8.0625]
See also
ewm_rate(), the sum per unit of clock time.ewm_mean(), the mean weighted by time.rheon.polars.stream.with_windows(), which computes the operators over a stream.Make a weighted mean, such as a VWAP, the forward VWAP built from two sums.
Notes
Looking back it is
sum lam ** (tau_t - t_j) * x_jover the rows at or before row t, less thanwindow_sizeolder, withlam = 2 ** (-1 / half_life)andtau_tthe row’s time. Looking ahead it issum lam ** (t_j - tau_t) * x_jover the rows after it, at mostwindow_sizelater. On distinct stamps it is Polars’ewm_sum_by, each value counted once at its own time. At a repeated stamp every row carries the stamp’s total, whereewm_sum_by’s is a running sum. Polars addedewm_sum_byin py-polars 1.43.0, so the example’s comparison with it needs that version or later.