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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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: Metric

Mean quantile loss.

Source code in src/fev/metrics.py
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
class MQL(Metric):
    """Mean quantile loss."""

    needs_quantiles: bool = True

    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:
        if len(quantile_levels) == 0:
            raise ValueError(f"{self.__class__.__name__} cannot be computed without quantile_levels")
        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, 3))  # [D]
        return float(np.mean(per_dim))

MSE

Bases: Metric

Mean squared error.

Source code in src/fev/metrics.py
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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: Metric

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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
class SQL(Metric):
    """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.
    """

    needs_quantiles: bool = True

    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:
        ql = _quantile_loss(y_true=y_true, q_pred=q_pred, quantile_levels=quantile_levels)  # [N, H, D, Q]
        ql_avg_q = np.nanmean(ql, axis=3)  # [N, H, D]
        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_avg_q / seasonal_error[:, None, :]  # [N, H, D]
        return float(np.mean(self._safemean(scaled, axis=(0, 1))))

WAPE

Bases: Metric

Weighted absolute percentage error.

Source code in src/fev/metrics.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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: Metric

Weighted quantile loss.

Source code in src/fev/metrics.py
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
class WQL(Metric):
    """Weighted quantile loss."""

    needs_quantiles: bool = True

    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:
        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, 3))  # [D]
        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)
        return float(np.mean(per_dim))

Functions

get_metric(metric: MetricConfig) -> Metric

Get a metric class by name or configuration.

Source code in src/fev/metrics.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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}")