The Bar Before the Model

Time SeriesMedium+20 XP+20

The Bar Before the Model

A bike-hire kiosk logs its rentals once a day, and the numbers follow a strong weekly rhythm: busy weekends, quiet Tuesdays. Before anyone trains a forecaster, the team wants the bar it will have to beat: the errors of the two free forecasts on the same test days.

The series is split at a cut-off. series[:cutoff] is the known past, and series[cutoff:] is the test block. Bikes are ordered in advance, so every forecast for the test block is written once, on the evening of the cut-off. No value from the test block is known when any forecast is made, not even the first test day's value when forecasting the second.

  • Naive: every test day is forecast as the last known value.
  • Seasonal naive, with a seasonal period of period steps: every test day is forecast as the most recent known value from the same point in the cycle, that is, a value a whole number of periods earlier that lies before the cut-off.

Score each forecast with the MAE (mean absolute error) over the test block.

Task: write baseline_errors(series, cutoff, period) returning a tuple (naive_mae, seasonal_mae), each rounded to 4 decimal places. You may assume period <= cutoff < len(series).

Whichever of the two MAEs is smaller is the bar. A model that can't get under it isn't worth deploying: a rule with nothing to train already does that well.

Examples

series = [42, 38, 45, 51, 60, 72, 66, 44, 40, 47, 50, 63, 75, 69, 46, 41, 49, 53, 61, 78, 70]
cutoff = 14
period = 7
returns (15.0, 2.0)
series = [120, 95, 101, 98, 130, 160, 124, 99, 104, 97, 135, 158, 128, 102, 100, 95, 133, 166, 121, 98, 107, 101]
cutoff = 12
period = 6
returns (44.5, 3.4)

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