Spinning text gif that says yits site

Deriving the Line of Best Fit

We will define the set of points P as follows

P={(1,1),(2,2),(3,4),}P=\left\{(1,1),(2,2),(3,4),\ldots\right\}

And the line of best fit will be defined as

y=mx+by=mx+b

Finally to express the error of a line just sum the error for each point. The error for a point is the squared difference between the point’s yy coordinate and the lines estimate for the point’s xx coordinate, mx+bmx+b . The reason for squaring it is to make it always positive. Otherwise a positive error and negative error could cancel out. The reason for squaring instead of using an absolute value is for ease of the derivative. As an expression for each point it is (ymxb)2(y-mx-b)^2 . The total error for the whole line can be written as:

E(m,b)=(x,y)ϵP(ymxb)2E(m,b)=\sum_{(x,y)\epsilon P}\left(y-mx-b\right)^2

Going forward I will be leaving the bounds off of the summations, as they will all be the same and only muddle up the notation. The next step after finding the error function is to find the minimum. It would be really convenient if there was one minimum and it was the only critical point on the function. After expanding the function it is evident that its highest coefficient term of both xx and yy is parabolic.

(y2+m2x2+b22ymx2yb2mxb)\sum\left(y^2+m^2x^2+b^2-2ymx-2yb-2mxb\right)

That shows that it will have only one critical point. Next is checking if the critical point is a minimum or if it is a maximum or saddle point. As bb goes to positive or negative infinity the error function must go to positive infinity.

A diagram showing how changing the y-intercept effects the error

Less obviously but with similar logic the same is true for mm .

A diagram showing how changing the slope effects the error

Having shown that the critical point is the point we are looking for, the next step is to find it. We will start by taking the partial derivatives of the error function.

Em=(2mx22yx+2xb)\frac{\partial E}{\partial m}=\sum\left(2mx^2-2yx+2xb\right)
Es=(2b2y+2mx)\frac{\partial E}{\partial s}=\sum\left(2b-2y+2mx\right)

Now all that remains is to find where both of those equal zero. Starting with Es\frac{\partial E}{\partial s} first set it to zero, then divide both sides by two. Then distribute the summations and you are left with:

0=by+mx0=\sum b-\sum y+\sum mx

The mm can be distributed out of the summation and the y\sum y can be moved to the other side.

y=b+mx\sum y=\sum b+m\sum x

For the next step remember what bounds on the summations are. The term b\sum b means the sum of bb added for each point. The term x\sum x means the total of all the xx coordinates. If we divide both sides by the number of points then b\sum b will change to bb and x\sum x will change to the average xx value. Now we have the equation

yˉ=mxˉ+b\bar{y}=m\bar{x}+b

showing that line of best fit goes through the average point. The other equation doesn’t turn out as neat, but still isn’t too bad to solve. Doing very similar steps as to the last equation you can find that

mx2+bx=xym\sum {x^2}+b\sum x=\sum {xy}

Changing all the summations to averages will not be as nice as last time, but still make it easier to work with.

mx2ˉ+bxˉ=xyˉm\bar{x^2}+b\bar{x}=\bar{xy}

You can then plug in the other formula tothat and solve for mm to get

m=xyˉxˉyˉx2ˉxˉ2m=\frac{\bar{xy}-\bar{x}\bar{y}}{\bar{x^2}\bar{x}^2}

The result from that can be used with the yˉ=mxˉ+b\bar{y}=m\bar{x}+b equation to find the line of best fit.

Created on: