__ __ __ __ /\ \ /\ \__/\ \__ __/\ \__ \ `\`\\/'/\_\ \ ,_\ ___ ____/\_\ \ ,_\ __ `\ `\ /'\/\ \ \ \/ /',__ /',__\/\ \ \ \/ /'__`\ `\ \ \ \ \ \ \ \_/\__, ` /\__, `\ \ \ \ \_/\ __/ \ \_\ \ \_\ \__\/\____/ \/\____/\ \_\ \__\ \____\ \/_/ \/_/\/__/\/___/ \/___/ \/_/\/__/\/____/

We will define the set of points $P$ as follows

$$P=\left\{\right(\mathrm{1,1}),(\mathrm{2,2}),(\mathrm{3,4})...\}$$And the line of best fit will be defined as

$$y=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 $y$ coordinate and the lines estimate for the point's $x$ coordinate, $mx+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 $(y-mx-b{)}^{2}$ . The total error for the whole line can be written as

$$E(m,b)=\sum _{(x,y)\in P}(y-mx-b{)}^{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 $x$ and $y$ is parabolic.
$$\mathrm{\Sigma}({y}^{2}+{m}^{2}{x}^{2}+{b}^{2}-2ymx-2yb+2mxb)$$
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 $b$ goes to positive or negative infinity the error function must go to positive infinity.

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

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.
$$\frac{\partial E}{\partial m}=\Sigma (2m{x}^{2}-2yx+2xb)$$
$$\frac{\partial E}{\partial s}=\Sigma (2b-2y+2mx)$$

Now all that remains is to find where both of those equal zero. Starting with $\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=\mathrm{\Sigma}b-\mathrm{\Sigma}y+\mathrm{\Sigma}mx$$The $m$ can be distributed out of the summation and the $\mathrm{\Sigma}y$ can be moved to the other side.

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

$$\stackrel{\u203e}{y}=m\stackrel{\u203e}{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

$$m\mathrm{\Sigma}{x}^{2}+b\mathrm{\Sigma}x=\mathrm{\Sigma}xy$$Changing all the summations to averages will not be as nice as last time, but still make it easier to work with.

$$m{x}^{2}+b\stackrel{\u203e}{x}=xy$$You can then plug in the other formula to that and solve for $m$ to get

$$m=\frac{xy-\stackrel{\u203e}{x}\phantom{\rule{0.1667em}{0ex}}\stackrel{\u203e}{y}}{{x}^{2}-{\stackrel{\u203e}{x}}^{2}}$$The result from the can be used in the $\stackrel{\u203e}{y}=m\stackrel{\u203e}{x}+b$ equation to find the line of best fit.