Machine learning for finance
Financial data breaks most standard ML assumptions. Applying scikit-learn defaults to a price series and reporting the accuracy is the answer that ends the interview.
Why the usual ML playbook fails here
| Assumption | Reality in finance |
|---|---|
| Samples are IID | returns are serially correlated and volatility clusters |
| Distribution is stationary | regimes change; a model fitted pre-2020 is fitted to a different market |
| Shuffling is harmless | shuffling leaks the future into training |
| Signal-to-noise is decent | it is terrible; a 53% hit rate can be a real edge, and 90% accuracy means a bug |
| More features help | with this much noise, more features mostly buy overfitting |
The last row is the one that changes behaviour. In most ML work you add features and validate; here the prior is that a complex model on raw prices finds noise, so simplicity and economic reasoning carry more weight.
Labelling
Predicting the next return directly is a low-information target. Better formulations:
- Fixed horizon with a threshold — up, down, or flat over N bars, with the flat band wide enough to be tradeable. Avoids training the model to predict noise around zero.
- Triple-barrier — label by whichever comes first: a profit target, a stop, or a time limit. This labels what the trade would actually have done, including the path, which fixed-horizon labelling ignores.
- Meta-labelling — a primary model decides direction, a secondary model decides whether to take the trade. It converts a hard directional problem into an easier precision problem and is how you attach position sizing to an existing rule-based strategy.
Validation
Standard k-fold is wrong: it shuffles, so the model trains on the future. Even a chronological split is not enough when labels span multiple bars.
- Walk-forward — fit on a window, test on the next, roll. The credible baseline.
- Purging — drop training samples whose label window overlaps the test set. Without it, an observation labelled over the next 10 bars leaks into a test set that starts 3 bars later.
- Embargo — leave a gap after the test window before resuming training, because serial correlation leaks backwards too.
- Combinatorial purged CV — many train/test path combinations, giving a distribution of out-of-sample results rather than one number.
Sample weighting
Overlapping labels mean samples are not independent, so a bar appearing in many label windows is effectively counted many times. Weight by uniqueness (the inverse of how many labels overlap that bar). Add time decay if recent regimes should matter more.
Financial data is also imbalanced in a specific way: large moves are rare and are exactly what you care about. Weighting samples by absolute return focuses the model on the observations that carry the payoff.
Features that are worth trying
Raw prices are non-stationary; models fitted to levels do not generalise. Transform first:
- Returns, log returns, and volatility-normalised returns
- Rolling statistics with a trailing window only — never centred
- Cross-sectional ranks (where does this asset sit within the universe today), which are naturally stationary
- Regime indicators: realised volatility, term structure, breadth
- Fractional differentiation when you want stationarity without destroying all memory
Every feature must be computable at decision time from data available then. The point-in-time discipline from 01_market_data.md applies to features, not just prices.
Multiple testing
If you test 100 strategies at the 5% level, you expect 5 false positives. The reported Sharpe of the best of many trials is a maximum, not an expectation, and needs deflating for the number of trials. Keeping a research log of everything you tried is not bureaucracy — it is the input to that correction.
Which models
Gradient-boosted trees (XGBoost, LightGBM, CatBoost) dominate tabular financial features and are the sensible default. Deep learning earns its place with sequence or alternative data — order book microstructure, text, images — not on a hundred engineered features over daily bars. See ../../ai_ml/02_classical_ml/ and ../../ai_ml/05_deep_learning/.
Linear models with strong regularisation remain competitive and are far easier to reason about when performance decays, which it will.
Interview angle
- “Why can’t you use standard cross-validation on financial data?” - it shuffles, so the model trains on the future. You need walk-forward with purging (drop training samples whose label window overlaps the test set) and an embargo after it.
- “Your classifier gets 85% accuracy predicting tomorrow’s direction. What’s wrong?” - a leak, almost certainly: a feature computed with future data, a centred rolling window, normalisation over the full series, or a label that shifted the wrong way. Real directional edges live near 51-55%.
- “What is triple-barrier labelling for?” - it labels what the trade would actually have done - hit the target, hit the stop, or timed out - so the model learns the path, not just the endpoint. Fixed-horizon labels ignore that a trade was stopped out on the way.
- “How do you handle overlapping labels?” - weight samples by uniqueness, since overlapping windows mean observations are not independent and the effective sample size is much smaller than the row count.
- “How do you avoid fooling yourself with backtests?” - track how many variants you tested and deflate the Sharpe accordingly, keep a holdout you touch once, and prefer results that sit on a broad parameter plateau rather than a spike.