Line f(0) = 0 closest to a set of points
Does anyone know a fast way to find a line closest to a set a points in python? (but the line should always cross the origin, in other words f(0) = 0)
Given the equation of the line y = mx + 0 I want to find the m that optimizes this distance to every point in the set.
The image above is an example, the line should be closest to all the points. I tried doing this using scipy.optimize.minimize_scalar but the performance was not good enough, I wonder if there is a faster algorithm or a analytical way of doing this.
The distance formula can be found here: https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line. You want to minimize the sum of the distances but the distance formula contains an absolute value which will created a discontinuity in the first derivative which will prevent numerical solvers from finding the solution. Alternatively you could minimize the square of the sum of the distances. However, it's not the same as minimizing the sum of the distances.