User:Vossman/3D Line Regression

From Wikipedia, the free encyclopedia

Fit a 3-Dimensional Line to Data Points[edit]

Setup variables[edit]

This problem seems similar to what simple linear regression does: fit a straight line to a set of data points. However, ordinary linear regression minimizes the sum of the squared deviations between the points and the line, and it defines the deviation as the distance in the vertical (Y) direction. The problem we are going to solve in this example minimizes the direct distance between the points and the line. The direct distance is along a line that runs from the point and is perpendicular to the target line. In the following figure, the distance d is the direct distance from the point at to the line.

The parametric equation for a 3D line is:

Where is some point on the line and is a vector defining the direction of the line. t is the parameter whose value is varied to define points on the line.

With this definition, there are six parameters: . But this overspecifies the line because a 3D line can be defined by 4 parameters as long as it is not parallel to one of the X, Y or Z planes.

When fitting a function to data, it is important that there are no mutually dependent (redundant) parameters in the function. If there are mutually dependent parameters, then there is no unique solution, and the fitting process will not converge.

So we need to eliminate two parameters.

Removing a parameter from the point on the line[edit]

Rather than allowing an arbitrary point to specify a point on the line, we will force to be 0 and make and be the coordinates on the X-Y plane where the line penetrates the plane (i.e., where Z is zero). This eliminates as a parameter that needs to be computed. We can do this as long as we know that the line is not parallel to the X-Y plane, so it intersects it at some point.

Removing a parameter from the direction vector[edit]

Next, we will work on the direction vector that defines the direction of the line. Scaling the direction vector by a non-zero factor changes its length but not its direction (e.g., the direction defined by the vector <1,2,3> is the same as <2,4,6>, but the second vector is twice as long). If we scale the direction vector by to force to be 1, then we can define a revised direction vector,

So we will force to be 1 and define and as multiples of .

This eliminates as a parameter that needs to be computed. Note that this is only valid if is not zero which means the line is not parallel to the X-Y plane. You can divide by or if you want to allow the line to be parallel to the X-Y plane but not some other plane.

Use cross-products[edit]

In addition,

so

For our least squares minimization, we want to minimize the square of the cross product in each dimension

Then for each variable: and , we take the derivative:

Bad Optimization[edit]

So, now we have 4 variables: to optimize from our list of points.

from this it is obvious that for each point, therefore:

we want to minimize this equation with respect to our four variables , so we can take the derivative with respect to each which in turn generates four equations for our four unknowns:

simplifying:

rearranging two of the equations:

therefore,

where N is the number of points

Try again[edit]

Distance from line to point, :

External links[edit]