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

MAPE

Bases: Metric

Mean absolute percentage error.

Source code in src/fev/metrics.py
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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
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
218
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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))

WQL

Bases: QuantileMetric

Weighted quantile loss.

Source code in src/fev/metrics.py
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
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}")