Trend seasonality and lag features
Quick answer
A time series is a sequence of observations ordered by time. Trend describes long-term movement, seasonality describes recurring patterns, and lag features use observations available before the prediction time. Examples are static Python snippets tested locally on CPU; Statsmodels and scikit-learn are required for selected examples. ByteMind execution is unverified.
Trailing mean
y=[4,6,8,10,12]print([(y[t-1],sum(y[t-3:t])/3,y[t]) for t in range(3,len(y))])Expected output (locally tested): [(8, 6.0, 10), (10, 8.0, 12)]. Each feature uses observations before the target.
How it works
For a regularly spaced series y[t], lag k at target t is y[t-k], with k positive. For a forecast made at origin o predicting y[o+h], any observed lag must have index at most o, and its publication time must also be at most o. For h=1, lag 1 is available if observations arrive immediately. A rolling mean of the previous w observations is sum(y[t-w:t])/w, not a centered window or one including y[t]. Trend may be represented by a time index; a known calendar variable such as weekday can represent seasonality. For period s, sin(2pit/s) and cos(2pit/s) avoid an artificial discontinuity at a cycle boundary. A period of seven means seven observations, not necessarily seven days unless sampling is daily. Check missing timestamps, duplicate times, time zones and changes in sampling; do not silently compress gaps. Known future promotions or calendar variables may be valid exogenous inputs; realized future weather or sales are not available unless a forecast of them was genuinely issued at the origin. Training and production must use the same as-of rules. Multi-step forecasts need recursive predictions, horizon-specific direct models, or a multi-output design; do not substitute realized intermediate values when they are not available.
Known versus observed inputs
origin=4; target=6features={"calendar":(6,0),"sales":(5,5),"issued_weather":(6,4)}print(sorted(k for k,(event,available) in features.items() if available<=origin))Expected output (locally tested): ['calendar', 'issued_weather']. Event time can be future if the feature was actually known at the origin.
Common mistakes
Do not use centered rolling windows, target-inclusive averages, random train-test splits, or future realized exogenous inputs. Check event time and availability time separately.
Practice and next steps
Construct lag-1, lag-7 and a three-point trailing mean for a daily series. Mark the earliest usable target and identify features unavailable at a two-day horizon.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Trend seasonality and lag features — knowledge check
Apply trend seasonality and lag features to small examples and debugging scenarios.
Which lag is available for a two-step forecast at origin o?
What does seasonality describe?
What makes a future calendar feature valid?
Which trailing mean avoids leakage for target t?
What should happen before computing daily lags?