# VB Weighted Least Squares Example

← All NMath Code Examples

```ï»¿Imports System

Imports CenterSpace.NMath.Core
Imports CenterSpace.NMath.Matrix

Namespace CenterSpace.NMath.Matrix.Examples.VisualBasic

' A .NET example in Visual Basic demonstrating the features of the classes for solving weighted least squares problems.
Module WeightedLeastSquaresExample

Sub Main()

' Construct some test data.
Dim A As New DoubleMatrix("5x2[1 2  1 3  1 6  1 10  1 7]")
Dim B As New DoubleVector("[ 3 6 8 10 11]")

' Constructs some arbitrary weights. Observations are given
' increasingly higher weights.
Dim Weights As New DoubleVector(A.Rows, 0.2, 0.2)

Console.WriteLine()
Console.WriteLine("Solving weighted least squares problem, Ax = b ")
Console.WriteLine()
Console.WriteLine("with A = ")
Console.WriteLine(A.ToTabDelimited())
Console.WriteLine("and b = " & B.ToString())
Console.WriteLine()
Console.WriteLine("with weights = " & Weights.ToString())
Console.WriteLine()

' Construct a weighted least squares object and solve the problem. The third
' parameter says do not prepend a column of ones to the input matrix A (the
' column of ones represents a constant term in the model) as the first column
' of A is already ones.
Dim WLS As New DoubleCOWeightedLeastSq(A, Weights, False)
Dim Solution As DoubleVector = WLS.Solve(B)
Console.WriteLine("Solution = " & Solution.ToString("G5"))
Console.WriteLine("Residuals = " & WLS.ResidualVector(B).ToString("G5"))

' Construct a random problem, obtain an ordinary least squares solution,
' then use the resulting residuals to solve the same problem using a
' weighted least squares - downweighting the outliers.
Dim RNG As New RandGenUniform(-1, 2, 24)
Dim Rows As Integer = 10
Dim Cols As Integer = 3
A = New DoubleMatrix(Rows, Cols, RNG)
B = New DoubleVector(Rows, RNG)

' Ordinary Least Squares (OLS):
Dim Ols As New DoubleQRLeastSq()
Ols.Factor(A)
Dim OlsSolution As DoubleVector = Ols.Solve(B)
Dim Residuals As DoubleVector = Ols.ResidualVector(B)
Console.WriteLine()
Console.WriteLine("OLS solution = " & OlsSolution.ToString("G5"))

' Now, construct a Bisquare weighting function object
'// and use it to compute weights:
Dim WeightingFunction As New DoubleBisquareWeightingFunction(A)
WeightingFunction.GetWeights(Residuals, Weights)

' Perform the factorization with the input matrix and weights. Once the factorization is
' done we can use the weighted least squares to quickly solve for different right hand
' sides. The third parameter to the Factor method indicates that we want a column of
' ones prepended to the input matrix A representing a constant term in the model.
WLS.Factor(A, Weights, True)

' Now solve the weighted least squares problem.
Dim WlsSolution1 As DoubleVector = WLS.Solve(B)
Console.WriteLine("WLS solution = " & WlsSolution1.ToString("G5"))

' Solve with a different right hand side.
Dim C As New DoubleVector(Rows, RNG)
Dim WlsSolution2 As DoubleVector = WLS.Solve(C)
Console.WriteLine("WLS solution 2 = " & WlsSolution2.ToString("G5"))

Console.WriteLine()
Console.WriteLine("Press Enter Key")