A small shop places its order every night for the next day’s demand. A model that has seen the next day’s sales, even through an innocent-looking average, can score very well in a test and still be useless at the moment the order is placed. A forecast therefore needs a stated point in time where the usable information stops and a stated distance ahead. This lesson defines both, builds lag features that respect them, and sets up the naive and seasonal-naive forecasts that every forecasting model should be compared with.
Key terms
Time series
Definition A time series is a sequence of observations of one quantity indexed by time, on this page at equally spaced steps, in which the order carries information. Neighboring observations are usually dependent, so the values of a time series are not interchangeable the way independent examples are. Forecasting predicts values at times after the last available observation. Interpolation estimates a value at a time between observed ones and may use observations on both sides, which a forecast never can.
In plain words Shuffling a time series destroys part of what it records. A random split of its rows puts later days into training and earlier days into evaluation, so the model learns from days after the ones it is graded on, which never happens when it is used. A time-ordered split trains on an earlier period and evaluates on a later one. When training rows are built from overlapping stretches of the series, nearby rows share values, so they are not independent examples either.
Example A constructed series records the demand of a small shop, in orders per day, for 12 days. The values are invented to keep the arithmetic transparent and come from no real shop. The table lists them three days per row.
| Days | First day | Second day | Third day |
|---|---|---|---|
| 1 to 3 | 20 | 24 | 18 |
| 4 to 6 | 22 | 26 | 20 |
| 7 to 9 | 24 | 28 | 22 |
| 10 to 12 | 26 | 30 | 24 |
Predicting day 10 from days 1 to 9 is a forecasting task. Estimating a lost day-10 value from the observed days 9 and 11 would be interpolation, a different question that uses more information.
Forecast origin and forecast horizon
Definition The forecast origin is the latest time whose information may be used when a forecast is made, and the forecast horizon is the number of steps from the origin to the target time. The forecast of made at origin is written . It may use only information available at time , which includes , values known in advance such as the calendar, and external inputs in the version that had actually arrived by time . On this page the forecast error is the actual value minus the forecast,
so a positive error means that the forecast was too low.
In plain words The origin fixes what the forecaster knows, and the horizon fixes how far ahead the forecast reaches. Every input needs an availability time as well as an event time, because a value can describe day 9 and still arrive on day 11, or be revised later into a cleaner version that nobody had at the origin. A weather forecast issued tonight may be used for tomorrow, while tomorrow’s actual weather may not. Accuracy should be reported for each horizon, since reaching farther ahead is usually harder.
Example The shop orders at the end of day 9 for day 10, so the origin is and the horizon is . A forecast of day 12 made at the same moment has horizon 3 and may still use only days 1 to 9. A prediction for day 10 made after day 10’s demand was recorded answers a different question, and scoring it as a forecast overstates what could be done at the origin.
Lag feature
Definition For a whole number , the lag- feature of a target time is the earlier value . A forecasting table puts the lag features of each target time in one row with the target as its label, and a row exists only when all its lags fall inside the series. For a forecast made at origin with horizon , the lag- value of the target is , which is observed at the origin exactly when . Window features follow the same rule. A trailing mean of the values is available at origin , while a centered mean around a time near the origin needs values after the origin.
In plain words A lag feature looks back a fixed number of steps from the day being predicted. The target sits in the same row as its features so that a model can learn from it and be scored against it, never as an input. Shifting a column the wrong way by one row quietly places each day’s own value, or a later one, among its features, so check the first and last contributing times of every feature instead of trusting a name such as moving average. For horizons longer than the shortest lag, a recursive strategy feeds earlier forecasts in as inputs, and a direct strategy fits a separate model for each horizon using only lags with . The two strategies make different errors, and a one-step score says little about the accuracy of either one at longer horizons.
Example For a one-day-ahead forecast of day 10, the lag-1 feature is day 9’s 22 orders, the lag-3 feature is day 7’s 24 orders, and the target is day 10’s 26 orders. The first rows with a lag-3 value are below, and days 1 to 3 have no such row, because their lag-3 values would lie before day 1.
| Target day | Lag 1 | Lag 3 | Target |
|---|---|---|---|
| 4 | 18 | 20 | 22 |
| 5 | 22 | 24 | 26 |
| 6 | 26 | 18 | 20 |
| 7 | 20 | 22 | 24 |
| 8 | 24 | 26 | 28 |
| 9 | 28 | 20 | 22 |
| 10 | 22 | 24 | 26 |
At origin 9 the trailing three-day mean is orders, built from days 7 to 9. A centered three-day mean for day 10 is , which uses day 10 itself and day 11. Here it even equals the target, and in general it hands the model part of the answer.
Seasonality
Definition A time series shows seasonality with period , a whole number of steps, when part of its variation repeats every steps, so that an observation is related to the observations steps earlier at the same position in the cycle. The period comes from the calendar or the process, for example for daily data with a weekly pattern or for monthly data with a yearly one. Seasonality differs from a trend, a lasting change in level, and the two often occur together.
In plain words Seasonality means that the same position in the cycle is a useful comparison, so a Monday is compared with earlier Mondays rather than with yesterday. The period should be fixed from knowledge of the process or chosen on development data, never by looking at the evaluation period.
Example The shop series was constructed with a three-day cycle, so . In every cycle the second day is the busiest and the third the quietest, and every value from day 4 to day 12 is exactly 2 orders above the value three days earlier,
The repeating shape is seasonality, and the rise of 2 orders per cycle is a trend. A weekly period would be wrong for this series, although weekly patterns are common in daily sales, because the period has to match the mechanism behind the data at hand.
Naive forecast
Definition The naive forecast, also called the last-value or persistence forecast, repeats the latest observation for every horizon ,
It has no fitted parameters and needs only one observed value. It serves as a baseline, a simple forecast that a proposed method should beat on the same origins, horizons and metric before its extra complexity is worth having.
In plain words The naive forecast assumes that the current level persists. It suits a series whose level wanders unpredictably, and it does poorly on a series with a strong seasonal pattern, because it always answers with the most recent position in that pattern.
Example Make one-day-ahead forecasts at origins 9, 10 and 11, adding each day once it is observed. The naive forecasts for days 10, 11 and 12 are 22, 26 and 30 orders, and the actual values are 26, 30 and 24. The errors are 4, 4 and orders, so the mean absolute error is
orders. The forecast for day 12 copies the peak of day 11 into a day that the cycle makes quiet.
Seasonal-naive forecast
Definition For a series with seasonal period and an origin , the seasonal-naive forecast repeats the latest observed value from the same position in the cycle,
where is the smallest whole number that is at least . For this is , the lag- value of the target, and forecasts more than steps ahead repeat the last observed cycle again. It has no fitted parameters.
In plain words The seasonal-naive forecast assumes that the pattern repeats and the level stays where it was one cycle ago. It is the natural baseline for a strongly seasonal series, and a model that cannot beat it has shown no skill beyond repeating the last cycle. Like the naive forecast, it does not follow a trend.
Example With , the one-day-ahead seasonal-naive forecasts at origins 9, 10 and 11 copy days 7, 8 and 9, giving 24, 28 and 22 orders against actual values 26, 30 and 24. Every error is , so the mean absolute error is 2 orders, well below the naive forecast’s . The errors also share their sign, so the forecast is always too low, by exactly the rise of 2 orders per cycle. A regression on the lag-3 value can learn that rise. Fitting
by least squares to the target days 4 to 9, whose lag-3 values come from days 1 to 6, gives and with zero training residuals, since each of these targets is its lag-3 value plus 2. The lag-3 values are not all equal, so the design matrix with an intercept column has full rank and the fit is unique. Kept fixed and applied to days 10 to 12, the fitted rule predicts 26, 30 and 24, all exactly right, a mean absolute error of 0. That happens only because the constructed series follows the same noiseless rule after day 9, and it does not show that real demand can be forecast without error.
Check yourself
- A series has a four-day cycle. At the end of day 12 the observed values for days 9 to 12 are 10, 14, 12 and 16. Give the naive and the seasonal-naive forecasts for days 13 and 14.
Show the answer
The origin is day 12, so day 13 has horizon 1 and day 14 has horizon 2. The naive forecast repeats the latest value, 16, for both days.
With , day 13 sits at the same position in the cycle as day 9 and day 14 at the same position as day 10, so the seasonal-naive forecasts are 10 and 14. By the formula, the indices are and . Both forecasts use only values observed by day 12.
- At the end of day 9, a model will forecast day 11 of the shop series. A colleague’s table gives this row the features lag 1 = 26, lag 2 = 22 and lag 3 = 28. Which of these may be used, and what do the seasonal-naive forecast and the fitted rule predict?
Show the answer
The horizon is , so only lags with are observed at the origin. Lag 1 of day 11 is day 10’s value 26, which has not happened at the end of day 9, so it may not be used. Lag 2 is day 9’s 22 and lag 3 is day 8’s 28, and both may be used.
The seasonal-naive forecast copies day , giving 28 orders. The fitted rule uses the lag-3 value 28 and predicts orders. A model that needs a lag-1 input at this horizon must either use a forecast of day 10 in its place, the recursive strategy, or be refitted for horizon 2 with lags of at least 2, the direct strategy. Filling in the actual value 26 afterward would leak information from after the origin.
- A teammate adds a centered three-day moving average of demand as a feature, evaluates a model with five-fold cross-validation on shuffled rows, and reports an error far below that of the seasonal-naive forecast. Identify two problems and describe an evaluation that fixes them.
Show the answer
The centered average for target day uses days , and , so it contains the target and the day after it. For day 10 of the shop series it equals 26, the target itself. Shuffled folds add a second leak, because the model is trained on days after many of the days it is scored on, and neighboring rows that share values can land on both sides of a split.
A repaired evaluation fixes the horizon first. Every feature is computed from values up to the forecast origin, for example a trailing mean of days to for a one-day-ahead forecast of day . The data are split in time order, any scaling or other fitted preprocessing is fitted on the training period only, and the model is scored on later forecasts. The naive and seasonal-naive forecasts are scored on the same origins, horizon and metric, so the comparison with them is fair.