811. Report RMSE, MAE and MAPE for a forecast
Why it matters Demand forecasts, latency predictors and LLM cost models are all judged with these metrics.
Three numbers summarise most regression and forecasting dashboards. MAE is the average absolute miss, RMSE punishes big misses more, and MAPE expresses the miss as a percentage of the true value.
Write regression_errors(y_true, y_pred) returning a dict with:
- "mae": mean |y - p|
- "rmse": sqrt(mean (y - p) ** 2)
- "mape": 100 * mean |y - p| / |y| over the rows where y != 0 only (nan if every y is 0)
def regression_errors(y_true, y_pred)Example 1
Input: regression_errors([100.0, 200.0], [110.0, 180.0])
Output: {'mae': 15.0, 'rmse': 15.8114, 'mape': 10.0}
Example 2
Input: y_true = [0.0, 4.0], y_pred = [1.0, 2.0]
Output: {'mae': 1.5, 'rmse': 1.581139, 'mape': 50.0}
Example 3
Input: y_true = [0.0], y_pred = [3.0]
Output: {'mae': 3.0, 'rmse': 3.0, 'mape': float('nan')}
Constraints
- Python 3 with NumPy. Code runs in your browser.
- Your code must pass all 4 tests to be accepted.
Hint 1
Compute the error vector e = y - p once.
Hint 2
For MAPE, build a mask of rows where y is not zero before dividing.
Common mistakes
- Dividing by zero in MAPE when a true value is 0.
- Taking the square root before averaging in RMSE.
Understand the idea
MAE and RMSE are in the units of y; RMSE >= MAE, with a bigger gap when a few errors are large. MAPE is unit free but undefined where y = 0, so those rows are skipped.
y = [100, 200], p = [110, 180]. Errors 10 and 20: MAE 15, RMSE sqrt(250) = 15.81, MAPE = (10% + 10%) / 2 = 10%.
mae = mean|e|, rmse = sqrt(mean e^2), mape = 100 mean(|e| / |y|) where y != 0