Time Series & Data Issues
P5.ml-methods.05 · Audience: Guest, IT/ML learner, Language professional · Prerequisites: Causal Inference & Experiments
Where next?
Later in ML Methods
Go up a level
P5.ml-methods.05 · Audience: Guest, IT/ML learner, Language professional · Prerequisites: Causal Inference & Experiments
Later in ML Methods
Go up a level
The most convincing offline score can be an artefact of how the data was handled, because the easiest thing to leak into training is information from the future or from the very rows you test on. This module works through the three situations where that leak happens most often in applied work — modelling data that arrives ordered in time, learning a class so rare that the usual scores flatter a useless model, and filling in values that are missing for a reason. Every trap is demonstrated with a handful of concrete numbers before the fix is given, since these judgement calls are exactly what applied-ML interviews single out.
When the Time Series pillar (P7) launches, it will promote the topics of this module — stationarity, ARIMA, and time-based validation — into full tracks of its own and cross-link back here. This module's address, P5.ml-methods.05, stays stable.
Why it matters — Regrouping is navigation-only: bookmarks, prerequisites, and coverage-matrix references to this module keep working unchanged.
Framing. Each topic hides a leakage trap: shuffling time series, resampling before the split, or imputing without asking why data is missing. Each step shows the trap in numbers.
A series is stationary when mean, variance, and autocovariance are time-invariant; most classical models assume it. ARIMA(p, d, q) = AR(p) + d orders of differencing to reach stationarity + MA(q). ACF/PACF diagnose the orders; ADF + KPSS test the assumption.
Worked example — differencing removes a trend. A trending series and its first difference ∇Xₜ = Xₜ − Xₜ₋₁:
X = [10, 12, 15, 19, 24] # rising mean -> non-stationary diff X = [ 2, 3, 4, 5] # first difference (d=1) # the differenced series has no trend -> closer to stationary
The raw series' mean climbs, so it is non-stationary; the first difference [2, 3, 4, 5] strips the level trend (that's the d in ARIMA). Validation caveat: time-ordered data must be split by time — use forward-chaining (train on the past, test on the future). Shuffling leaks future information backward and inflates offline scores.
Interview-depth applied ML: time-series stationarity and autocorrelation, class imbalance and SMOTE pitfalls, and missing-data mechanisms — with the assumptions, the leakage traps, and the 'why it matters' framing.
Back to the foundations: probability and statistics basics live in the P1 statistics-core track, and Advanced Probability (P1.statistics-advanced.01) covers the estimation and sampling machinery.
A series is (weakly) stationary when its statistical properties are time-invariant: a constant mean, a constant variance, and an autocovariance γ(h) = Cov(Xₜ, Xₜ₊ₕ) depending only on the lag h, never on absolute time t. Most classical time-series models assume stationarity, because a shifting mean or variance makes yesterday's fitted parameters useless tomorrow.
Autocorrelation quantifies self-dependence across lags. The ACF is the normalised autocovariance ρ(h) = γ(h)/γ(0); the PACF removes the influence of the intermediate lags, isolating the direct lag-h effect. Together they diagnose the order of a model — a sharp PACF cut-off suggests AR order p, a sharp ACF cut-off suggests MA order q.
Testing stationarity. Use the ADF test (null: a unit root / non-stationary) alongside the KPSS test (null: stationary) — they are complementary, so reading both guards against a misleading single verdict. When a series is non-stationary, restore stationarity by differencing (∇Xₜ = Xₜ − Xₜ₋₁), de-trending, or a variance-stabilising transform (log, Box–Cox).
ARIMA(p, d, q) ties this together: an AR(p) term (regress on p past values), d orders of differencing to reach stationarity, and an MA(q) term (regress on q past errors). Seasonal SARIMA(p, d, q)(P, D, Q)ₛ adds seasonal AR/MA/differencing at period s.
Why it matters — validation. Time-ordered data must be split by time, never shuffled. Use forward-chaining (expanding- or rolling-window) cross-validation: always train on the past and test on the future. Random shuffling leaks future information backward (look-ahead leakage), inflating offline scores that then collapse in production.
When positives are rare (fraud, disease, defaults), accuracy is misleading: a model that predicts "negative" for everyone scores 99% on a 1%-positive dataset while catching nothing. The optimiser, minimising average loss, is happy to ignore the minority class entirely.
Remedies.
Pitfalls of SMOTE.
The right way to handle missing values depends entirely on why they are missing — Rubin's three mechanisms.
Imputation cautions. Mean imputation understates variance (every gap collapses to one value, deflating standard errors and correlations). Prefer multiple imputation — impute several times to propagate the uncertainty — and add missingness indicator flags so a model can learn whether the fact of being missing is itself predictive.
Rigorous references for applied time-series and data hygiene.
| Type | Resource |
|---|---|
| Book | Hyndman & Athanasopoulos, Forecasting: Principles and Practice — ARIMA |
| Book | Little & Rubin, Statistical Analysis with Missing Data — MCAR/MAR/MNAR |
| Paper | Chawla et al., SMOTE (JAIR 2002) — the original synthetic over-sampling |
| In-app | P5.ml-methods.06 — Model Evaluation, Selection & Interpretability, for the PR-AUC / calibration these tasks need |
| In-app | P1.statistics-advanced.01 — S7-adv Advanced Probability, for the estimation and sampling theory |
Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.
Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.
A scratch console for this page's ideas — ungraded, nothing you run here is recorded.
Scratch console
A scratch console with the scientific stack (pandas, numpy, scikit-learn). Runs on the server — no network, resource-limited and measured.
Output appears here.
Loading your notes...