Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleFind a linear least squares fit for a set of points in Visual Basic 6
DescriptionThis example shows how to find a linear least squares fit for a set of points in Visual Basic 6.
Keywordsalgorithms, mathematics, least squares, linear least squares, curve fitting, graphics, Visual Basic 6, VB 6
CategoriesAlgorithms, Algorithms, Graphics, Graphics
 

Suppose you have a set of data points that you believe were generated by a process that should ideally be linear. In that case, you might like to find the best parameters m and b to make the line y = m * x + b fit those points as closely as possible.

A common approach to this problem is to minimize the sum of the squares of the vertical distances between the line and the points. For example, suppose the point P0 = (x0, y0) is one of your data points. The vertical error squared for that point is the difference between y0 and the line's Y coordinate for that X position. In this case, that's y0 - (m * x0 + b). To calculate the total error squared, square this error and add up the errors squared for all of the points.

Keep in mind that you know all of the points so for given values of m and b you can easily loop through all of the points and calculate the error.

Here's a function that does just that:
 
' Return the error squared.
Public Function ErrorSquared(ByVal PtX As Collection, ByVal _
    PtY As Collection, ByVal m As Double, ByVal b As Double) _
    As Double
Dim i As Integer
Dim total As Double
Dim dy As Double

    total = 0
    For i = 1 To PtX.Count
        dy = PtY.Item(i) - (m * PtX.Item(i) + b)
        total = total + dy * dy
    Next i
    ErrorSquared = total
End Function
 
This code loops through the points subtracting each point's Y coordinate from the coordinate of that line at the point's X position. It squares the error and adds it to the total. When it finishes its loop, the method returns the total of the squared errors.

As a mathematical equation, the error function E is:

Sum[(yi - (m * xi + b)^2]

where the sum is performed over all of the points (xi, yi).

To find the least squares fit, you need to minimize this function E(m, b). That sounds intimidating until you remember that the xi and yi values are all known--they're the values you're trying to fit with the line.

The only variables in this equation are m and b so it's relatively easy to minimize this equation by using a little calculus. Simply take the partial derivatives of E with respect to m and b, set the two resulting equations equal to 0, and solve for m and b.

Taking the partial derivative with respect to m and rearranging a bit to gather common terms and pull constants out of the sums you get:

2 * (m * Sum[xi^2] + b * Sum[xi] - Sum[xi * yi])

Taking the partial derivative with respect to b and rearranging a bit you get:

2 * (m * Sum[xi] + b * Sum[1] - Sum[yi])

To find the minimum for the error function, you set these two equations equal to 0 and solve for m and b.

To make working with the equations easier, let:

(substitutions)

If you make these substitutions and set the equations equal to 0 you get:

(equations)

Solving for m and b gives:

(equations)

Again these look like intimidating equations but all of the S's are values that you can calculate given the data points that you are trying to fit.

The following code calculates the S's and uses them to find the linear least squares fit for the points in a List(Of PointF).

 
' Find the least squares linear fit.
' Return the total error.
Public Function FindLinearLeastSquaresFit(ByVal PtX As _
    Collection, ByVal PtY As Collection, ByRef m As Double, _
    ByRef b As Double) As Double
Dim S1 As Double
Dim Sx As Double
Dim Sy As Double
Dim Sxx As Double
Dim Sxy As Double
Dim i As Integer

    ' Perform the calculation.
    ' Find the values S1, Sx, Sy, Sxx, and Sxy.
    S1 = PtX.Count
    Sx = 0
    Sy = 0
    Sxx = 0
    Sxy = 0
    For i = 1 To PtX.Count
        Sx = Sx + PtX.Item(i)
        Sy = Sy + PtY.Item(i)
        Sxx = Sxx + PtX.Item(i) * PtX.Item(i)
        Sxy = Sxy + PtX.Item(i) * PtY.Item(i)
    Next i

    ' Solve for m and b.
    m = (Sxy * S1 - Sx * Sy) / (Sxx * S1 - Sx * Sx)
    b = (Sxy * Sx - Sy * Sxx) / (Sx * Sx - S1 * Sxx)

    FindLinearLeastSquaresFit = Sqr(ErrorSquared(PtX, PtY, _
        m, b))
End Function
 
For a slightly different explanation, see my DevX article Predicting Your Firm's Future with Least Squares, Part I (free registration required).
 
 
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated