Search code examples
least-squares

least squares equation for a vertical line


Given the following 2d points:

213 106.8

214 189

214 293.4

213 324

223 414

I want to find an equation for the least squares vertical axis line that runs through them. My plan is to get a line equation so I can test subsequent points for their distances to that least squares line.

Thanks


Solution

  • Strictly speaking, a least squares fit is not defined for a vertical line (since the error for each point is measured parallel to the Y axis).

    However, if you swap X and Y, you can find the horizontal line with the best least squares fit. It works out to simply the mean of the Y coordinate values:

    The equation for a horizontal line is simply y = b.

    The error at each point (xi, yi) is (yi - b).

    The sum of the squares of the errors is SSE = sum( (yi - b)2). We wish to find the value of b that minimizes SSE. Take the partial derivative of SSE with respect to b and set it to zero:

    sum(-2(yi - b)) = 0

    Simplifying,

    sum(yi) - Nb = 0

    and

    b = sum(yi)/N

    So in your case, averaging the X coordinates gives you the X coordinate of the vertical line that best fits your points.