rheon.polars.ops.ewm_var

rheon.polars.ops.ewm_var(input: str | pl.Expr, *, half_life: float | Duration, window_size: float | Duration | None = None, closed: Literal['right', 'left', 'both', 'none'] = 'right', min_samples: int = 1, partial: Literal['keep', 'null', 'drop'] | None = None, bias: bool = False) → pl.Expr[source]

The EW variance of a column about its time-weighted mean, over each row’s window.

Use it for the spread of a value as it stands on each row, looking back. For its square root, use ewm_std(). For a spread that one wild row does not move, use ewm_quantile() (its Notes).

Parameters

input

The values the window reads: a column name, or an element-wise expression of the row, such as pl.col("price") * pl.col("quantity") or increment(). It may hold no other window operator.

half_life

How fast a row’s weight fades with its distance in clock time from the row the window is read at: a row half_life away weighs half as much. A number of clock units on a numeric clock, or a duration ("10s", a timedelta, pl.duration(seconds=10)) on a temporal one. Required. float("inf") weighs every row of the window evenly, and for this operator needs window_size: with_windows(), or a spec that takes it in a formula target, refuses it without one.

window_size

The 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 past gap_cap or a session change), decayed.

closed

Which ends of the window are in: "right" (the default, as in Polars’ rolling_*_by), "left", "both" or "none". The module’s table of closed (rheon.polars.ops) gives each end, looking back.

min_samples

The fewest rows with a value a window must hold to give a value, as in Polars’ rolling functions. Default 1. An integer from 1 to 2**32 - 1.

partial

What 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". It needs window_size, since without one no window is cut short.

bias

False (the default) multiplies the variance by the correction for unequal weights, V1 ** 2 / (V1 ** 2 - V2), as Polars’ ewm_var does. True leaves it out.

Returns

A pl.Expr that rheon.polars.stream.with_windows() computes as a column, or that a spec takes as its target. Its value is null where the window holds fewer than min_samples rows with a value. It is null too where partial="null" meets a cut window, where a forward window is still open at the input’s end, and after a reset (the module’s partial table). At a row whose value is missing, it gives the window as it stands. While one row carries all the weight it is null under the default bias=False, as Polars’ first row is, and 0 under bias=True.

Raises

TypeError

When:

  • input is neither a column name nor a pl.Expr;

  • half_life or window_size is neither a number nor a duration (text such as "10s", a timedelta or pl.duration);

  • min_samples is not an integer (a WrongTypeError, also a ValueError);

  • bias is not True or False.

ValueError

When:

  • half_life is NaN or not above 0, or window_size is NaN, infinite or not above 0;

  • half_life or window_size is text that is not a duration, such as "abc";

  • closed or partial is a word it does not know, or partial is given without window_size;

  • min_samples is below 1 or above 2**32 - 1;

  • input holds 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.

rheon.polars.stream.with_windows() refuses the rest when it builds its query, such as a window looking ahead under group with 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, v=rp.ewm_var("x", half_life=1.0), clock="t", gap_cap=10)
polars = rows.select(pl.col("x").ewm_var(half_life=1.0, adjust=False))   # 1 a row
print(out["v"].round(4).to_list(), polars["x"].round(4).to_list())   # the same as Polars'
[None, 0.5, 1.1, 1.6905, 2.1588] [None, 0.5, 1.1, 1.6905, 2.1588]

See also

Notes

Each value weighs m_i, the decayed mass of its held interval inside the window, as in ewm_mean(). The variance is sum m_i (x_i - mu) ** 2 / V1, with mu = sum m_i x_i / V1 and V1 = sum m_i. Unless bias, it is multiplied by V1 ** 2 / (V1 ** 2 - V2), with V2 = sum m_i ** 2: the correction for unequal weights that Polars’ ewm_var applies to its own.

On a clock that steps by 1 a row, with half_life in rows, and an input with no nulls, this is Polars’ ewm_var(half_life=..., adjust=False, bias=...). adjust=False is the form ewm_mean() already mirrors, since ewm_mean_by holds a stretch’s first value from before it. So the first row weighs (1 - alpha) ** (n - 1), with alpha = 1 - lam, as adjust=False weighs it. A window that holds one value gives null under the default bias=False and 0 exactly, at any level, under bias=True. Polars’ ewm_var gives that row null too from py-polars 1.44, and 0 before it, so on an older Polars the example’s first value from Polars reads 0.0.

With a null in the input the two differ, from the first row after it, under either setting of Polars’ ignore_nulls. This operator follows ewm_mean(), which follows ewm_mean_by: a null row moves nothing, and the last value is held across the longer step to the next row with a value, so that row weighs the whole step. Polars’ ewm_var has no such mode.