Unsupervised Learning, Dimensionality Reduction, and Financial-Data Pitfalls
PCA and clustering as structure-finders - and the research-integrity essentials: data leakage, look-ahead bias, and backtest overfitting.
Leads to: These pitfalls govern every strategy backtest in Phases 14-18; leakage recurs in time-series (12.6).
Learning Objectives
Click a status chip to cycle: Not started → In progress → Studied → Practiced → Needs review → Mastered.
- Derive principal components as the eigenvectors of the covariance / SVD of the data.
- Explain k-means clustering, its objective, and its non-convex local-minimum behavior.
- Define data leakage and give the exact preprocessing pattern that prevents it.
- Distinguish look-ahead bias, survivorship bias, and backtest overfitting, with financial examples.
- Quantify how multiple testing inflates a backtest's apparent Sharpe ratio.
Key Vocabulary
- PCA
- Principal Component Analysis: orthogonal directions of maximal variance, the eigenvectors of the covariance matrix.
- Explained variance ratio
- The share \(\lambda_j/\sum_k\lambda_k\) of total variance captured by component \(j\).
- k-means
- Partition points into \(K\) clusters minimizing within-cluster squared distance to centroids; non-convex, local optima.
- Data leakage
- Information from outside the training fold (or from the future) contaminating the fit, inflating measured performance.
- Look-ahead bias
- Using data not available at decision time - a special, time-flavored leakage.
- Survivorship bias
- Studying only assets that survived, silently dropping the delisted/defaulted ones and overstating returns.
- Backtest overfitting
- Selecting a strategy by searching many variants, so the winner's performance is largely luck (selection bias).
Intuition & Motivation
Which is why this lesson's real subject is research integrity. Financial data punish carelessness. If you scale or select features using the whole dataset before splitting, the test set has already whispered to the model - leakage. If a rule uses tomorrow's information today - look-ahead bias. If you study only the funds that survived - survivorship. And if you try a thousand strategies and report the best - backtest overfitting. Each produces a beautiful backtest and a losing live account. Knowing the mathematics of these traps is the difference between a quant and a curve-fitter.
PCA: directions of maximal variance
Center the \(n\times p\) data \(X\) (subtract column means). The first principal direction maximizes the projected variance:
whose solution is the top eigenvector of \(\hat\Sigma\) (eigenvalue = the variance along it). Subsequent components are the remaining eigenvectors, orthogonal and ordered by variance. Equivalently, from the SVD \(X=UDV^\top\), the columns of \(V\) are the principal directions and \(d_j^2/(n-1)\) the variances. Keeping the top \(r\) gives the best rank-\(r\) approximation (Eckart-Young). In finance the leading components of a return covariance are recognizable market and sector factors.
k-means clustering
k-means minimizes total within-cluster squared distance \(\sum_{k}\sum_{i\in C_k}\|x_i-\mu_k\|^2\) by alternating assign (each point to its nearest centroid) and update (each centroid to its cluster mean). The objective is non-convex, so the result depends on initialization - restart many times and keep the best, or use k-means++ seeding. You must choose \(K\) (elbow/silhouette), and the method presumes roughly spherical, equal-size clusters - often false for financial data.
The pitfalls: where quant research dies
Now the core of the lesson. Each trap below turns an honest question into a self-deception.
Backtest overfitting, quantified
Suppose you try \(N\) independent strategies, each with a true Sharpe of 0 and an estimated Sharpe that is noisy with standard error \(\approx 1/\sqrt{T}\) (in the same annualized units, over \(T\) years). The maximum of \(N\) such noise draws grows roughly like
so with \(N=1000\) trials over \(T=10\) years the best purely-random strategy shows an apparent Sharpe near \(\sqrt{2\log 1000}/\sqrt{10}\approx 1.2\) - entirely spurious. This is why a headline Sharpe is meaningless without the trial count; the deflated Sharpe ratio corrects for \(N\), and honest research pre-registers hypotheses and reserves an untouched out-of-time set.
Interactive: measure the leakage from scaling before the split
- Standardizing, imputing, or running PCA on the full dataset before splitting - the most common and most invisible leak.
- Selecting features by their correlation with the target on all data, then ‘validating’ on a subset of the same data.
- Backtesting on a survivorship-biased universe (today's index members) and reporting the inflated premium.
- Aligning data to reference dates rather than publication dates, trading on information you could not have had.
- Reporting the best of many strategies with no trial-count adjustment - the winner is mostly luck (eq. 11.17).
- Wrap all preprocessing in a pipeline that is fit inside each CV fold; never touch test statistics during fitting.
- Use point-in-time datasets (as-reported, delisting-inclusive) to kill survivorship and look-ahead bias at the source.
- For overlapping labels, purge training samples near the test window and add an embargo gap (Lopez de Prado).
- Track your trial count and deflate the Sharpe; pre-register the hypothesis and keep one out-of-time set truly untouched.
- Treat PCA factors as unlabeled and unstable - re-estimate on rolling windows and expect the axes to drift (non-stationarity).
Knowledge Check
Practical Exercise
A colleague reports a cross-sectional equity strategy with in-sample Sharpe 2.5. On inspection: features were standardized on the full sample, the universe is the current index, and 300 feature combinations were screened. (a) Name each specific bias present. (b) Explain the mechanism by which each inflates the Sharpe. (c) Redesign the study to be leakage-free and state how you would report the result honestly.
(a) Leakage (full-sample standardization), survivorship bias (current-index universe), and backtest overfitting / selection bias (300 combinations, best reported).
(b) Full-sample scaling leaks future distributional information into every training fold, tightening fit; the surviving-names universe drops the delisted losers, mechanically lifting mean return and cutting drawdown; screening 300 variants and keeping the max is a maximum over noise, adding roughly \(\sqrt{2\log 300}/\sqrt{T}\) of spurious Sharpe.
(c) Use a point-in-time, delisting-inclusive universe; fit all preprocessing inside a walk-forward CV with purge and embargo; freeze the feature set / hypothesis before touching a final out-of-time hold-out; report the number of configurations tried and a deflated Sharpe, plus performance on the untouched hold-out. If the honest, out-of-time Sharpe is, say, 0.4, that - not 2.5 - is the number to trust.
Lesson Summary
Formula Sheet Additions
- Did I split BEFORE any scaling, imputation, feature selection, or PCA?
- Is every timestamp a publication (available) time, not a reference time?
- Does my universe include delisted/defaulted names (no survivorship)?
- Did I record the number of configurations tried and deflate the Sharpe?
- Is there a final out-of-time set I have never looked at?
Retrieval Practice
Close the lesson and answer from memory before checking. This is deliberate, effortful recall - the single highest-yield study action.
A: Maximize projected variance \(w^\top\hat\Sigma w\) over unit \(w\); the solution is the top eigenvector of \(\hat\Sigma\) (from the SVD, the top right singular vector).
A: Split first; fit every transform (scaling, imputation, selection, PCA) on the training fold only, then apply it to validation/test.
A: About \(\sqrt{2\log N}/\sqrt{T}\); e.g. \(N=1000\), \(T=10\) gives roughly 1.2 - purely spurious, corrected by a deflated Sharpe.
Flashcards
Click to flip. These feed the site-wide spaced-repetition queue.
Completion Checklist
- I can explain the core ideas in my own words
- I worked the derivations/examples by hand
- I completed the interactive workbench(es)
- I passed the knowledge check
Source References
This lesson synthesizes and paraphrases concepts from the sources below. No copyrighted text, problem sets, or solutions are reproduced. Return to the originals for full depth.
- The Elements of Statistical Learning (Hastie, Tibshirani & Friedman, 2nd ed.) current - ESL 14 - Ch. 14: unsupervised learning - PCA, k-means, and the dangers of finding structure in noise.
- Time Series Analysis (James Hamilton, 1994) foundational - Hamilton 18 - Non-stationarity and spurious relationships motivating point-in-time, forward-only validation.