Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions chap_core/assessment/flat_representations.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,15 @@ class DataDimension(StrEnum):
DataDimension.time_period: (str, None),
DataDimension.horizon_distance: (int, Check.ge(1)),
}


def origin_periods(forecasts: pd.DataFrame) -> pd.Series:
"""The last period each forecast row's run had data for: its target period minus its horizon."""
origins = {
(period, horizon): (TimePeriod.parse(period) - horizon * TimePeriod.parse(period).time_delta).id
for period, horizon in forecasts[["time_period", "horizon_distance"]].drop_duplicates().itertuples(index=False)
}
return pd.Series(
[origins[key] for key in zip(forecasts.time_period, forecasts.horizon_distance, strict=True)],
index=forecasts.index,
)
258 changes: 258 additions & 0 deletions chap_core/assessment/selection_bias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
"""Selection bias of picking the best model among several evaluated under one setup, estimated from the split points.

Picking the model with the best score over the splits flatters that model: part of its lead
is luck, and it is gone on new data. The bias is estimated with a bootstrap over the split
points, after BBC-CV (Tsamardinos, Greasidou and Borboudakis 2018, Machine Learning 107). No
model is run again and nothing is fitted. One draw takes as many split points as there are,
with replacement, and picks the model with the best comparison with the baseline over the
drawn ones, a split drawn twice counting twice. The pick is then read on the splits the draw
left out, which it was not picked on, next to the final model f (the one picked on all splits):

gap = value of f on the left-out splits - value of the draw's pick on the left-out splits

The gap is zero whenever the draw picks f, so a model that wins every draw has no selection
bias, however its score moves over time. The final model was picked with the left-out splits
included, so a draw that picks another model shows how much of f's lead on them was luck. The
estimated bias is the mean gap over the draws, and the estimated value on new data is the
final model's validation value minus it.

The paper's estimate is the mean of the picks' left-out values, and its bias the validation
value minus that. That is the same number when every split is left out equally often. Here
both values are read on the same left-out splits, so which splits a draw happens to leave out
does not move the estimate.

The paper draws single, independent observations. Split points are not independent, so two
things are added. Splits are drawn in blocks of consecutive ones (a circular block bootstrap),
so a model's good or bad stretch stays together. And with several horizon distances, the
forecasts of neighbouring split points share target periods, so a left-out split that shares a
month with a drawn one would not be independent of the pick: a left-out split is only scored
when it is more than (largest - smallest horizon distance) splits from every drawn one. That
gap assumes consecutive split points (stride 1). Without it the estimated bias comes out too
low. With it, few draws have a split left to score when the split points are few, and when
fewer than half of them have one, no estimate is given.

The estimate of one specification is rough, and it is too low when models do well or badly
together for longer stretches than a block.

Scores pool org units and, by default, every horizon distance, so the final model and its
validation value are the ones the leaderboard ranks on. Only forecasts every candidate and the
baseline made are scored, so all models are compared on the same forecasts.
"""

from __future__ import annotations

from dataclasses import dataclass
from functools import reduce
from typing import TYPE_CHECKING

import numpy as np

from chap_core.assessment.flat_representations import FlatForecasts, FlatObserved, origin_periods
from chap_core.assessment.metrics import get_metric
from chap_core.assessment.metrics.base import AggregationOp
from chap_core.time_period import TimePeriod

if TYPE_CHECKING:
import pandas as pd

from chap_core.assessment.evaluation import FlatEvaluationData
from chap_core.assessment.metrics.base import Metric

KEYS = ["location", "time_period", "horizon_distance"]


class SelectionBiasError(Exception):
"""Raised when the analysis cannot run: unknown metric or baseline, too few models or splits."""


def selection_bias_metric(metric_id: str) -> Metric:
"""The metric to estimate the selection bias on. Raises SelectionBiasError if it cannot be used for it."""
metric_cls = get_metric(metric_id)
if metric_cls is None:
raise SelectionBiasError(f"Unknown metric '{metric_id}'")
if metric_cls.spec.comparison_op is None or metric_cls.spec.aggregation_op != AggregationOp.MEAN:
raise SelectionBiasError(f"Metric '{metric_id}' needs a comparison operator and a mean aggregation")
return metric_cls()


@dataclass
class SplitScores:
"""Per-split sums of a mean-aggregated metric, on the forecasts every model made."""

model_sums: np.ndarray # (splits, models)
baseline_sums: np.ndarray # (splits,)
counts: np.ndarray # (splits,), forecasts scored


def _comparisons(metric: Metric, model_sums: np.ndarray, baseline_sum: float, count: float) -> np.ndarray:
return np.array([metric.compare(s / count, baseline_sum / count) for s in model_sums], dtype=float)


N_DRAWS = 2000
MIN_BLOCK_SIZE = 3
MIN_SCORED_SHARE = 0.5


def left_out_splits(drawn: np.ndarray, gap_splits: int = 0) -> np.ndarray:
"""The splits a draw is scored on: not drawn, and more than gap_splits from every drawn split.

drawn is a boolean array with the splits, in time order, on its last axis.
"""
near = drawn.copy()
for shift in range(1, gap_splits + 1):
near[..., shift:] |= drawn[..., :-shift]
near[..., :-shift] |= drawn[..., shift:]
return ~near


@dataclass
class SelectionBiasEstimate:
final_index: int
final_values: np.ndarray
estimated_bias: float
block_size: int
n_draws: int
n_draws_scored: int
n_draws_picked: np.ndarray # (models,), scored draws that pick each model


def estimate_selection_bias(
metric: Metric, scores: SplitScores, gap_splits: int = 0, n_draws: int = N_DRAWS, seed: int = 0
) -> SelectionBiasEstimate:
"""Estimate the final pick's bias with a block bootstrap over the split points.

Each draw picks the best model on the drawn splits and is scored on the splits it left out,
more than gap_splits from every drawn one. Blocks are max(3, gap_splits + 1) consecutive
splits. The seed is fixed, so the same scores give the same estimate.

Raises:
SelectionBiasError: fewer than half of the draws leave out a split to score.
"""
n, n_models = scores.model_sums.shape
block_size = max(MIN_BLOCK_SIZE, gap_splits + 1)
final_values = _comparisons(metric, scores.model_sums.sum(0), scores.baseline_sums.sum(), scores.counts.sum())
final_index = int(np.nanargmax(final_values))

rng = np.random.default_rng(seed)
starts = rng.integers(0, n, size=(n_draws, -(-n // block_size)))
splits = ((starts[:, :, None] + np.arange(block_size)) % n).reshape(n_draws, -1)[:, :n]
times_drawn = np.zeros((n_draws, n))
np.add.at(times_drawn, (np.arange(n_draws)[:, None], splits), 1)
left_out = left_out_splits(times_drawn > 0, gap_splits)
scored = left_out.any(1)
if scored.sum() < MIN_SCORED_SHARE * n_draws:
raise SelectionBiasError(
f"Too few split points ({n}): only {scored.mean():.0%} of the draws leave out a split point to score, "
"and at least half must"
+ (
f". A left-out split point is only scored when it is more than {gap_splits} splits from every "
"drawn one, so they share no target period: use more split points, or one horizon distance"
if gap_splits
else ""
)
)

gaps = [] # one per scored draw: zero when the draw picks the final model
picked = np.zeros(n_models, dtype=int)
for times, test in zip(times_drawn[scored], left_out[scored], strict=True):
drawn_values = _comparisons(
metric, times @ scores.model_sums, times @ scores.baseline_sums, times @ scores.counts
)
pick = int(np.nanargmax(drawn_values))
picked[pick] += 1
final_value, pick_value = _comparisons(
metric,
scores.model_sums[test][:, [final_index, pick]].sum(0),
scores.baseline_sums[test].sum(),
scores.counts[test].sum(),
)
gaps.append(0.0 if pick == final_index else final_value - pick_value)
return SelectionBiasEstimate(
final_index=final_index,
final_values=final_values,
estimated_bias=float(np.nanmean(gaps)),
block_size=block_size,
n_draws=n_draws,
n_draws_scored=int(scored.sum()),
n_draws_picked=picked,
)


def period_ids(periods: pd.Series) -> pd.Series:
"""Period strings in one format ('2022-08' and '202208' both become '202208'), so observations and forecasts match."""
ids = {period: TimePeriod.parse(str(period)).id for period in periods.unique()}
return periods.map(ids)


def split_scores(
metric: Metric,
observations: pd.DataFrame,
model_forecasts: list[pd.DataFrame],
horizon_distance: int | None = None,
) -> tuple[list[str], list[int], SplitScores]:
"""Per-split metric sums of each model (the last one the baseline) on the forecasts all of them made.

Scores every horizon distance, or only horizon_distance when it is given. Returns the split
periods, the horizon distances scored and the sums.
"""
observed = observations[observations.disease_cases.notna()]
observed = FlatObserved.validate(observed.assign(time_period=period_ids(observed.time_period)))
detailed = []
for i, forecasts in enumerate(model_forecasts):
if horizon_distance is not None:
forecasts = forecasts[forecasts.horizon_distance == horizon_distance]
forecasts = forecasts.assign(time_period=period_ids(forecasts.time_period))
if forecasts.empty:
raise SelectionBiasError(
"No forecasts" + (f" at horizon distance {horizon_distance}" if horizon_distance is not None else "")
)
scored = metric.get_detailed_metric(observed, FlatForecasts.validate(forecasts))
detailed.append(scored[[*KEYS, "metric"]].rename(columns={"metric": i}))
common = reduce(lambda left, right: left.merge(right, on=KEYS), detailed).dropna()
common["origin"] = origin_periods(common)
sums = common.groupby("origin")[list(range(len(model_forecasts)))].sum().sort_index()
counts = common.groupby("origin").size().reindex(sums.index).to_numpy(float)
values = sums.to_numpy(float)
horizons = sorted(int(h) for h in common.horizon_distance.unique())
return (
list(sums.index),
horizons,
SplitScores(model_sums=values[:, :-1], baseline_sums=values[:, -1], counts=counts),
)


@dataclass
class SelectionBiasAnalysis:
"""The estimate for a set of candidate models, with the split points and horizon distances it was made on."""

split_periods: list[str]
horizon_distances: list[int]
gap_splits: int
estimate: SelectionBiasEstimate


def analyse_selection_bias(
metric: Metric,
candidates: list[FlatEvaluationData],
baseline: FlatEvaluationData,
horizon_distance: int | None = None,
) -> SelectionBiasAnalysis:
"""Estimate the selection bias of the best of the candidate evaluations, compared with the baseline evaluation.

The evaluations must be of the same dataset under the same setup. Scores every horizon
distance unless horizon_distance picks one. The estimate's indices follow ``candidates``.

Raises:
SelectionBiasError: no forecasts to score, or too few split points for the gap between
drawn and left-out splits.
"""
periods, horizons, scores = split_scores(
metric, candidates[0].observations, [flat.forecasts for flat in [*candidates, baseline]], horizon_distance
)
gap_splits = horizons[-1] - horizons[0]
return SelectionBiasAnalysis(
split_periods=periods,
horizon_distances=horizons,
gap_splits=gap_splits,
estimate=estimate_selection_bias(metric, scores, gap_splits),
)
Loading
Loading