Compute symmetric MAPE

Why it matters Capacity forecasts for GPU demand are often reported in sMAPE.

MAPE blows up near zero. sMAPE divides by the average size of the actual and forecast instead.

Write smape(y, yhat). Return the mean over points of 2 |y - yhat| / (|y| + |yhat|) times 100, counting a point as 0 when both are 0.

Signaturedef smape(y, yhat)

Example

smape([100, 0], [110, 0]) is (2 * 10 / 210 + 0) / 2 * 100, about 4.7619.

Understand the idea

MAPE divides the error by the actual value, so it explodes when the actual is near zero. sMAPE divides by the average size of the actual and the forecast instead, which keeps it bounded between 0 and 200 percent. Worked example: y = [50, 20], yhat = [40, 20]. Point 1: 2 * |50 - 40| / (50 + 40) = 20 / 90 = about 0.2222. Point 2: the error is 0, so it scores 0. The mean is 0.1111, times 100 gives about 11.11. Rule: for each point compute 2 * |y - yhat| / (|y| + |yhat|), count it as 0 when both y and yhat are 0 (to avoid 0/0), take the mean over points and multiply by 100. The factor 2 is what turns the sum in the denominator into an average; dropping it halves every error. Used in: forecasting competitions, demand and traffic forecasting, and comparing models across series with very different scales.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks all 3.

  • Sample 1: examplesmape([100, 0], [110, 0])
  • Sample 2: all exactsmape([1, 2, 3], [1, 2, 3])
  • Test 3: mixedsmape([10, -5, 3], [8, -4, 0])