Advanced

Simple models and predictions

Quants build models to predict prices, trends, or whether a single trade wins. This page covers the simplest kinds and why they're a starting point, no model is the answer, but a careful one is better than guessing.

What you will see

Accuracy on held-out data: 0.67

About 2 min of reading

Try It

A model turns features into a prediction, but a good one is only proven on data it never trained on. I fit a line to a noisy trend, predict a direction, and measure how well it holds up on held-out examples.

1

I fit a straight line by hand to a small set of points and print the slope and intercept so the trend is captured before checking whether it generalizes.

x = [1, 2, 3, 4] y = [2.1, 3.9, 6.2, 7.8] n = len(x) mean_x = sum(x) / n mean_y = sum(y) / n slope = sum((x[i] - mean_x) * (y[i] - mean_y) for i in range(n)) / sum((x[i] - mean_x) ** 2 for i in range(n)) intercept = mean_y - slope * mean_x print("Simple models and predictions") print("Slope: %.4f" % slope) print("Intercept: %.4f" % intercept)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Simple models and predictions Slope: 1.9400 Intercept: 0.1500
A clean fit on history is not an edge. Fit the line, then ask whether it still holds on prices the model never saw.
2

I turn a single score into a yes/no direction and print the prediction, noting that one gut feeling is rarely enough on its own.

score = 0.6 if score > 0.5: print("Predict: up") else: print("Predict: down")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Predict: up
A single whisper of a signal is noise. Real systems gather many of these and listen to them together.
3

I compare predictions to reality on unseen data and print the accuracy so it is clear whether the model has any edge at all over a coin flip.

y_test = ["up", "down", "up"] preds = ["up", "up", "up"] correct = sum(1 for a, b in zip(y_test, preds) if a == b) accuracy = correct / len(y_test) print("Accuracy on held-out data: %.2f" % accuracy)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Accuracy on held-out data: 0.67
In finance, a tiny real edge is worth a lot and anything less than that is guessing with extra steps. A model is a tool for nudging risk, never a crystal ball.

Data Flow

A model trains on past data, validates on new data, and is used only as one input among many before any real decision.
1 Gather features
↓
2 Train on past data
↓
3 Validate on new data
↓
4 Use as a hint
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

x = [1, 2, 3, 4] sets nothing on screen
y = [2.1, 3.9, 6.2, 7.8] sets nothing on screen
n = len(x) sets nothing on screen
mean_x = sum(x) / n sets nothing on screen
mean_y = sum(y) / n sets nothing on screen
slope = sum((x[i] - mean_x) * (y[i] - mean_y) for i in range(n)) / sum((x[i] - mean_x) ** 2 for i in range(n)) sets nothing on screen
intercept = mean_y - slope * mean_x sets nothing on screen
print("Simple models and predictions") Simple models and predictions
print("Slope: %.4f" % slope) Slope: 1.9400
print("Intercept: %.4f" % intercept) Intercept: 0.1500
Why this works
Train and validate

Split the data: fit the model on one part, test it on another. If it only works on what it trained on, it overfit and will fail the moment it touches real prices.

Models are wrong too

No model ever predicts the future perfectly, markets move on news, fear, and luck that no history contains. A model is a tool for nudging risk, not a crystal ball. Anyone selling you the latter doesn't understand what they built.

Examples

Fitting a line to the trend

import numpy as np
x = np.array([1, 2, 3, 4])
y = np.array([2.1, 3.9, 6.2, 7.8])
slope, intercept = np.polyfit(x, y, 1)

That line captured the upward trend in the noise. Real models do the same thing with hundreds of inputs, but the idea never changes: fit something, then check whether it still holds on data it never saw. The clever part is the checking, not the fit.

Predicting which way it goes

if score > 0.5:
    predict('up')
else:
    predict('down')

A simple classifier turns a pile of features into a single yes/no bet. It's a single whisper of a signal on its own. Real systems gather many of these and listen to them together, because one gut feeling is rarely enough.

Measuring whether the model is right

from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, preds)

Compare predictions against reality on data the model never trained on. If the accuracy barely beats 50%, the model has no edge at all, it's just guessing with extra steps. In finance, a tiny real edge is worth a lot, and anything less isn't one.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Managing risk and drawdown
Previous Lesson
Managing risk and drawdown
Next Lesson
Factor investing
Factor investing →