On a stream: the rolling window¶
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.

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)
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.

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())
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 ¶
Bases: Metric
Rolling window over any metric with revert.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric
|
Metric
|
the underlying metric. It must implement |
required |
window
|
int | None
|
window length in samples. Exactly one of |
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 |
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
update ¶
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 |
None
|
w
|
float
|
sample weight. |
1.0
|
Returns:
| Type | Description |
|---|---|
Rolling
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Source code in dense_armor/core/evaluation/metrics/rolling.py
revert ¶
Undo one update, dropping the newest entry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
Any
|
the label that was passed to |
required |
y_pred
|
Any
|
the prediction that was passed to |
required |
w
|
float
|
the same weight used in |
1.0
|
Returns:
| Type | Description |
|---|---|
Rolling
|
|
Source code in dense_armor/core/evaluation/metrics/rolling.py
merge ¶
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: |
required |
Returns:
| Type | Description |
|---|---|
Rolling
|
A new :class: |
Raises:
| Type | Description |
|---|---|
TypeError
|
if |
ValueError
|
if the configuration does not match. |
Source code in dense_armor/core/evaluation/metrics/rolling.py
Details¶
Choices¶
- Base implementation:
Rollinginherits fromMetricand 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.