Skip to content
Machine Learning & Data Science

Time Series interview questions and answers

50 questions, 49 with code. About 2 h 30 min to read them all.

By TestLab · Updated

Plan for an interview
50 questions

Track what you know and what to revise.

Sign in to track progress

You can open a few full answers without signing in. Sign in to read all

1

What is a time series?

A time series is a sequence of observations of the same quantity recorded in time order, usually at regular intervals, such as daily sales, hourly temperature or monthly unemployment. What makes it special is that the order matters: each value tends to depend on previous values, so the data has trend, seasonality and autocorrelation that ordinary models assume away. Analysing it lets you understand patterns and forecast the future, at the cost of special care with validation and leakage.

Read full answer

Applies totime series concepts (examples in pandas 2.2+ and statsmodels 0.14+, Python 3.12)

Components

  • Trend: the long-term increase or decrease (e.g. growing website traffic).

  • Seasonality: patterns that repeat with a fixed period (weekly, yearly).

  • Cycles: longer, irregular rises and falls (business cycles) without a fixed period.

  • Noise (residual): random variation that the other components do not explain.

Key Properties

Property Meaning
Frequency Spacing between observations (hourly, daily, monthly)
Autocorrelation Correlation of the series with lagged copies of itself
Stationarity Constant mean, variance and autocorrelation over time; many models assume it
Univariate vs multivariate One variable versus several related variables over time

Common Tasks

  • Forecasting: predict future values (demand, prices, load).

  • Anomaly detection: find unusual points (server incidents, fraud).

  • Decomposition: separate trend and seasonality to understand drivers.

  • Classification: label whole sequences (ECG heartbeats, activity recognition).

Code Example

import numpy as np
import pandas as pd
from statsmodels.tsa.seasonal import seasonal_decompose

rng = np.random.default_rng(42)
idx = pd.date_range("2020-01-01", periods=48, freq="MS")          # monthly data, 4 years
trend = np.linspace(100, 160, 48)
season = 10 * np.sin(2 * np.pi * idx.month / 12)
sales = pd.Series(trend + season + rng.normal(0, 3, 48), index=idx, name="sales")

print(sales.head(3))
print("lag-1 autocorrelation:", round(sales.autocorr(lag=1), 3))   # strongly positive

parts = seasonal_decompose(sales, model="additive", period=12)
print(parts.seasonal.iloc[:12].round(1).tolist())                 # one year of the seasonal pattern

Common Pitfalls

  • Shuffling data before splitting; use time-based splits.

  • Leakage: features or scaling that use future values.

  • Ignoring seasonality, which makes forecasts systematically wrong at certain times.

  • Always compare models with a naive baseline (last value or same period last year).

2

What is stationarity in time series and why does it matter?

A stationary time series has statistical properties that do not change over time: a constant mean, a constant variance and an autocorrelation that depends only on the lag, not on when you look. It matters because many classical models, such as ARMA, assume stationarity, and patterns learned from the past only carry over to the future if the process behaves the same way. Most real series are not stationary, so you test for it and transform the data (differencing, logs) to make it so.

Read full answer

Applies tostatsmodels 0.14+ (Python 3.12)

Types

  • Strict stationarity: the whole joint distribution is unchanged by shifts in time.

  • Weak (covariance) stationarity: constant mean and variance, and autocovariance that depends only on the lag. This is what is usually meant in practice.

Common Causes of Non-Stationarity

  • Trend: the mean increases or decreases over time.

  • Seasonality: the mean varies with the calendar.

  • Changing variance: volatility grows with the level, or comes in bursts (finance).

  • Unit root / random walk: shocks persist forever.

  • Structural breaks: a sudden shift after an event.

Testing

Test Null hypothesis Stationary if
Augmented Dickey-Fuller (ADF) Series has a unit root (non-stationary) p-value < 0.05 (reject the null)
KPSS Series is stationary p-value > 0.05 (fail to reject)

Using both is informative: if ADF rejects and KPSS does not, the series is likely stationary.

Making a Series Stationary

  • Differencing: y_t - y_{t-1} removes a stochastic trend; seasonal differencing y_t - y_{t-12} removes yearly seasonality in monthly data.

  • Log or Box-Cox transform: stabilizes variance that grows with the level.

  • Detrending: subtract a fitted trend.

Code Example

import numpy as np
import pandas as pd
from statsmodels.tsa.stattools import adfuller, kpss

rng = np.random.default_rng(42)
random_walk = pd.Series(np.cumsum(rng.normal(0, 1, 500)))   # non-stationary
differenced = random_walk.diff().dropna()                   # stationary white noise

for name, s in [("random walk", random_walk), ("first difference", differenced)]:
    adf_p = adfuller(s)[1]
    kpss_p = kpss(s, regression="c", nlags="auto")[1]       # p-values are bounded to [0.01, 0.1]
    print(f"{name}: ADF p={adf_p:.3f}, KPSS p={kpss_p:.3f}")
# Random walk: ADF p large, KPSS p small. Differenced: ADF p near 0, KPSS p >= 0.05.

Common Pitfalls

  • Over-differencing, which introduces artificial negative autocorrelation.

  • Relying on a single test; small samples give low power.

  • Forgetting to invert the transformation when producing forecasts.

  • Assuming tree-based or deep models do not care: trends outside the training range still break them.

3

How does time series data differ from cross-sectional data?

Time series data follows one entity (a store, a stock, a sensor) through many points in time, while cross-sectional data captures many entities at a single point in time, such as survey answers from 1,000 customers this month. The key difference is dependence: time series observations are ordered and correlated with their own past, while cross-sectional rows are usually treated as independent. This changes how you model, validate and interpret the data.

Read full answer

Applies totime series concepts (examples in pandas 2.2+, Python 3.12)

Comparison

Aspect Time series Cross-sectional
Structure One unit, many time points Many units, one time point
Order Matters; cannot be shuffled Usually irrelevant
Dependence Autocorrelated observations Typically assumed independent
Typical questions How will it evolve? Is there seasonality or trend? How do units differ? What explains differences?
Typical models ARIMA, exponential smoothing, state-space, lag-feature ML, RNNs Linear/logistic regression, tree models
Validation Time-based holdout, expanding windows Random K-fold cross-validation
Example Daily revenue of one store for 3 years Revenue of 500 stores in March

Panel (Longitudinal) Data

Panel data combines both: many units observed over many time points (500 stores, daily, for 3 years). It allows modelling differences between units and dynamics over time, for example with fixed-effects models or global forecasting models trained on all series.

Code Example

import numpy as np
import pandas as pd

rng = np.random.default_rng(42)
dates = pd.date_range("2024-01-01", periods=5, freq="D")
stores = ["A", "B", "C"]

panel = pd.DataFrame(
    [(d, s, rng.integers(80, 120)) for d in dates for s in stores],
    columns=["date", "store", "sales"],
)

time_series = panel[panel.store == "A"].set_index("date")["sales"]   # one store over time
cross_section = panel[panel.date == "2024-01-03"].set_index("store")["sales"]  # all stores, one day
print("time series (store A):\n", time_series)
print("cross-section (2024-01-03):\n", cross_section)

Why the Difference Matters

  • Validation: random K-fold on time series leaks future information; use TimeSeriesSplit.

  • Standard errors: autocorrelation makes ordinary regression standard errors too small; use HAC (Newey-West) errors or time-series models.

  • Features: time series models use lags and rolling statistics; cross-sectional models use attributes of each unit.

Common Pitfalls

  • Treating time series rows as independent samples.

  • Pooling cross-sectional data from different periods without accounting for time effects.

  • Calling panel data “time series” and ignoring differences between units.

4

What is seasonality in time series and how do you detect it?

Seasonality is a pattern in a time series that repeats at a fixed, known period, such as higher retail sales every December, more electricity use every afternoon or less web traffic every weekend. Detecting it matters because a model that ignores it makes systematic errors at the same time every cycle. You detect it with plots, autocorrelation at the seasonal lag, decomposition and frequency analysis; the challenge is separating true seasonality from trend, cycles and noise.

Read full answer

Applies topandas 2.2+ and statsmodels 0.14+ (Python 3.12)

Seasonality Versus Cycles

Aspect Seasonality Cycle
Period Fixed (day, week, year) Variable, often several years
Cause Calendar, weather, habits Economic or business conditions
Example Ice cream sales peak every summer Recession and recovery

Detection Methods

  1. Line plot: look for repeating peaks and troughs.

  2. Seasonal subseries / box plots: group by month or weekday and compare distributions.

  3. ACF: significant spikes at the seasonal lag (12 for monthly data with yearly seasonality, 7 for daily data with weekly seasonality) and its multiples.

  4. Decomposition: seasonal_decompose or STL separates a seasonal component; compare its size with the residual’s.

  5. Periodogram / Fourier analysis: peaks at the seasonal frequency.

  6. Statistical tests: e.g. comparing models with and without seasonal terms, or tests for seasonal unit roots.

Code Example

import numpy as np
import pandas as pd
from statsmodels.tsa.seasonal import STL
from statsmodels.tsa.stattools import acf

rng = np.random.default_rng(42)
idx = pd.date_range("2018-01-01", periods=72, freq="MS")
y = pd.Series(50 + 0.5 * np.arange(72) + 12 * np.sin(2 * np.pi * (idx.month - 1) / 12)
              + rng.normal(0, 2, 72), index=idx)

# 1. Average by month (after removing the trend with a 12-month rolling mean)
detrended = y - y.rolling(12, center=True).mean()
print(detrended.groupby(detrended.index.month).mean().round(1))

# 2. Autocorrelation at the seasonal lag
r = acf(y.diff().dropna(), nlags=24)
print("ACF at lag 12:", round(r[12], 2), "| lag 6:", round(r[6], 2))

# 3. STL decomposition and seasonal strength
res = STL(y, period=12).fit()
strength = max(0, 1 - res.resid.var() / (res.seasonal + res.resid).var())
print("seasonal strength (0-1):", round(strength, 2))     # close to 1 means strong seasonality

Handling Seasonality

  • Seasonal models: SARIMA, Holt-Winters (ETS), Prophet.

  • Seasonal differencing (y_t - y_{t-12}).

  • Calendar features (month, weekday, holidays) or Fourier terms in regression and ML models.

Common Pitfalls

  • Missing multiple seasonalities (daily and weekly in hourly data).

  • Confusing seasonality with holidays that move (Easter, Diwali); model them as events.

  • Using too short a history: you need at least two full cycles to estimate seasonality.

5

What is a trend in time series analysis?

A trend is the long-term direction of a time series: a persistent increase, decrease or change in level over many periods, such as rising online sales over several years. It is separate from short-term seasonal patterns and random noise. Identifying the trend helps you understand growth or decline and is essential for forecasting, but trends can change, so extrapolating them blindly is risky.

Read full answer

Applies totime series concepts (examples in statsmodels 0.14+, Python 3.12)

Types of Trend

Type Description Example
Linear Constant increase per period Steady subscriber growth
Exponential Constant percentage growth Early-stage adoption
Damped Growth that slows toward a ceiling Market saturation
Deterministic A fixed function of time Linear time trend
Stochastic Random walk with drift; shocks persist Many economic and price series
Changing (piecewise) Slope changes at certain points Growth before and after a policy change

How to Identify a Trend

  • Plot the series and a moving average (e.g. 12-month) to smooth out seasonality.

  • Decompose with seasonal_decompose or STL to extract the trend component.

  • Fit a regression on time; check the slope’s size and significance.

  • Unit-root tests (ADF, KPSS) to tell stochastic from deterministic trends.

How to Handle a Trend

  • Model it: Holt’s linear or damped trend (exponential smoothing), ARIMA with d = 1, time terms in regression, changepoints in Prophet.

  • Remove it: differencing (stochastic trend) or detrending by subtracting a fitted line (deterministic trend).

  • Transform: logs turn exponential growth into a linear trend.

Code Example

import numpy as np
import pandas as pd
from statsmodels.tsa.seasonal import STL

rng = np.random.default_rng(42)
idx = pd.date_range("2019-01-01", periods=60, freq="MS")
y = pd.Series(200 + 3 * np.arange(60) + 15 * np.sin(2 * np.pi * idx.month / 12)
              + rng.normal(0, 5, 60), index=idx)

trend = STL(y, period=12).fit().trend
slope = np.polyfit(np.arange(60), trend.to_numpy(), deg=1)[0]
print(f"estimated trend: about {slope:.2f} units per month")       # close to 3

moving_avg = y.rolling(window=12, center=True).mean()                # simple smoother
print(moving_avg.dropna().iloc[[0, -1]].round(1))

Common Pitfalls

  • Spurious trends: random walks often look like trends over short windows.

  • Extrapolating far ahead: trends rarely continue unchanged; use damped trends for long horizons.

  • Confusing trend with a long cycle when the history is short.

  • Detrending a stochastic trend with a straight line leaves non-stationary residuals; difference instead.

6

What is the difference between white noise and a random walk?

White noise is a sequence of uncorrelated random values with a constant mean (usually zero) and constant variance: each value tells you nothing about the next. A random walk is the running sum of white noise: each value equals the previous value plus a new random shock, so shocks accumulate and the series wanders without returning to a fixed mean. White noise is stationary and unpredictable; a random walk is non-stationary, and its best forecast is simply the last observed value.

Read full answer

Applies totime series concepts (examples in NumPy 2.x and statsmodels 0.14+, Python 3.12)

Definitions

  • White noise: e_t with E[e_t] = 0, Var(e_t) = sigma^2, and Corr(e_t, e_s) = 0 for t != s.

  • Random walk: y_t = y_{t-1} + e_t, so y_t = y_0 + e_1 + e_2 + ... + e_t.

  • Random walk with drift: y_t = c + y_{t-1} + e_t, adding a constant step c each period.

Comparison

Property White noise Random walk
Stationary Yes No (unit root)
Mean Constant Wanders; no fixed level
Variance Constant sigma^2 Grows with time: t * sigma^2
ACF Near zero at all lags > 0 Very high, decays slowly
Best forecast The mean The last value (naive forecast)
Relationship — Its first difference is white noise

Why It Matters

  • Model diagnostics: a good model leaves residuals that look like white noise (check with the Ljung-Box test).

  • Benchmarking: many financial prices are close to random walks, so the naive forecast is a tough baseline.

  • Stationarity: differencing a random walk produces white noise, which is why ARIMA uses d = 1.

Code Example

import numpy as np
from statsmodels.stats.diagnostic import acorr_ljungbox
from statsmodels.tsa.stattools import acf, adfuller

rng = np.random.default_rng(42)
noise = rng.normal(0, 1, 1000)
walk = np.cumsum(noise)

for name, s in [("white noise", noise), ("random walk", walk)]:
    print(f"{name}: lag-1 ACF={acf(s, nlags=1)[1]:.2f}, ADF p={adfuller(s)[1]:.3f}, "
          f"variance of 1st half={s[:500].var():.1f}, 2nd half={s[500:].var():.1f}")

lb = acorr_ljungbox(noise, lags=[10])
print("Ljung-Box p for white noise:", round(float(lb["lb_pvalue"].iloc[0]), 3))   # large: no autocorrelation

Common Pitfalls

  • Mistaking a random walk’s wandering for a real trend or pattern.

  • Fitting models to random-walk levels and getting spurious correlations between unrelated series.

  • Using only Gaussian white noise in mind: white noise only requires zero autocorrelation; independent and identically distributed noise is a stronger condition.

7

What is autocorrelation and how is it measured in time series?

Autocorrelation is the correlation between a time series and a lagged copy of itself: how strongly today’s value is related to the value k periods ago. It shows memory and repeating patterns in the data, such as persistence (high lag-1 autocorrelation) or seasonality (spikes at lag 12 in monthly data). It is measured with the autocorrelation function (ACF), and the partial autocorrelation function (PACF) isolates the direct effect of each lag.

Read full answer

Applies tostatsmodels 0.14+ (Python 3.12)

Formula

The sample autocorrelation at lag k:

r_k = sum_{t=k+1..n} (y_t - y_bar)(y_{t-k} - y_bar) / sum_{t=1..n} (y_t - y_bar)^2

  • y_t: value at time t; y_bar: the series mean; n: number of observations.

  • r_k ranges from -1 to 1; r_0 = 1.

For white noise, about 95% of r_k values fall within ±1.96 / sqrt(n); spikes outside this band are significant.

ACF Versus PACF

Function Measures Typical use
ACF Total correlation at lag k, including effects through intermediate lags MA order, seasonality, persistence
PACF Correlation at lag k after removing the effect of lags 1 to k-1 AR order

Interpreting Patterns

  • Slow decay in the ACF: trend or unit root; difference the series.

  • Spikes at seasonal lags: seasonality.

  • AR(p): PACF cuts off after lag p, ACF decays.

  • MA(q): ACF cuts off after lag q, PACF decays.

Code Example

import numpy as np
import pandas as pd
from statsmodels.stats.diagnostic import acorr_ljungbox
from statsmodels.tsa.stattools import acf, pacf

rng = np.random.default_rng(42)
n = 500
y = np.zeros(n)
e = rng.normal(0, 1, n)
for t in range(1, n):
    y[t] = 0.7 * y[t - 1] + e[t]                # AR(1) process with phi = 0.7

r = acf(y, nlags=5)
p = pacf(y, nlags=5)
print("ACF :", r.round(2))    # roughly 1, 0.7, 0.49, 0.34 ... (geometric decay)
print("PACF:", p.round(2))    # roughly 1, 0.7, then near 0
print("significance band: ±", round(1.96 / np.sqrt(n), 3))
print("pandas lag-1:", round(pd.Series(y).autocorr(lag=1), 2))

lb = acorr_ljungbox(y, lags=[10])               # joint test that the first 10 autocorrelations are 0
print("Ljung-Box p-value:", float(lb["lb_pvalue"].iloc[0]))   # tiny: significant autocorrelation

Common Pitfalls

  • Computing autocorrelation on a trending series: everything looks correlated. Difference first.

  • Treating autocorrelated observations as independent in regression, which underestimates standard errors.

  • Reading every spike as meaningful: with 40 lags, about 2 will exceed the 95% band by chance.

8

What is the purpose of differencing in time series analysis?

Differencing transforms a time series into the changes between consecutive observations, y'_t = y_t - y_{t-1}, to remove trends and make the series stationary. Seasonal differencing, y_t - y_{t-s}, removes repeating seasonal patterns with period s. It is the “I” (integrated) part of ARIMA, letting stationary models handle trending data; the trade-off is that over-differencing adds noise and artificial negative autocorrelation.

Read full answer

Applies topandas 2.2+ and statsmodels 0.14+ (Python 3.12)

Types of Differencing

Type Formula Removes
First difference y_t - y_{t-1} Linear or stochastic trend (unit root)
Second difference (y_t - y_{t-1}) - (y_{t-1} - y_{t-2}) Quadratic trend; rarely needed
Seasonal difference y_t - y_{t-s} Seasonal pattern with period s (e.g. 12 for monthly)
Log difference log(y_t) - log(y_{t-1}) Exponential growth; approximately the percentage change

How Much to Difference

  1. Test the original series with ADF and KPSS.

  2. Difference once and test again; most series need d = 0 or 1, sometimes 2.

  3. Use seasonal differencing (D = 1) if strong seasonality remains.

  4. Watch for over-differencing: a lag-1 autocorrelation near -0.5 or lower, or a larger variance after differencing.

  5. pmdarima.ndiffs and nsdiffs automate this choice, but check the result.

Code Example

import numpy as np
import pandas as pd
from statsmodels.tsa.stattools import adfuller

rng = np.random.default_rng(42)
idx = pd.date_range("2018-01-01", periods=84, freq="MS")
y = pd.Series(100 + 2 * np.arange(84) + 20 * np.sin(2 * np.pi * idx.month / 12)
              + np.cumsum(rng.normal(0, 2, 84)), index=idx)

first = y.diff().dropna()                  # removes the trend
seasonal = y.diff(12).dropna()             # removes yearly seasonality (and much of the trend)
both = y.diff(12).diff().dropna()          # seasonal + first difference

for name, s in [("original", y), ("first diff", first), ("seasonal diff", seasonal), ("both", both)]:
    print(f"{name:13s} ADF p={adfuller(s)[1]:.3f}  std={s.std():.2f}")

# Inverting a first difference to get back to levels
restored = first.cumsum() + y.iloc[0]
print("restored equals original:", np.allclose(restored, y.iloc[1:]))

Differencing Inside Models

  • ARIMA(order=(p, d, q)) differences d times internally and returns forecasts on the original scale.

  • SARIMAX(..., seasonal_order=(P, D, Q, s)) applies seasonal differencing D times.

Common Pitfalls

  • Forgetting to invert differencing when producing forecasts by hand.

  • Differencing a series that only has a deterministic trend; detrending can be better.

  • Differencing removes the long-run level information, which matters for cointegrated series (use error correction models instead).

9

What is an autoregressive (AR) model in time series?

An autoregressive (AR) model predicts the current value of a time series as a linear combination of its own previous values plus random noise. An AR(p) model uses the last p values. It captures persistence and momentum, for example temperature that stays high on consecutive days. AR models are simple and interpretable, but they assume the series is stationary and the relationships are linear.

Read full answer

Applies tostatsmodels 0.14+ (Python 3.12)

The Model

y_t = c + phi_1 * y_{t-1} + phi_2 * y_{t-2} + ... + phi_p * y_{t-p} + e_t

  • y_t: value at time t.

  • c: constant (related to the mean: mu = c / (1 - sum(phi_i)) for a stationary AR).

  • phi_1 ... phi_p: coefficients showing how much each past value influences the present.

  • e_t: white noise error.

Stationarity condition: for AR(1), |phi_1| < 1. In general, the roots of the characteristic polynomial must lie outside the unit circle. With phi_1 = 1, AR(1) becomes a random walk.

Behaviour

AR(1) coefficient Behaviour
0 < phi < 1 Smooth persistence; shocks fade gradually
phi near 1 Very persistent; slow return to the mean
-1 < phi < 0 Oscillation around the mean
phi = 1 Random walk (non-stationary)

Identifying the Order

  • PACF cuts off after lag p; the ACF decays gradually.

  • Information criteria (AIC, BIC) compare candidate orders.

Code Example

import numpy as np
from statsmodels.tsa.ar_model import AutoReg, ar_select_order

rng = np.random.default_rng(42)
n = 600
y = np.zeros(n)
e = rng.normal(0, 1, n)
for t in range(2, n):
    y[t] = 0.6 * y[t - 1] - 0.3 * y[t - 2] + e[t]      # true AR(2)

sel = ar_select_order(y, maxlag=10, ic="bic")
print("selected lags:", sel.ar_lags)                    # expected [1, 2]

res = AutoReg(y[:-12], lags=2).fit()
print(res.params.round(2))                              # const near 0, about 0.6 and -0.3
forecast = res.predict(start=n - 12, end=n - 1)
print("MAE on last 12 points:", np.mean(np.abs(forecast - y[-12:])).round(2))

Common Pitfalls

  • Fitting AR to a non-stationary series; difference first or use ARIMA.

  • Choosing too many lags, which overfits; prefer BIC for parsimony.

  • Expecting long-horizon forecasts to stay informative: AR forecasts converge to the mean.

10

What is a moving average (MA) model in time series?

A moving average (MA) model expresses the current value of a time series as its mean plus the current random shock and a weighted sum of recent past shocks (forecast errors). An MA(q) model uses the last q shocks, so a shock affects the series for exactly q periods and then disappears. It captures short-lived effects, such as a one-off promotion whose impact lasts a few days. Note that this is different from the moving average used for smoothing.

Read full answer

Applies tostatsmodels 0.14+ (Python 3.12)

The Model

y_t = mu + e_t + theta_1 * e_{t-1} + theta_2 * e_{t-2} + ... + theta_q * e_{t-q}

  • mu: the series mean.

  • e_t: white noise shock at time t (unobserved).

  • theta_1 ... theta_q: weights of past shocks.

Properties: - An MA(q) process is always stationary. - Its ACF is non-zero up to lag q and exactly zero afterwards (a cut-off). - Invertibility (e.g. |theta_1| < 1 for MA(1)) is required so the model can be rewritten as an infinite AR and estimated uniquely.

MA Model Versus Moving-Average Smoothing

Aspect MA(q) model Moving-average smoother
What it averages Past random shocks (errors) Past observed values
Purpose Statistical model for forecasting Smoothing or trend estimation
Example y_t = mu + e_t + 0.5 e_{t-1} (y_t + y_{t-1} + y_{t-2}) / 3

Identifying the Order

  • ACF cuts off after lag q; the PACF decays gradually.

  • Confirm with AIC/BIC and residual diagnostics.

Code Example

import numpy as np
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.tsa.stattools import acf

rng = np.random.default_rng(42)
n = 800
e = rng.normal(0, 1, n + 1)
y = 5 + e[1:] + 0.7 * e[:-1]                  # true MA(1): mu = 5, theta = 0.7

print("ACF:", acf(y, nlags=4).round(2))        # lag 1 near 0.7 / (1 + 0.49) = 0.47, then near 0

res = ARIMA(y, order=(0, 0, 1)).fit()          # MA(1) with a constant
print(res.params.round(2))                     # const near 5, ma.L1 near 0.7, sigma2 near 1
print("forecast:", res.forecast(steps=3).round(2))   # after 1 step, the forecast is just the mean

Common Pitfalls

  • Confusing the MA model with a rolling mean.

  • Expecting MA forecasts to carry information beyond q steps: they revert to the mean.

  • Non-invertible estimates, which signal a mis-specified or over-differenced model.

11

What is the ARMA model in time series?

The ARMA(p, q) model combines an autoregressive part, which uses the last p values of the series, with a moving average part, which uses the last q random shocks, to describe a stationary time series. Combining both often fits real data with fewer parameters than a pure AR or pure MA model. Its main limitations are that it assumes stationarity and linear relationships, which is why ARIMA and SARIMA extend it for trends and seasonality.

Read full answer

Applies tostatsmodels 0.14+ (Python 3.12)

The Model

y_t = c + phi_1 y_{t-1} + ... + phi_p y_{t-p} + e_t + theta_1 e_{t-1} + ... + theta_q e_{t-q}

  • phi_i: AR coefficients (dependence on past values).

  • theta_j: MA coefficients (dependence on past shocks).

  • e_t: white noise.

  • Stationarity depends on the AR part; invertibility depends on the MA part.

Identifying p and q

Pattern ACF PACF
AR(p) Decays gradually Cuts off after lag p
MA(q) Cuts off after lag q Decays gradually
ARMA(p, q) Decays after lag q Decays after lag p

Because both functions decay for ARMA, it is common to fit a small grid of (p, q) values and choose by AIC or BIC, then check that residuals are white noise.

Code Example

import itertools
import numpy as np
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.tsa.arima_process import ArmaProcess
from statsmodels.stats.diagnostic import acorr_ljungbox

# Simulate ARMA(1,1): phi = 0.6, theta = 0.4 (ArmaProcess uses lag-polynomial signs)
process = ArmaProcess(ar=[1, -0.6], ma=[1, 0.4])
y = process.generate_sample(nsample=600, distrvs=np.random.default_rng(42).standard_normal)

best = min(
    ((p, q, ARIMA(y, order=(p, 0, q)).fit().bic) for p, q in itertools.product(range(3), range(3))),
    key=lambda x: x[2],
)
print("best (p, q) by BIC:", best[:2])                # expected (1, 1)

res = ARIMA(y, order=(1, 0, 1)).fit()
print(res.params.round(2))                             # ar.L1 near 0.6, ma.L1 near 0.4
print("Ljung-Box p on residuals:", round(float(acorr_ljungbox(res.resid, lags=[10])["lb_pvalue"].iloc[0]), 3))

When to Use ARMA

  • Stationary series with short-term dependence: returns, differenced sales, residuals of another model.

  • As a building block: ARIMA = ARMA on a differenced series; SARIMA adds seasonal ARMA terms.

Common Pitfalls

  • Fitting ARMA to trending data (use ARIMA with d >= 1).

  • Over-parameterizing: AR and MA terms can cancel each other (near-common roots), giving unstable estimates.

  • Skipping residual diagnostics.

12

How does ARIMA extend the ARMA model?

ARIMA(p, d, q) extends ARMA by adding integration: it differences the series d times to remove trends and make it stationary, fits an ARMA(p, q) model to the differenced series, and then integrates (cumulatively sums) the forecasts back to the original scale. This lets the ARMA framework handle non-stationary series with trends or random-walk behaviour. Its limitations are that it does not model seasonality (SARIMA does) or changing variance (GARCH does).

Read full answer

Applies tostatsmodels 0.14+ (Python 3.12)

The Three Parts

Parameter Meaning How to choose
p Number of autoregressive lags PACF of the differenced series, AIC/BIC
d Number of differences ADF/KPSS tests; usually 0, 1 or 2
q Number of moving-average lags ACF of the differenced series, AIC/BIC

With backshift notation (B y_t = y_{t-1}):

phi(B) (1 - B)^d y_t = c + theta(B) e_t

  • (1 - B)^d is the differencing; phi(B) and theta(B) are the AR and MA polynomials.

Special Cases

  • ARIMA(0, 1, 0): random walk (with a constant: random walk with drift).

  • ARIMA(0, 1, 1): equivalent to simple exponential smoothing.

  • ARIMA(p, 0, q): plain ARMA.

Code Example

import numpy as np
import pandas as pd
from statsmodels.tsa.arima.model import ARIMA

rng = np.random.default_rng(42)
idx = pd.date_range("2016-01-01", periods=96, freq="MS")
y = pd.Series(np.cumsum(1.5 + rng.normal(0, 3, 96)) + 100, index=idx)   # random walk with drift

train, test = y[:-12], y[-12:]
res = ARIMA(train, order=(1, 1, 1), trend="t").fit()   # with d=1, trend="t" adds a drift term
fc = res.get_forecast(steps=12)

comparison = pd.DataFrame({"actual": test, "forecast": fc.predicted_mean,
                           "lower": fc.conf_int().iloc[:, 0], "upper": fc.conf_int().iloc[:, 1]})
print(comparison.round(1).head())
mae = (comparison.actual - comparison.forecast).abs().mean()
naive_mae = (test - train.iloc[-1]).abs().mean()
print(f"ARIMA MAE={mae:.2f} vs naive MAE={naive_mae:.2f}")

Workflow

  1. Plot the series; check for trend, seasonality and changing variance (log if needed).

  2. Choose d with stationarity tests.

  3. Choose p and q with ACF/PACF and information criteria (or pmdarima.auto_arima).

  4. Check residuals: no autocorrelation (Ljung-Box), roughly normal.

  5. Forecast and compare with naive baselines on a time-based holdout.

Common Pitfalls

  • Using ARIMA on clearly seasonal data instead of SARIMA.

  • A constant term with d = 1 creates a drift (linear trend in forecasts); include it deliberately.

  • Over-differencing (d = 2 when 1 is enough).

13

What do the ACF and PACF tell you in time series analysis?

The ACF (autocorrelation function) shows how strongly a series correlates with itself at each lag, including indirect effects passed through intermediate lags, while the PACF (partial autocorrelation function) shows the direct correlation at each lag after removing the effects of shorter lags. Together they reveal persistence, seasonality and non-stationarity, and they are the classic tools for choosing the AR order p and MA order q of ARIMA models. They are guides rather than rules, so information criteria and residual checks complete the choice.

Read full answer

Applies tostatsmodels 0.14+ (Python 3.12)

Reading the Plots

Pattern ACF PACF Suggests
AR(p) Gradual (exponential or oscillating) decay Sharp cut-off after lag p AR order p
MA(q) Sharp cut-off after lag q Gradual decay MA order q
ARMA(p, q) Decays Decays Try small grids, use AIC/BIC
Very slow ACF decay Stays high for many lags Large spike at lag 1 Non-stationary; difference
Spikes at lags s, 2s Seasonal pattern Seasonal spikes Seasonal terms (SARIMA)
All within bands Nothing significant Nothing significant White noise; little to model

The shaded band in statsmodels plots is approximately ±1.96 / sqrt(n); bars outside it are significant at about the 5% level.

Other Uses

  • Residual diagnostics: the ACF of model residuals should show no significant spikes.

  • Feature engineering: significant lags suggest which lag features to include in ML models.

  • Seasonality detection: spikes at 7 (daily data), 12 (monthly) or 24 (hourly).

Code Example

import matplotlib
matplotlib.use("Agg")                       # render to a file without a display
import matplotlib.pyplot as plt
import numpy as np
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.tsa.arima_process import ArmaProcess

y = ArmaProcess(ar=[1, -0.75], ma=[1]).generate_sample(          # AR(1) with phi = 0.75
    nsample=500, distrvs=np.random.default_rng(42).standard_normal)

fig, axes = plt.subplots(1, 2, figsize=(10, 3))
plot_acf(y, lags=20, ax=axes[0])            # gradual decay
plot_pacf(y, lags=20, ax=axes[1], method="ywm")   # single spike at lag 1
fig.tight_layout()
fig.savefig("acf_pacf.png")
print("saved acf_pacf.png: expect a geometric ACF decay and one PACF spike")

Common Pitfalls

  • Reading ACF/PACF on a non-stationary series; difference first.

  • Treating every bar slightly outside the band as meaningful; some are false positives.

  • Ignoring that real data often shows mixed patterns; confirm with AIC/BIC and residual tests.

14

Why is lag selection important in ARMA and ARIMA models?

Lag selection chooses how many past values (p) and past errors (q) an ARMA or ARIMA model uses. It matters because too few lags leave autocorrelation in the residuals (an underfitted model with biased forecasts), while too many lags add parameters that fit noise, make estimates unstable and hurt out-of-sample accuracy. Good lag selection balances fit and simplicity using ACF/PACF patterns, information criteria and residual diagnostics.

Read full answer

Applies tostatsmodels 0.14+ (Python 3.12)

Methods

Method How it works Notes
ACF / PACF inspection PACF cut-off suggests p, ACF cut-off suggests q Quick, but ambiguous for mixed models
AIC 2k - 2 ln(L) Favours better fit; may pick larger models
BIC k ln(n) - 2 ln(L) Stronger penalty; favours simpler models
AICc AIC corrected for small samples Good default for short series
Out-of-sample validation Rolling-origin forecast error for each candidate Most directly aligned with forecasting goals
Residual tests Ljung-Box on residuals Confirms no remaining autocorrelation

Here k is the number of parameters, L the maximized likelihood and n the number of observations.

Consequences of Wrong Choices

  • Too few lags: residuals remain autocorrelated; forecasts miss predictable structure; coefficient estimates are biased.

  • Too many lags: overfitting, wide confidence intervals, possible cancelling AR and MA roots, slower estimation.

Code Example

import itertools
import numpy as np
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.tsa.arima_process import ArmaProcess
from statsmodels.stats.diagnostic import acorr_ljungbox

y = ArmaProcess(ar=[1, -0.5, -0.2], ma=[1, 0.3]).generate_sample(   # ARMA(2,1)
    nsample=500, distrvs=np.random.default_rng(42).standard_normal)

results = []
for p, q in itertools.product(range(4), range(4)):
    res = ARIMA(y, order=(p, 0, q)).fit()
    lb_p = float(acorr_ljungbox(res.resid, lags=[10], model_df=p + q)["lb_pvalue"].iloc[0])
    results.append((p, q, round(res.aic, 1), round(res.bic, 1), round(lb_p, 3)))

for row in sorted(results, key=lambda r: r[3])[:5]:
    print("p=%d q=%d AIC=%.1f BIC=%.1f Ljung-Box p=%.3f" % row)
# The best BIC models should be small (around p=2, q=1) with Ljung-Box p > 0.05.

Practical Tips

  • Restrict the search to small orders (p, q <= 3 for most business data).

  • Prefer the simplest model whose residuals pass diagnostics.

  • For forecasting, confirm with time-based validation, not just in-sample criteria.

Common Pitfalls

  • Comparing AIC across models fitted on different data (e.g. different d): the likelihoods are not comparable.

  • Selecting lags on the full dataset and then reporting test error on part of it (leakage of model choice).

15

How does SARIMA handle seasonality?

SARIMA (Seasonal ARIMA) handles seasonality by adding a second, seasonal set of ARIMA terms that operate at the seasonal lag s, written SARIMA(p, d, q)(P, D, Q)s. Seasonal differencing (D) removes the repeating pattern, and seasonal AR (P) and MA (Q) terms model the remaining relationships between the same season in different years. It handles a single, fixed seasonal period well; multiple seasonalities or very long periods need other approaches.

Read full answer

Applies tostatsmodels 0.14+ (Python 3.12)

Parameters

Parameter Meaning
p, d, q Non-seasonal AR order, differencing and MA order
P Seasonal AR order: dependence on y_{t-s}, y_{t-2s} …
D Seasonal differencing: y_t - y_{t-s}
Q Seasonal MA order: dependence on shocks e_{t-s} …
s Season length: 12 for monthly-yearly, 7 for daily-weekly, 4 for quarterly

The model multiplies the seasonal and non-seasonal polynomials:

phi(B) Phi(B^s) (1 - B)^d (1 - B^s)^D y_t = theta(B) Theta(B^s) e_t

Choosing Seasonal Orders

  • Seasonal spikes in the ACF at lags s, 2s that decay slowly suggest D = 1.

  • After differencing, a seasonal PACF spike at lag s suggests P = 1; a seasonal ACF spike at lag s suggests Q = 1.

  • The “airline model” SARIMA(0,1,1)(0,1,1)12 is a strong default for monthly data with trend and seasonality.

Code Example

import numpy as np
import pandas as pd
from statsmodels.tsa.statespace.sarimax import SARIMAX

rng = np.random.default_rng(42)
idx = pd.date_range("2015-01-01", periods=120, freq="MS")
y = pd.Series(200 + 1.2 * np.arange(120) + 25 * np.sin(2 * np.pi * (idx.month - 1) / 12)
              + rng.normal(0, 5, 120), index=idx)

train, test = y[:-12], y[-12:]
model = SARIMAX(train, order=(0, 1, 1), seasonal_order=(0, 1, 1, 12))   # airline model
res = model.fit(disp=False)

fc = res.forecast(steps=12)
mae = (test - fc).abs().mean()
seasonal_naive_mae = (test - train[-12:].to_numpy()).abs().mean()     # same month last year
print(res.params.round(3))
print(f"SARIMA MAE={mae:.2f} | seasonal naive MAE={seasonal_naive_mae:.2f}")

Limitations and Alternatives

  • Multiple seasonalities (hourly data with daily and weekly patterns): use TBATS, Prophet, MSTL decomposition, or regression with Fourier terms.

  • Long periods (s = 365 for daily data): SARIMA becomes slow and unstable; use Fourier terms in a SARIMAX regression instead.

  • External drivers (price, holidays): add them as exogenous variables in SARIMAX.

Common Pitfalls

  • Using both d = 1 and D = 1 when one is enough (over-differencing).

  • Too short a history: you need several full seasons.

  • Ignoring calendar effects such as moving holidays or different month lengths.

16

What is exponential smoothing and when should you use it for forecasting?

Exponential smoothing forecasts a time series as a weighted average of past observations in which the weights decrease exponentially, so recent data counts most. Extensions add a trend (Holt’s method) and seasonality (Holt-Winters), and the ETS framework gives them a full statistical model with prediction intervals. It is a fast, robust choice for business series with clear level, trend and seasonal patterns; its trade-off is that it cannot use external variables or capture complex non-linear effects.

Read full answer

Applies tostatsmodels 0.14+ (Python 3.12)

The Family

Method Components Use when
Simple exponential smoothing (SES) Level No trend or seasonality
Holt’s linear Level + trend Trend, no seasonality
Damped trend Level + trend that flattens Long horizons; trend unlikely to continue forever
Holt-Winters additive Level + trend + seasonal amounts Seasonal swings of constant size
Holt-Winters multiplicative Level + trend + seasonal factors Seasonal swings that grow with the level

Simple Exponential Smoothing

level_t = alpha * y_t + (1 - alpha) * level_{t-1}, and the forecast for every future period is level_t.

  • alpha (0-1) is the smoothing parameter: near 1 reacts quickly to changes, near 0 smooths heavily.

  • Holt adds a trend equation with parameter beta; Holt-Winters adds a seasonal equation with gamma.

When to Use It

  • Many series to forecast quickly (thousands of products).

  • Short-to-medium horizons with stable patterns.

  • As a strong benchmark: ETS often matches or beats more complex models on business data.

Code Example

import numpy as np
import pandas as pd
from statsmodels.tsa.holtwinters import ExponentialSmoothing

rng = np.random.default_rng(42)
idx = pd.date_range("2017-01-01", periods=96, freq="MS")
level = 100 + 1.5 * np.arange(96)
y = pd.Series(level * (1 + 0.2 * np.sin(2 * np.pi * (idx.month - 1) / 12)) + rng.normal(0, 4, 96), index=idx)

train, test = y[:-12], y[-12:]
model = ExponentialSmoothing(train, trend="add", damped_trend=True,
                             seasonal="mul", seasonal_periods=12).fit()
fc = model.forecast(12)

print({k: round(v, 3) for k, v in model.params.items()
       if k in ("smoothing_level", "smoothing_trend", "smoothing_seasonal", "damping_trend")})
print("MAE:", round((test - fc).abs().mean(), 2),
      "| seasonal naive MAE:", round((test - train[-12:].to_numpy()).abs().mean(), 2))

For prediction intervals, use statsmodels.tsa.exponential_smoothing.ets.ETSModel, which provides a full state-space formulation.

Common Pitfalls

  • Using SES on trending data: forecasts lag behind the trend.

  • Multiplicative seasonality with zeros or negative values (not allowed).

  • Undamped trends on long horizons, which extrapolate unrealistically.

17

What are the steps to build a time series forecasting model?

Building a time series forecasting model is a structured process: define the forecasting problem, prepare and explore the data, create a time-based validation scheme with baselines, fit candidate models, evaluate them on held-out periods, then deploy and monitor. The order matters, because leakage from future data and missing baselines are the most common reasons forecasts look good in development and fail in production.

Read full answer

Applies tostatsmodels 0.14+ and scikit-learn 1.5+ (Python 3.12)

Steps

  1. Define the problem: what to forecast, at what granularity (daily, weekly), horizon (next 7 days, next 12 months), and what decision it supports. Decide whether you need point forecasts or intervals.

  2. Collect and clean data: fix the frequency, fill or flag gaps, handle outliers, align time zones, aggregate correctly.

  3. Explore: plot the series, decompose it (STL), check trend, seasonality, stationarity (ADF/KPSS) and autocorrelation (ACF/PACF), and note events and structural breaks.

  4. Set up validation: a final holdout at the end of the series, plus rolling-origin (expanding window) cross-validation.

  5. Baselines: naive (last value), seasonal naive (same period last cycle), and moving average.

  6. Feature engineering (for ML models): lags, rolling statistics, calendar features, holidays, promotions, weather, all computed without future information.

  7. Fit candidate models: ETS, ARIMA/SARIMA(X), Prophet, gradient boosting with lags, and deep models for large datasets.

  8. Evaluate: MAE, RMSE, MAPE/sMAPE, MASE across folds and horizons; check residuals.

  9. Select and refit on all available data.

  10. Deploy and monitor: track forecast error over time, detect drift, retrain on a schedule.

Code Example: Baseline Versus Model With Rolling-Origin Validation

import numpy as np
import pandas as pd
from statsmodels.tsa.holtwinters import ExponentialSmoothing

rng = np.random.default_rng(42)
idx = pd.date_range("2016-01-01", periods=96, freq="MS")
y = pd.Series(300 + 2 * np.arange(96) + 30 * np.sin(2 * np.pi * (idx.month - 1) / 12)
              + rng.normal(0, 8, 96), index=idx)

horizon, results = 6, {"seasonal_naive": [], "holt_winters": []}
for origin in range(60, 96 - horizon + 1, 6):                     # expanding window
    train, test = y.iloc[:origin], y.iloc[origin:origin + horizon]
    snaive = train.iloc[-12:].to_numpy()[:horizon]
    hw = ExponentialSmoothing(train, trend="add", seasonal="add", seasonal_periods=12).fit().forecast(horizon)
    results["seasonal_naive"].append(np.mean(np.abs(test.to_numpy() - snaive)))
    results["holt_winters"].append(np.mean(np.abs(test.to_numpy() - hw.to_numpy())))

for name, maes in results.items():
    print(f"{name}: mean MAE over {len(maes)} folds = {np.mean(maes):.2f}")

Common Pitfalls

  • Random train/test splits.

  • Features computed with future data (centered rolling windows, scaling on the full series).

  • No baseline comparison.

  • Evaluating only one-step-ahead when decisions need multi-step forecasts.

18

Which metrics are used to evaluate time series forecasts?

Time series forecast metrics measure how far forecasts are from actual values, and the right one depends on the scale of the data, how errors are penalized and whether you compare across series. The common choices are MAE, RMSE, MAPE, sMAPE and MASE for point forecasts, plus coverage and pinball loss for prediction intervals and quantiles. No single metric is best; always compare against a naive baseline on the same validation periods.

Read full answer

Applies totime series concepts (examples in NumPy 2.x, Python 3.12)

Point Forecast Metrics

With actual values y_t, forecasts f_t, errors e_t = y_t - f_t and n forecast points:

Metric Formula Notes
MAE ‘mean( e_t )‘ Same units as the data; robust; optimal forecast is the median
RMSE sqrt(mean(e_t^2)) Penalizes large errors more; optimal forecast is the mean
MAPE ‘100 * mean( e_t / y_t )‘ Scale-free percentage; undefined at zero, explodes near zero, penalizes over-forecasts more
sMAPE ‘100 * mean(2 e_t / ( y_t + f_t ))‘ Bounded version of MAPE, but still unstable near zero
MASE MAE / MAE of the in-sample (seasonal) naive forecast Scale-free; below 1 means better than naive; works with zeros
WAPE ‘sum( e_t ) / sum( y_t )‘ Popular in retail; weights by volume
Bias (mean error) mean(e_t) Shows systematic over- or under-forecasting

Interval and Probabilistic Metrics

  • Coverage: share of actual values inside the prediction interval (e.g. a 90% interval should cover about 90%).

  • Interval width: narrower is better at the same coverage.

  • Pinball (quantile) loss: evaluates quantile forecasts.

  • CRPS: evaluates whole predictive distributions.

Code Example

import numpy as np

def forecast_metrics(y_train, y_true, y_pred, season=1):
    y_train, y_true, y_pred = map(np.asarray, (y_train, y_true, y_pred))
    e = y_true - y_pred
    naive_mae = np.mean(np.abs(y_train[season:] - y_train[:-season]))   # in-sample (seasonal) naive
    return {
        "MAE": np.mean(np.abs(e)),
        "RMSE": np.sqrt(np.mean(e ** 2)),
        "MAPE%": 100 * np.mean(np.abs(e / y_true)),
        "sMAPE%": 100 * np.mean(2 * np.abs(e) / (np.abs(y_true) + np.abs(y_pred))),
        "MASE": np.mean(np.abs(e)) / naive_mae,
        "bias": np.mean(e),
    }

train = [100, 104, 99, 107, 110, 108, 115, 117]
actual = [120, 118, 125]
forecast = [118, 121, 122]
print({k: round(v, 3) for k, v in forecast_metrics(train, actual, forecast).items()})
# MAE = (2 + 3 + 3) / 3 = 2.667; bias = (2 - 3 + 3) / 3 = 0.667

Choosing a Metric

  • Business cost is linear in error: MAE or WAPE.

  • Large errors are especially costly: RMSE.

  • Comparing many series of different scales: MASE or WAPE.

  • Avoid MAPE with zeros or small values (intermittent demand).

Common Pitfalls

  • Reporting only one metric; RMSE and MAE can disagree.

  • Comparing metrics computed on different horizons or test periods.

  • Ignoring bias: a model can have a good MAE while consistently under-forecasting.

19

How do you prevent overfitting in time series forecasting models?

Preventing overfitting in time series forecasting means making sure the model learns patterns that persist, not noise or one-off events in the history. The main tools are strict time-based validation (rolling-origin evaluation on future periods), comparing against simple baselines, limiting model complexity (few lags, regularization, early stopping) and avoiding leakage from future information. A sign of overfitting is a model that fits the training period much better than the validation periods.

Read full answer

Applies totime series concepts (examples in scikit-learn 1.5+, Python 3.12)

Detection

  • In-sample versus out-of-sample error: large gaps indicate overfitting.

  • Rolling-origin validation: errors should be stable across several forecast origins, not good on one lucky split.

  • Baseline comparison: a complex model that cannot beat the seasonal naive forecast is not learning real structure.

  • Residual diagnostics: training residuals that are suspiciously small, or validation residuals that are autocorrelated.

Prevention Techniques

Technique How it helps
Time-based validation (TimeSeriesSplit, expanding windows) Measures true forecasting ability
Simpler models first ETS or ARIMA with low orders; add complexity only if validation improves
Information criteria (AIC, BIC) Penalize extra parameters in statistical models
Limit lags and features Fewer lag and rolling features in ML models; keep only those that help in validation
Regularization Ridge/Lasso, tree depth limits, min_child_samples, dropout in neural networks
Early stopping on a later validation period Stops boosting or neural training before it memorizes
Remove leakage Features use only past data; scaling fitted on training periods only
Ensembles Averaging several reasonable models reduces variance
Handle one-off events Flag or remove outliers and special events so the model does not learn them as patterns

Code Example

import numpy as np
import pandas as pd
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import TimeSeriesSplit

rng = np.random.default_rng(42)
n = 600
y = pd.Series(10 * np.sin(2 * np.pi * np.arange(n) / 7) + np.arange(n) * 0.05 + rng.normal(0, 2, n))
X = pd.concat({f"lag_{k}": y.shift(k) for k in (1, 2, 7, 14)}, axis=1).dropna()   # past values only
target = y.loc[X.index]

for depth in (2, 8):
    train_err, val_err = [], []
    for tr, va in TimeSeriesSplit(n_splits=5).split(X):
        m = GradientBoostingRegressor(max_depth=depth, n_estimators=300, random_state=42)
        m.fit(X.iloc[tr], target.iloc[tr])
        train_err.append(mean_absolute_error(target.iloc[tr], m.predict(X.iloc[tr])))
        val_err.append(mean_absolute_error(target.iloc[va], m.predict(X.iloc[va])))
    print(f"max_depth={depth}: train MAE={np.mean(train_err):.2f}, validation MAE={np.mean(val_err):.2f}")
# The deeper model fits training data much better but does not validate better: overfitting.

Note that tree models cannot extrapolate the upward trend beyond the training range; differencing the target or adding a trend model helps.

Common Pitfalls

  • Tuning many hyperparameters against one validation window.

  • Using centered rolling means as features (they include future values).

  • Re-using the final test period during model development.

20

How can machine learning models be used for time series forecasting?

Machine learning for time series forecasting turns the series into a supervised learning problem: each row’s target is a future value, and its features are past values (lags), rolling statistics, calendar information and external drivers. Models such as gradient boosting, random forests and neural networks then learn non-linear patterns and interactions that classical models struggle with, and one “global” model can be trained across many related series. The trade-off is more feature engineering, careful leakage control, and weak extrapolation of trends for tree-based models.

Read full answer

Applies toscikit-learn 1.5+ and LightGBM 4.x (Python 3.12)

Approaches

Approach Description Strengths
Lag-feature regression (trees, boosting) Lags, rolling stats, calendar and exogenous features Strong on tabular-style business data; handles many drivers
Global models One model trained on many series (products, stores) Shares patterns; works for short series
Linear models with features Ridge/Lasso on lags and Fourier terms Extrapolate trends; interpretable
Neural networks LSTM, temporal CNN, N-BEATS, transformers Large datasets, complex dynamics
Hybrid Statistical model for trend/seasonality + ML on residuals Combines extrapolation with flexibility

Multi-Step Strategies

  • Recursive: predict one step, feed the prediction back as a lag, repeat. Simple, but errors accumulate.

  • Direct: train a separate model for each horizon (t+1, t+2, …). More models, no error feedback.

  • Multi-output: one model predicts the whole horizon at once (common in neural networks).

Code Example

import numpy as np
import pandas as pd
from sklearn.ensemble import HistGradientBoostingRegressor
from sklearn.metrics import mean_absolute_error

rng = np.random.default_rng(42)
idx = pd.date_range("2022-01-01", periods=730, freq="D")
y = pd.Series(100 + 15 * (idx.dayofweek >= 5) + 10 * np.sin(2 * np.pi * idx.dayofyear / 365)
              + rng.normal(0, 4, 730), index=idx, name="sales")

df = pd.DataFrame({"sales": y})
for k in (1, 7, 14):
    df[f"lag_{k}"] = y.shift(k)
df["roll_mean_7"] = y.shift(1).rolling(7).mean()        # shift first: only past values
df["dayofweek"] = idx.dayofweek
df["dayofyear"] = idx.dayofyear
df = df.dropna()

split = df.index[-60]                                    # last 60 days for testing
train, test = df[df.index < split], df[df.index >= split]
features = [c for c in df.columns if c != "sales"]

model = HistGradientBoostingRegressor(max_iter=300, learning_rate=0.05, random_state=42)
model.fit(train[features], train["sales"])
pred = model.predict(test[features])                     # one-step-ahead (lags use actual history)

print("model MAE:", round(mean_absolute_error(test["sales"], pred), 2))
print("seasonal naive MAE:", round(mean_absolute_error(test["sales"], test["lag_7"]), 2))

Common Pitfalls

  • Leakage: rolling features without shift(1), or target encoding using future data.

  • Random cross-validation: use TimeSeriesSplit or rolling origins.

  • Trends outside the training range: tree models predict at most the highest value they have seen; detrend or difference first.

  • Evaluating one-step-ahead when you need longer horizons: simulate the real forecasting setup.

21

How does cross-validation work for time series data?

Cross-validation for time series must respect time order: models are always trained on past data and evaluated on later data, never the reverse. Instead of random K-fold splits, you use rolling-origin evaluation, where the training window expands (or slides) forward and each fold tests on the following period. This gives a realistic, stable estimate of forecasting performance; the trade-off is more model fits and fewer independent test periods than random cross-validation.

Read full answer

Applies toscikit-learn 1.5+ (Python 3.12)

Why Random K-Fold Fails

  • Training on future data and testing on the past leaks information.

  • Neighbouring observations are correlated, so a test point is “predicted” from almost identical training neighbours.

  • The resulting scores are optimistic and do not reflect real forecasting.

Schemes

Scheme Training window Use when
Expanding window Grows from the start of the series Patterns are stable; use all history
Sliding window Fixed length, moves forward Old data is less relevant (drift)
Blocked / with gap Leaves a gap between train and test Lag features would otherwise overlap; reflects reporting delays
Single final holdout Last period only Final check after model selection

Code Example

import numpy as np
from sklearn.linear_model import Ridge
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import TimeSeriesSplit

rng = np.random.default_rng(42)
n = 300
t = np.arange(n)
y = 0.05 * t + 5 * np.sin(2 * np.pi * t / 12) + rng.normal(0, 1, n)
X = np.column_stack([t, np.sin(2 * np.pi * t / 12), np.cos(2 * np.pi * t / 12)])

tscv = TimeSeriesSplit(n_splits=5, test_size=24, gap=0)        # expanding window, 24-step test folds
for fold, (tr, te) in enumerate(tscv.split(X)):
    model = Ridge(alpha=1.0).fit(X[tr], y[tr])
    mae = mean_absolute_error(y[te], model.predict(X[te]))
    print(f"fold {fold}: train {tr[0]}-{tr[-1]}, test {te[0]}-{te[-1]}, MAE={mae:.2f}")

# Sliding window: cap the training size
sliding = TimeSeriesSplit(n_splits=5, test_size=24, max_train_size=120)
print([len(tr) for tr, _ in sliding.split(X)])                 # training size never exceeds 120

Practical Tips

  • Make each test fold as long as your real forecast horizon.

  • Use a gap equal to any delay between data availability and the forecast.

  • Fit all preprocessing (scalers, encoders, feature selection) inside each fold.

  • Report the mean and spread of errors across folds, and per horizon if forecasting several steps.

  • Keep a final untouched holdout for the chosen model.

Common Pitfalls

  • Random K-fold or shuffled splits.

  • Rolling features computed on the full series before splitting.

  • Choosing hyperparameters on the same folds used to report final performance without a final holdout.

22

How is rolling-window analysis used in time series?

Rolling-window analysis computes statistics or fits models over a fixed-length window that moves through the series one step (or block) at a time, such as a 30-day rolling mean or a 2-year rolling regression. It reveals how a series’ properties change over time (trend, volatility, relationships) and provides realistic, leakage-free features and backtests. The trade-off is choosing the window length: short windows react quickly but are noisy, long windows are stable but slow to reflect change.

Read full answer

Applies topandas 2.2+ (Python 3.12)

Uses

Use Example
Smoothing 7-day rolling mean of daily cases
Volatility tracking 21-day rolling standard deviation of returns
Stability of relationships Rolling correlation or rolling regression beta between a stock and an index
Feature engineering Rolling means, min/max and counts as model inputs
Anomaly detection Flag points far from the rolling mean (rolling z-score)
Backtesting Re-fit a model on a sliding window and forecast the next period

Considerations

  • Window length: match the phenomenon (7 days for weekly patterns, 252 trading days for yearly).

  • Alignment: trailing windows use only past data; centered windows use future data and must not be used for forecasting features.

  • Minimum observations: min_periods controls early values with incomplete windows.

  • Time-based windows: rolling("30D") handles irregular timestamps.

  • Exponentially weighted windows: ewm gives more weight to recent values and avoids hard cut-offs.

Code Example

import numpy as np
import pandas as pd

rng = np.random.default_rng(42)
idx = pd.date_range("2024-01-01", periods=200, freq="D")
returns = pd.Series(rng.normal(0, 0.01, 200), index=idx)
returns.iloc[120:150] *= 3                                  # a high-volatility period

df = pd.DataFrame({"ret": returns})
df["vol_21d"] = df.ret.rolling(21, min_periods=21).std() * np.sqrt(252)     # annualized volatility
df["mean_7d_past"] = df.ret.shift(1).rolling(7).mean()      # feature: uses only past values
df["ewm_vol"] = df.ret.ewm(span=21, adjust=False).std()
z = (df.ret - df.ret.rolling(30).mean()) / df.ret.rolling(30).std()
df["anomaly"] = z.abs() > 3

print(df[["vol_21d"]].iloc[[60, 140, 190]].round(3))        # volatility rises in the turbulent period
print("anomalies flagged:", int(df.anomaly.sum()))

Common Pitfalls

  • Using centered or unshifted windows for features, which leaks the current or future values.

  • Comparing rolling statistics computed with different window lengths.

  • Interpreting rolling correlations from short windows as meaningful changes; they are noisy.

  • Forgetting that the first window - 1 values are missing.

23

How does the ARCH model handle time series volatility?

The ARCH model (Autoregressive Conditional Heteroskedasticity) handles volatility by letting the variance of today’s error depend on the size of recent squared errors. After a large shock, the model predicts higher variance for the next periods, which captures volatility clustering: calm periods followed by turbulent ones, as seen in stock returns. ARCH models forecast risk rather than the level of the series; their limitation is that many lags are often needed, which GARCH solves more compactly.

Read full answer

Applies toarch 7.x and statsmodels 0.14+ (Python 3.12)

The Model

For returns r_t with mean mu:

  • r_t = mu + e_t, with e_t = sigma_t * z_t, where z_t is standard white noise.

  • ARCH(q) variance: sigma_t^2 = omega + alpha_1 * e_{t-1}^2 + ... + alpha_q * e_{t-q}^2

Symbols: - sigma_t^2: conditional variance at time t, given the past. - omega > 0: baseline variance. - alpha_i >= 0: how strongly past squared shocks raise today’s variance. - For a finite long-run variance, sum(alpha_i) < 1; then the unconditional variance is omega / (1 - sum(alpha_i)).

Why It Matters

  • Heteroskedasticity: ordinary models assume constant error variance; financial returns violate this.

  • Risk forecasting: Value at Risk (VaR), option pricing and position sizing need volatility forecasts.

  • Better inference: correct standard errors when variance changes.

Testing for ARCH Effects

  • Look at the ACF of squared returns or residuals: significant autocorrelation suggests ARCH effects.

  • Engle’s ARCH LM test: statsmodels.stats.diagnostic.het_arch; a small p-value indicates ARCH effects.

Code Example

import numpy as np
from arch import arch_model
from statsmodels.stats.diagnostic import het_arch

rng = np.random.default_rng(42)
n, omega, alpha = 2000, 0.2, 0.6
e = np.zeros(n)
for t in range(1, n):                               # simulate ARCH(1)
    sigma2 = omega + alpha * e[t - 1] ** 2
    e[t] = np.sqrt(sigma2) * rng.standard_normal()

print("ARCH LM test p-value:", round(het_arch(e, nlags=5)[1], 6))   # tiny: ARCH effects present

res = arch_model(e, mean="Zero", vol="ARCH", p=1).fit(disp="off")
print(res.params.round(3))                           # omega near 0.2, alpha[1] near 0.6
print(res.forecast(horizon=3).variance.iloc[-1].round(3))           # next 3 variance forecasts

Limitations

  • Needs many lags to capture persistent volatility (GARCH fixes this).

  • Symmetric: positive and negative shocks raise volatility equally (EGARCH and GJR-GARCH address asymmetry).

  • Normal errors often underestimate fat tails; Student-t errors help.

Common Pitfalls

  • Fitting ARCH to prices instead of returns.

  • Ignoring the mean model: remove autocorrelation in the mean (e.g. with an AR term) first.

  • Scaling: very small returns (e.g. 0.001) can cause optimizer problems; the arch package suggests scaling returns to percentages.

24

What is the GARCH model and how is it applied?

GARCH (Generalized ARCH) models time-varying volatility by making today’s variance depend on both recent squared shocks and the previous period’s variance. A GARCH(1,1) model, with just three parameters, captures volatility clustering and persistence in most financial return series, which made it the standard tool for risk measurement. It forecasts volatility rather than direction; its limitations include symmetric response to good and bad news and normal errors that understate extreme moves.

Read full answer

Applies toarch 7.x (Python 3.12)

The GARCH(1,1) Model

  • Returns: r_t = mu + e_t, e_t = sigma_t * z_t, where z_t has mean 0 and variance 1.

  • Variance: sigma_t^2 = omega + alpha * e_{t-1}^2 + beta * sigma_{t-1}^2

Symbols: - omega > 0: constant term. - alpha >= 0: reaction to the latest shock (news). - beta >= 0: persistence of past variance. - alpha + beta < 1: required for a finite long-run variance omega / (1 - alpha - beta). Values close to 1 (common in finance, e.g. 0.98) mean shocks to volatility fade slowly. - Half-life of a volatility shock: log(0.5) / log(alpha + beta) periods.

Variants

Model Feature
GARCH(p, q) More lags
EGARCH Models log-variance; asymmetric effects without positivity constraints
GJR-GARCH Extra term for negative shocks (leverage effect: bad news raises volatility more)
GARCH with Student-t errors Fat tails
Multivariate GARCH (DCC) Time-varying correlations between assets

Applications

  • Value at Risk (VaR) and Expected Shortfall: volatility forecasts give the quantiles of potential losses.

  • Option pricing and implied-volatility comparisons.

  • Portfolio allocation: volatility targeting and risk parity.

  • Risk monitoring: detecting regime changes in volatility.

  • Also outside finance: electricity prices, inflation uncertainty.

Code Example

import numpy as np
from arch import arch_model

rng = np.random.default_rng(42)
n, omega, alpha, beta = 2500, 0.05, 0.08, 0.9
r, sigma2 = np.zeros(n), np.full(n, omega / (1 - alpha - beta))
for t in range(1, n):                                    # simulate GARCH(1,1) percent returns
    sigma2[t] = omega + alpha * r[t - 1] ** 2 + beta * sigma2[t - 1]
    r[t] = np.sqrt(sigma2[t]) * rng.standard_normal()

model = arch_model(r, mean="Constant", vol="GARCH", p=1, q=1, dist="t")
res = model.fit(disp="off")
print(res.params.round(3))                               # alpha[1] near 0.08, beta[1] near 0.9
persistence = res.params["alpha[1]"] + res.params["beta[1]"]
print("persistence:", round(persistence, 3), "| half-life:", round(np.log(0.5) / np.log(persistence), 1), "days")

fc = res.forecast(horizon=5)
print("5-day volatility forecast (%):", np.sqrt(fc.variance.iloc[-1]).round(3).tolist())

Common Pitfalls

  • Fitting on prices instead of returns.

  • Ignoring structural breaks, which make alpha + beta look close to 1.

  • Assuming normal errors for VaR; check standardized residuals for fat tails.

  • Interpreting GARCH as predicting returns; it predicts their variance.

25

What are cointegration and error correction models in time series?

Cointegration describes two or more non-stationary series that move together over the long run, so that some linear combination of them is stationary, for example the prices of two closely substitutable commodities or short- and long-term interest rates. An error correction model (ECM) uses that relationship: it models short-term changes in each series plus a correction term that pulls them back toward their long-run equilibrium when they drift apart. This avoids spurious regressions on trending data while keeping long-run information that plain differencing would throw away.

Read full answer

Applies tostatsmodels 0.14+ (Python 3.12)

Cointegration

  • Series x_t and y_t are each I(1): non-stationary, but stationary after one difference.

  • They are cointegrated if y_t - beta * x_t = u_t is stationary for some beta.

  • beta is the long-run relationship; u_t is the deviation from equilibrium.

Spurious Regression

Regressing one random walk on another unrelated random walk often gives a high R^2 and “significant” coefficients by chance. Checking for cointegration distinguishes real long-run relationships from spurious ones.

Tests

Test Use
Engle-Granger Two series: regress y on x, test the residuals for a unit root (statsmodels.tsa.stattools.coint)
Johansen Several series; finds the number of cointegrating relationships (coint_johansen)

Error Correction Model

Δy_t = a + gamma * (y_{t-1} - beta * x_{t-1}) + b * Δx_t + e_t

  • Δy_t, Δx_t: short-term changes.

  • y_{t-1} - beta * x_{t-1}: last period’s deviation from equilibrium.

  • gamma < 0: speed of adjustment; e.g. gamma = -0.3 closes about 30% of the gap each period.

  • With several series, the multivariate version is the VECM.

Code Example

import numpy as np
import statsmodels.api as sm
from statsmodels.tsa.stattools import coint

rng = np.random.default_rng(42)
n = 500
x = np.cumsum(rng.normal(0, 1, n))                     # random walk
y = 2.0 * x + rng.normal(0, 1, n)                      # cointegrated with x (beta = 2)
z = np.cumsum(rng.normal(0, 1, n))                     # unrelated random walk

print("coint(y, x) p-value:", round(coint(y, x)[1], 4))   # small: cointegrated
print("coint(z, x) p-value:", round(coint(z, x)[1], 4))   # large: not cointegrated

# Step 1: long-run relationship
long_run = sm.OLS(y, sm.add_constant(x)).fit()
resid = long_run.resid                                  # deviation from equilibrium

# Step 2: error correction model on differences
dy, dx = np.diff(y), np.diff(x)
ecm = sm.OLS(dy, sm.add_constant(np.column_stack([resid[:-1], dx]))).fit()
print("beta:", round(long_run.params[1], 2), "| speed of adjustment gamma:", round(ecm.params[1], 2))

For several series, use statsmodels.tsa.vector_ar.vecm.VECM with the rank chosen by select_coint_rank.

Applications

  • Pairs trading: trade the spread between two cointegrated assets when it deviates from equilibrium.

  • Macroeconomics: consumption and income, money demand, exchange rates and prices.

Common Pitfalls

  • Testing cointegration on series that are already stationary.

  • Assuming the relationship is permanent; structural breaks can end cointegration.

  • Using Engle-Granger with more than two series, where it can miss relationships.

26

What are the advantages of state-space models and the Kalman filter?

State-space models describe a time series as noisy observations of an unobserved (latent) state that evolves over time, such as a true level and trend behind noisy sales figures. The Kalman filter is the algorithm that estimates that hidden state step by step, updating its estimate each time a new observation arrives. Together they handle missing data, time-varying parameters, multiple components and real-time updating in one coherent framework; the trade-off is more complex specification and, for the basic Kalman filter, assumptions of linearity and Gaussian noise.

Read full answer

Applies tostatsmodels 0.14+ (Python 3.12)

The Framework

  • State equation: x_t = F x_{t-1} + w_t (how the hidden state evolves; w_t is process noise).

  • Observation equation: y_t = H x_t + v_t (how observations relate to the state; v_t is measurement noise).

  • Kalman filter: alternates predict (project the state forward) and update (correct it with the new observation, weighted by the Kalman gain, which balances trust in the model against trust in the data).

  • Kalman smoother: uses all data, past and future, to estimate past states more accurately.

Advantages

Advantage Why
Missing data The filter simply skips the update step when an observation is missing
Unified modelling ARIMA, exponential smoothing and structural models (level, trend, seasonal, regression) can all be written in state-space form
Interpretable components Separate estimates of level, trend and seasonality with uncertainty
Time-varying parameters Regression coefficients can evolve over time
Real-time updating Efficient recursive updates as data streams in
Likelihood-based estimation Maximum likelihood, confidence intervals, model comparison
Multivariate data Several observed series driven by shared states
Irregular sampling Handles uneven intervals naturally

Code Example

import numpy as np
import pandas as pd
from statsmodels.tsa.statespace.structural import UnobservedComponents

rng = np.random.default_rng(42)
idx = pd.date_range("2018-01-01", periods=72, freq="MS")
level = 100 + np.cumsum(0.8 + rng.normal(0, 0.5, 72))                # slowly drifting level
y = pd.Series(level + 8 * np.sin(2 * np.pi * (idx.month - 1) / 12) + rng.normal(0, 3, 72), index=idx)
y.iloc[[10, 11, 40]] = np.nan                                         # missing observations are fine

model = UnobservedComponents(y, level="local linear trend", freq_seasonal=[{"period": 12, "harmonics": 2}])
res = model.fit(disp=False)

smoothed_level = res.level["smoothed"]                                # Kalman-smoothed hidden level
print("estimated level at last month:", round(float(smoothed_level[-1]), 1),
      "| true level:", round(level[-1], 1))
print("forecast next 3 months:", res.forecast(3).round(1).tolist())

Limitations

  • The standard Kalman filter assumes linear dynamics and Gaussian noise; non-linear problems need the extended or unscented Kalman filter, or particle filters.

  • Specifying the right components and initial values takes care.

  • Estimation can be slow or unstable for large models.

Common Pitfalls

  • Mis-specified noise variances: too small process noise makes the filter slow to react; too large makes it chase noise.

  • Using filtered (real-time) and smoothed (hindsight) estimates interchangeably; only filtered values are available when forecasting.

27

What is the difference between univariate and multivariate time series analysis?

Univariate time series analysis models a single variable using only its own past, for example forecasting electricity demand from past demand. Multivariate time series analysis models several variables together, using the relationships between them, such as demand together with temperature and price, or several interacting economic indicators. Multivariate models can be more accurate and explain how variables influence each other, but they need more data, more parameters and forecasts of any external inputs.

Read full answer

Applies tostatsmodels 0.14+ (Python 3.12)

Comparison

Aspect Univariate Multivariate
Inputs Past values of one series Past values of several series, plus exogenous variables
Models ARIMA, ETS, Prophet, univariate LSTM SARIMAX (exogenous inputs), VAR, VECM, state-space, ML with multiple features
Captures Trend, seasonality, autocorrelation Also cross-correlations, lead-lag effects, feedback
Data and parameters Fewer More (VAR parameters grow with the square of the number of variables)
Forecasting needs Only the series’ history Future values of external drivers, or models for them
Interpretability Simple Impulse responses, Granger causality

Types of Multivariate Setups

  • Exogenous regressors: one target with external inputs that are not modelled (SARIMAX with temperature, holidays).

  • Joint (endogenous) systems: all variables influence each other (VAR: GDP, inflation and interest rates).

  • Cointegrated systems: long-run equilibrium between series (VECM).

  • Global ML models: many series and features in one model.

Code Example: VAR

import numpy as np
import pandas as pd
from statsmodels.tsa.api import VAR

rng = np.random.default_rng(42)
n = 300
x, y = np.zeros(n), np.zeros(n)
for t in range(1, n):                         # x drives y with a one-step lag
    x[t] = 0.5 * x[t - 1] + rng.normal()
    y[t] = 0.3 * y[t - 1] + 0.6 * x[t - 1] + rng.normal()
data = pd.DataFrame({"x": x, "y": y})

res = VAR(data).fit(maxlags=4, ic="aic")
print("selected lag order:", res.k_ar)
print(res.params.round(2))                    # row L1.x, column y: near 0.6
print("3-step forecast:\n", res.forecast(data.values[-res.k_ar:], steps=3).round(2))

When to Choose Which

  • Univariate: many series to forecast quickly, short horizons, no reliable drivers.

  • Multivariate: strong known drivers, policy or scenario analysis, or variables that affect each other.

Common Pitfalls

  • Using drivers that are not known at forecast time (future temperature must itself be forecast).

  • Fitting VAR to non-stationary data without differencing or a VECM.

  • Too many variables and lags for the data length, which overfits.

28

What is Granger causality in time series analysis?

Granger causality tests whether past values of one time series help predict another series beyond what that series’ own past already predicts. If adding lags of x significantly improves forecasts of y, then x “Granger-causes” y. It is a statistical notion of predictive precedence, not true causation: a third variable driving both, or measurement timing, can produce Granger causality without any causal link.

Read full answer

Applies tostatsmodels 0.14+ (Python 3.12)

How the Test Works

Compare two models for y_t:

  • Restricted: y_t = a_0 + sum_{i=1..p} a_i y_{t-i} + e_t

  • Unrestricted: y_t = a_0 + sum_{i=1..p} a_i y_{t-i} + sum_{i=1..p} b_i x_{t-i} + e_t

The null hypothesis is b_1 = ... = b_p = 0 (lags of x add nothing). An F-test (or chi-square test) compares the fits; a small p-value means x Granger-causes y.

Requirements

  • Stationarity: test on stationary series (difference or transform first); otherwise results can be spurious. For cointegrated series, use a VECM-based approach (or the Toda-Yamamoto procedure).

  • Lag length: chosen by information criteria; results can change with the number of lags.

  • Both directions: test x -> y and y -> x; feedback in both directions is possible.

Code Example

import numpy as np
import pandas as pd
from statsmodels.tsa.stattools import grangercausalitytests

rng = np.random.default_rng(42)
n = 500
x = rng.normal(size=n)
y = np.zeros(n)
for t in range(2, n):
    y[t] = 0.4 * y[t - 1] + 0.5 * x[t - 2] + rng.normal()   # x leads y by two steps

data = pd.DataFrame({"y": y, "x": x})
# Tests whether the SECOND column (x) Granger-causes the FIRST (y)
res_xy = grangercausalitytests(data[["y", "x"]], maxlag=3)
res_yx = grangercausalitytests(data[["x", "y"]], maxlag=3)

for lag in (1, 2, 3):
    p_xy = res_xy[lag][0]["ssr_ftest"][1]
    p_yx = res_yx[lag][0]["ssr_ftest"][1]
    print(f"lag {lag}: x->y p={p_xy:.4f}, y->x p={p_yx:.4f}")
# x->y becomes highly significant once lag 2 is included; y->x stays insignificant.

Interpretation and Limits

  • Not true causation: omitted variables, common drivers or measurement delays can create Granger causality.

  • Linear test: non-linear relationships can be missed.

  • Sensitive to sampling frequency: aggregated data (monthly instead of daily) can hide or create lead-lag effects.

  • Multiple testing: testing many pairs and lags inflates false positives.

Common Pitfalls

  • Running the test on trending (non-stationary) data.

  • Reading “x Granger-causes y” as “x causes y” in a report.

  • Reversing the column order in grangercausalitytests.

29

How would you build a time series model to forecast stock prices?

Forecasting stock prices is one of the hardest time series problems, because markets are close to efficient: prices behave much like a random walk, so past prices alone carry little predictable information about future returns. A sound approach models returns rather than prices, compares every model against the naive “tomorrow equals today” forecast, focuses on what is more predictable (volatility, risk), and evaluates with strict walk-forward backtests including transaction costs. The trade-off is that apparent predictive power often disappears out of sample.

Read full answer

Applies totime series concepts (examples in pandas 2.2+ and arch 7.x, Python 3.12)

Approach

  1. Define the target: daily log returns r_t = log(P_t / P_{t-1}), or direction, or volatility; avoid modelling raw prices directly.

  2. Data: adjusted prices (splits, dividends), volume, and possibly market indices, sector data, rates and macro releases, all aligned to when they were known.

  3. Baselines: random walk (zero expected return, last price), historical mean return.

  4. Models:

  5. Mean: ARMA on returns (usually weak), factor models, gradient boosting on engineered features.

  6. Volatility: GARCH-family models, which are much more predictable than returns.

  7. Deep learning: only with large data and strict validation; easy to overfit.

  8. Features: lagged returns, rolling volatility, momentum, volume changes, market and sector returns, all computed from past data only.

  9. Validation: walk-forward (expanding or sliding window) backtests, with a gap for data availability.

  10. Evaluate: error versus the random walk, directional accuracy, and if trading, risk-adjusted returns (Sharpe ratio) net of costs and slippage.

Code Example

import numpy as np
import pandas as pd
from arch import arch_model

rng = np.random.default_rng(42)
idx = pd.bdate_range("2020-01-01", periods=1000)
returns = pd.Series(rng.standard_t(df=5, size=1000) * 0.01, index=idx)   # stand-in daily returns
prices = 100 * np.exp(returns.cumsum())

# Walk-forward: predict tomorrow's return with the trailing 20-day mean versus zero (random walk)
pred_mean = returns.shift(1).rolling(20).mean()
mask = pred_mean.notna()
mae_mean = (returns[mask] - pred_mean[mask]).abs().mean()
mae_rw = returns[mask].abs().mean()
print(f"MAE trailing-mean: {mae_mean:.5f} | MAE zero-return (random walk): {mae_rw:.5f}")

# Volatility is more forecastable: GARCH(1,1) on percentage returns
res = arch_model(returns * 100, vol="GARCH", p=1, q=1, dist="t").fit(disp="off")
print("next-day volatility forecast (%):", round(float(np.sqrt(res.forecast(horizon=1).variance.iloc[-1, 0])), 3))

Common Pitfalls

  • Look-ahead bias: using closing prices to trade at the same close, or data revised later.

  • Survivorship bias: testing only on companies that still exist.

  • Overfitting: trying many strategies and reporting the best backtest.

  • Ignoring costs: small edges vanish after fees and slippage.

  • Plotting predicted prices against actual prices: a lagged copy looks impressive but is just the naive forecast.

30

How is time series analysis used for retail demand forecasting?

Retail demand forecasting predicts how many units of each product will sell at each store or channel over coming days or weeks, so retailers can order, stock and staff correctly. Time series analysis captures trend, weekly and yearly seasonality, holidays and promotions, and modern practice combines statistical baselines with global machine learning models trained across many products. The main challenges are thousands of series, intermittent demand for slow sellers, and the fact that sales understate demand when items are out of stock.

Read full answer

Applies totime series concepts (examples in pandas 2.2+ and scikit-learn 1.5+, Python 3.12)

Approach

  1. Define the forecast: SKU x store x day (or week), horizon matching lead times (e.g. 1-4 weeks).

  2. Prepare data: sales history, prices, promotions, holidays and events, store openings, stock-outs (to correct censored demand), weather if relevant.

  3. Baselines: seasonal naive (same day last week), moving averages.

  4. Models:

  5. Statistical per series: ETS, ARIMA for high-volume items.

  6. Intermittent demand: Croston’s method or its variants (SBA, TSB) for items with many zero days.

  7. Global ML: gradient boosting (LightGBM) on all series with lag, rolling, calendar, price and promotion features; often the strongest in practice.

  8. Hierarchical reconciliation: make store, region and total forecasts consistent.

  9. Probabilistic forecasts: quantiles (e.g. P50, P90) to set safety stock.

  10. Evaluate: WAPE, bias and MASE per category, on rolling-origin backtests.

  11. Operationalize: daily or weekly runs, feeding replenishment systems; monitor errors.

Code Example: Global Model Across Products

import numpy as np
import pandas as pd
from sklearn.ensemble import HistGradientBoostingRegressor

rng = np.random.default_rng(42)
dates = pd.date_range("2023-01-01", periods=365, freq="D")
rows = []
for sku in range(20):
    base = rng.uniform(5, 50)
    promo = rng.random(365) < 0.1
    demand = base * (1 + 0.3 * (dates.dayofweek >= 5)) * (1 + 0.5 * promo) + rng.normal(0, 2, 365)
    rows.append(pd.DataFrame({"date": dates, "sku": sku, "promo": promo.astype(int),
                              "units": np.clip(demand, 0, None).round()}))
df = pd.concat(rows).sort_values(["sku", "date"])

g = df.groupby("sku")["units"]
df["lag_7"] = g.shift(7)
df["roll_mean_28"] = g.transform(lambda s: s.shift(7).rolling(28).mean())   # only data known 7 days ahead
df["dow"] = df.date.dt.dayofweek
df = df.dropna()

train, test = df[df.date < "2023-12-01"], df[df.date >= "2023-12-01"]
features = ["sku", "promo", "lag_7", "roll_mean_28", "dow"]
model = HistGradientBoostingRegressor(categorical_features=[0], random_state=42).fit(train[features], train.units)
pred = model.predict(test[features])

wape = np.abs(test.units - pred).sum() / test.units.sum()
wape_naive = np.abs(test.units - test.lag_7).sum() / test.units.sum()
print(f"WAPE model={wape:.3f} | seasonal naive={wape_naive:.3f}")

Common Pitfalls

  • Treating stock-out days (zero sales) as zero demand.

  • Promotions known in the future not included as features.

  • Using MAPE on slow sellers with many zeros.

  • Forecasting each SKU in isolation when they share patterns.

31

What should you consider when using time series analysis for climate research?

Time series analysis in climate research studies long records of temperature, precipitation, sea level, ice extent and greenhouse gases to detect trends, attribute changes and project the future. It needs special care because climate data has strong seasonal cycles, long-term natural variability (such as El Niño), autocorrelation, changing measurement methods and gaps, and the stakes of misinterpretation are high. Sound analysis separates the signal from natural variability and quantifies uncertainty honestly.

Read full answer

Applies totime series concepts (examples in statsmodels 0.14+, Python 3.12)

Key Considerations

Consideration Why it matters Approach
Seasonality Annual cycles dominate monthly data Use anomalies relative to a baseline climatology (e.g. 1991-2020 monthly means)
Autocorrelation Inflates the apparent significance of trends Trend tests that account for it (Newey-West errors, prewhitening, block bootstrap)
Natural variability ENSO, volcanic eruptions and solar cycles cause swings Include them as regressors, or use long records
Data homogeneity Station moves, new instruments and urbanization create artificial jumps Homogenization, metadata review, changepoint detection
Missing data and coverage Sparse early records, polar gaps Careful interpolation, uncertainty estimates
Non-linear trends and breaks Warming rates change over time Piecewise or smooth trends (LOESS, splines)
Extremes Policy cares about heatwaves and floods, not just means Extreme value analysis (GEV, peaks over threshold)
Spatial dependence Neighbouring stations are correlated Spatio-temporal models, gridded datasets
Attribution Correlation is not cause Physical models and detection-attribution methods
Uncertainty communication Results inform policy Confidence intervals, sensitivity analyses

Code Example: Trend With Autocorrelation-Robust Errors

import numpy as np
import pandas as pd
import statsmodels.api as sm

rng = np.random.default_rng(42)
idx = pd.date_range("1980-01-01", periods=540, freq="MS")
season = 8 * np.sin(2 * np.pi * (idx.month - 1) / 12)
noise = np.zeros(540)
for t in range(1, 540):
    noise[t] = 0.6 * noise[t - 1] + rng.normal(0, 0.3)       # autocorrelated variability
temp = pd.Series(14 + 0.0015 * np.arange(540) + season + noise, index=idx)

# 1. Anomalies: subtract the mean of each calendar month over a baseline period
baseline = temp["1981":"2010"].groupby(temp["1981":"2010"].index.month).mean()
anom = temp - baseline.reindex(temp.index.month).to_numpy()

# 2. Linear trend with HAC (Newey-West) standard errors
X = sm.add_constant(np.arange(len(anom)) / 120)               # time in decades
ols = sm.OLS(anom.to_numpy(), X).fit()
hac = sm.OLS(anom.to_numpy(), X).fit(cov_type="HAC", cov_kwds={"maxlags": 12})
print(f"trend: {hac.params[1]:.3f} °C per decade")             # about 0.18
print(f"std error  OLS: {ols.bse[1]:.4f}  HAC: {hac.bse[1]:.4f}")   # HAC is larger and more honest

Common Pitfalls

  • Fitting trends to short windows that start or end at extreme years (cherry-picking).

  • Ignoring autocorrelation, which overstates significance.

  • Mixing raw values with anomalies from different baselines.

  • Extrapolating statistical trends far into the future instead of using physical climate models.

32

How can time series models improve inventory forecasting in supply chains?

Time series models improve inventory management by forecasting demand, and its uncertainty, over the replenishment lead time, so companies order enough to meet service targets without holding excess stock. Better forecasts reduce stock-outs and overstock, and probabilistic forecasts let you set safety stock from real uncertainty instead of rules of thumb. The challenges are intermittent demand, long and variable lead times, and the bullwhip effect, where small demand changes become large swings upstream.

Read full answer

Applies totime series concepts (examples in NumPy 2.x and SciPy 1.13+, Python 3.12)

How Forecasts Feed Inventory Decisions

  1. Forecast demand per item and location over the lead time, including trend, seasonality, promotions and events.

  2. Quantify uncertainty: forecast error standard deviation or quantile forecasts.

  3. Set safety stock: for a target cycle service level, SS = z * sigma_LT, where z is the normal quantile (1.65 for 95%) and sigma_LT the standard deviation of demand (or forecast error) over the lead time.

  4. Reorder point: ROP = expected demand over the lead time + SS.

  5. Order quantity: e.g. economic order quantity, or order-up-to levels for periodic review.

  6. Monitor service levels and forecast errors; adjust.

Model Choices

Demand pattern Model
Smooth, seasonal ETS / Holt-Winters, SARIMA
Driven by price and promotions Regression or gradient boosting with features
Intermittent (many zeros) Croston, SBA, TSB
Many related items Global ML models, hierarchical forecasting
Highly uncertain Quantile or probabilistic forecasts

Code Example

import numpy as np
from scipy.stats import norm
from statsmodels.tsa.holtwinters import ExponentialSmoothing

rng = np.random.default_rng(42)
weeks = 156
demand = 200 + 30 * np.sin(2 * np.pi * np.arange(weeks) / 52) + rng.normal(0, 15, weeks)

train = demand[:-12]
model = ExponentialSmoothing(train, trend=None, seasonal="add", seasonal_periods=52).fit()
resid_std = np.std(train - model.fittedvalues)                 # one-step error spread

lead_time_weeks, service_level = 3, 0.95
forecast_lt = model.forecast(lead_time_weeks).sum()             # expected demand over the lead time
sigma_lt = resid_std * np.sqrt(lead_time_weeks)                 # assumes independent weekly errors
safety_stock = norm.ppf(service_level) * sigma_lt
reorder_point = forecast_lt + safety_stock
print(f"lead-time demand={forecast_lt:.0f}, safety stock={safety_stock:.0f}, reorder point={reorder_point:.0f}")

Common Pitfalls

  • Using sales instead of demand when stock-outs hide lost sales.

  • Assuming forecast errors are independent and normal when they are autocorrelated or skewed; quantile forecasts are safer.

  • Ignoring lead-time variability, which also needs safety stock.

  • Optimizing forecast accuracy metrics instead of inventory cost and service level.

33

How is time series analysis used for anomaly detection in system monitoring?

Time series anomaly detection for system monitoring finds unusual behaviour in metrics such as CPU usage, latency, error rates or request volume, so teams can respond to incidents early. The core idea is to forecast or model what “normal” looks like, including daily and weekly patterns and trends, and flag points that deviate too much. The main challenges are seasonality, changing baselines, noisy metrics and alert fatigue from false positives.

Read full answer

Applies totime series concepts (examples in pandas 2.2+ and statsmodels 0.14+, Python 3.12)

Challenges

Challenge Example
Seasonality Traffic is high at noon and low at 3 am; a static threshold fails
Trends and level shifts Gradual growth, or a new release changes the baseline
Noise and spikes Short bursts that are harmless
Many metrics Thousands of dashboards; tuning each by hand is impossible
Labels Few labelled incidents to evaluate against
Alert fatigue Too many false alarms and engineers ignore alerts
Correlated metrics One incident triggers dozens of alerts

Strategies

  • Rolling statistics: flag points beyond k rolling standard deviations (simple, but sensitive to seasonality).

  • Seasonal decomposition: STL removes daily and weekly patterns; detect anomalies in the residual using a robust z-score (median and MAD).

  • Forecast-based: forecast the next values (ETS, SARIMA, Prophet); alert when the actual value falls outside the prediction interval.

  • ML methods: Isolation Forest on windows of features; autoencoders or LSTMs for multivariate metrics.

  • Change-point detection: CUSUM or PELT for persistent level shifts.

  • Alert design: require persistence (e.g. 3 of 5 minutes), group related alerts, add severity levels and suppress during deployments.

Code Example

import numpy as np
import pandas as pd
from statsmodels.tsa.seasonal import STL

rng = np.random.default_rng(42)
idx = pd.date_range("2024-03-01", periods=24 * 14, freq="h")          # two weeks of hourly data
latency = 120 + 40 * np.sin(2 * np.pi * idx.hour / 24) + rng.normal(0, 5, len(idx))
latency[200] += 90                                                     # incident spike
latency[250:256] += 45                                                 # short degradation
y = pd.Series(latency, index=idx)

resid = STL(y, period=24, robust=True).fit().resid
mad = np.median(np.abs(resid - np.median(resid)))
robust_z = 0.6745 * (resid - np.median(resid)) / mad                  # MAD-based z-score
alerts = robust_z.abs() > 4

persistent = alerts.astype(int).rolling(3).sum() >= 2                             # 2 of the last 3 hours
print("points flagged:", list(np.where(alerts)[0]))
print("persistent alerts start at:", list(np.where(persistent & ~persistent.shift(1, fill_value=False))[0]))

Common Pitfalls

  • Static thresholds on seasonal metrics.

  • Training “normal” models on data that includes past incidents.

  • Measuring success by detection only; track precision (false alarms) and time-to-detect.

34

How would you forecast electricity consumption with time series analysis?

Forecasting electricity consumption predicts load (in MW or kWh) for the next hours, days or months so that utilities can schedule generation, buy power and plan the grid. Load has strong, multiple seasonalities (daily, weekly, yearly), depends heavily on temperature and on holidays, and changes with economic activity and new technologies. A good approach combines calendar and weather features with a model that handles multiple seasonalities, validated on rolling forecasts against a seasonal naive baseline.

Read full answer

Applies totime series concepts (examples in pandas 2.2+ and scikit-learn 1.5+, Python 3.12)

Approach

  1. Define horizon and granularity: short-term (hourly, next 1-7 days) for operations; long-term (monthly, years) for planning.

  2. Data: historical load, temperature (actual and forecast), humidity, daylight, holidays, special events, and for long term, population and economic indicators.

  3. Explore: daily and weekly profiles, yearly seasonality, the U-shaped relationship with temperature (heating and cooling), holiday effects.

  4. Features:

  5. Calendar: hour, weekday, month, holidays, daylight-saving changes.

  6. Weather: temperature, heating and cooling degree-hours, lagged temperature (buildings respond slowly).

  7. Lags: same hour yesterday and last week.

  8. Models:

Model Notes
Seasonal naive (same hour last week) Baseline
SARIMAX / TBATS / MSTL + ARIMA Handle multiple seasonalities; weather as regressors
Gradient boosting with features Strong for short-term load
Neural networks (LSTM, temporal CNN) Large datasets, many meters
Probabilistic / quantile models Reserve planning
  1. Evaluate: MAPE (load is far from zero, so it is safe here), MAE, peak-hour errors; rolling-origin backtests.

Code Example

import numpy as np
import pandas as pd
from sklearn.ensemble import HistGradientBoostingRegressor

rng = np.random.default_rng(42)
idx = pd.date_range("2023-01-01", periods=24 * 365, freq="h")
temp = 15 + 10 * np.sin(2 * np.pi * (idx.dayofyear - 110) / 365) + rng.normal(0, 2, len(idx))
load = (1000 + 200 * np.sin(2 * np.pi * (idx.hour - 7) / 24) - 150 * (idx.dayofweek >= 5)
        + 8 * (temp - 18) ** 2 + rng.normal(0, 30, len(idx)))
df = pd.DataFrame({"load": load, "temp": temp, "hour": idx.hour, "dow": idx.dayofweek}, index=idx)
df["lag_168"] = df.load.shift(168)                      # same hour last week
df = df.dropna()

train, test = df[:"2023-11-30"], df["2023-12-01":]
features = ["temp", "hour", "dow", "lag_168"]
model = HistGradientBoostingRegressor(max_iter=400, learning_rate=0.05, random_state=42)
model.fit(train[features], train.load)
pred = model.predict(test[features])

mape = np.mean(np.abs((test.load - pred) / test.load)) * 100
mape_naive = np.mean(np.abs((test.load - test.lag_168) / test.load)) * 100
print(f"MAPE model={mape:.2f}% | seasonal naive={mape_naive:.2f}%")

In production, the temperature feature must come from a weather forecast, not observed values, which adds error.

Common Pitfalls

  • Using actual future temperature in backtests (unrealistically good results).

  • Ignoring daylight-saving transitions and holidays.

  • Not modelling the non-linear temperature effect.

35

How do you implement simple exponential smoothing in Python?

Simple exponential smoothing (SES) forecasts a series with no clear trend or seasonality by keeping a smoothed “level” that is updated with each observation: the new level is a weighted average of the latest value and the previous level. Implementing it takes a single loop, plus a search for the smoothing parameter alpha that minimizes one-step-ahead errors. It is fast and robust, but its forecast is flat, so it lags behind any trend.

Read full answer

Applies toNumPy 2.x and statsmodels 0.14+ (Python 3.12)

The Method

  • Level update: l_t = alpha * y_t + (1 - alpha) * l_{t-1}

  • Forecast for any future horizon: y_hat_{t+h} = l_t

  • alpha (between 0 and 1): high values react quickly to new data; low values smooth heavily.

  • Initialization: commonly l_0 = y_1, or estimated together with alpha.

Expanding the recursion shows exponentially decaying weights: l_t = alpha y_t + alpha(1 - alpha) y_{t-1} + alpha(1 - alpha)^2 y_{t-2} + ...

Code Example

import numpy as np
from statsmodels.tsa.holtwinters import SimpleExpSmoothing


def simple_exponential_smoothing(y, alpha, horizon=1):
    """Return (fitted one-step-ahead forecasts, final level, future forecasts)."""
    y = np.asarray(y, dtype=float)
    if not 0 < alpha <= 1:
        raise ValueError("alpha must be in (0, 1]")
    level = y[0]                                     # initialize with the first observation
    fitted = np.empty_like(y)
    for t, value in enumerate(y):
        fitted[t] = level                            # forecast made before seeing y[t]
        level = alpha * value + (1 - alpha) * level  # update with the new observation
    return fitted, level, np.full(horizon, level)


def best_alpha(y, grid=np.linspace(0.01, 1.0, 100)):
    """Choose alpha by minimizing the sum of squared one-step-ahead errors."""
    y = np.asarray(y, dtype=float)
    sse = [np.sum((y[1:] - simple_exponential_smoothing(y, a)[0][1:]) ** 2) for a in grid]
    return grid[int(np.argmin(sse))]


rng = np.random.default_rng(42)
y = 50 + np.cumsum(rng.normal(0, 0.5, 120)) + rng.normal(0, 2, 120)   # drifting level + noise

alpha = best_alpha(y)
fitted, level, forecast = simple_exponential_smoothing(y, alpha, horizon=5)
print(f"chosen alpha={alpha:.2f}, final level={level:.2f}, forecast={forecast.round(2)}")

ref = SimpleExpSmoothing(y, initialization_method="known", initial_level=y[0]).fit(
    smoothing_level=alpha, optimized=False)
print("matches statsmodels:", np.isclose(ref.forecast(1)[0], level))

When to Use It

  • Series without trend or seasonality: stable demand, sensor readings around a level.

  • As a component of Holt and Holt-Winters methods.

  • As a benchmark (it is equivalent to ARIMA(0,1,1)).

Common Pitfalls

  • Using SES on trending or seasonal data.

  • Choosing alpha on the test period.

  • Expecting different forecasts for different horizons: SES forecasts are flat.

36

How do you detect seasonality in a time series with pandas?

Detecting seasonality with pandas combines simple, transparent checks: group the series by a calendar unit (month, weekday, hour) after removing the trend and see whether the averages differ consistently, compute autocorrelation at candidate seasonal lags, and compare seasonal strength across periods. pandas handles the grouping, resampling and lagging; statsmodels adds decomposition when you need it. The trade-off is that simple averages can be misled by trends, outliers and short histories.

Read full answer

Applies topandas 2.2+ (Python 3.12)

Steps

  1. Set a DatetimeIndex with a regular frequency (asfreq or resample).

  2. Remove the trend: subtract a centered rolling mean whose window equals the candidate period.

  3. Group by the calendar unit and compare the means of the detrended values.

  4. Measure consistency: is the pattern the same in each year (or week)?

  5. Check autocorrelation at the candidate lag with Series.autocorr(lag).

  6. Quantify strength: the share of variance explained by the seasonal profile.

Code Example

import numpy as np
import pandas as pd


def seasonality_report(y: pd.Series, period: int, key):
    """Return seasonal profile, its strength (0-1) and the autocorrelation at the seasonal lag."""
    trend = y.rolling(window=period, center=True, min_periods=period).mean()
    detrended = (y - trend).dropna()
    profile = detrended.groupby(key(detrended.index)).mean()          # average seasonal effect
    seasonal = pd.Series(profile.reindex(key(detrended.index)).to_numpy(), index=detrended.index)
    strength = max(0.0, 1 - (detrended - seasonal).var() / detrended.var())
    acf_at_period = y.diff().autocorr(lag=period)                      # differencing removes the trend
    return profile, strength, acf_at_period


rng = np.random.default_rng(42)
idx = pd.date_range("2021-01-01", periods=3 * 365, freq="D")
sales = pd.Series(200 + 0.1 * np.arange(len(idx)) + 30 * (idx.dayofweek >= 5)
                  + rng.normal(0, 10, len(idx)), index=idx)

profile, strength, acf7 = seasonality_report(sales, period=7, key=lambda i: i.dayofweek)
print("weekday effect:\n", profile.round(1))           # weekends (5, 6) clearly higher
print(f"weekly seasonal strength={strength:.2f}, lag-7 autocorrelation={acf7:.2f}")

monthly = sales.resample("MS").mean()
_, strength_m, _ = seasonality_report(monthly, period=12, key=lambda i: i.month)
print(f"yearly seasonal strength on monthly means={strength_m:.2f}")   # near 0: none simulated

Interpreting the Results

  • Strength above about 0.6 and a clearly positive autocorrelation at the period: strong seasonality.

  • Strength near 0: no meaningful seasonality at that period.

  • Check several periods (7, 30, 365 for daily data; 24 and 168 for hourly).

Common Pitfalls

  • Grouping raw values without detrending: a trend makes later months look “seasonally” higher.

  • Too short a history: fewer than two full cycles cannot confirm seasonality.

  • Irregular timestamps: resample to a regular frequency first.

37

How do you fit an ARIMA model in Python and plot its forecasts?

Fitting an ARIMA model in Python with statsmodels involves preparing a regularly spaced series, choosing the orders (p, d, q) with stationarity tests and information criteria, fitting statsmodels.tsa.arima.model.ARIMA, checking the residuals, and plotting forecasts with confidence intervals against a held-out period. It gives interpretable forecasts with uncertainty; the trade-off is that ARIMA assumes linear dynamics and needs seasonal terms (SARIMA) when seasonality is present.

Read full answer

Applies tostatsmodels 0.14+ and matplotlib 3.8+ (Python 3.12)

Code Example

import itertools
import matplotlib
matplotlib.use("Agg")                        # write to a file; remove in a notebook
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from statsmodels.stats.diagnostic import acorr_ljungbox
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.tsa.stattools import adfuller

# 1. Data: a monthly series with a stochastic trend
rng = np.random.default_rng(42)
idx = pd.date_range("2015-01-01", periods=120, freq="MS")
e = rng.normal(0, 2, 120)
y = pd.Series(100 + np.cumsum(0.8 + e + 0.5 * np.r_[0, e[:-1]]), index=idx, name="value")
train, test = y[:-18], y[-18:]

# 2. Choose d with the ADF test
d = 0 if adfuller(train)[1] < 0.05 else 1
print("ADF p (levels):", round(adfuller(train)[1], 3), "-> d =", d)

# 3. Choose p and q by AIC on a small grid
trend = "t" if d == 1 else "c"               # drift term when differencing
candidates = [(p, q, ARIMA(train, order=(p, d, q), trend=trend).fit().aic)
              for p, q in itertools.product(range(3), range(3))]
p, q, aic = min(candidates, key=lambda c: c[2])
print(f"chosen order: ({p}, {d}, {q}), AIC={aic:.1f}")

# 4. Fit and check residuals
res = ARIMA(train, order=(p, d, q), trend=trend).fit()
print("Ljung-Box p:", round(float(acorr_ljungbox(res.resid[d:], lags=[12])["lb_pvalue"].iloc[0]), 3))

# 5. Forecast with intervals and evaluate
fc = res.get_forecast(steps=len(test))
mean, ci = fc.predicted_mean, fc.conf_int(alpha=0.05)
print("MAE:", round((test - mean).abs().mean(), 2), "| naive MAE:", round((test - train.iloc[-1]).abs().mean(), 2))

# 6. Plot
fig, ax = plt.subplots(figsize=(10, 4))
train.plot(ax=ax, label="train")
test.plot(ax=ax, label="actual")
mean.plot(ax=ax, label="forecast")
ax.fill_between(ci.index, ci.iloc[:, 0], ci.iloc[:, 1], alpha=0.2, label="95% interval")
ax.legend(); ax.set_title(f"ARIMA({p},{d},{q}) forecast")
fig.tight_layout(); fig.savefig("arima_forecast.png")

Notes

  • res.summary() shows coefficients, standard errors and diagnostics.

  • res.plot_diagnostics() draws residual plots, a histogram, a Q-Q plot and the residual ACF.

  • pmdarima.auto_arima automates the order search, but understand what it picks.

  • For seasonal data, use SARIMAX(order=..., seasonal_order=(P, D, Q, s)).

Common Pitfalls

  • Selecting orders on the full series and then evaluating on part of it.

  • Ignoring the widening intervals: long-horizon ARIMA forecasts are uncertain.

  • Forgetting that differencing makes the first d residuals meaningless (skipped above).

38

How do you fit and interpret a GARCH model on financial returns?

Fitting a GARCH model to financial data means converting prices to returns, specifying a mean model and a GARCH(1,1) variance model (often with Student-t errors), estimating it by maximum likelihood with the arch package, and interpreting the parameters as the reaction of volatility to news (alpha), its persistence (beta) and its long-run level. The output gives conditional volatility over time and volatility forecasts for risk management. It models risk, not the direction of prices.

Read full answer

Applies toarch 7.x and pandas 2.2+ (Python 3.12)

Code Example

import numpy as np
import pandas as pd
from arch import arch_model
from statsmodels.stats.diagnostic import acorr_ljungbox

# 1. Prices -> percentage log returns (simulated here; use your own price series)
rng = np.random.default_rng(42)
n, omega, alpha, beta = 2000, 0.03, 0.09, 0.89
r, s2 = np.zeros(n), np.full(n, omega / (1 - alpha - beta))
for t in range(1, n):
    s2[t] = omega + alpha * r[t - 1] ** 2 + beta * s2[t - 1]
    r[t] = 0.03 + np.sqrt(s2[t]) * rng.standard_t(6) / np.sqrt(6 / 4)   # unit-variance t shocks
idx = pd.bdate_range("2017-01-02", periods=n)
prices = pd.Series(100 * np.exp(np.cumsum(r) / 100), index=idx)
returns = 100 * np.log(prices).diff().dropna()            # percent scale helps the optimizer

# 2. Fit GARCH(1,1) with a constant mean and Student-t errors
res = arch_model(returns, mean="Constant", vol="GARCH", p=1, q=1, dist="t").fit(disp="off")
print(res.summary().tables[1])                          # mean model
print(res.params.round(4))

# 3. Interpret
a, b, w = res.params["alpha[1]"], res.params["beta[1]"], res.params["omega"]
print(f"persistence alpha+beta = {a + b:.3f}")
print(f"long-run daily volatility = {np.sqrt(w / (1 - a - b)):.2f}%  (annualized {np.sqrt(252 * w / (1 - a - b)):.1f}%)")
print(f"half-life of volatility shocks = {np.log(0.5) / np.log(a + b):.1f} days")

# 4. Diagnostics: standardized residuals should have no remaining ARCH effects
std_resid = res.std_resid.dropna()
print("Ljung-Box p on squared std. residuals:",
      round(float(acorr_ljungbox(std_resid ** 2, lags=[10])["lb_pvalue"].iloc[0]), 3))

# 5. Forecast volatility for the next 5 days
print("5-day volatility forecast (%):", np.sqrt(res.forecast(horizon=5).variance.iloc[-1]).round(3).tolist())

Interpreting the Parameters

Parameter Meaning Typical daily equity values
mu Average return Small, often insignificant
omega Baseline variance Small positive
alpha[1] Reaction to yesterday’s squared shock 0.05-0.15
beta[1] Persistence of variance 0.80-0.95
nu Degrees of freedom of the t distribution 4-10: fat tails
  • alpha + beta close to 1 means volatility shocks decay slowly.

  • A small nu confirms fat tails, so a normal distribution would understate extreme losses.

  • res.conditional_volatility gives the estimated volatility path; plot it to see calm and turbulent regimes.

Using the Results

  • Value at Risk: 1-day 99% VaR is approximately -(mu + sigma_forecast * q_0.01), where q_0.01 is the 1% quantile of the fitted standardized distribution.

  • Volatility targeting: scale positions inversely to forecast volatility.

Common Pitfalls

  • Fitting on prices or on very small decimal returns.

  • Ignoring the leverage effect; compare with GJR-GARCH (o=1) using AIC/BIC.

  • Using in-sample volatility to judge forecast skill; evaluate out of sample against realized volatility.

39

How do you decompose a time series into trend, seasonality and residuals with statsmodels?

Decomposing a time series splits it into a trend (long-term movement), a seasonal component (repeating pattern) and residuals (what remains). In statsmodels, seasonal_decompose does a classical decomposition with moving averages, and STL (Seasonal-Trend decomposition using LOESS) does a more flexible, robust one. Decomposition is used to understand data, detect anomalies in the residuals and build seasonally adjusted series; the trade-off is that classical decomposition is rigid and loses values at the edges.

Read full answer

Applies tostatsmodels 0.14+ and matplotlib 3.8+ (Python 3.12)

Additive Versus Multiplicative

  • Additive: y_t = T_t + S_t + R_t, when seasonal swings have a constant size.

  • Multiplicative: y_t = T_t * S_t * R_t, when swings grow with the level (often fixed by taking logs and using an additive model).

Classical Versus STL

Aspect seasonal_decompose STL
Method Centered moving average + average seasonal profile Iterative LOESS smoothing
Seasonal pattern Fixed every year Can change slowly over time
Edges Trend missing for the first and last half-period Estimated everywhere
Outliers Sensitive robust=True downweights them
Multiplicative Supported directly Use logs

Code Example

import matplotlib
matplotlib.use("Agg")                         # save to a file; remove in a notebook
import numpy as np
import pandas as pd
from statsmodels.tsa.seasonal import STL, seasonal_decompose

rng = np.random.default_rng(42)
idx = pd.date_range("2016-01-01", periods=96, freq="MS")
trend = 100 + 1.2 * np.arange(96)
season = 1 + 0.15 * np.sin(2 * np.pi * (idx.month - 1) / 12)
y = pd.Series(trend * season * rng.normal(1, 0.02, 96), index=idx, name="sales")
y.iloc[50] *= 1.4                                           # an outlier

# 1. Classical multiplicative decomposition
classic = seasonal_decompose(y, model="multiplicative", period=12)
print("classical seasonal factors (Jan-Mar):", classic.seasonal.iloc[:3].round(3).tolist())
print("missing trend values at the edges:", int(classic.trend.isna().sum()))   # 12

# 2. Robust STL on the log series (multiplicative -> additive)
stl = STL(np.log(y), period=12, robust=True).fit()
components = pd.DataFrame({"trend": np.exp(stl.trend), "seasonal_factor": np.exp(stl.seasonal),
                           "residual": stl.resid})
print(components.iloc[48:53].round(3))                      # the outlier shows up in the residual

seasonally_adjusted = y / np.exp(stl.seasonal)
fig = stl.plot()
fig.savefig("stl_decomposition.png")

Uses

  • Exploration: how strong are trend and seasonality?

  • Seasonal adjustment: compare months fairly (e.g. economic indicators).

  • Anomaly detection: large residuals.

  • Forecasting: forecast the seasonally adjusted series and add the seasonal component back (STLForecast in statsmodels).

Common Pitfalls

  • Choosing the wrong period (e.g. 12 for weekly data).

  • Using additive decomposition when seasonal swings grow with the level.

  • Using the centered trend as a forecasting feature (it uses future data).

40

How do you calculate and plot the ACF and PACF in Python?

The ACF and PACF can be calculated with statsmodels.tsa.stattools.acf and pacf, which return the values (and optionally confidence intervals), and plotted with plot_acf and plot_pacf from statsmodels.graphics.tsaplots. Wrapping both in one function makes it easy to inspect any series, including its differenced version, before choosing ARIMA orders. The main decisions are the number of lags, whether to difference first, and how to read the significance bands.

Read full answer

Applies tostatsmodels 0.14+ and matplotlib 3.8+ (Python 3.12)

Code Example

import matplotlib
matplotlib.use("Agg")                         # save to a file; remove in a notebook
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.tsa.stattools import acf, pacf


def acf_pacf_report(series, lags=24, diff=0, alpha=0.05, path="acf_pacf.png"):
    """Compute ACF/PACF (optionally after differencing), list significant lags and save a plot."""
    s = pd.Series(series, dtype=float).dropna()
    for _ in range(diff):
        s = s.diff().dropna()
    lags = min(lags, len(s) // 2 - 1)                 # PACF needs lags < n / 2

    acf_vals, acf_ci = acf(s, nlags=lags, alpha=alpha)
    pacf_vals, pacf_ci = pacf(s, nlags=lags, alpha=alpha, method="ywm")
    band = 1.96 / np.sqrt(len(s))                     # approximate 95% band for white noise
    sig_acf = [k for k in range(1, lags + 1) if abs(acf_vals[k]) > band]
    sig_pacf = [k for k in range(1, lags + 1) if abs(pacf_vals[k]) > band]

    fig, axes = plt.subplots(2, 1, figsize=(10, 6))
    plot_acf(s, lags=lags, alpha=alpha, ax=axes[0], title=f"ACF (diff={diff})")
    plot_pacf(s, lags=lags, alpha=alpha, ax=axes[1], method="ywm", title=f"PACF (diff={diff})")
    fig.tight_layout()
    fig.savefig(path)
    plt.close(fig)
    return {"significant_acf_lags": sig_acf, "significant_pacf_lags": sig_pacf}


rng = np.random.default_rng(42)
e = rng.normal(size=400)
y = np.zeros(400)
for t in range(2, 400):
    y[t] = 0.5 * y[t - 1] + 0.3 * y[t - 2] + e[t]     # AR(2)

print(acf_pacf_report(y, lags=20))
# Expect many significant ACF lags (gradual decay) and PACF significant at lags 1-2.

How to Read the Output

  • PACF significant at 1-2, then not: AR(2).

  • ACF significant at 1, then not: MA(1).

  • ACF decaying very slowly: non-stationary; call again with diff=1.

  • Spikes at seasonal lags (12, 24): seasonal terms needed.

Notes

  • statsmodels’ plotted bands use Bartlett’s formula for the ACF, which widens with lag; the simple 1.96 / sqrt(n) band in the function is the white-noise approximation.

  • method="ywm" (Yule-Walker with MLE adjustment) is a stable PACF choice; "ols" is another common option.

Common Pitfalls

  • Too many lags relative to the series length.

  • Reading plots of a trending series, where everything looks correlated.

  • Treating isolated borderline spikes at high lags as real structure.

41

How would you forecast tourist arrivals with time series data?

Forecasting tourist arrivals predicts how many visitors will come to a destination each month or week, for planning hotels, transport, staffing and marketing. Tourism data has strong yearly seasonality, holiday and event effects, long-term growth and sensitivity to shocks such as pandemics, economic downturns, exchange rates or travel restrictions. A good strategy combines seasonal models with relevant drivers, handles structural breaks explicitly and reports scenario-based or interval forecasts.

Read full answer

Applies totime series concepts (examples in statsmodels 0.14+, Python 3.12)

Strategy

  1. Data: monthly arrivals by origin market, plus drivers: exchange rates, airfares or flight capacity, origin-country income, holidays (including moving ones like Easter), major events, search trends and bookings as leading indicators.

  2. Explore: seasonality by origin market, trend, outliers and breaks (e.g. 2020-2021).

  3. Handle breaks: exclude or flag shock periods with dummy variables, or model recovery separately, so they do not distort seasonality and trend.

  4. Transform: log arrivals to stabilize variance and make seasonality additive.

  5. Models:

Model Use
Seasonal naive, ETS Baselines
SARIMA / SARIMAX Seasonality plus drivers (exchange rate, airline seats)
Regression with leading indicators Bookings or search interest give early signals
Hierarchical models Total arrivals consistent with arrivals per origin market
Scenario analysis Optimistic, base and pessimistic paths after shocks
  1. Evaluate: rolling-origin backtests for 1-12 month horizons; MAPE and MASE.

Code Example

import numpy as np
import pandas as pd
from statsmodels.tsa.statespace.sarimax import SARIMAX

rng = np.random.default_rng(42)
idx = pd.date_range("2012-01-01", periods=156, freq="MS")
season = 0.35 * np.sin(2 * np.pi * (idx.month - 4) / 12)
shock = ((idx >= "2020-03-01") & (idx <= "2021-06-01")).astype(float)       # travel restrictions
arrivals = pd.Series(np.exp(10 + 0.004 * np.arange(156) + season - 1.5 * shock
                            + rng.normal(0, 0.05, 156)), index=idx)

y = np.log(arrivals)
exog = pd.DataFrame({"shock": shock}, index=idx)
train_end = "2023-12-01"
model = SARIMAX(y[:train_end], exog=exog[:train_end], order=(1, 1, 1),
                seasonal_order=(0, 1, 1, 12)).fit(disp=False)

future_exog = pd.DataFrame({"shock": np.zeros(12)}, index=pd.date_range("2024-01-01", periods=12, freq="MS"))
fc = model.get_forecast(steps=12, exog=future_exog)
forecast = np.exp(fc.predicted_mean)                      # back to arrivals (median forecast)
ci = np.exp(fc.conf_int())
print(pd.DataFrame({"forecast": forecast.round(0), "low": ci.iloc[:, 0].round(0),
                    "high": ci.iloc[:, 1].round(0)}).head(6))

Exponentiating a log forecast gives the median, not the mean; apply a bias adjustment if the mean is needed.

Common Pitfalls

  • Letting shock years distort the seasonal pattern.

  • Using drivers whose future values are unknown without forecasting them too.

  • Single-point forecasts without intervals or scenarios for decision-makers.

42

How would you analyse and forecast server load with time series?

Analysing and predicting server load uses metrics such as requests per second, CPU or memory usage to understand daily and weekly patterns, detect anomalies and forecast future demand, so systems can autoscale in time and teams can plan capacity. A practical approach aggregates the metrics to a suitable interval, models multiple seasonalities and trend, adds known events (deployments, campaigns), and forecasts upper quantiles rather than just averages, because under-provisioning is costlier than over-provisioning.

Read full answer

Applies totime series concepts (examples in pandas 2.2+ and statsmodels 0.14+, Python 3.12)

Approach

  1. Collect and aggregate: e.g. 5-minute or hourly averages and peaks (p95) of requests and CPU.

  2. Explore: daily and weekly cycles, growth trend, spikes from releases, batch jobs or marketing events.

  3. Clean: mark outages (load drops to zero) and incidents so the model does not learn them as normal.

  4. Model:

Horizon Goal Model
Minutes ahead Autoscaling Short-term forecasts: ETS, gradient boosting with recent lags
Days ahead Scheduling, alert baselines MSTL / TBATS / Prophet, gradient boosting with calendar features
Months ahead Capacity planning Trend models on daily peaks with growth scenarios
  1. Forecast quantiles: provision for the 95th percentile plus headroom.

  2. Detect anomalies: actual load outside the forecast interval.

  3. Validate with rolling-origin backtests; measure peak errors, not just averages.

Code Example

import numpy as np
import pandas as pd
from statsmodels.tsa.seasonal import MSTL

rng = np.random.default_rng(42)
idx = pd.date_range("2024-05-01", periods=24 * 42, freq="h")          # six weeks hourly
daily = 300 * np.sin(2 * np.pi * (idx.hour - 8) / 24).clip(min=-0.3)
weekly = -150 * (idx.dayofweek >= 5)
load = pd.Series(1000 + 2 * np.arange(len(idx)) / 24 + daily + weekly + rng.normal(0, 40, len(idx)),
                 index=idx, name="requests_per_s")

res = MSTL(load, periods=(24, 24 * 7)).fit()                          # daily and weekly seasonality
print(res.seasonal.iloc[:3].round(1))                                 # one column per period

# Simple forecast for the next week: last-week seasonal pattern + trend extrapolation
trend = res.trend
slope = (trend.iloc[-1] - trend.iloc[-24 * 7]) / (24 * 7)
future_idx = pd.date_range(load.index[-1] + pd.Timedelta(hours=1), periods=24 * 7, freq="h")
seasonal_next = res.seasonal.iloc[-24 * 7:].sum(axis=1).to_numpy()
forecast = trend.iloc[-1] + slope * np.arange(1, 24 * 7 + 1) + seasonal_next
resid_q95 = np.quantile(res.resid, 0.95)
capacity_needed = (forecast + resid_q95).max() * 1.2                 # p95 noise + 20% headroom
print(f"peak forecast next week: {forecast.max():.0f} req/s; plan for {capacity_needed:.0f} req/s")

Common Pitfalls

  • Forecasting averages when incidents happen at peaks.

  • Training on data with outages or load tests included.

  • Ignoring planned events (product launches, sales) known in advance; add them as features.

  • Assuming linear growth forever; revisit capacity plans regularly.

43

How can time series data help optimize pricing over time?

Using time series data for pricing means learning how demand responds to price over time, separating that response from trend, seasonality and promotions, and then choosing prices for future periods that maximize revenue or profit within business constraints. The core is estimating price elasticity from historical sales and prices, combined with demand forecasts. The difficulty is that historical prices were not set randomly, so naive estimates confuse price effects with demand conditions; experiments or careful modelling are needed.

Read full answer

Applies totime series concepts (examples in statsmodels 0.14+, Python 3.12)

Approach

  1. Collect data: units sold, own price, competitor prices, promotions, stock levels, calendar and seasonality, marketing spend.

  2. Model demand as a function of price and other drivers, controlling for trend and seasonality: log(Q_t) = a + e * log(P_t) + seasonal and trend terms + controls + error, where e is the price elasticity (e.g. -1.8 means a 1% price rise reduces quantity by 1.8%).

  3. Address endogeneity: prices often rise when demand is high. Use price experiments (A/B or switchback tests), instrumental variables (e.g. cost shocks), or at least rich controls.

  4. Forecast demand at candidate prices for upcoming periods.

  5. Optimize: choose prices maximizing expected profit (P - c) * Q(P), subject to constraints (price ladders, competitor gaps, inventory).

  6. Monitor and learn: dynamic pricing systems update estimates as new data arrives (bandit or Bayesian methods).

Code Example

import numpy as np
import pandas as pd
import statsmodels.formula.api as smf

rng = np.random.default_rng(42)
weeks = pd.date_range("2022-01-03", periods=156, freq="W-MON")
price = np.round(rng.uniform(8, 12, 156), 2)                           # varied prices (e.g. tests)
season = 0.2 * np.sin(2 * np.pi * weeks.isocalendar().week.astype(int).to_numpy() / 52)
units = np.exp(6 - 1.8 * np.log(price) + season + 0.002 * np.arange(156) + rng.normal(0, 0.1, 156))
df = pd.DataFrame({"units": units, "price": price, "t": np.arange(156),
                   "sin52": np.sin(2 * np.pi * weeks.isocalendar().week.astype(int).to_numpy() / 52)})

fit = smf.ols("np.log(units) ~ np.log(price) + t + sin52", data=df).fit()
elasticity = fit.params["np.log(price)"]
print(f"estimated elasticity: {elasticity:.2f}")                       # close to -1.8

# Profit-maximizing price with constant elasticity: P* = c * e / (1 + e), for e < -1
unit_cost = 6.0
print(f"optimal price at unit cost {unit_cost}: {unit_cost * elasticity / (1 + elasticity):.2f}")

The closed-form optimum applies only to constant-elasticity demand without constraints; in practice, simulate profit across a grid of allowed prices using the demand forecast.

Common Pitfalls

  • Estimating elasticity from prices that were set in response to demand (biased).

  • Ignoring cross-price effects (cannibalization between products) and competitor reactions.

  • Short-term revenue gains that harm long-term customer trust; add fairness and stability constraints.

45

How do you measure the impact of promotional campaigns on sales with time series?

Measuring a promotion’s impact means estimating the incremental sales it caused: actual sales during and after the campaign minus the sales that would have happened without it (the counterfactual). Time series methods build that counterfactual from pre-campaign patterns, seasonality and unaffected control series, using interrupted time series regression, synthetic control or Bayesian structural time series. The main difficulty is that the counterfactual is never observed, so the result depends on good controls and on accounting for pull-forward and cannibalization effects.

Read full answer

Applies totime series concepts (examples in statsmodels 0.14+, Python 3.12)

Approaches

Method Idea Needs
Interrupted time series (segmented regression) Model trend and seasonality, add a campaign indicator Long, stable pre-period
Difference-in-differences Compare change in promoted versus similar non-promoted stores/products Valid control group with parallel trends
Synthetic control Weighted combination of control series that tracks the treated series before the campaign Several unaffected control series
Bayesian structural time series (e.g. CausalImpact) State-space model with control series as regressors; posterior counterfactual with intervals Control series not affected by the campaign
Randomized experiments (geo or store tests) Randomly assign promotions The gold standard when feasible

What to Account For

  • Seasonality and holidays overlapping the campaign.

  • Pull-forward: customers buying earlier, causing a post-campaign dip.

  • Cannibalization: sales shifting from other products or channels.

  • Halo effects: increases in related products.

  • Margin: incremental profit after discount costs, not just incremental units.

Code Example: Interrupted Time Series With a Control Series

import numpy as np
import pandas as pd
import statsmodels.formula.api as smf

rng = np.random.default_rng(42)
idx = pd.date_range("2024-01-01", periods=180, freq="D")
common = 100 + 10 * (idx.dayofweek >= 5) + 0.05 * np.arange(180)          # shared demand pattern
control = common + rng.normal(0, 3, 180)                                    # store without promotion
promo = ((idx >= "2024-05-01") & (idx < "2024-05-15")).astype(int)
post = ((idx >= "2024-05-15") & (idx < "2024-05-22")).astype(int)
treated = 1.1 * common + 25 * promo - 8 * post + rng.normal(0, 3, 180)     # lift, then pull-forward dip

df = pd.DataFrame({"treated": treated, "control": control, "promo": promo, "post": post,
                   "dow": idx.dayofweek.astype(str)})
fit = smf.ols("treated ~ control + C(dow) + promo + post", data=df).fit(cov_type="HAC", cov_kwds={"maxlags": 7})

lift, dip = fit.params["promo"], fit.params["post"]
print(f"daily lift during promo: {lift:.1f} units (95% CI {fit.conf_int().loc['promo'].round(1).tolist()})")
print(f"daily change after promo: {dip:.1f} units")
print(f"net incremental units: {lift * promo.sum() + dip * post.sum():.0f}")

Common Pitfalls

  • Comparing campaign weeks with the previous weeks without adjusting for seasonality.

  • Control series that were themselves affected (spillover from the campaign).

  • Ignoring the post-campaign period, which overstates the effect.

  • Reporting revenue lift without discount cost.

46

What are current research areas in time series forecasting?

Current research in time series analysis and forecasting focuses on foundation models pretrained on huge collections of series, deep architectures such as transformers and modern MLP- and state-space-based models, probabilistic and hierarchical forecasting, forecasting under distribution shift, causal and counterfactual analysis, and making models efficient and interpretable. A recurring finding is that well-tuned simple baselines remain hard to beat, so rigorous benchmarking is itself an active research theme.

Read full answer

Applies totime series research concepts

Active Areas

Area Focus Examples
Time series foundation models Pretrain on many datasets; zero-shot forecasting on new series Chronos, TimesFM, Moirai, Lag-Llama
Deep forecasting architectures Long-horizon accuracy with efficiency Temporal Fusion Transformer, PatchTST, N-BEATS / N-HiTS, TiDE, simple linear models (DLinear) as strong baselines
State-space and long-sequence models Long memory with linear cost Structured state-space models (S4, Mamba-style)
Probabilistic forecasting Full predictive distributions, not points DeepAR, quantile regression, conformal prediction for valid intervals
Hierarchical and grouped forecasting Coherent forecasts across product/region levels Optimal reconciliation (MinT)
Distribution shift and non-stationarity Robustness to regime changes Reversible instance normalization, online and continual learning
Causal inference with time series Effects of interventions Synthetic control, structural time series, causal discovery
Multimodal forecasting Combine series with text, images or events News-informed financial or demand forecasting; LLM-based approaches
Anomaly detection and change points Reliable detection at scale Deep and self-supervised detectors, benchmark critiques
Interpretability Explaining forecasts Attention analysis, decomposition-based models, SHAP for time series
Efficient and edge forecasting Small, fast models Distillation, lightweight architectures
Benchmarking practice Fair comparisons Large benchmarks (Monash, M-competitions), leakage-free evaluation

Practical Takeaways

  • Foundation models are promising for zero-shot and cold-start situations but should be benchmarked against ETS, seasonal naive and gradient boosting on your own data.

  • Probabilistic forecasts and calibrated intervals are increasingly expected in production.

  • Global models trained across many series are now standard in retail and energy.

Interview Tips

  • Mention one or two concrete models and why they matter (e.g. PatchTST splits series into patches like tokens; N-BEATS uses stacked fully connected blocks).

  • Show awareness of the critique literature: simple linear models have matched complex transformers on several long-horizon benchmarks.

  • Avoid claiming any single model is state of the art for all data.

47

How are Fourier transforms used to analyse time series?

The Fourier transform expresses a time series as a sum of sine and cosine waves of different frequencies, so you can see which periodic cycles are present and how strong each is. In time series work it is used to detect seasonality and hidden periodicities (via the periodogram), filter out noise or unwanted frequencies, and create Fourier features (sine/cosine terms) that model complex seasonality in regression and forecasting. Its limitation is that it assumes the frequencies are constant over the whole series; changing patterns need wavelets or short-time transforms.

Read full answer

Applies toNumPy 2.x and SciPy 1.13+ (Python 3.12)

Key Ideas

  • Discrete Fourier Transform (DFT): X_k = sum_{t=0..n-1} x_t * exp(-2 pi i k t / n), giving amplitude and phase for frequency k / n cycles per time step. The FFT computes it in O(n log n).

  • Periodogram: the squared magnitude |X_k|^2, showing power at each frequency. A peak at frequency f means a cycle with period 1 / f.

  • Frequency resolution: with n observations, you can resolve periods up to about n and distinguish frequencies spaced 1 / n apart.

Uses

Use How
Detect seasonality Peaks in the periodogram (e.g. period 7 in daily data)
Multiple seasonalities Several peaks (24 and 168 in hourly data)
Filtering Zero out high-frequency components (denoise) or low ones (detrend), then inverse FFT
Fourier features Add sin(2 pi k t / P) and cos(2 pi k t / P) for k = 1..K to regression or ML models
Spectral analysis Signals, vibration, EEG, climate oscillations

Code Example

import numpy as np
from scipy.signal import periodogram

rng = np.random.default_rng(42)
n = 24 * 7 * 8                                            # eight weeks of hourly data
t = np.arange(n)
y = 10 * np.sin(2 * np.pi * t / 24) + 4 * np.sin(2 * np.pi * t / 168) + rng.normal(0, 2, n)

freqs, power = periodogram(y - y.mean(), fs=1.0)          # fs = 1 sample per hour
top = np.argsort(power)[::-1][:2]
print("dominant periods (hours):", np.round(1 / freqs[top], 1))   # about 24 and 168

# Low-pass filter: keep only components with periods longer than 12 hours
spectrum = np.fft.rfft(y)
f = np.fft.rfftfreq(n, d=1.0)
spectrum[f > 1 / 12] = 0
smooth = np.fft.irfft(spectrum, n=n)
print("noise std removed:", round(np.std(y - smooth), 2))

# Fourier features for a forecasting model (K harmonics of the weekly cycle)
K, P = 3, 168
features = np.column_stack([fn(2 * np.pi * k * t / P) for k in range(1, K + 1) for fn in (np.sin, np.cos)])
print("Fourier feature matrix:", features.shape)          # (1344, 6)

Common Pitfalls

  • Not removing the mean or trend first, which puts most power at the lowest frequencies.

  • Spectral leakage when the series does not contain whole cycles; apply a window (e.g. Hann) or use Welch’s method.

  • Applying Fourier analysis to patterns whose period changes over time; use wavelets or STFT.

48

What is wavelet analysis in time series?

Wavelet analysis decomposes a time series into components that are localized in both time and frequency, using short, wave-like functions (wavelets) that are stretched and shifted along the series. Unlike the Fourier transform, which tells you which frequencies exist over the whole series, wavelets show when each frequency is present, which makes them ideal for non-stationary data with changing cycles, bursts or transients. The trade-off is more choices (wavelet type, scales) and results that are harder to interpret than a simple spectrum.

Read full answer

Applies toPyWavelets 1.6+ (Python 3.12)

Fourier Versus Wavelets

Aspect Fourier transform Wavelet transform
Basis functions Infinite sines and cosines Short, localized wavelets
Output Frequency content of the whole series Time-frequency (time-scale) map
Changing periodicities Smeared out Located in time
Typical use Stable seasonality Bursts, transients, evolving cycles

Two Main Transforms

  • Continuous wavelet transform (CWT): computes coefficients at many scales and every time point, producing a scalogram (heat map of power over time and period). Common wavelet: Morlet.

  • Discrete wavelet transform (DWT): splits the series into an approximation (low frequency) and detail coefficients (high frequency) at successive levels; fast and invertible. Common wavelets: Daubechies (db4), Haar.

Applications

  • Denoising: shrink small detail coefficients (wavelet thresholding) and reconstruct.

  • Detecting changing cycles: El Niño variability, business cycles, heart rate variability.

  • Feature extraction: wavelet energies by level as inputs to classifiers (machine vibration, EEG).

  • Multi-resolution forecasting: forecast each level separately and recombine.

  • Anomaly detection: sudden spikes in high-frequency detail coefficients.

Code Example

import numpy as np
import pywt

rng = np.random.default_rng(42)
n = 1024
t = np.arange(n)
signal = np.where(t < 512, np.sin(2 * np.pi * t / 64), np.sin(2 * np.pi * t / 16))   # period changes halfway
noisy = signal + rng.normal(0, 0.4, n)

# 1. Continuous wavelet transform: which period dominates at each time?
scales = np.arange(2, 128)
coef, freqs = pywt.cwt(noisy, scales, "morl")
periods = 1 / freqs
dominant = periods[np.abs(coef).argmax(axis=0)]
print("dominant period early:", round(float(np.median(dominant[100:400])), 1),
      "| late:", round(float(np.median(dominant[600:900])), 1))   # about 64 then about 16

# 2. Discrete wavelet denoising (soft thresholding of detail coefficients)
coeffs = pywt.wavedec(noisy, "db4", level=4)
sigma = np.median(np.abs(coeffs[-1])) / 0.6745                   # noise estimate from finest level
thresh = sigma * np.sqrt(2 * np.log(n))                          # universal threshold
denoised = pywt.waverec([coeffs[0]] + [pywt.threshold(c, thresh, mode="soft") for c in coeffs[1:]], "db4")
print("error before:", round(np.std(noisy - signal), 3), "after:", round(np.std(denoised[:n] - signal), 3))

Common Pitfalls

  • Edge effects (the “cone of influence”): coefficients near the series boundaries are unreliable.

  • Choosing a wavelet without considering the signal shape.

  • Reading scales as periods without converting them (the conversion depends on the wavelet).

49

What is the potential of RNNs for time series forecasting?

Recurrent neural networks (RNNs) process a time series step by step while carrying a hidden state that summarizes the past, which makes them a natural fit for sequential data. Their potential in forecasting lies in learning non-linear dynamics, using many input variables and covariates, producing multi-step and probabilistic forecasts, and training one global model across thousands of related series. Their weaknesses are the need for large datasets, slower sequential training, sensitivity to tuning and, for plain RNNs, vanishing gradients that LSTM and GRU cells were designed to fix.

Read full answer

Applies todeep learning concepts (examples in PyTorch 2.x, Python 3.12)

RNN Variants

Variant Key idea Notes
Simple (Elman) RNN h_t = tanh(W_x x_t + W_h h_{t-1} + b) Struggles with long dependencies (vanishing gradients)
LSTM Gates and a cell state control memory Captures long-range patterns; most widely used
GRU Fewer gates (update, reset) Similar accuracy, faster
Bidirectional RNN Reads sequences both ways Useful for classification or smoothing, not for forecasting (uses the future)
Encoder-decoder (seq2seq) Encodes history, decodes a forecast sequence Multi-step forecasting
DeepAR-style models Autoregressive RNN that outputs distribution parameters Probabilistic forecasts across many series

Where RNNs Shine

  • Many related series: demand for thousands of products, where a global model learns shared patterns.

  • Rich covariates: prices, promotions, weather and calendar features interacting non-linearly.

  • Irregular or complex dynamics that linear models cannot capture.

  • Probabilistic outputs for inventory and capacity decisions.

Limitations

  • Data hungry: on a single short series, ETS or ARIMA usually wins.

  • Sequential computation: slower to train than CNNs and transformers, which process time steps in parallel.

  • Extrapolation: like other neural nets, they handle levels outside the training range poorly unless the data is normalized per window or series.

  • Competition: temporal CNNs, transformers, N-BEATS and gradient boosting with lag features are strong alternatives.

Code Example: A GRU Forecaster

import torch
from torch import nn

class GRUForecaster(nn.Module):
    def __init__(self, n_features, hidden=32, horizon=7):
        super().__init__()
        self.gru = nn.GRU(n_features, hidden, batch_first=True)
        self.head = nn.Linear(hidden, horizon)

    def forward(self, x):                 # x: (batch, time steps, features)
        _, h = self.gru(x)                # h: (1, batch, hidden), the final hidden state
        return self.head(h[-1])           # (batch, horizon)

torch.manual_seed(42)
model = GRUForecaster(n_features=3)
history = torch.randn(16, 28, 3)          # 16 windows of 28 days with 3 features (sales, price, promo)
print(model(history).shape)               # torch.Size([16, 7]): next 7 days

Common Pitfalls

  • Using bidirectional layers for forecasting, which leaks future information.

  • Random train/test splits instead of time-based ones.

  • Skipping baselines; report seasonal naive and ETS alongside the RNN.

  • Not normalizing each series, so large-volume series dominate training.

50

How can LSTM networks be used for complex time series analysis?

LSTM networks (Long Short-Term Memory) are recurrent neural networks with gates that control what information to keep, update or forget, which lets them learn long-range, non-linear dependencies in sequences. For complex time series they are used for multi-step forecasting with many input variables, sequence classification (such as activity recognition from sensors), anomaly detection via prediction or reconstruction error, and sequence-to-sequence tasks. They need more data, tuning and compute than classical models, and on many business series a tuned statistical or gradient-boosting model performs as well.

Read full answer

Applies toPyTorch 2.x (Python 3.12)

How an LSTM Cell Works

At each time step, with input x_t, previous hidden state h_{t-1} and cell state c_{t-1}:

  • Forget gate f_t = sigmoid(W_f [h_{t-1}, x_t] + b_f): how much of the old cell state to keep.

  • Input gate i_t and candidate g_t = tanh(...): what new information to add.

  • Cell update c_t = f_t * c_{t-1} + i_t * g_t: the additive update helps gradients flow over long sequences.

  • Output gate o_t and hidden state h_t = o_t * tanh(c_t).

Uses for Complex Tasks

Task Setup
Multivariate forecasting Windows of many features -> next value(s)
Multi-step forecasting Direct multi-output head, or encoder-decoder (seq2seq)
Sequence classification Final hidden state -> class (ECG, activity recognition)
Anomaly detection Large prediction or reconstruction error (LSTM autoencoder)
Global models One LSTM trained across many related series (e.g. DeepAR-style)

Code Example

import numpy as np
import torch
from torch import nn

torch.manual_seed(42)
rng = np.random.default_rng(42)
t = np.arange(2000)
series = np.sin(2 * np.pi * t / 50) + 0.5 * np.sin(2 * np.pi * t / 7) + 0.1 * rng.normal(size=2000)

window, horizon = 60, 10
X = np.stack([series[i:i + window] for i in range(len(series) - window - horizon)])
Y = np.stack([series[i + window:i + window + horizon] for i in range(len(series) - window - horizon)])
split = int(0.8 * len(X))                                          # time-ordered split
mean, std = series[:split + window].mean(), series[:split + window].std()   # scale with training data only
to_t = lambda a: torch.tensor((a - mean) / std, dtype=torch.float32)
X_tr, Y_tr, X_te, Y_te = to_t(X[:split]).unsqueeze(-1), to_t(Y[:split]), to_t(X[split:]).unsqueeze(-1), to_t(Y[split:])

class LSTMForecaster(nn.Module):
    def __init__(self, hidden=64, horizon=10):
        super().__init__()
        self.lstm = nn.LSTM(input_size=1, hidden_size=hidden, num_layers=2, batch_first=True, dropout=0.1)
        self.head = nn.Linear(hidden, horizon)            # direct multi-step output

    def forward(self, x):
        out, _ = self.lstm(x)
        return self.head(out[:, -1])                      # last time step's hidden state

model = LSTMForecaster(horizon=horizon)
opt = torch.optim.Adam(model.parameters(), lr=1e-3)
for epoch in range(15):
    for i in torch.randperm(len(X_tr)).split(64):         # shuffling windows within training is fine
        loss = nn.functional.mse_loss(model(X_tr[i]), Y_tr[i])
        opt.zero_grad(); loss.backward(); opt.step()

model.eval()
with torch.no_grad():
    mae = (model(X_te) - Y_te).abs().mean().item() * std
naive_mae = np.abs(Y[split:] - X[split:, -1:]).mean()     # repeat the last value
print(f"LSTM MAE={mae:.3f} | naive MAE={naive_mae:.3f}")

Common Pitfalls

  • Scaling with statistics from the whole series (leakage).

  • Comparing only with other neural models; always include naive and statistical baselines.

  • Long training on small datasets, which overfits.

  • Forgetting that transformers, temporal CNNs and simple linear models often match LSTMs on long horizons.

Sign in to keep reading

You've opened 2 full answers. Sign in to read all 50 Time Series answers, mark what you know and pick up where you left off.

Report a mistake

What's wrong?

All Machine Learning & Data Science subjects