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
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.
The most generic solution would be to apply Total Least Squares
This finds (a, b, d) to minimize the sum of squared perpendicular distances (ax+by=d (a^2+b^2=1): |ax + by – d|). This can handle vertical lines, such as 0x+1y=0.
However, this is a bit more difficult to implement, so the solution offered by @Jim Lewis might be fine and more practical..
If you want the line of best fit to be vertical (i.e. x = constant), the y-values are irrelevant. Simply take the square root of the mean of the squares of the x-values.
精彩评论