Separating Repetition From Change
A demand increase might be a lasting change or a normal part of the weekly schedule. Comparing like positions in a cycle helps us distinguish those possibilities.
13 sections · 4 questions · 2 coding challenges
The arrow keys move between sections too.
Describing Changes When Levels Would Not Sit Still
BOTIE
Let's try differencing on a record where we know the trend and repeating cycle we put into it. We can watch what each subtraction removes. A real record may retain changing behaviour afterwards, so differencing is something to inspect, not an automatic guarantee of stationarity.
Sources for this history
Is Demand Growing, or Is It Monday Again?
Botie sees high demand every time a scheduled irrigation cycle begins. A model that treats each rise as a new upward trend will repeatedly overreact. We need to compare today's demand with a relevant earlier position.
An ordinary difference compares neighbouring observations. A seasonal difference compares observations one full cycle apart. Let's see which pattern each comparison removes and what information we must keep to return to the original units.
The repeating demand values are invented to keep the seasonal comparison visible.
Step 1 of 3
A rising level
Adjacent observations increase by the same amount in this invented record.
Vertical: Recorded level
Horizontal: Day
Step 2 of 3
Look at the changes
Neighbouring differences remove this particular linear trend.
Day 2 minus day 1: 12 − 10 = 2 Day 3 minus day 2: 14 − 12 = 2 Day 4 minus day 3: 16 − 14 = 2
Step 3 of 3
Compare matching cycle positions
With a two-position cycle, compare each reading with the one two steps earlier.
Cycle 1: 10, 20 Cycle 2: 12, 22 First position: 12 − 10 = 2 Second position: 22 − 20 = 2
A seasonal difference compares corresponding positions in successive cycles
The aligned lists refer to matching positions in successive cycles. Their difference measures change beyond the repeated reference.
Did demand rise beyond its usual repeating pattern?
The reference is the same position in an earlier cycle. Change the period and check which observations now get compared before interpreting their differences.
BOTIE
Differences from the preceding seasonal cycle
- 02
- 12
Follow the operations
1. Chronological demand
- 010
- 120
- 212
- 322
2. Seasonal period in observations
2
3. Read later targets
- 012
- 122
4. Read earlier references
- 010
- 120
5. Subtract
- 02
- 12
6. Result
- 02
- 12
The period is supplied rather than discovered. Differencing alone does not establish stationarity.
1. Name Trend, Seasonality, and Irregular Variation
A trend is a persistent movement in level. Seasonality is a pattern recurring at a known calendar or sampling period. Irregular variation includes changes these descriptions do not explain.
Do not infer a reliable seasonal pattern from one repetition. Inspect several cycles and understand the schedule that might produce it. A changing sampling interval can also make an apparent period misleading.
Example period = 2 observations
Matching positions: 0 with 2, 1 with 3The period counts observations here, not an unspecified number of days.
2. Subtract a Relevant Earlier Level
An ordinary first difference removes the previous observation. A seasonal difference removes the observation one period earlier. The operations ask different questions and can sometimes be combined.
A stable-looking difference series is evidence to inspect, not proof that all distributional properties remain constant. Stationarity is stronger than simply having no obvious trend in one short plot.
Ordinary difference: d[t] = y[t] - y[t-1]
Seasonal difference: d[t] = y[t] - y[t-period]The transformation discards initial levels from its output. Keep them for inversion.
3. Return a Change Forecast to the Original Units
If a model predicts tomorrow's ordinary difference, add it to the last observed level to obtain tomorrow's level forecast. For later horizons, use earlier predicted levels when the actual future levels are still unavailable.
For seasonal differences, use the matching seasonal reference. Depending on the horizon, that reference may itself be a forecast. Record which quantities are measured and which are predicted.
Last observed level = 16
Predicted next change = 2
Next level forecast = 16 + 2 = 18A forecast in differenced units cannot be compared directly with an observed level.
Compare two positions with their previous cycle
Here the illustrative pattern repeats every two readings. Put the earlier cycle beside the later one so each subtraction compares the same position.
import numpy as np
earlier_cycle = np.array([8, 12])
later_cycle = np.array([11, 13])
change = later_cycle - earlier_cycle
print(change.tolist())
print((earlier_cycle + change).tolist())[3, 1]
[11, 13]The changes retain their cycle positions. Adding them to the correct earlier values recovers the later levels; differences alone do not preserve the original level.
4. Avoid Removing More Structure Than Necessary
Differencing a series that was already stable can amplify noise and create unnecessary dependence between adjacent changes. Decide the transformation from training history and diagnostics, then evaluate the complete forecasting procedure.
The workshop calculates seasonal changes with a supplied period. Try a period of one to recover ordinary differencing, and a repeating cycle to see zero seasonal changes.
Seasonal naive forecasting instead copies the previous matching level directly. It provides a useful baseline without fitting a model to the differences.
5. A Difference Forecast Still Needs a Starting Level
Predicting a change removes the level from the model's target. To recover demand, add the predicted change to the last known level. At later horizons, keep adding changes to the level reconstructed so far.
Seasonal differences use a different reference: the corresponding earlier season position. If that reference lies in the forecast future, use the previously reconstructed forecast, not a secretly available observation. Save the initial history required to undo every transformation.
6. Rebuild Demand from Predicted Daily Changes
The first forecast begins from the last observed demand. Each later forecast begins from the preceding forecast. Cumulative sums express this recurrence once the changes have been predicted.
The example supplies changes to isolate the inversion. It does not fit a model or establish that these changes are predictable.
Undo ordinary differencing
The last observed demand is 20 litres.
import numpy as np
last = 20.
predicted_change = np.array([2., -1., 3.])
level = last + np.cumsum(predicted_change)
print(level.tolist())
print(np.diff(np.r_[last, level]).tolist())[22.0, 21.0, 24.0]
[2.0, -1.0, 3.0]Differencing the reconstructed path recovers the supplied changes, which checks the bookkeeping.
7. Differencing Also Changes the Noise
Subtracting adjacent noisy readings includes noise from both readings. Consecutive differences share one of those noise terms with opposite signs, so differencing can introduce negative short-lag dependence even when the original noise was independent.
Do not repeatedly difference until a plot merely looks flat. Compare suitable models and inspect residuals. A trend, a seasonal pattern, a level shift and a measurement fault can require different explanations despite all producing visible changes over time.
Check yourself
Questions on Parts 1 to 7
Question 1 of 4
What are the seasonal differences?
Use the information below.
Levels: [10,20,12,22]. Seasonal period: 2 observations.Try it with Botie
Build with Botie
Write and run Python