Learning From a Series' Own Past
A trailing average gives every lag the same influence. An autoregressive model fits different coefficients for the earlier readings and then uses that same rule to predict ahead.
13 sections · 4 questions · 2 coding challenges
The arrow keys move between sections too.
Predicting a Record From Earlier Values of Itself
BOTIE
Later ARIMA methods combined autoregressive terms with other components, including differencing. Our exercise fits a small autoregression directly with least squares; it is not an automatic ARIMA-selection procedure.
Does Yesterday Matter More Than the Day Before?
Botie's moving mean treats the previous two days equally. But demand may respond more strongly to the most recent day, or alternate with a schedule. We can let labelled historical input-target pairs determine those contributions.
The machinery is familiar regression. What changes is the source of its features and the care needed when using a prediction as the input to another prediction.
The coefficients below are supplied; fitting them is covered in the Python calculation.
Step 1 of 3
Keep the familiar supervised table
Earlier demand values form features; the following observation is the target.
| Training row | Older reading | Recent reading | Next reading |
|---|---|---|---|
| 1 | 2 | 3 | 4 |
| 2 | 3 | 4 | 5 |
| 3 | 4 | 5 | 6 |
1
- Older reading
- 2
- Recent reading
- 3
- Next reading
- 4
2
- Older reading
- 3
- Recent reading
- 4
- Next reading
- 5
3
- Older reading
- 4
- Recent reading
- 5
- Next reading
- 6
Step 2 of 3
Give the lags separate coefficients
A fitted rule can weight the newer value more heavily instead of imposing equal weights.
| Part of the prediction | Coefficient |
|---|---|
| Older weight | 0.25 |
| Newer weight | 0.75 |
| Intercept | 1 |
Older weight
- Coefficient
- 0.25
Newer weight
- Coefficient
- 0.75
Intercept
- Coefficient
- 1
Step 3 of 3
Use a forecast when a future reading is unavailable
Beyond one step, a recursive forecast shifts the history and inserts its own prediction.
Known window: [8, 12] Next prediction = (0.25 × 8) + (0.75 × 12) + 1 = 12 Next window: [12 observed, 12 predicted]
Earlier values can become inputs to a fitted prediction rule
The weights assign separate contributions to the same lag positions used during fitting.
What does each earlier day contribute to tomorrow's forecast?
The coefficients belong to particular lag positions. Change one past reading and check its contribution before combining it with the other lags and the intercept.
BOTIE
Next forecast from the supplied coefficients
12
Follow the operations
1. Older-to-newer demand window
- 08
- 112
2. Lag coefficients in the same order
- 00.25
- 10.75
3. Dot product
11
4. Fitted intercept
1
5. Add
12
6. Result
12
This applies supplied coefficients for one horizon. It does not fit them or provide forecast intervals.
1. Build the Regression Without Changing the Time Order
Each row contains a fixed number of preceding observations and one later target. Add a column of ones to fit an intercept. Least squares then finds coefficients that minimise squared training residuals.
Neighbouring rows overlap in their histories. Randomly splitting them can give an unrealistic evaluation of forecasting into a later period.
prediction = intercept + older_weight × older + newer_weight × newerThe column order is part of the fitted model and must travel with it.
2. Fit With a Linear Solver
Use a least-squares solver rather than explicitly inverting a matrix. The solver can handle an overdetermined design and report rank information. Perfectly correlated lag columns can make individual coefficients poorly determined even when predictions look reasonable.
Choose the lag count using time-respecting validation. More lags add parameters and can overfit a short record.
design = np.column_stack([np.ones(len(X)), X])
coefficients = np.linalg.lstsq(design, targets, rcond=None)[0]Check predictions and held-out behaviour rather than treating every fitted coefficient as a causal explanation.
3. Distinguish One-Step and Recursive Forecasting
For a one-step forecast, every lag in the current window is observed. For later horizons from the same origin, some lag values are not yet known. A recursive procedure substitutes its previous predictions.
An alternative fits separate direct models for different horizons. These are different forecasting procedures and should be evaluated under the horizon they will actually serve.
History = [8,12]
Rule = 1 + 0.25 × older + 0.75 × newer
Next = 1 + 0.25 × 8 + 0.75 × 12 = 12
Following = 1 + 0.25 × 12 + 0.75 × 12 = 13The second calculation uses a prediction, not a secretly revealed observation.
Use one lag with already fitted coefficients
Before fitting a rule, check what its coefficients mean. This supplied rule combines an intercept with half of the latest reading.
import numpy as np
coefficients = np.array([1.0, 0.5])
latest_reading = 8.0
row = np.array([1.0, latest_reading])
contributions = row * coefficients
print(contributions.tolist())
print(float(contributions.sum()))[1.0, 4.0]
5.0The constant column contributes the intercept; the other column contributes the lagged demand. Practice will estimate coefficients from a new sequence instead of receiving them.
4. Inspect Stability and Uncertainty
Repeated application can magnify errors when a fitted recurrence is unstable. Even a stable fitted rule leaves unexplained variation, so a point forecast should not be read as a guarantee.
Plot residuals in time order, compare with simple baselines, and test on later origins. Uncertainty intervals need assumptions about future disturbances and parameter uncertainty, beyond the deterministic workshop calculation.
Botie's construction applies supplied lag coefficients. The Python task also fits a one-lag model from a small historical sequence.
5. A Smooth Record Can Make Lag Coefficients Hard to Separate
When yesterday and the day before are almost identical across the training record, their columns contain nearly the same information. Many pairs of coefficients can give similar predictions. Individual coefficient interpretations become fragile even when the fitted curve looks stable.
Inspect the design rank and sensitivity to the training period. Regularisation may stabilise estimation, but coefficients in an observational forecasting rule do not become causal effects merely because a penalty was added.
6. A Small Error Can Grow during Recursive Forecasting
For a one-lag rule, a difference in the current value is multiplied by the lag coefficient at each recursive step. A magnitude above one amplifies that difference. More complicated lag models need a corresponding recurrence analysis.
This does not mean every growing series is invalid. It means a model intended to describe stable fluctuations must be checked against the behaviour of its fitted recurrence.
Propagate the same initial forecast discrepancy
No new observations or disturbances are added. Only the recurrence changes.
import numpy as np
for coefficient in [.8, 1.2]:
discrepancy = 1.
path = []
for _ in range(4):
discrepancy *= coefficient
path.append(round(discrepancy, 4))
print(path)[0.8, 0.64, 0.512, 0.4096]
[1.2, 1.44, 1.728, 2.0736]The second recurrence magnifies the original discrepancy. Forecast intervals must also account for new disturbances and parameter uncertainty, which this trace deliberately omits.
7. Residuals Can Still Contain a Schedule the Model Missed
Place residuals in time order. Repeated positive residuals at the same schedule position suggest structure that the fitted features did not explain. Compare residuals with lagged residuals and with known operating conditions.
Use those patterns to form a new modelling hypothesis, then test it on later data. Selecting many lags after repeatedly inspecting the final test period turns that period into model-development data.
Check yourself
Questions on Parts 1 to 7
Question 1 of 4
What is the next forecast?
Use the information below.
Rule: 1 + 0.25 × older + 0.75 × newer. Known window: [8,12].Try it with Botie
Build with Botie
Write and run Python