Sub topic 13
Estimating the line with ordinary least squares
This chapter assumes you know the mean, variance, covariance and correlation, and the equation of a straight line. It also assumes you know the simple regression model from the previous chapter, Y = b0 + b1X + ε. Here you learn how the intercept b0 and the slope b1 are chosen from sample data, how to compute them by hand, and how to read them.
The problem: many lines, one best line
A scatter of points can be summarized by many lines. Each line gives a fitted value for every observation, written ŷᵢ = b0 + b1·xᵢ. The gap between the observed and fitted value is the residual:
eᵢ = yᵢ − ŷᵢ
Residuals can be positive or negative, so adding them up lets errors cancel. Ordinary least squares (OLS) avoids this by squaring each residual first. It picks the b0 and b1 that minimize the sum of squared errors:
SSE = Σ eᵢ² = Σ (yᵢ − b0 − b1·xᵢ)²
Squaring also penalizes large misses more than small ones. A residual of 4 counts as much as four residuals of 2.
The OLS formulas
Calculus gives a closed-form answer for the minimizing values:
- Slope: b1 = Cov(X, Y) / Var(X)
- Intercept: b0 = Ȳ − b1·X̄
Cov and Var must use the same divisor, usually n − 1. If you work with raw deviations, the divisor cancels:
b1 = Σ(xᵢ − x̄)(yᵢ − ȳ) / Σ(xᵢ − x̄)²
The numerator is often called Sxy and the denominator Sxx.
The slope is also tied to the correlation r:
b1 = r × (sY / sX)
Correlation is unit-free and sits between −1 and +1. The slope rescales it into units of Y per unit of X. If r = 0, the slope is 0. The slope always has the same sign as the correlation.
A worked example
Take five observations: x = 1, 2, 3, 4, 5 and y = 2.1, 3.9, 6.2, 7.8, 10.1.
- The means are x̄ = 3 and ȳ = 6.02.
- Sxx = 4 + 1 + 0 + 1 + 4 = 10.
- Sxy = (−2)(−3.92) + (−1)(−2.12) + 0 + (1)(1.78) + (2)(4.08) = 19.9.
- The slope is b1 = 19.9 / 10 = 1.99.
- The intercept is b0 = 6.02 − 1.99 × 3 = 0.05.
The fitted line is ŷ = 0.05 + 1.99x. At x = 3 the fitted value is 6.02, which is exactly ȳ. This is not a coincidence, as the next section explains.
Properties of the fitted line
These facts follow from the way OLS is built. They are useful as checks on hand calculations.
- The line passes through the point (x̄, ȳ). This is what the intercept formula says.
- The residuals sum to zero, and the mean of the fitted values equals ȳ.
- The residuals are uncorrelated with x in the sample. If they were not, you could reduce SSE by tilting the line.
- Among all lines, this one has the smallest SSE. The SSE is the quantity the later chapters use to compute R², the standard error of estimate and the F-statistic.
Checking the arithmetic in code
The script below computes the slope and intercept from the definitions, confirms the properties above, and compares the result with NumPy's built-in least-squares fit.
import numpy as np
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([2.1, 3.9, 6.2, 7.8, 10.1])
x_bar, y_bar = x.mean(), y.mean()
sxy = ((x - x_bar) * (y - y_bar)).sum()
sxx = ((x - x_bar) ** 2).sum()
b1 = sxy / sxx
b0 = y_bar - b1 * x_bar
y_hat = b0 + b1 * x
resid = y - y_hat
r = np.corrcoef(x, y)[0, 1]
b1_from_r = r * y.std(ddof=1) / x.std(ddof=1)
slope_np, intercept_np = np.polyfit(x, y, deg=1)
print("b0, b1:", round(b0, 4), round(b1, 4))
print("polyfit:", round(intercept_np, 4), round(slope_np, 4))
print("b1 from r:", round(b1_from_r, 4))
print("sum of residuals:", round(resid.sum(), 10))
print("corr(resid, x):", round(np.corrcoef(resid, x)[0, 1], 10))
print("SSE:", round((resid ** 2).sum(), 4))
print("prediction at x = 6:", round(b0 + b1 * 6, 4))The script prints b0 = 0.05 and b1 = 1.99 from all three routes, a residual sum of 0, a correlation of 0 between residuals and x, SSE = 0.107, and a prediction of 11.99 at x = 6.
Interpreting the coefficients
- Slope. The expected change in Y for a one-unit increase in X. Here, each extra unit of x goes with about 1.99 more units of y.
- Intercept. The expected value of Y when X = 0. It is only meaningful if X = 0 is a sensible value inside the range of the data.
In finance the interpretation depends on the variables. When you regress a stock's return on the market's return, the slope is the stock's beta. The intercept is the return the stock earns when the market return is zero, which is why it is often read as alpha.
Common errors
| Error | Why it is wrong | Fix |
|---|---|---|
| Swapping X and Y | The regression of Y on X is not the inverse of the regression of X on Y, because the slope is Cov/Var(X). | Name the dependent variable first and divide by the variance of the independent variable. |
| Mixing divisors | Using Cov with n − 1 and Var with n changes the slope by a factor of n/(n − 1). | Use the same divisor for both, or use the deviation sums Sxy and Sxx. |
| Reading the intercept literally | X = 0 may be outside the data, so the intercept is an extrapolation. | Check that 0 is a plausible value of X before interpreting b0. |
| Predicting far outside the sample | The linear relationship is only supported where there is data. | Keep predictions inside the observed range of X, or flag them as extrapolations. |
| Confusing slope and correlation | A steep slope does not mean a strong relationship. It depends on units. | Use r and R² for strength and b1 for size of effect. |
Where OLS fits in the larger chapter sequence
OLS gives you point estimates only. The estimates vary from sample to sample, so b1 has its own sampling distribution and standard error, just as a sample mean does. The next chapters use SSE to measure fit and then test whether the true slope differs from zero. The assumptions that make those tests valid are also covered later. OLS itself only needs variation in X, meaning Sxx > 0. If every x is the same, the slope formula divides by zero.
Exercise
A sample of 30 observations gives Cov(X, Y) = 18, Var(X) = 9, x̄ = 5 and ȳ = 14. (a) Find b1 and b0. (b) Predict Y at x = 7. (c) If sY = 4.5 and sX = 3, what is the correlation between X and Y?
Show answer
(a) b1 = 18 / 9 = 2, and b0 = 14 − 2 × 5 = 4. (b) ŷ = 4 + 2 × 7 = 18. (c) sX = 3 because Var(X) = 9, so r = b1 × sX / sY = 2 × 3 / 4.5 = 1.33. A correlation cannot exceed 1, so the data are inconsistent. With sY = 4.5 the covariance of 18 is impossible, since the maximum is sX × sY = 13.5. If sY = 6.5, for example, r = 2 × 3 / 6.5 = 0.92, which is valid.
Exercise
A regression of a fund's monthly return on the market's monthly return (both in %) gives b0 = 0.30 and b1 = 1.20. The market return averaged 0.80% over the sample. What is the fund's average return, and what does the residual sum equal?
Show answer
The line passes through the sample means, so the fund's mean return is ȳ = 0.30 + 1.20 × 0.80 = 1.26%. The residuals from an OLS fit with an intercept always sum to zero.