Sub topic 1
Starting point: simple linear regression in brief
Multiple regression extends simple linear regression. Every later chapter uses the same vocabulary: slope, residual, sum of squares, standard error, t-test, and R-squared. This chapter reviews those ideas with one predictor, so the later chapters can add more predictors without introducing new notation.
Prerequisites
This chapter assumes you already know:
- Descriptive statistics: mean, variance, standard deviation, covariance, and correlation.
- Hypothesis testing: a null hypothesis, a test statistic, a critical value, a p-value, and a confidence interval.
- The t-distribution and degrees of freedom.
- Basic Python with NumPy, if you want to run the code. The code is optional for following the text.
You do not need matrix algebra. Every formula here is written in sums and averages.
The model
Simple linear regression describes how a dependent variable Y (the quantity you want to explain or forecast) relates to one independent variable X (the explanatory variable). The population model is:
Yi = b0 + b1 × Xi + ε_i, for i = 1, ..., n
- b0 is the intercept: the expected value of Y when X is 0.
- b1 is the slope: the expected change in Y for a one-unit increase in X.
- ε_i is the error term: everything that moves Y but is not captured by X.
You never observe b0 and b1. You estimate them from a sample, and the estimates are written b̂0 and b̂1. The fitted value for observation i is Ŷi = b̂0 + b̂1 × Xi. The residual is ei = Yi − Ŷ_i, the gap between what happened and what the line predicted.
The intercept often has no practical meaning. If X is a stock's market return, an intercept is interpretable. If X is a firm's leverage ratio and no firm has a ratio near zero, the intercept is only an anchor for the line.
Ordinary least squares
Ordinary least squares (OLS) picks the line that minimizes the sum of squared residuals. Squaring stops positive and negative residuals from cancelling and penalizes large misses more than small ones. The solution has a closed form:
- b̂1 = Cov(X, Y) / Var(X) = Σ (Xi − X̄)(Yi − Ȳ) / Σ (X_i − X̄)²
- b̂0 = Ȳ − b̂1 × X̄
The second formula shows that the fitted line always passes through the point (X̄, Ȳ). The first shows that the slope is the covariance scaled by the variance of X. It also equals the correlation r times (sY / sX), so the slope and the correlation always have the same sign.
Decomposing variation
Total variation in Y splits into a part the regression explains and a part it does not:
- SST (total sum of squares) = Σ (Y_i − Ȳ)²
- RSS (regression sum of squares) = Σ (Ŷ_i − Ȳ)²
- SSE (sum of squared errors) = Σ (Yi − Ŷi)²
These satisfy SST = RSS + SSE. Some textbooks call the explained part SSR or ESS, so check the notation when you read other sources. This tutorial uses RSS for the explained part and SSE for the unexplained part.
Three summary measures come from this split.
- R-squared = RSS / SST. It is the fraction of the variation in Y that the model explains. In simple regression it equals the squared correlation between X and Y.
- Standard error of estimate (SEE) = √(SSE / (n − 2)). It is the typical size of a residual, in the units of Y. The divisor is n − 2 because two parameters, b̂0 and b̂1, were estimated from the data.
- F-statistic = (RSS / 1) / (SSE / (n − 2)). It tests whether the slope is zero. With one predictor it is the square of the slope's t-statistic.
The ANOVA table (analysis of variance table) organizes these pieces:
| Source | Degrees of freedom | Sum of squares | Mean square |
|---|---|---|---|
| Regression | 1 | RSS | MSR = RSS / 1 |
| Error | n − 2 | SSE | MSE = SSE / (n − 2) |
| Total | n − 1 | SST |
Note that MSE is SEE squared. The F-test gets a full chapter later, when there are several slopes to test together.
Testing the slope
The standard error of the slope measures how much b̂1 would vary from sample to sample:
s(b̂1) = SEE / √(Σ (X_i − X̄)²)
This formula explains three practical facts. A smaller SEE (a tighter fit) gives a more precise slope. A larger n gives a more precise slope. More spread in X also gives a more precise slope, because the denominator grows.
To test H0: b1 = 0 against Ha: b1 ≠ 0, compute:
t = (b̂1 − 0) / s(b̂1), with n − 2 degrees of freedom
Reject H0 when |t| exceeds the critical value, or equivalently when the p-value falls below the significance level. A confidence interval for the slope is b̂1 ± t_critical × s(b̂1). If it excludes 0, the two-sided test rejects at the matching level. To test a different null, such as b1 = 1 for a beta, replace 0 in the numerator with the hypothesized value.
Worked example
An analyst regresses a fund's monthly excess return (Y) on the market's monthly excess return (X) using n = 32 months. The output gives:
- b̂0 = 0.10 and b̂1 = 0.80
- SST = 100, SSE = 36, so RSS = 64
Work through the measures:
- R² = 64 / 100 = 0.64. The market explains 64% of the variation in the fund's excess return. The correlation is √0.64 = 0.80, positive because the slope is positive.
- SEE = √(36 / 30) = √1.2 ≈ 1.095, in percentage points per month.
- F = (64 / 1) / (36 / 30) = 64 / 1.2 ≈ 53.33.
- The slope's t-statistic is √53.33 ≈ 7.30, so s(b̂1) = 0.80 / 7.30 ≈ 0.1095.
- The critical t value for 30 degrees of freedom at a 5% two-sided level is 2.042. The 95% confidence interval is 0.80 ± 2.042 × 0.1095, which is about (0.576, 1.024).
The interval excludes 0, so the slope is statistically significant. It also contains 1.0, so you cannot reject that the fund's beta equals 1 at the 5% level.
The predicted excess return when the market's excess return is 5% is 0.10 + 0.80 × 5 = 4.10%.
Computing it in Python
The script below computes the estimates from the formulas above and checks them against statsmodels. It uses simulated data, so it runs without any files.
import numpy as np
import statsmodels.api as sm
rng = np.random.default_rng(7)
n = 60
x = rng.normal(loc=5.0, scale=2.0, size=n)
y = 1.5 + 0.8 * x + rng.normal(scale=1.0, size=n)
# Closed-form OLS
x_bar, y_bar = x.mean(), y.mean()
sxx = np.sum((x - x_bar) ** 2)
b1 = np.sum((x - x_bar) * (y - y_bar)) / sxx
b0 = y_bar - b1 * x_bar
# Variation decomposition and test statistics
resid = y - (b0 + b1 * x)
sse = np.sum(resid ** 2)
sst = np.sum((y - y_bar) ** 2)
rss = sst - sse
r2 = rss / sst
see = np.sqrt(sse / (n - 2))
se_b1 = see / np.sqrt(sxx)
t_b1 = b1 / se_b1
f_stat = rss / (sse / (n - 2))
print("manual:", round(b0, 4), round(b1, 4), round(r2, 4), round(t_b1, 4), round(f_stat, 4))
# Library check
model = sm.OLS(y, sm.add_constant(x)).fit()
print("statsmodels:", model.params.round(4), round(model.rsquared, 4),
round(model.tvalues[1], 4), round(model.fvalue, 4))
print("F equals t squared:", np.isclose(f_stat, t_b1 ** 2))The manual and library values agree, and the last line prints True. That confirms F = t² in simple regression. Once a second predictor is added, this identity no longer holds, and the F-test becomes a separate tool.
Assumptions behind the numbers
The t-tests, confidence intervals, and F-test above are valid only if the following conditions hold. They are the foundation for the diagnostic chapters later in this tutorial.
- Linearity: the relationship between X and Y is linear in the parameters.
- Independent variable not random in a way that correlates with ε: X is uncorrelated with the error term.
- Homoskedasticity: the error variance is constant across observations.
- Independence: errors are not correlated with one another.
- Normality: errors are normally distributed, which matters mainly for small samples.
When one fails, the slope estimates may still be fine, but the standard errors, and therefore every t-statistic and p-value, can be wrong. Later chapters cover how to detect and fix each failure.
Common errors to avoid
- Using n − 1 degrees of freedom. Simple regression estimates two parameters, so the degrees of freedom for the t-test are n − 2.
- Reading R² as a test. A high R² does not mean the slope is significant, and a low R² does not mean it is insignificant. R² describes fit, and the t-test and F-test describe significance.
- Mixing up SEE and the slope's standard error. SEE measures the spread of residuals around the line. The slope's standard error measures the uncertainty in b̂1.
- Forgetting units. The slope is in units of Y per unit of X. Changing X from percent to decimals multiplies the slope by 100 and leaves the t-statistic unchanged.
Exercise
A regression on n = 27 observations gives b̂1 = 1.5 and SEE = 2.0. The sum Σ (X_i − X̄)² is 25. Test H0: b1 = 0 at the 5% two-sided level. The critical t value for 25 degrees of freedom is 2.060.
Show answer
s(b̂1) = SEE / √25 = 2.0 / 5 = 0.40. The t-statistic is 1.5 / 0.40 = 3.75, with n − 2 = 25 degrees of freedom. Since 3.75 exceeds 2.060, reject H0 and conclude the slope is significantly different from zero. The 95% confidence interval is 1.5 ± 2.060 × 0.40, or (0.676, 2.324).
Exercise
A simple regression with n = 52 has SST = 200 and R² = 0.25. Find SSE, SEE, and the F-statistic. Then state what the t-statistic of the slope must be in absolute value.
Show answer
RSS = 0.25 × 200 = 50, so SSE = 200 − 50 = 150. SEE = √(150 / 50) = √3 ≈ 1.732, because n − 2 = 50. F = (50 / 1) / (150 / 50) = 50 / 3 ≈ 16.67. In simple regression F = t², so |t| = √16.67 ≈ 4.08.