3. What machine learning is, exactly
Level 10: Machine Learning Applied to Trading
There is a much-repeated popular definition: machine learning is the field of computer science that gives computers the ability to learn without being explicitly programmed to do so. It works as a headline, but it is vague: it says neither what learning means nor how to check whether it has really happened.
Machine learning was born during the early development of artificial intelligence and pattern recognition and, like every technology, it grew into an industry hand in hand with business. There is a widely used formal definition, due to Tom M. Mitchell:
"A computer program is said to learn from experience E with respect to some class of tasks T and performance measure P, if its performance at tasks in T, as measured by P, improves with experience E."
The definition says two important things: our program has to learn from data, and it has to improve its performance. Both parts are demandable and checkable.
The classic example is predicting the price of a house: first we feed the algorithm data such as current prices by square meter, number of rooms and so on. From that data the algorithm learns and improves its estimates by reducing prediction errors. We can measure performance by running the algorithm on new input data whose outcome we know and observing the error rate. Our goal will be to bring that error rate down.
Translated into a trading system, the three pieces look like this:
- T (task): predict the sign of the S&P 500's return on the following day.
- P (performance measure): directional accuracy on data the model has not seen during training; or better still, in production, the Sharpe ratio of the resulting strategy.
- E (experience): the history of prices, volumes and indicators it is trained on.
This decomposition is more than an academic exercise. It forces you to fix the metric before training, which is exactly the point at which most self-deception creeps into quantitative trading. And it forces P to be measured on unseen data: if P improves on the very data you learned from, you have proved nothing, because memorizing also drives the in-sample error down.
# Mitchell's definition, written as code
def learns(model, growing_experience, test_task, metric):
results = []
for E in growing_experience: # more and more training data
model.fit(E["X"], E["y"])
p = metric(test_task["y"], model.predict(test_task["X"]))
results.append(p)
# It learns if P improves as E grows, measured ALWAYS on unseen data
return results[-1] > results[0], resultsIf that function returns False, you have a program that processes data, but not a program that learns.