Skip to content

Metrics

For the precise mathematical definitions of metrics, see AutoGluon documentation.

Note: Currently, multivariate metrics are computed by first computing the univariate metric on each target column and then averaging the results, similar to the following:

metric_value = np.mean(
    [metric.compute_metric(test_data[col], predictions[col])
    for col in task.target_columns]
)
For some metrics like WAPE, this leads to results that are different from first concatenating all target columns into a single array and computing the metric on it.

metrics

Classes

MAE

Bases: Metric

Mean absolute error.

Source code in src/fev/metrics.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
class MAE(Metric):
    """Mean absolute error."""

    def compute(
        self,
        *,
        y_true: np.ndarray,
        y_pred: np.ndarray,
        y_past: np.ndarray,
        y_past_lengths: np.ndarray,
        q_pred: np.ndarray,
        seasonality: int,
        quantile_levels: list[float],
    ) -> float:
        per_dim = np.nanmean(np.abs(y_true - y_pred), axis=(0, 1))  # [D]
        return float(np.mean(per_dim))

MAEB

Bases: Metric

Mean absolute error plus an absolute mean bias penalty. Equals MAE when the forecast is unbiased.

Source code in src/fev/metrics.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
class MAEB(Metric):
    """Mean absolute error plus an absolute mean bias penalty. Equals MAE when the forecast is unbiased."""

    def compute(
        self,
        *,
        y_true: np.ndarray,
        y_pred: np.ndarray,
        y_past: np.ndarray,
        y_past_lengths: np.ndarray,
        q_pred: np.ndarray,
        seasonality: int,
        quantile_levels: list[float],
    ) -> float:
        abs_err_per_dim = np.nanmean(np.abs(y_true - y_pred), axis=(0, 1))  # [D]
        bias_per_dim = np.nanmean(y_pred - y_true, axis=(0, 1))  # [D]
        per_dim = abs_err_per_dim + np.abs(bias_per_dim)
        return float(np.mean(per_dim))

MAPE

Bases: Metric

Mean absolute percentage error.

Source code in src/fev/metrics.py
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
class MAPE(Metric):
    """Mean absolute percentage error."""

    def compute(
        self,
        *,
        y_true: np.ndarray,
        y_pred: np.ndarray,
        y_past: np.ndarray,
        y_past_lengths: np.ndarray,
        q_pred: np.ndarray,
        seasonality: int,
        quantile_levels: list[float],
    ) -> float:
        ratio = np.abs(y_true - y_pred) / np.abs(y_true)  # [N, H, D]
        return float(np.mean(self._safemean(ratio, axis=(0, 1))))

MASE

Bases: Metric

Mean absolute scaled error.

Warning: Items with undefined in-sample seasonal error (e.g., history shorter than seasonality, all-NaN history, or zero seasonal error) are excluded from aggregation.

Source code in src/fev/metrics.py
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
class MASE(Metric):
    """Mean absolute scaled error.

    Warning:
        Items with undefined in-sample seasonal error (e.g., history shorter than `seasonality`,
        all-NaN history, or zero seasonal error) are excluded from aggregation.
    """

    def __init__(self, epsilon: float = 0.0) -> None:
        self.epsilon = epsilon

    def compute(
        self,
        *,
        y_true: np.ndarray,
        y_pred: np.ndarray,
        y_past: np.ndarray,
        y_past_lengths: np.ndarray,
        q_pred: np.ndarray,
        seasonality: int,
        quantile_levels: list[float],
    ) -> float:
        seasonal_error = _abs_seasonal_error_per_item(
            y_past=y_past, y_past_lengths=y_past_lengths, seasonality=seasonality
        )  # [N, D]
        seasonal_error = np.clip(seasonal_error, self.epsilon, None)
        scaled = np.abs(y_true - y_pred) / seasonal_error[:, None, :]  # [N, H, D]
        return float(np.mean(self._safemean(scaled, axis=(0, 1))))

MQL

Bases: QuantileMetric

Mean quantile loss.

Source code in src/fev/metrics.py
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
class MQL(QuantileMetric):
    """Mean quantile loss."""

    def _per_quantile_level(
        self,
        *,
        y_true: np.ndarray,
        y_pred: np.ndarray,
        y_past: np.ndarray,
        y_past_lengths: np.ndarray,
        q_pred: np.ndarray,
        seasonality: int,
        quantile_levels: list[float],
    ) -> np.ndarray:
        ql = _quantile_loss(y_true=y_true, q_pred=q_pred, quantile_levels=quantile_levels)  # [N, H, D, Q]
        per_dim = np.nanmean(ql, axis=(0, 1))  # [D, Q]
        return np.mean(per_dim, axis=0)  # [Q]

MSE

Bases: Metric

Mean squared error.

Source code in src/fev/metrics.py
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
class MSE(Metric):
    """Mean squared error."""

    def compute(
        self,
        *,
        y_true: np.ndarray,
        y_pred: np.ndarray,
        y_past: np.ndarray,
        y_past_lengths: np.ndarray,
        q_pred: np.ndarray,
        seasonality: int,
        quantile_levels: list[float],
    ) -> float:
        per_dim = np.nanmean((y_true - y_pred) ** 2, axis=(0, 1))  # [D]
        return float(np.mean(per_dim))

RMSE

Bases: Metric

Root mean squared error.

Source code in src/fev/metrics.py
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
class RMSE(Metric):
    """Root mean squared error."""

    def compute(
        self,
        *,
        y_true: np.ndarray,
        y_pred: np.ndarray,
        y_past: np.ndarray,
        y_past_lengths: np.ndarray,
        q_pred: np.ndarray,
        seasonality: int,
        quantile_levels: list[float],
    ) -> float:
        per_dim = np.sqrt(np.nanmean((y_true - y_pred) ** 2, axis=(0, 1)))  # [D]
        return float(np.mean(per_dim))

RMSLE

Bases: Metric

Root mean squared logarithmic error.

Source code in src/fev/metrics.py
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
class RMSLE(Metric):
    """Root mean squared logarithmic error."""

    def compute(
        self,
        *,
        y_true: np.ndarray,
        y_pred: np.ndarray,
        y_past: np.ndarray,
        y_past_lengths: np.ndarray,
        q_pred: np.ndarray,
        seasonality: int,
        quantile_levels: list[float],
    ) -> float:
        per_dim = np.sqrt(np.nanmean((np.log1p(y_true) - np.log1p(y_pred)) ** 2, axis=(0, 1)))  # [D]
        return float(np.mean(per_dim))

RMSSE

Bases: Metric

Root mean squared scaled error.

Warning: Items with undefined in-sample seasonal error (e.g., history shorter than seasonality, all-NaN history, or zero seasonal error) are excluded from aggregation.

Source code in src/fev/metrics.py
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
class RMSSE(Metric):
    """Root mean squared scaled error.

    Warning:
        Items with undefined in-sample seasonal error (e.g., history shorter than `seasonality`,
        all-NaN history, or zero seasonal error) are excluded from aggregation.
    """

    def __init__(self, epsilon: float = 0.0) -> None:
        self.epsilon = epsilon

    def compute(
        self,
        *,
        y_true: np.ndarray,
        y_pred: np.ndarray,
        y_past: np.ndarray,
        y_past_lengths: np.ndarray,
        q_pred: np.ndarray,
        seasonality: int,
        quantile_levels: list[float],
    ) -> float:
        seasonal_error = _squared_seasonal_error_per_item(
            y_past=y_past, y_past_lengths=y_past_lengths, seasonality=seasonality
        )  # [N, D]
        seasonal_error = np.clip(seasonal_error, self.epsilon, None)
        scaled = (y_true - y_pred) ** 2 / seasonal_error[:, None, :]  # [N, H, D]
        return float(np.mean(np.sqrt(self._safemean(scaled, axis=(0, 1)))))

SMAPE

Bases: Metric

Symmetric mean absolute percentage error.

Source code in src/fev/metrics.py
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
class SMAPE(Metric):
    """Symmetric mean absolute percentage error."""

    def compute(
        self,
        *,
        y_true: np.ndarray,
        y_pred: np.ndarray,
        y_past: np.ndarray,
        y_past_lengths: np.ndarray,
        q_pred: np.ndarray,
        seasonality: int,
        quantile_levels: list[float],
    ) -> float:
        val = 2 * np.abs(y_true - y_pred) / (np.abs(y_true) + np.abs(y_pred))  # [N, H, D]
        return float(np.mean(self._safemean(val, axis=(0, 1))))

SQL

Bases: QuantileMetric

Scaled quantile loss.

Warning: Items with undefined in-sample seasonal error (e.g., history shorter than seasonality, all-NaN history, or zero seasonal error) are excluded from aggregation.

Source code in src/fev/metrics.py
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
class SQL(QuantileMetric):
    """Scaled quantile loss.

    Warning:
        Items with undefined in-sample seasonal error (e.g., history shorter than `seasonality`,
        all-NaN history, or zero seasonal error) are excluded from aggregation.
    """

    def __init__(self, epsilon: float = 0.0) -> None:
        self.epsilon = epsilon

    def _per_quantile_level(
        self,
        *,
        y_true: np.ndarray,
        y_pred: np.ndarray,
        y_past: np.ndarray,
        y_past_lengths: np.ndarray,
        q_pred: np.ndarray,
        seasonality: int,
        quantile_levels: list[float],
    ) -> np.ndarray:
        ql = _quantile_loss(y_true=y_true, q_pred=q_pred, quantile_levels=quantile_levels)  # [N, H, D, Q]
        seasonal_error = _abs_seasonal_error_per_item(
            y_past=y_past, y_past_lengths=y_past_lengths, seasonality=seasonality
        )  # [N, D]
        seasonal_error = np.clip(seasonal_error, self.epsilon, None)
        scaled = ql / seasonal_error[:, None, :, None]  # [N, H, D, Q]
        per_dim = self._safemean(scaled, axis=(0, 1))  # [D, Q]
        return np.mean(per_dim, axis=0)  # [Q]

WAPE

Bases: Metric

Weighted absolute percentage error.

Source code in src/fev/metrics.py
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
class WAPE(Metric):
    """Weighted absolute percentage error."""

    def __init__(self, epsilon: float = 0.0) -> None:
        self.epsilon = epsilon

    def compute(
        self,
        *,
        y_true: np.ndarray,
        y_pred: np.ndarray,
        y_past: np.ndarray,
        y_past_lengths: np.ndarray,
        q_pred: np.ndarray,
        seasonality: int,
        quantile_levels: list[float],
    ) -> float:
        abs_err_per_dim = np.nanmean(np.abs(y_true - y_pred), axis=(0, 1))  # [D]
        abs_true_per_dim = np.nanmean(np.abs(y_true), axis=(0, 1))  # [D]
        per_dim = abs_err_per_dim / np.maximum(abs_true_per_dim, self.epsilon)
        return float(np.mean(per_dim))

WAPEB

Bases: Metric

Weighted absolute percentage error plus an absolute bias penalty (scale-free MAEB; VN1 challenge metric).

Equals WAPE when the forecast is unbiased.

Source code in src/fev/metrics.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
class WAPEB(Metric):
    """Weighted absolute percentage error plus an absolute bias penalty (scale-free MAEB; VN1 challenge metric).

    Equals WAPE when the forecast is unbiased.
    """

    def __init__(self, epsilon: float = 0.0) -> None:
        self.epsilon = epsilon

    def compute(
        self,
        *,
        y_true: np.ndarray,
        y_pred: np.ndarray,
        y_past: np.ndarray,
        y_past_lengths: np.ndarray,
        q_pred: np.ndarray,
        seasonality: int,
        quantile_levels: list[float],
    ) -> float:
        abs_err_per_dim = np.nanmean(np.abs(y_true - y_pred), axis=(0, 1))  # [D]
        bias_per_dim = np.nanmean(y_pred - y_true, axis=(0, 1))  # [D]
        abs_true_per_dim = np.nanmean(np.abs(y_true), axis=(0, 1))  # [D]
        per_dim = (abs_err_per_dim + np.abs(bias_per_dim)) / np.maximum(abs_true_per_dim, self.epsilon)
        return float(np.mean(per_dim))

WQL

Bases: QuantileMetric

Weighted quantile loss.

Source code in src/fev/metrics.py
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
class WQL(QuantileMetric):
    """Weighted quantile loss."""

    def __init__(self, epsilon: float = 0.0) -> None:
        self.epsilon = epsilon

    def _per_quantile_level(
        self,
        *,
        y_true: np.ndarray,
        y_pred: np.ndarray,
        y_past: np.ndarray,
        y_past_lengths: np.ndarray,
        q_pred: np.ndarray,
        seasonality: int,
        quantile_levels: list[float],
    ) -> np.ndarray:
        ql = _quantile_loss(y_true=y_true, q_pred=q_pred, quantile_levels=quantile_levels)  # [N, H, D, Q]
        ql_per_dim = np.nanmean(ql, axis=(0, 1))  # [D, Q]
        abs_true_per_dim = np.nanmean(np.abs(y_true), axis=(0, 1))  # [D]
        per_dim = ql_per_dim / np.maximum(abs_true_per_dim, self.epsilon)[:, None]  # [D, Q]
        return np.mean(per_dim, axis=0)  # [Q]

Functions:

get_metric(metric: MetricConfig) -> Metric

Get a metric class by name or configuration.

Source code in src/fev/metrics.py
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def get_metric(metric: MetricConfig) -> Metric:
    """Get a metric class by name or configuration."""
    metric_name = metric if isinstance(metric, str) else metric["name"]
    try:
        metric_type = AVAILABLE_METRICS[metric_name.upper()]
    except KeyError:
        raise ValueError(
            f"Evaluation metric '{metric_name}' is not available. Available metrics: {sorted(AVAILABLE_METRICS)}"
        )

    if isinstance(metric, str):
        return metric_type()
    elif isinstance(metric, dict):
        return metric_type(**{k: v for k, v in metric.items() if k != "name"})
    else:
        raise ValueError(f"Invalid metric configuration: {metric}")