Example: Linear Regression

The coefficients of a simple linear regression model, without an intercept, are computed.
using System;
using Imsl.Stat;

public class LinearRegressionEx1
{
	public static void  Main(String[] args)
	{
		// y = 4*x0 + 3*x1
		LinearRegression r = new LinearRegression(2, false);
		double[] c = new double[]{4, 3};
		double[] x0 = {1, 5};
		double[] x1 = {0, 2};
		double[] x2 = {-1, 4};
		
		r.Update(x0, 1 * c[0] + 5 * c[1]);
		r.Update(x1, 0 * c[0] + 2 * c[1]);
		r.Update(x2, - 1 * c[0] + 4 * c[1]);
		double[] coef = r.GetCoefficients();
		Console.Out.WriteLine
			("The computed regression coefficients are {" + 
			coef[0] + ", " + coef[1] + "}");
	}
}

Output

The computed regression coefficients are {4, 3}

Link to C# source.