Imports System Imports System.Globalization Imports System.Threading Imports CenterSpace.NMath.Core Namespace CenterSpace.NMath.Core.Examples.VisualBasic ' A .NET example in Visual Basic showing how to use the least squares classes to solve ' linear least squares problems. Module LeastSquaresExample Sub Main() Dim Original As CultureInfo = Thread.CurrentThread.CurrentCulture ' This example uses strings representing numbers in the US locale ' so change the current culture info. For example, "$4.30" Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US") ' Calculate the slope and intercept of the linear least squares fit ' through the five points: ' (20, .446) (30, .601), (40, .786), (50, .928), (60, .950) Dim A As New DoubleMatrix("5x1[20.0 30.0 40.0 50.0 60.0]") Dim Y As New DoubleVector("[.446 .601 .786 .928 .950]") ' Back to original culture Thread.CurrentThread.CurrentCulture = Original ' We want our straight line to be of the form y = mx + b, where b is ' not necessarily equal to zero. Thus we will set the third ' constructor argument to true so that we calculate the intercept ' parameter. Dim Lsq As New DoubleLeastSquares(A, Y, True) Console.WriteLine() Console.WriteLine() Console.WriteLine("Y-intercept = {0}", Lsq.X(0)) Console.WriteLine("Slope = {0}", Lsq.X(1)) ' We can look at the residuals which are the difference between the ' actual value of y at a point x, and the corresponding point y on ' line for the same x. Console.WriteLine("Residuals = {0}", Lsq.Residuals.ToString("F3")) ' Finally, we can look at the residual sum of squares, which is the ' sum of the squares of the elements in the residual vector. Console.WriteLine("Residual Sum of Squares (RSS) = {0}", Lsq.ResidualSumOfSquares.ToString("F3")) ' The least squares class can also be used to solve "rank-deficient" least ' square problems: A = New DoubleMatrix("6x4 [0 9 -6 3 -3 0 -3 0 1 3 -1 1 1 3 -1 1 -2 0 -2 0 3 6 -1 2]") Y = New DoubleVector("[-3 5 -2 2 1 -2]") ' For this problem we will specify a tolerance for computing the effective rank ' of the matrix A, and we will not have the class add an intercept parameter ' for us. Lsq = New DoubleLeastSquares(A, Y, 0.0000000001) Console.WriteLine("Least squares solution = {0}", Lsq.X.ToString("F3")) Console.Write("Rank computed using a tolerance of ") Console.Write(Lsq.Tolerance.ToString() + ", = ") Console.WriteLine(Lsq.Rank.ToString()) ' You can even use the least squares class to solve under-determined systems ' (the case where A has more columns than rows). A = New DoubleMatrix("6x4 [-3 -1 6 -5 5 4 -6 8 7 5 0 -4 -7 4 0 3 -7 7 -8 2 3 4 2 -4]") Y = New DoubleVector("[-3 1 8 -2]") Lsq = New DoubleLeastSquares(A.Transpose(), Y, 0.00000001) Console.Write("Solution to under-determined system = ") Console.WriteLine(Lsq.X.ToString("F3")) Console.Write("Rank computed using a tolerance of ") Console.Write(Lsq.Tolerance.ToString() + " is ") Console.WriteLine(Lsq.Rank.ToString()) Console.WriteLine() Console.WriteLine("Press Enter Key") Console.Read() End Sub End Module End Namespace← All NMath Code Examples