# How to determine the best fit slope of a line?

I was going back through linear regression and I found two equations for finding the best fit slope given X and y. This is one: $$m=\frac{\sum_{i=0}^{n}(x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=0}^{n}(x_i - \bar{x}^2)}$$ and this is the other: $$m=\frac{\bar{x}.\bar{y}-\bar{xy}}{\bar{x}^2 - \bar{x^2}}$$ I programmed both of them with Numpy and tested the second implementation with the same set of numbers and got the same slope value, but the first implementation yielded different results). What's the difference between those three and which should I use?

Here is my implementation of the first equation:

m = (np.sum(np.dot(X - np.mean(X), y - np.mean(y)))) / (np.sum(np.square(X - np.mean(X))))


and the second:

m = (np.dot(np.mean(X), np.mean(y)) - np.mean(np.dot(X, y))) / (np.square(np.mean(X)) - np.mean(np.square(X)))

• What are the sums in your second line of code? There are no sums in your question.
– Dave
Jul 20 at 15:55
• en.wikipedia.org/wiki/Simple_linear_regression Jul 20 at 16:06
• @Dave yeah sorry I mixed them up, the sums are in the first equation. Jul 20 at 17:32

It looks like there are a couple of problems.

First, the python code for the 1st equation does not implement it correctly. The first line of code (for the first equation in the OP) should be

m = (np.sum(np.dot(X - np.mean(X), y - np.mean(y)))) / np.sum(X - np.square(np.mean(X)))


However the first equation is wrong. It's not:

$$m=\frac{\sum_{i=0}^{n}(x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=0}^{n}(x_i - \bar{x}^2)}$$

It's

$$m=\frac{\sum_{i=0}^{n}(x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=0}^{n}(x_i - \bar{x})^2}$$

So in (Num)Python that would be:

m = (np.sum(np.dot(X - np.mean(X), y - np.mean(y)))) / (np.sum(np.square(X - np.mean(X))))

• Thank you for your help Robert, I implemented the first equation correctly I guess I just wrote it in Latex wrong, my original question was what is the difference, if any, between the two equations as to getting the best fit slope? Jul 22 at 1:25
• You're welcome. They are equavalent (just expand the brackets in the first one and rearrange). There is a comutational benefit in using the 2nd one, since it does not require any summations and therefore will be more efficient. Jul 22 at 10:46