Skip to main content

Time Series

Time series data has a temporal ordering that fundamentally changes how you approach modeling. You cannot randomly shuffle it, you must respect chronological order, and specialized algorithms exist to handle its unique characteristics like trend, seasonality, and autocorrelation.

Components of Time Series​

Every time series can be decomposed into these components:

ComponentWhat It IsExample
TrendLong-term increase or decreasePopulation growth, rising stock market over decades
SeasonalityRegular, repeating patterns at fixed intervalsHigher ice cream sales in summer, holiday shopping spikes
CyclicalLong-term fluctuations without fixed periodBusiness cycles, economic booms and recessions
Residual (Noise)Random, unexplained variationUnexpected events, measurement error
# Decompose a time series into its components
from statsmodels.tsa.seasonal import seasonal_decompose

result = seasonal_decompose(data, model='additive', period=12)
result.plot()
# Shows: observed, trend, seasonal, and residual components

Algorithm Selection for Time Series​

AlgorithmBest ForKey Capability
ARIMA / SARIMASingle time series, statistical approachGood for stationary data with clear trend/seasonality. Cannot use related time series
Exponential Smoothing (ETS)Simple time series with trend and/or seasonalityLightweight and fast. Cannot use related time series or metadata
DeepARMultiple related time series, cold-start scenariosLearns patterns across related series. Handles missing values (NaN). Probabilistic forecasts
CNN-QRForecasting with related data, promotions, holidaysSupports related time series, item metadata, and future known values

Decision Guide​

Single series, simple                    → ARIMA
Multiple related series OR new products → DeepAR (cold-start capability)
Need related features + promotions → CNN-QR
Simple trend/seasonality → Exponential Smoothing
Key Insight

"Predict demand for a NEW product with no history" = DeepAR. It can generate forecasts for new items by learning patterns from similar items in the training set (cold-start capability). No other approach handles this as naturally.

Critical Rules for Time Series​

RuleWhy It Matters
NEVER randomly split time series dataRandom splitting leaks future information into training. Always split chronologically: train on past, test on future
Check stationarity before ARIMAARIMA assumes stationarity (constant mean and variance). Use the Dickey-Fuller test or STL decomposition to verify
DeepAR handles missing values as NaNA unique capability — no need to impute missing target values
DeepAR handles cold-startCan predict for new items based on patterns learned from similar items
caution

The single most important rule for time series validation: always split chronologically. Train on the past, validate on the future. Random splitting creates data leakage that produces unrealistically optimistic results.

Stationarity​

A stationary time series has a constant mean and variance over time. Many statistical models (ARIMA) require stationarity.

How to check:

  • Visual inspection of the time series plot
  • Dickey-Fuller test (statistical test for unit root)
  • STL decomposition to separate trend and seasonality

How to make data stationary:

  • Differencing (subtract previous value)
  • Log transformation (stabilize variance)
  • Detrending (remove trend component)
from statsmodels.tsa.stattools import adfuller

# Augmented Dickey-Fuller test
result = adfuller(time_series)
p_value = result[1]
if p_value < 0.05:
print("Stationary (reject null hypothesis)")
else:
print("Non-stationary — apply differencing")

Time Series Validation​

from sklearn.model_selection import TimeSeriesSplit

# Expanding window cross-validation
tscv = TimeSeriesSplit(n_splits=5)
for train_idx, test_idx in tscv.split(X):
# train_idx always precedes test_idx chronologically
X_train, X_test = X[train_idx], X[test_idx]
y_train, y_test = y[train_idx], y[test_idx]

Flashcards​

1 / 8
Question

What are the four components of a time series?

Click to reveal
Answer

1) Trend — long-term direction. 2) Seasonality — regular repeating patterns at fixed intervals. 3) Cyclical — long-term fluctuations without fixed period. 4) Residual — random unexplained variation (noise).