Time series and temporal evaluation
What is time-series forecasting?
A time series is an ordered set of observations indexed by time, such as daily demand, hourly temperature or a sampled waveform. Forecasting estimates future values using information available when the forecast is issued. A horizon is how far ahead to predict; the forecast origin is the last known time. Trend is longer-term movement, seasonality is a repeating pattern at a known period, and irregular variation is the remaining movement. Missing or irregular timestamps must be handled explicitly; this lesson assumes evenly spaced days and complete numeric observations.
Start with simple baselines
Six fictional daily counts are [10, 20, 11, 21, 12, 22]. Hold out the final two; the first four are available at the origin. A naive forecast repeats the last known value (21). A seasonal naive forecast with period two repeats the final observed two-day cycle [11, 21]. Period two is chosen as a teaching assumption, not discovered from the test set. A fixed-origin forecast must not look at day five to predict day six.
def seasonal_forecast(history, period, horizon): if type(period) is not int or not 1 <= period <= len(history): raise ValueError("period must fit observed history") if type(horizon) is not int or horizon < 0: raise ValueError("nonnegative integer horizon required") cycle = history[-period:] return [cycle[i % period] for i in range(horizon)]
history = [10, 20, 11, 21]print(seasonal_forecast(history, 1, 2))print(seasonal_forecast(history, 2, 2))print(seasonal_forecast(history, 2, 5))The naive predictions are [21, 21], the seasonal predictions [11, 21], and five seasonal steps are [11, 21, 11, 21, 11]. Against [12, 22], the naive MAE is (9+1)/2 = 5 and seasonal MAE is (1+1)/2 = 1. These two test observations are far too few to prove superiority; compare across earlier validation origins and reserve a final test interval.
Evaluate without future leakage
Train on past observations and validate on later observations. Random row splitting can let future patterns leak into earlier forecasts. Lag features use prior known observations; a centered rolling mean or a same-day target included in inputs is not available at forecast time. Fit preprocessing on each training window only. Use a gap when feature or target availability requires it. A one-step rolling forecast can update its history after each actual arrives; a fixed-origin multi-step forecast cannot. This distinction changes the task, not just the implementation.
Rolling-origin backtesting
At each origin, predict the next horizon values without reading them, then compare with the actuals. Move to the next origin and use only observations now assumed available. The code calculates pooled MAE over all origin/horizon pairs, with equal weight per pair. Horizons can overlap; repeated targets are intentional because they represent different forecasts. Use the same origins for all competing methods. The minimum history includes at least one full cycle. Invalid configurations raise ValueError.
def backtest_mae(series, period, horizon, min_history): if type(horizon) is not int or horizon < 1: raise ValueError("positive horizon required") if type(period) is not int or type(min_history) is not int or not 1 <= period <= min_history <= len(series) - horizon: raise ValueError("no valid forecast origins") errors = [] for origin in range(min_history, len(series) - horizon + 1): cycle = series[origin-period:origin] predicted = [cycle[i % period] for i in range(horizon)] actual = series[origin:origin+horizon] errors.extend(abs(p-a) for p, a in zip(predicted, actual)) return sum(errors) / len(errors)
series = [10, 20, 11, 21, 12, 22]print(backtest_mae(series, 1, 2, 4))print(backtest_mae(series, 2, 2, 4))print(backtest_mae(series, 2, 1, 2))The outputs are 5.0, 1.0 and 1.0. With minimum history four and horizon two, only one origin exists; with history two and horizon one, four origins exist. A larger dataset is needed for stable evaluation. In production, account for late data and availability, compare error by horizon and time regime, and evaluate prediction intervals for both coverage and width. Point predictions are not guarantees. Forecasts of future demand need not match realized sales when stockouts occur.
Practice and recap
Given [3, 7, 4, 8, 5, 9], hold out the last two. Compare naive [8, 8] to seasonal [4, 8]. MAE values are 2 and 1 respectively. Explain why selecting a period using the final two days would contaminate evaluation. Keep a fixed test origin for the final report.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Time series and temporal evaluation quiz
Apply the lesson to small synthetic inputs.
With history [10,20,11,21], period two and horizon three, what is the seasonal forecast?
Which split best approximates future daily-demand prediction with a fixed test interval?
For a fixed-origin two-day forecast issued today, which information may be used to predict day two?
Predictions [11,21] and actuals [12,22] give what MAE?
Why can the same actual observation appear in several rolling-origin forecast error pairs?