R-squared
R² compares the errors of your line with the errors of the dumbest forecast, the average. It says what share of the variation in y the model accounts for.
01The formula
- the share of the spread in y that the fitted line explains, between 0 and 1
- the fitted line's value at the i-th point
- the average of y
- the sum of squared errors left over after the fit
- the sum of squared distances of y from its average: the total spread
02See it move
03Where it is used
- Quant researcherChecks how much of next-day return a signal explains; in markets even a fraction of a percent can be worth trading.
- Portfolio managerReads the R² of a fund on its benchmark to see how much of its return is just the market.
- Risk managerUses the R² of a hedge regression to judge how much risk the hedge really removes.
04Why traders use it
A slope can be large but useless if the noise around it is huge. R² tells you how much of the outcome is predictable, and with one signal it is simply the squared correlation.
05Worked example
With slope 1.00 and noise 1.00 on 80 points, the line leaves 79.1 of a total spread of 262.3, so R² = 69.8%. The population value for these settings is 59.0%; a single sample can land some way off it.
06How it is built
- Measure the total spread : the error if you always forecast the average.
- Fit the line and measure what is left, .
- The explained share is .
- For a straight line fitted by least squares this equals the squared correlation, .
07The common mistake
Expecting high R² in markets. Daily return forecasts with an R² of one percent can be very profitable, while a backtest with a high R² is usually a sign of overfitting or a look-ahead bug.
08In Python and LaTeX
import numpy as np rng = np.random.default_rng(7) n = 80 x = 1.2 * rng.standard_normal(n) y = 0.2 + 1.0 * x + 1.0 * rng.standard_normal(n) b, a = np.polyfit(x, y, 1) r2 = 1 - np.sum((y - a - b * x)**2) / np.sum((y - y.mean())**2) print(np.isclose(r2, np.corrcoef(x, y)[0, 1]**2)) # True: R² is the squared correlation
R^2 = 1 - \frac{SS_{\text{res}}}{SS_{\text{tot}}} = 1 - \frac{\sum_i (y_i - \hat y_i)^2}{\sum_i (y_i - \bar y)^2}09Where it fits
Rather not code the maths yourself? Describe your trading idea and let AI build the bot.
Build a bot with AgenKitThe worked example and the model run the same code, so every number matches. Models simplify markets on purpose. This is education, not financial advice.