Chapter 3 of 5

The OLS Solution

Hand calculation, step by step

OLS finds the one line with the smallest total squared error. Let's calculate it by hand.

100200300400500600House Size (sqft)$0$50k$100k$150k$200k$250k$300kPrice ($)Some guess

Step 1: Find the averages. (mean size) and (mean price).

100200300400500600House Size (sqft)$0$50k$100k$150k$200k$250k$300kPrice ($)
x̄ (mean size) 0 sqft
ȳ (mean price) $0

Step 2: How far is each point from the average? These are deviations.

100200300400500600House Size (sqft)$0$50k$100k$150k$200k$250k$300kPrice ($)

Step 3: Multiply the deviations together. Sum them up, then divide by n − 1 — that's the covariance.

Cov(x,y)=i=1n(xixˉ)(yiyˉ)n1\text{Cov}(x, y) = \frac{\sum_{i=1}^{n}(x_i - \bar{x})(y_i - \bar{y})}{n - 1}
#xi - x̄×yi - ȳ=product
1-246×-77,183=18,961,323
2-226×-142,551=32,169,039
3-217×-56,882=12,324,462
4-144×-68,300=9,812,452
5-122×-47,959=5,835,028
6-88×-8,850=775,862
7-84×-14,406=1,205,313
835×46,272=1,634,939
952×5,486=287,094
10110×19,034=2,100,070
11113×64,752=7,338,545
12154×15,569=2,402,795
13199×91,526=18,244,156
14222×115,675=25,718,379
15237×57,819=13,722,344
Sum / (n - 1) =10,895,128.7
Cov(x, y) 0.0

Step 4: Square the x-deviations and average them. That's the variance of x.

Var(x)=i=1n(xixˉ)2n1\text{Var}(x) = \frac{\sum_{i=1}^{n}(x_i - \bar{x})^2}{n - 1}
#xi - x̄(xi - x̄)²
1-24660,352
2-22650,925
3-21746,944
4-14420,640
5-12214,803
6-887,685
7-847,000
8351,248
9522,739
1011012,173
1111312,844
1215423,819
1319939,734
1422249,432
1523756,327
Sum / (n - 1) =29,047.7
Var(x) 0.0

Step 5: Divide covariance by variance. That's β₁, the slope.

β1=Cov(x,y)Var(x)\beta_1 = \frac{\text{Cov}(x, y)}{\text{Var}(x)}
Cov(x,y) 10,895,128.7
/
Var(x) 29,047.7
=
β₁ (slope) 0.00

For every additional square foot, the predicted price increases by $375.


Step 6: The intercept. β₀ = ȳ − β₁ · x̄

β0=yˉβ1xˉ\beta_0 = \bar{y} - \beta_1 \cdot \bar{x}
ȳ (mean price) 174,664 from Step 1
β₁ (slope) 375.08 from Step 5
×
x̄ (mean size) 348 from Step 1
=
β₀ (intercept) $0

A hypothetical 0 sqft house would cost $44,262. (It's the baseline, not a real prediction.)

Built with SvelteKit + D3.js