[BUG] `EmpiricalCoverage` and `ConstraintViolation` compare `y_true` against another variable's interval for multivariate data with 2+ coverages; non-alphabetical variable names misalign `PinballLoss` too
sktime/performance_metrics/forecasting/probabilistic/_classes.py:691 (EmpiricalCoverage), same line at :913 (ConstraintViolation):
scores = np.unique(np.round(y_pred.columns.get_level_values(1), 7))
no_scores = len(scores)
vars = np.unique(y_pred.columns.get_level_values(0))
y_true_np = np.tile(y_true_np, no_scores)
truth_array = (y_true_np >= lower).astype(int) * (y_true_np <= upper).astype(
int
)_check_ys sorts the prediction columns at :337 (y_pred_inner.sort_index(axis=1, level=[0, 1], inplace=True)), so lower and upper are ordered (v0,c0), (v0,c1), (v1,c0), (v1,c1). The output columns are built the same way (pd.MultiIndex.from_product([vars, scores])). np.tile builds the truth as v0, v1, v0, v1. With at least 2 variables and 2 coverages, the off-diagonal cells compare one variable's truth against the other variable's interval. PinballLoss handles the same layout with np.repeat(y_true, axis=1, repeats=len(alphas)) at :579, which pairs the columns correctly.
There is a second trigger, which also reaches PinballLoss. y_true goes through check_array at :279, which drops the column names and keeps the user's column order, while y_pred is sorted by variable name at :337. If the columns of y_true are not in sorted order, each variable is scored against another variable's forecast, even with a single coverage or quantile.
Both triggers are reached from evaluate. sktime/forecasting/model_evaluation/_functions.py calls metric(y_test, y_pred) directly, with y_pred from predict_interval / predict_quantiles. Those methods build their columns with pd.MultiIndex.from_product([featnames, ...]) in the column order of y, and nothing reorders them before the metric.
Measured
Metric class code at HEAD 80ea2ef. The class definitions were extracted from _classes.py and run against numpy 2.3.1, pandas 2.3.1 and scikit-learn 1.7.0 (check_array). The scitype check and mtype conversion were identity, because the inputs are already in the inner pred_interval / pred_quantiles format. Every case has 4 rows with constant values. Truth: a = 0, b = 100 (and c = 200 in the 3-variable case).
| case | metric | result | expected |
|---|---|---|---|
2 vars x coverages 0.8/0.9, every interval contains its own truth (a: [-1,1], [-2,2]; b: [99,101], [98,102]) |
EmpiricalCoverage(multioutput="raw_values", score_average=False) |
[email protected]=1, [email protected]=0, [email protected]=0, [email protected]=1 (mean 0.5) |
all 1 |
| same | ConstraintViolation(...) |
0, 98, 99, 0 |
all 0 |
2 vars x 2 coverages, every interval misses its own truth (a's intervals around 100, b's around 0) |
EmpiricalCoverage() |
0.5 | 0.0 |
| 3 vars x coverages 0.7/0.8/0.9/0.99, every interval contains its own truth | EmpiricalCoverage() |
0.5 | 1.0 |
| control: 1 var x 2 coverages, containing | EmpiricalCoverage() / ConstraintViolation() |
1.0 / 0.0 | 1.0 / 0.0 |
control: 2 vars (a, b) x 1 coverage, containing |
EmpiricalCoverage() / ConstraintViolation() |
1.0 / 0.0 | 1.0 / 0.0 |
y_true columns ["sales"=100, "returns"=0], coverage 0.9, sales: [98,102], returns: [-2,2] |
EmpiricalCoverage(raw_values, score_average=False) / ConstraintViolation(...) |
0, 0 / 98, 98 |
1, 1 / 0, 0 |
y_true columns ["sales"=100, "returns"=0], 0.5 quantile equal to the truth |
PinballLoss(multioutput="raw_values") |
returns=50, sales=50 |
0, 0 |
control: same perfect forecast, columns ["a", "b"] |
PinballLoss(multioutput="raw_values") |
0, 0 |
0, 0 |
In rows 1 and 3, a forecaster whose intervals always contain the truth and one whose intervals never do both score 0.5 coverage.
Replacing np.tile(y_true_np, no_scores) with np.repeat(y_true_np, no_scores, axis=1) in both classes fixes rows 1 to 4 (1.0 everywhere / 0 everywhere / 0.0 everywhere / 1.0). The sales/returns rows stay wrong after that change, because they need y_true aligned by name.
Reproduction:
import pandas as pd
from sktime.performance_metrics.forecasting.probabilistic import (
ConstraintViolation, EmpiricalCoverage, PinballLoss,
)
n = 4
y_true = pd.DataFrame({"a": [0.0] * n, "b": [100.0] * n})
spec = {("a", 0.8): (-1, 1), ("a", 0.9): (-2, 2), ("b", 0.8): (99, 101), ("b", 0.9): (98, 102)}
cols = {}
for (v, c), (lo, hi) in spec.items():
cols[(v, c, "lower")] = [float(lo)] * n
cols[(v, c, "upper")] = [float(hi)] * n
y_pred = pd.DataFrame(cols)
print(EmpiricalCoverage(multioutput="raw_values", score_average=False)(y_true, y_pred))
print(ConstraintViolation(multioutput="raw_values", score_average=False)(y_true, y_pred))
y_true2 = pd.DataFrame({"sales": [100.0] * n, "returns": [0.0] * n})
q = pd.DataFrame({("sales", 0.5): [100.0] * n, ("returns", 0.5): [0.0] * n})
print(PinballLoss(multioutput="raw_values")(y_true2, q))Consequence
For any multivariate forecast evaluated at 2 or more coverages, EmpiricalCoverage and ConstraintViolation report values that do not describe the intervals: some cells are computed against the wrong series, and the averages mix correct and wrong cells. EmpiricalCoverage is tagged lower_is_better=False, and its docstring points to evaluate and ForecastingGridSearchCV, so benchmarking and tuning on it rank models by these values.
Whenever the columns of y are not in sorted order, all three metrics score each variable against a different variable's forecast. A perfect median forecast then gets a large pinball loss. Array-valued multioutput weights have the same problem: they are documented as following y_true's order but are applied to the per-variable scores in sorted order.
In probabilistic/tests/test_probabilistic_metrics.py, the sample_data fixture used by helper_check_output includes the 3 variables x 4 coverages shape, so the np.tile path runs in CI. Its columns are the integers 0, 1, 2, which sort in the same order as y_true, and the helper checks only the type and length of the output. test_multioutput_weighted uses var1/var2, which are already sorted, and a single interval, and it checks output types, lengths and the score index, not values. Neither trigger is covered at the value level.
Suggested fix
- At
:691and:913, usenp.repeat(y_true_np, no_scores, axis=1), which is the patternPinballLossalready uses at:579for the same sorted column layout. - In
_check_ys, wheny_trueis aDataFrame, reorder its columns by name to the sorted variable level ofy_pred_innerbeforecheck_array, and raise if the labels do not match instead of pairing by position. Array-valuedmultioutputweights need the same permutation. A numpyy_truehas no names and keeps positional pairing. - Add value-level regression tests next to
test_empirical_coverage_boundary_equality, the test added with the earlierEmpiricalCoveragefix in #8372. Use 2 or more variables, 2 or more coverages, non-alphabetical names, and intervals that contain every truth. Expected results: coverage 1.0, violation 0.0, and pinball 0 for a median forecast equal to the truth. Add one case with intervals that miss every truth, expected coverage 0.0.
Source: sktime/sktime