Extreme Optimization > QuickStart Samples > Least Squares QuickStart Sample (C#)

Extreme Optimization QuickStart Samples

Least Squares QuickStart Sample (C#)

Illustrates solving least squares problems using the LeastSquaresSolver class (Extreme.Mathematics.Calculus namespace) in Visual Basic .NET.

VB.NET code Back to QuickStart Samples

using System;

namespace Extreme.Mathematics.QuickStart.CSharp
{
    // The GeneralMatrix and LeastSquaresSolver classes reside in the 
    // Extreme.Mathematics.LinearAlgebra namespace.
    using Extreme.Mathematics.LinearAlgebra;

    /// <summary>
    /// Illustrates solving least squares problems using the 
    /// LeastSquaresSolver class in the Extreme.Mathematics.LinearAlgebra 
    /// namespace of the Extreme Optimization Mathematics Library
    /// for .NET.
    /// </summary>
    class LinearEquations
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            // A least squares problem consists in finding
            // the solution to an overdetermined system of
            // simultaneous linear equations so that the
            // sum of the squares of the error is minimal.
            //
            // A common application is fitting data to a
            // curve. See the CurveFitting sample application
            // for a complete example.

            // Let's start with a general matrix. This will be
            // the matrix a in the left hand side ax=b:
            GeneralMatrix 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
                });
            // Here is the right hand side:
            Vector b = new GeneralVector(1, 3, 6, 11, 15, 21);
            Matrix b2 = new GeneralMatrix(6, 2, new double[]
                {
                    1, 3, 6, 11, 15, 21,
                    1, 2, 3, 4, 5, 7
                });
            Console.WriteLine("a = {0}", a);
            Console.WriteLine("b = {0}", b);

            //
            // The LeastSquaresSolver class
            //

            // The following creates an instance of the
            // LeastSquaresSolver class for our problem:
            LeastSquaresSolver solver = new LeastSquaresSolver(a, b);
            // We can specify the solution method: normal
            // equations or QR decomposition. In most cases,
            // a QR decomposition is the most desirable:
            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"));
            // More detailed information is available from
            // additional methods.
            // The values of the right hand side predicted 
            // by the solution:
            Console.WriteLine("Predictions = {0}", solver.GetPredictions().ToString("F4"));
            // The residues (errors) of the solution:
            Console.WriteLine("Residuals = {0}", solver.GetResidues().ToString("F4"));
            // The total sum of squares of the residues:
            Console.WriteLine("Residual square error = {0}",
                solver.GetResidualSumOfSquares().ToString("F4"));

            //
            // Direct normal equations
            //

            // Alternatively, you can create a least squares
            // solution by providing the normal equations
            // directly. This may be useful when it is easy
            // to calculate the normal equations directly.
            // 
            // Here, we'll just calculate the normal equation:
            SymmetricMatrix aTa = SymmetricMatrix.FromOuterProduct(a);
            Vector aTb = b * a;
            // We find the solution by solving the normal equations
            // directly:
            x = aTa.Solve(aTb);
            Console.WriteLine("x = {0}", x.ToString("F4"));
            // However, properties of the least squares solution, such as
            // error estimates and residuals are not available.
            Console.Write("Press Enter key to exit...");
            Console.ReadLine();
        }
    }
}
Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Information
Resources
Contact Us
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