Skip to content

On a stream: choosing a model

← On a stream

Which of several models is better today? best_of runs the models on the same stream with the same predict-then-learn protocol, keeps one metric per model, and tells which one is ahead; it can change its answer as the stream goes on. A robot choosing between two classifiers is in the last section.

The running accuracy of 25 forecasters that say up when a noisy signal passes a threshold c, for thresholds from -1.2 to 1.2.

1. A first example: two forecasts of a stock index

Ten days of the direction of a stock index, 1 for up and 0 for down. Two models: one says the last direction it knows, the other says always up.

from dense_armor.core.evaluation.evaluate import best_of
from dense_armor.core.evaluation.metrics import Accuracy


class LastValue:
    def __init__(self):
        self.last = 0

    def learn_one(self, x, y, t=None):
        self.last = y
        return self

    def predict_one(self, x, t=None):
        return self.last


class AlwaysUp:
    def learn_one(self, x, y, t=None):
        return self

    def predict_one(self, x, t=None):
        return 1


stock_up = [1, 1, 0, 1, 0, 0, 1, 1, 1, 1]
stream = [({}, y) for y in stock_up]
sel = best_of([LastValue(), AlwaysUp()], Accuracy, stream=stream)
print([round(v, 3) for v in sel.scores()], sel.current_best())
[0.5, 0.7] 1

Count by hand. The model that says the last direction is right on 5 days out of 10, 0.5. The model that always says up is right on the 7 up days, 0.7. So current_best() is the index 1, the second model. The second argument of best_of is the metric class: each model gets its own fresh metric.

Three forecasters on the same stock index over 250 days: the running accuracy of the thresholds -1, 0 and 1.

On 250 days the forecaster with the threshold −1.0 reads 0.69 at day 100 and 0.684 at day 250, the one with the threshold 0.0 reads 0.81 and 0.816, and the one with the threshold 1.0 reads 0.71 and 0.728: the threshold 0.0 is ahead at both days.

The picture follows 300 days of a stock index. A noisy signal (the true return plus a noise of 0.7) is available each day, and 25 forecasters say "up" when the signal is above a threshold c between −1.2 and 1.2. Each point of the surface is the running accuracy of one forecaster after that many days. At day 300 the best forecaster is the one with the threshold 0.0 and it reads 0.813, while the threshold −1.2 reads 0.643 and the threshold 1.2 reads 0.683.

class or function what it does
best_of runs several models on a stream and returns the selection
OnlineModelSelection tracks several models on a stream and exposes the current best

2. What the selection keeps

OnlineModelSelection keeps the models and one fresh metric for each, built by the metric_factory that it is given, and feeds all of them the same stream with the same protocol as progressive_val_score. scores() returns the current score of each model, current_best() the index of the best one (it takes into account whether a bigger or a smaller score is better), and best_model() the model itself.

Source: the docstring of the module dense_armor.core.evaluation.evaluate.model_selection says that several models run on the same stream with the same prequential protocol, with one metric per model, and cites Ksieniewicz, P., Zyblewski, P. (2020). Stream-learn: open-source Python library for difficult data stream batch analysis. arXiv:2001.11077. The docstrings of best_of and of OnlineModelSelection cite no paper.

3. The parameters

Parameter Meaning Default
models the estimators none
metric_factory a callable returning a fresh metric: one metric per model none
stream (best_of) a stream on which the selection is run immediately None
delay label delay, as in progressive_val_score None
every (OnlineModelSelection) if given, records the trace every every samples None

OnlineModelSelection raises ValueError if models is empty.

4. The same choice on a robot joint

"Fast in either direction" is not a straight cut on \(\dot q\), so a linear softmax model cannot learn it, while the Gaussian model, which learns a different spread per class, can.

import numpy as np
from dense_armor.core.evaluation.evaluate import best_of
from dense_armor.core.online.learn.online_classifiers import OnlineGaussianNB, OnlineSoftmaxRegression
from dense_armor.core.evaluation.metrics import Accuracy

rng = np.random.default_rng(0)
s = [({"qd": v}, int(abs(v) > 0.5)) for v in rng.normal(0, 1, 2000)]
sel = best_of([OnlineGaussianNB(), OnlineSoftmaxRegression()], Accuracy, stream=s)
print([round(v, 3) for v in sel.scores()], sel.current_best())
[0.941, 0.621] 0

"Fast in either direction" is not a straight cut on \(\dot q\), so a linear softmax model cannot learn it (0.621), while the Gaussian model, which learns a different spread per class, can (0.941). current_best() returns the index 0 and best_model() the model itself. The second argument is the metric class: each model gets its own fresh metric.

API reference

best_of

best_of(
    models: Sequence[Any],
    metric_factory: Callable[[], Metric],
    stream: Iterable[Any] | None = None,
    delay: float | None = None,
) -> OnlineModelSelection

Return an :class:OnlineModelSelection for models.

If stream is given, the selection is run immediately on it.

Parameters:

Name Type Description Default
models Sequence[Any]

the estimators.

required
metric_factory Callable[[], Metric]

a callable returning a fresh metric.

required
stream Iterable[Any] | None

optional stream to run the selection on.

None
delay float | None

label delay for the stream.

None

Returns:

Name Type Description
The OnlineModelSelection

class:OnlineModelSelection.

Examples:

>>> from dense_armor.core.evaluation.evaluate import best_of
>>> from dense_armor.core.evaluation.metrics import Accuracy
>>> class LastValue:
...     def __init__(self):
...         self.last = 0
...     def learn_one(self, x, y, t=None):
...         self.last = y
...         return self
...     def predict_one(self, x, t=None):
...         return self.last
>>> class AlwaysUp:
...     def learn_one(self, x, y, t=None):
...         return self
...     def predict_one(self, x, t=None):
...         return 1
>>> stock_up = [1, 1, 0, 1, 0, 0, 1, 1, 1, 1]
>>> stream = [({}, y) for y in stock_up]
>>> sel = best_of([LastValue(), AlwaysUp()], Accuracy, stream=stream)
>>> [round(v, 3) for v in sel.scores()], sel.current_best()
([0.5, 0.7], 1)
Source code in dense_armor/core/evaluation/evaluate/model_selection.py
def best_of(
    models: Sequence[Any],
    metric_factory: Callable[[], Metric],
    stream: Iterable[Any] | None = None,
    delay: float | None = None,
) -> OnlineModelSelection:
    """Return an :class:`OnlineModelSelection` for ``models``.

    If ``stream`` is given, the selection is run immediately on it.

    Args:
        models: the estimators.
        metric_factory: a callable returning a fresh metric.
        stream: optional stream to run the selection on.
        delay: label delay for the stream.

    Returns:
        The :class:`OnlineModelSelection`.

    Examples:
        >>> from dense_armor.core.evaluation.evaluate import best_of
        >>> from dense_armor.core.evaluation.metrics import Accuracy
        >>> class LastValue:
        ...     def __init__(self):
        ...         self.last = 0
        ...     def learn_one(self, x, y, t=None):
        ...         self.last = y
        ...         return self
        ...     def predict_one(self, x, t=None):
        ...         return self.last
        >>> class AlwaysUp:
        ...     def learn_one(self, x, y, t=None):
        ...         return self
        ...     def predict_one(self, x, t=None):
        ...         return 1
        >>> stock_up = [1, 1, 0, 1, 0, 0, 1, 1, 1, 1]
        >>> stream = [({}, y) for y in stock_up]
        >>> sel = best_of([LastValue(), AlwaysUp()], Accuracy, stream=stream)
        >>> [round(v, 3) for v in sel.scores()], sel.current_best()
        ([0.5, 0.7], 1)
    """
    sel = OnlineModelSelection(models, metric_factory, delay=delay)
    if stream is not None:
        sel.run(stream)
    return sel

OnlineModelSelection

OnlineModelSelection(
    models: Sequence[Any],
    metric_factory: Callable[[], Metric],
    delay: float | None = None,
    every: int | None = None,
)

Track several models on a stream and expose the current best.

Parameters:

Name Type Description Default
models Sequence[Any]

the estimators.

required
metric_factory Callable[[], Metric]

a callable returning a fresh metric. The selection uses one metric per model.

required
delay float | None

label delay forwarded to :func:progressive_val_score.

None
every int | None

if given, records the trace every every samples.

None

Raises:

Type Description
ValueError

if models is empty.

Examples:

>>> from dense_armor.core.evaluation.evaluate import OnlineModelSelection
>>> from dense_armor.core.evaluation.metrics import Accuracy
>>> class LastValue:
...     def __init__(self):
...         self.last = 0
...     def learn_one(self, x, y, t=None):
...         self.last = y
...         return self
...     def predict_one(self, x, t=None):
...         return self.last
>>> class AlwaysUp:
...     def learn_one(self, x, y, t=None):
...         return self
...     def predict_one(self, x, t=None):
...         return 1
>>> stock_up = [1, 1, 0, 1, 0, 0, 1, 1, 1, 1]
>>> stream = [({}, y) for y in stock_up]
>>> sel = OnlineModelSelection([LastValue(), AlwaysUp()], Accuracy)
>>> _ = sel.run(stream)
>>> [round(v, 3) for v in sel.scores()], sel.current_best()
([0.5, 0.7], 1)
Source code in dense_armor/core/evaluation/evaluate/model_selection.py
def __init__(
    self,
    models: Sequence[Any],
    metric_factory: Callable[[], Metric],
    delay: float | None = None,
    every: int | None = None,
) -> None:
    if not models:
        raise ValueError("models must be non-empty")
    self.models = list(models)
    self.metric_factory = metric_factory
    self.metrics: list[Metric] = [metric_factory() for _ in models]
    self.delay = delay
    self.every = every

run

run(stream: Iterable[Any]) -> list[Metric]

Run every model on the same stream.

Parameters:

Name Type Description Default
stream Iterable[Any]

the iterable of (x, y) pairs. It is materialised once so every model sees the same samples in the same order.

required

Returns:

Type Description
list[Metric]

The list of metrics, one per model.

Source code in dense_armor/core/evaluation/evaluate/model_selection.py
def run(
    self, stream: Iterable[Any]
) -> list[Metric]:
    """Run every model on the same stream.

    Args:
        stream: the iterable of ``(x, y)`` pairs. It is materialised
            once so every model sees the same samples in the same
            order.

    Returns:
        The list of metrics, one per model.
    """
    from dense_armor.core.evaluation.evaluate.prequential import (
        progressive_val_score,
    )

    samples = list(stream)
    for model, metric in zip(self.models, self.metrics):
        progressive_val_score(
            samples, model, metric, delay=self.delay
        )
    return self.metrics

scores

scores() -> list[float]

Current metric value per model.

Source code in dense_armor/core/evaluation/evaluate/model_selection.py
def scores(self) -> list[float]:
    """Current metric value per model."""
    return [m.get() for m in self.metrics]

current_best

current_best() -> int

Index of the model with the best metric value.

bigger_is_better of the metric is respected.

Raises:

Type Description
ValueError

if there are several models and none of them has been scored yet (the choice would be arbitrary).

Source code in dense_armor/core/evaluation/evaluate/model_selection.py
def current_best(self) -> int:
    """Index of the model with the best metric value.

    ``bigger_is_better`` of the metric is respected.

    Raises:
        ValueError: if there are several models and none of them has
            been scored yet (the choice would be arbitrary).
    """
    if len(self.metrics) > 1 and all(m.n_ == 0 for m in self.metrics):
        raise ValueError("the models have not been run yet")
    sign = 1.0 if self.metrics[0].bigger_is_better else -1.0
    scores = [sign * m.get() for m in self.metrics]
    return max(range(len(scores)), key=scores.__getitem__)

best_model

best_model() -> Any

The model with the best metric value.

Source code in dense_armor/core/evaluation/evaluate/model_selection.py
def best_model(self) -> Any:
    """The model with the best metric value."""
    return self.models[self.current_best()]

Details

Choices

  • Base implementation: best_of builds an OnlineModelSelection and, if a stream is given, runs it immediately.
  • The figures are drawn with the library by docs/assets/metrics/make_stream_figures.py. The surface uses one stream of 300 days (a true return and a signal that is the return plus a noise of 0.7), scored with progressive_val_score(..., every=1) for the 25 thresholds; the second figure uses the first 250 of those days.