Extreme Optimization > Mathematics Library for .NET > User's Guide > Current Page > Solving Least Squares Problems

Extreme Optimization Mathematics Library for .NET

Solving Least Squares Problems

The Extreme Optimization Mathematics Library for .NET supports a simple mechanism for solving linear least squares problems.

The LeastSquaresSolver Class

The LeastSquaresSolver class implements the calculation of least squares solutions. The constructor takes two arguments: the matrix of observations, and the vector of outcomes:

Matrix a = new GeneralMatrix(6, 4, new double[]
    {
        1, 1, 1, 1, 1, 1,
        1, 2, 3, 4, 5, 6,
        1, 4, 9, 16, 25, 36,
        1, 2, 1, 2, 1, 2
    });
Vector b = new GeneralVector(1, 3, 6, 11, 15, 21);
LeastSquaresSolver solver = new LeastSquaresSolver(a, b);
C#VB.NET 

Least squares problems can be solved using a variety of techniques. The method to use is specified by the SolutionMethod property. This property is of type LeastSquaresSolutionMethod and can take on the following values:

Value Description
NormalEquations The solution is found by solving the normal equations. This method is often the fastest, but is also less stable.
QRDecomposition The solution is found by first calculating the QR decomposition of the observation matrix. This is the default.
CompleteOrthogonalDecomposition The solution is found by first calculating the complete orthogonal decomposition of the observation matrix. This is a rank-revealing enhancement of the QR decomposition. (Reserved for future use.)
SingularValueDecomposition The solution is found by first calculating the singular value decomposition of the observation matrix. This is the slowest but most robust method.

The Solve method performs the actual calculation. This method returns a Vector containing the least squares solution. This vector is also available through the Solution property.

solver.SolutionMethod = LeastSquaresSolutionMethod.QRDecomposition;
// The Solve method calculates the solution:
Vector x = solver.Solve();
Console.WriteLine("x = {0}", x.ToString("F4"));
// The Solution property also returns the solution:
Console.WriteLine("x = {0}", solver.Solution.ToString("F4"));
C#VB.NET 

Various other methods let you retrieve more information about the least squares solution. The GetPredictions method returns the vector of the outcomes predicted by the solution. The GetResidues method returns the residues: the difference between the predicted and actual outcome.

By default, the least squares solver uses an algorithm based on the QR decomposition of the matrix of observations. Depending on the numerical properties of the observation matrix, it may be acceptable to calculate the solution using the normal equations. This is faster, especially for problems with many more observations than variables, but the numerical accuracy tends to be questionable for larger numbers of variables.

Methods using a complete orthogonal decomposition and the singular value decomposition will be supported in the future.

Directly Solving the Normal Equations

In some instances, it is convenient and cheap to accumulate the normal equations directly. If the condition of the observation matrix is good enough, a least squares solution may be obtained by solving the accumulated equations. The following example illustrates this procedure:

SymmetricMatrix aTa = SymmetricMatrix.FromOuterProduct(a);
Vector aTb = b * a;
Vector x = aTa.Solve(aTb);
Console.WriteLine("x = {0}", x.ToString("F4"));
C#VB.NET 

References

G. H. Golub, C. F. Van Loan, Matrix Computations (3rd Ed), Johns Hopkins University Press, 1996.

Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Search

"The Extreme Optimization Statistics Library for .NET is a major boon for those doing statistical work in .NET. I strongly recommend this product."
- Marc Brooks

"I have made it my mission to institutionalize the value of good API design.  I strongly believe that this is key to making developers more productive and happy on our platform. It is clear that you value good API design in your work, and take to heart developer productivity and synergy with the .NET framework."
- Brad Abrams,
Lead Program Manager, Microsoft.

This is a partial list of companies who are using our libraries:
ABB Robotics
Allstate
Applied Materials
Arcam
Astra Schedule
Babson College
Canadian Council on Learning
Canyon Associates
Caxton Associates
CECity
Constellation Energy
CreditSights
DeepOcean
Duke University
Dynamotive
Elecsoft
Engelhard Corporation
Epcor
Equipoise Software
Galileo International
GAM UK
Gammex
GlaxoSmithKline
Global Matrix
The Hartford
Infinera Corporation
Intel
JDS Uniphase
LaBranche & Co.
Learning & Skills Council
Jacobs Consultancy
Litman Gregory
Lucas Systems
Malvern Instruments
Medrio
Merck & Co.
Mintera.
Monitor Software
MorningStar
NanoString Technologies
Paletta Invent
Parametric Portfolio Associates
Prosanos
RATA Associates
RiskShield
Ramboll
Standard & Poor's
Strategic Analysis Corporation
Univ. of Alicante
Univ. of South Carolina
vielife
Xerox
US Army