Skip to content

On a stream: the rolling window

← On a stream

A forecast that was good last month may be bad today: the accuracy of all the days remembers everything and reacts slowly. Rolling keeps a metric over the last window samples, or the last window_s seconds, and forgets the rest. A small window follows a change fast but jumps around; a large window is steady but slow. A robot sampled at 100 Hz uses the window in seconds, in the last section.

The running Rolling accuracy of a stock index forecast that is right 80% of the days and then 50%, for 25 window sizes from 10 to 200 days.

1. A first example: the forecast of a stock index

Ten days of an alarm for the crash days of a stock index, scored on all the days and on a window of five.

from dense_armor.core.evaluation.metrics import Accuracy, Rolling

crash = [0, 0, 1, 0, 0, 1, 0, 0, 0, 1]
alarm = [0, 1, 1, 0, 0, 1, 0, 1, 0, 0]
whole, recent = Accuracy(), Rolling(Accuracy(), window=5)
for truth, flag in zip(crash, alarm):
    whole.update(truth, flag)
    recent.update(truth, flag)
print(whole.get(), recent.get(), recent.count)
0.7 0.6 5

Count by hand. The alarm is right on days 1, 3, 4, 5, 6, 7 and 9, and wrong on days 2, 8 and 10: 7 of 10, so 0.7. The window holds days 6 to 10: right on 6, 7 and 9, wrong on 8 and 10, so 3 of 5 = 0.6.

A stock index forecast over 400 days: the accuracy of all the days and of windows of 100 and of 20 days, with the change at day 200.

On the same 400 days the accuracy of all the days reads 0.78 at day 150, 0.744 at day 250 and 0.635 at day 400. The window of 100 days reads 0.76, 0.69 and 0.46, and the window of 20 days reads 0.75, 0.65 and 0.55. The dashed line is the change at day 200: at day 400 the accuracy of all the days still says 0.635 although the forecast has been right on half the days for 200 days.

The picture follows 400 days of a forecast of a stock index that is right on 80% of the days for the first 200 days and on 50% after that, for 25 window sizes. Each point of the surface is the Rolling accuracy after that many days. At day 400 the window of 97 days reads 0.4536 and the window of 200 days reads 0.49, both close to the 0.5 of the last 200 days, while the window of 10 days reads 0.6. At day 250, 50 days after the change, the window of 97 days has already fallen to 0.6907 and the window of 200 days is still at 0.725.

class what it does
Rolling any metric that can undo an update, over the last samples or the last seconds

2. What the metric keeps

Rolling wraps another metric and keeps the pairs that are in the window. When a new pair comes in and the window is full, it asks the wrapped metric to undo the oldest pair (revert) and then adds the new one, so the wrapped metric always holds exactly the window. This is why any metric with revert can be rolled, and why R2Score and RollingAUC, which cannot undo an update, are refused.

With window_s the window holds the pairs with \(t_\text{now} - t < \text{window\_s}\) on the time stamps: at 100 Hz a window of 0.05 s holds 5 samples. A dropped sample does not stretch the window, because it slides on the stamps and not on the count.

Source: the docstrings of the module dense_armor.core.evaluation.metrics.rolling and of Rolling describe the mechanism and cite no paper.

3. The parameters

Parameter Meaning Default
metric the underlying metric; it must implement revert none
window window length in samples None
window_s window length in seconds, on the time stamps t of the pairs None

Exactly one of window and window_s must be given. Rolling raises ValueError if neither or both are given, if window < 1, if window_s <= 0, or if the metric cannot undo an update.

4. The same window on a robot joint

A robot changes over time; the accuracy of last week says little about now. At 100 Hz a window of 0.05 s holds 5 samples.

from dense_armor.core.evaluation.metrics import Accuracy, Rolling

m = Rolling(Accuracy(), window_s=0.05)
for i in range(20):
    m.update(1 if i < 17 else 0, 1, t=i * 0.01)
print(m.count, m.get())
5 0.4

At 100 Hz a window of 0.05 s holds 5 samples: the window keeps the samples with \(t_\text{now} - t < 0.05\). The last three predictions are wrong, so the accuracy over the window is 2/5. The window uses the timestamps, not the sample count, so a dropped sample does not stretch it. Any metric with revert can be rolled.

API reference

Rolling

Rolling(
    metric: Metric,
    window: int | None = None,
    window_s: float | None = None,
)

Bases: Metric

Rolling window over any metric with revert.

Parameters:

Name Type Description Default
metric Metric

the underlying metric. It must implement revert.

required
window int | None

window length in samples. Exactly one of window and window_s must be given.

None
window_s float | None

window length in seconds. The window slides on the timestamps as they come in.

None

Raises:

Type Description
ValueError

if neither or both of window and window_s are given, or if window < 1, or if window_s <= 0, or if the metric cannot undo an update (R2Score, RollingAUC).

Examples:

>>> from dense_armor.core.evaluation.metrics import Accuracy
>>> from dense_armor.core.evaluation.metrics.rolling import Rolling
>>> m = Rolling(Accuracy(), window=5)
>>> for a, b in [(1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (0, 1)]:
...     _ = m.update(a, b)
>>> m.get()
0.8
Source code in dense_armor/core/evaluation/metrics/rolling.py
def __init__(
    self,
    metric: Metric,
    window: int | None = None,
    window_s: float | None = None,
) -> None:
    super().__init__()
    if (window is None) == (window_s is None):
        raise ValueError(
            "exactly one of 'window' and 'window_s' must be given"
        )
    if window is not None and window < 1:
        raise ValueError(f"window must be >= 1, got {window}")
    if window_s is not None and window_s <= 0:
        raise ValueError(f"window_s must be > 0, got {window_s}")
    if not getattr(metric, "supports_revert", True):
        raise ValueError(
            f"{type(metric).__name__} cannot undo an update and "
            "cannot be wrapped in Rolling"
        )
    self.metric = metric
    self.window = window
    self.window_s = window_s
    self.role_ = metric.role_
    self.bigger_is_better = metric.bigger_is_better
    self.entries_: deque[tuple[float | None, Any, Any, float]] = deque()
    self._last_t_: float | None = None

update

update(
    y_true: Any,
    y_pred: Any,
    t: float | None = None,
    w: float = 1.0,
) -> Rolling

Add a sample and evict the entries that left the window.

Parameters:

Name Type Description Default
y_true Any

the observed label.

required
y_pred Any

the predicted label.

required
t float | None

timestamp in seconds; required for the window_s mode.

None
w float

sample weight.

1.0

Returns:

Type Description
Rolling

self.

Raises:

Type Description
ValueError

if t is not increasing, or if t is missing in the window_s mode.

Source code in dense_armor/core/evaluation/metrics/rolling.py
def update(
    self,
    y_true: Any,
    y_pred: Any,
    t: float | None = None,
    w: float = 1.0,
) -> "Rolling":
    """Add a sample and evict the entries that left the window.

    Args:
        y_true: the observed label.
        y_pred: the predicted label.
        t: timestamp in seconds; required for the ``window_s`` mode.
        w: sample weight.

    Returns:
        ``self``.

    Raises:
        ValueError: if ``t`` is not increasing, or if ``t`` is missing
            in the ``window_s`` mode.
    """
    if self.window_s is not None and t is None:
        raise ValueError("window_s mode needs the timestamp t")
    if t is not None:
        t = float(t)
        if self._last_t_ is not None and t <= self._last_t_:
            raise ValueError(
                f"non-increasing t: previous {self._last_t_}, got {t}"
            )
        self._last_t_ = t
    self.entries_.append((t, y_true, y_pred, w))
    self.metric.update(y_true, y_pred, t=t, w=w)
    self.n_ += 1
    if self.window is not None:
        self._evict_samples()
    elif self.window_s is not None and t is not None:
        self._evict_seconds(t)
    return self

revert

revert(y_true: Any, y_pred: Any, w: float = 1.0) -> Rolling

Undo one update, dropping the newest entry.

Parameters:

Name Type Description Default
y_true Any

the label that was passed to update.

required
y_pred Any

the prediction that was passed to update.

required
w float

the same weight used in update.

1.0

Returns:

Type Description
Rolling

self.

Source code in dense_armor/core/evaluation/metrics/rolling.py
def revert(self, y_true: Any, y_pred: Any, w: float = 1.0) -> "Rolling":
    """Undo one update, dropping the newest entry.

    Args:
        y_true: the label that was passed to ``update``.
        y_pred: the prediction that was passed to ``update``.
        w: the same weight used in ``update``.

    Returns:
        ``self``.
    """
    if self.entries_:
        self.entries_.pop()
    self.metric.revert(y_true, y_pred, w=w)
    self.n_ -= 1
    return self

merge

merge(other: Metric) -> Rolling

Combine two rolling windows.

Only the last window entries of the union are kept; the intermediate contributions to the underlying metric are not recovered, so this is a pragmatic merge for reporting, not the exact state of the union. After a merge the underlying metric holds both windows while only the last window entries can be reverted: a further update on the merged object does not give the rolling value, so the result is read with get and not updated.

Parameters:

Name Type Description Default
other Metric

another :class:Rolling with the same configuration.

required

Returns:

Type Description
Rolling

A new :class:Rolling with the combined window.

Raises:

Type Description
TypeError

if other is not a :class:Rolling.

ValueError

if the configuration does not match.

Source code in dense_armor/core/evaluation/metrics/rolling.py
def merge(self, other: Metric) -> "Rolling":
    """Combine two rolling windows.

    Only the last ``window`` entries of the union are kept; the
    intermediate contributions to the underlying metric are not
    recovered, so this is a pragmatic merge for reporting, not the
    exact state of the union. After a merge the underlying metric
    holds both windows while only the last ``window`` entries can be
    reverted: a further ``update`` on the merged object does not give
    the rolling value, so the result is read with ``get`` and not
    updated.

    Args:
        other: another :class:`Rolling` with the same configuration.

    Returns:
        A new :class:`Rolling` with the combined window.

    Raises:
        TypeError: if ``other`` is not a :class:`Rolling`.
        ValueError: if the configuration does not match.
    """
    if not isinstance(other, Rolling):
        raise TypeError(
            f"merge expects Rolling, got {type(other).__name__}"
        )
    if (self.window, self.window_s) != (other.window, other.window_s):
        raise ValueError("merge requires the same window configuration")
    m = Rolling(
        self.metric.merge(other.metric),
        window=self.window,
        window_s=self.window_s,
    )
    union = list(self.entries_) + list(other.entries_)
    if self.window is not None:
        union = union[-self.window:]
    m.entries_ = deque(union)
    m.n_ = self.n_ + other.n_
    m.n_missing_ = self.n_missing_ + other.n_missing_
    return m

Details

Choices

  • Base implementation: Rolling inherits from Metric and delegates to the wrapped metric.
  • The figures are drawn with the library by docs/assets/metrics/make_stream_figures.py. The surface uses one stream of 400 days, the same random draws for the 25 window sizes (10, 17, 25, ... up to 200 days, the whole numbers below a grid of 25 steps); the second figure uses the same 400 days.