Machine learning for quantitative finance in ten ideas
On this page
- 1. The target decides the problem
- 2. The data is part of the model
- 3. A linear model is a weighted sum
- 4. Every feature has a timestamp
- 5. A tree asks one question at a time
- 6. Nearest neighbours store everything
- 7. The gap between seen and unseen is the whole game
- 8. A score needs a split, a metric and a baseline
- 9. Averaging helps only when the errors differ
- 10. Model choice is a systems decision
This is the map of the series: ten ideas, one short section each, in the order each one needs the one before it. Each section says what the idea is and why it matters, then links to the post that works it through with formulas, worked numbers and scikit-learn code. Read this page to see the whole shape, then go to the post for the idea you need.
The posts share one problem, so the ideas connect: from a stock’s own past prices, predict how volatile it will be next month (how much its price will swing). The inputs a model learns from, such as last month’s volatilityHow much an asset's price spikes and dips; most often measured as the standard deviation of its per-period returns., are called featuresAn input variable a model uses to make its prediction; also called a predictor or an independent variable..
Every number a model sees must have been knowable at the time. Every score needs a split, a metric and a baseline. Everything else is a choice of model.
The ten ideas at a glance, grouped by the job each one does (the grouping is mine; the numbers are the sections below):
1. The target decides the problem
Before choosing a model, decide what you are predicting. A number (next month’s volatility) makes it a regressionA prediction problem whose answer is a number; when the answer is a category it is classification instead. problem; a labelThe outputs a model learns to predict; also called the target or the response. (“will next month be a stress month, far more volatile than usual, yes or no?”) makes it a classificationA prediction problem whose answer is a category, such as yes or no, rather than a number. problem, and the same inputs can serve both. Every target is part pattern, part noise nobody can predict, and the noise sets a floor no model gets under. On next-month volatility that floor is high: the best model in the series removes only about a third of the error you get by guessing the average every time. Knowing the floor stops you chasing a perfect model that cannot exist. Full post: What is machine learning?.
2. The data is part of the model
The algorithm gets the attention, but the choices made about the data (which rows to keep, how to fill gaps, how to treat extreme days, how to split into training and test) change the result as much as the algorithm does. A few extreme days can tilt a fitted line on their own; shuffling rows before splitting lets the model learn from days that come after the ones it is tested on, which it could never do live, so the test score looks better than the model really is. The post walks through each choice and what it does to the score. Full post: The data is part of the model.
3. A linear model is a weighted sum
The simplest model multiplies each input by a weight and adds them up. Learning means choosing the weights that make the misses on the training dataThe observed examples a method learns from: they are used to teach it how to make its estimate. as small as possible. Each weight reads as “one unit more of this input, everything else held fixed, moves the prediction by this much”, which makes the model easy to explain. RidgeLeast squares with a penalty that shrinks the coefficients towards zero; a tuning parameter sets how strong the shrinking is. and lassoLike ridge, it shrinks coefficients towards zero, but with a large enough penalty it sets some exactly to zero, so it also picks variables. are versions that keep the weights small, so the model does not lean too hard on any one input. Full post: Linear regression.
4. Every feature has a timestamp
For every input, ask: when would I have known this value, and could I compute it live? LeakageInformation that would not be available at prediction time being used to build a model, which makes its performance estimates overly optimistic. is an input that secretly contains the answer, usually because it uses information from the future. It is a common way a model looks brilliant and then fails in production. In the series, adding the market’s volatility over the very month being predicted made the model look clearly better than the honest version. That model could never run for real: at prediction time, that month has not happened yet. Full post: Every feature has a timestamp.
5. A tree asks one question at a time
A decision treeA model that splits the inputs into simple regions with splitting rules drawn as a tree, and predicts from the training rows in each region. is a chain of yes/no questions, such as “was last month’s volatility above 25% a year?”, each one splitting the data into two groups, until each group gets one answer. It is easy to read and needs no scaling (putting the inputs on a common scale first). Its danger is that, left alone, it keeps asking questions until every group holds only one answer: perfect on the past and much worse on new months. Stopping it after three questions in a row did much better on new months than letting it grow. Full post: Decision trees.
6. Nearest neighbours store everything
A completely different idea: learn nothing up front, keep every training row, and answer a new question by finding the most similar past rows and averaging what happened to them. Two things follow. “Similar” means close in distance, so the inputs must be put on the same scale first, or the input with the biggest numbers decides alone. And the model can only repeat what it has seen, so it cannot predict beyond the range of its past. The number of neighbours is the dial: few neighbours follow every quirk, many neighbours smooth them away. Full post: Nearest neighbours.
7. The gap between seen and unseen is the whole game
Making a model more flexible (more questions in a tree, fewer neighbours in nearest neighboursPredict for a new point by finding the K closest training points and averaging their answers.) always lowers its error on the data it trained on. Error on new data falls at first, then rises, because the model starts memorising noise. Compare the error on training data with the error on new data to diagnose any model: both high means it is too simple (underfitWhen a model is too simple for the data, the opposite end from overfitting, so its test error is high.); low on training and much higher on new data means it memorised (overfitWhen a flexible model follows the noise in its training data too closely, so its predictions on new data are poor.). More data helps the second case, not the first. Full post: Bias and variance.
8. A score needs a split, a metric and a baseline
A score means nothing on its own. You need to know which data it was measured on (data the model trained on always flatters it), what it measures, and what a trivial guess scores on the same data. For rare events, accuracy misleads: a model that always says “calm” is right most of the time and useless. The cut-off matters too: the model gives each month a probabilityHow often an event happens if you could repeat a trial without end: the share of trials where it happens. of stress, and you choose the probability above which it raises an alarm. The same model caught about seven in ten real stress months with the cut-off at 50% and more than eight in ten with it at 20%, at the price of four times the false alarms. Choosing the cut-off is a business decision, not a modelling one. Full post: Scoring a model.
9. Averaging helps only when the errors differ
If several models make different mistakes, averaging them cancels part of each mistake; if they all make the same mistake, the average makes it too. BaggingA general way to reduce a method's variance, used most with decision trees: fit on many bootstrap samples and combine. trains many trees, each on a different random sample of the data, and averages them. Random forestsMany decision trees, each grown on a bootstrap sample, where every split may only choose from a random subset of the features. go further and force the trees to differ (each split may only look at a few randomly chosen features), which is why they usually beat plain bagging. Bagging a straight line changes almost nothing: a line barely moves when the data changes a little, so every copy draws nearly the same line. Full post: Bagging.
10. Model choice is a systems decision
The models differ in more than accuracy: how long they take to train, how fast they answer, how much memory they need, whether they can be explained, and whether they can predict beyond what they have seen. In the series, the most volatile month in the training data was 138% a year, and the month after it came in at 52%. Asked about a month twice as wild as anything seen, the tree and neighbour models answered between 53% and 61%, close to that stored outcome, because they can only repeat what they have stored. The straight line followed its slope to 178%. Neither is “right”; it depends on which failure your system can live with. Choose for the system the model runs in, not for the top of a leaderboard. Full post: Choosing a model.