PracticeStatistical
1 min read
◆ The PatternTurning raw timestamps into predictive signals
Time series features fall into three categories: lag features (past values), rolling statistics (windowed mean/std/min/max), and calendar features (day of week, month, holiday flags). Fourier features encode seasonality as continuous sine/cosine pairs.
// Interactive — feature construction
| Feature Type | Examples | Captures |
|---|---|---|
| Lag features | y(t-1), y(t-7), y(t-30) | Autoregressive patterns |
| Rolling stats | rolling_mean(7), rolling_std(30) | Trend, volatility |
| Calendar | day_of_week, month, is_holiday | Seasonal effects |
| Fourier | sin(2πt/365), cos(2πt/365) | Smooth cyclicality |
Pattern bridge: Lag features are the tabular version of what AR models learn implicitly. Rolling statistics like rolling mean and rolling std are exactly moving averages and Bollinger Bands from technical analysis.
Worked example
Daily changes of a random walk are unpredictable by construction. A 3-day rolling mean that includes today correlates 0.58 with today’s value — it contains it. Shift the window by one day and the correlation is −0.02. Most too-good time-series features are leaks like this one.
Where it misleads
- Every rolling, lag and target-encoded feature must be computed only from data available at prediction time.
- Features built on the full dataset (normalisation, fills, encodings) leak too; build them inside each training window.
- Hundreds of automatic features invite overfitting; keep the ones that survive a time-ordered validation.
In code
rng = np.random.default_rng(13) y = pd.Series(rng.normal(0, 1, 2_000).cumsum()).diff().dropna() # daily changes: unpredictable leaky = y.rolling(3).mean() # includes today's value honest = y.shift(1).rolling(3).mean() # only yesterday and before corr_leaky, corr_honest = y.corr(leaky), y.corr(honest)
Assumes import numpy as np and import pandas as pd. Each snippet simulates or makes up its own series, as the comments say.
The same idea with a library
# Python — time series features df['lag_1'] = df['y'].shift(1) df['lag_7'] = df['y'].shift(7) df['roll_mean_7'] = df['y'].rolling(7).mean() df['roll_std_30'] = df['y'].rolling(30).std() df['dow'] = df.index.dayofweek df['month'] = df.index.month # Fourier features for yearly seasonality df['sin_365'] = np.sin(2 * np.pi * df.index.dayofyear / 365) df['cos_365'] = np.cos(2 * np.pi * df.index.dayofyear / 365)
Sources
- S. Kaufman, S. Rosset, C. Perlich & O. Stitelman, “Leakage in Data Mining: Formulation, Detection, and Avoidance”, ACM Transactions on Knowledge Discovery from Data 6(4), 2012
- M. Christ, N. Braun, J. Neuffer & A. W. Kempa-Liehr, “Time Series FeatuRe Extraction on Basis of Scalable Hypothesis Tests (tsfresh)”, Neurocomputing 307, 2018
- Forecasting: Principles and Practice (3rd ed.), R. J. Hyndman & G. Athanasopoulos, OTexts, 2021
Last reviewed October 2026 · How this site checks its content · Report a mistake
Linked from 1