A Week of Water From One Evening

Time SeriesMedium+20 XP+20

A Week of Water From One Evening

A city water utility forecasts daily water demand, in megalitres. Its model is a linear rule on lag features, fitted earlier:

y^t=b+w1 yt−1+w2 yt−2+⋯+wp yt−p\hat y_t = b + w_1\,y_{t-1} + w_2\,y_{t-2} + \dots + w_p\,y_{t-p}y^​t​=b+w1​yt−1​+w2​yt−2​+⋯+wp​yt−p​

weights[0] is w1w_1w1​, the weight on lag 1 (the day just before the one being forecast), weights[1] is w2w_2w2​, and so on. bias is bbb.

The model was tested one day ahead and looked strong. Now the pumping team wants every day of the test block forecast on the evening of the cut-off, all at once. Before anyone trusts that, score the same model both ways on the same test days:

  • One step at a time. Each test day is forecast on the evening before it. By then every earlier test day has happened, so every lag is a real value.
  • Recursive. Every test day is forecast on the evening of the cut-off. The first test day uses real values only. After that, each forecast is added to the history as if it were real, and the next day is forecast from it. The guesses become the lags.

Task: write rollout_maes(history, future, weights, bias).

  • history holds the real daily values up to the cut-off, oldest first. You may assume len(history) >= len(weights).
  • future holds the real values of the test block, in order.
  • Score each strategy with the MAE (mean absolute error) over every day of future.

Return a tuple (one_step_mae, recursive_mae), each rounded to 4 decimal places.

Same model, same days. Any gap between the two numbers comes only from where the lags came from.

Examples

history = [40, 42]
future = [44, 43, 45]
weights = [0.5, 0.5]
bias = 0
returns (1.5, 2.75)
history = [118, 121, 125, 122]
future = [127, 133, 138, 141, 144]
weights = [0.5, 0.3, 0.1]
bias = 12.5
returns (6.74, 13.3464)

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run