Lesson 12 of 15

R-squared & Residuals

R-squared & Residuals

After fitting an OLS line, we need to assess how well it fits the data.

Residuals are the differences between actual and predicted values: ei=yiy^i=yi(βxi+α)e_i = y_i - \hat{y}_i = y_i - (\beta x_i + \alpha)

R-squared (coefficient of determination) measures the proportion of variance explained: R2=1SSresSStotR^2 = 1 - \frac{SS_{res}}{SS_{tot}}

where:

  • SSres=(yiy^i)2SS_{res} = \sum (y_i - \hat{y}_i)^2 — residual sum of squares
  • SStot=(yiyˉ)2SS_{tot} = \sum (y_i - \bar{y})^2 — total sum of squares

R2=1R^2 = 1 means perfect fit; R2=0R^2 = 0 means the regression explains nothing (as good as just predicting the mean). In factor models, R2R^2 represents how much of an asset's variance is explained by the factors.

Your Task

Implement:

  • r_squared(xs, ys) — coefficient of determination using OLS fit
  • residuals(xs, ys) — list of OLS residuals
Python runtime loading...
Loading...
Click "Run" to execute your code.