Ice Cream Forecast Check

EasymathAlso known as Mean Squared Error

A beach kiosk uses a small model to predict each day's ice-cream sales. At the end of the week the owner has two lists, predicted and actual, and wants one number for how far off the model was. A miss of 40 cones means melted stock or angry customers, so big misses should count much more than small ones. Training a model means making a number like this smaller, step by step.

Your task

Write forecastError(predicted, actual). For each day, the miss is predicted[i] - actual[i]. Square every miss, then return the average of those squares.

The two lists always have the same length. If both are empty, return 0.

forecastError(predicted: float[], actual: float[]) → float Numbers are accepted within 1e-6.

Examples

Example 1
Input
predicted = [120, 80, 150]
actual = [100, 90, 150]
Output
166.66666666666666
Why

Misses 20, -10 and 0 square to 400, 100 and 0. The average is 500 / 3 = 166.666…

Example 2
Input
predicted = [50, 70]
actual = [60, 60]
Output
100
Why

The misses are -10 and +10. Averaging them first gives 0, as if the model were perfect. Squaring first gives 100 and 100, so the answer is 100.

Constraints

  • 0 ≤ predicted.length = actual.length ≤ 10,000
  • -1,000,000 ≤ predicted[i], actual[i] ≤ 1,000,000
  • Answers within 0.000001 of the expected value (or relatively that close) are accepted.
Start with brute force

There is no slow way: you must look at every day once, and one loop does that. The traps are wrong formulas. Averaging the misses and then squaring lets them cancel. Absolute values give a different measure (mean absolute error) that punishes big misses less. Forgetting to divide gives a total that grows with the number of days.

Hints

0 of 4 shown

Walkthrough and solution

Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.

Ctrl+Enter runs

Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.

Write code