New Version 5.0!

Try it for free with our fully functional 60-day trial version.

Download now!

QuickStart Samples

Polynomial Regression QuickStart Sample (C#)

Illustrates how to fit data to polynomials using the PolynomialRegressionModel class in C#.

Visual Basic code F# code IronPython code Back to QuickStart Samples

using System;

using Extreme.Mathematics;
using Extreme.Statistics;

namespace Extreme.Numerics.QuickStart.CSharp
{
	/// <summary>
	/// Illustrates the use of the PolynomialRegressionModel class
	/// to perform polynomial regression.
	/// </summary>
	class PolynomialRegression
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			// Polynomial regression can be performed using 
			// the PolynomialRegressionModel class.
			//
			// This QuickStart sample uses data from the National Institute
			// for Standards and Technology's Statistical Reference Datasets
			// library at http://www.itl.nist.gov/div898/strd/.

			// Note that, due to round-off error, the results here will not be exactly
			// the same as the NIST results, which were calculated using 500 digits
			// of precision!

			// We use the 'Pontius' dataset, which contains measurement data
			// from the calibration of load cells. The independent variable is the load.
			// The dependent variable is the deflection.
			double[] deflectionData = 
			{
				.11019, .21956, .32949, .43899, .54803, .65694, .76562, 
				.87487, .98292, 1.09146, 1.20001, 1.30822, 1.41599, 1.52399,
				1.63194, 1.73947, 1.84646, 1.95392, 2.06128, 2.16844, .11052,
				.22018, .32939, .43886, .54798, .65739, .76596, .87474, .98300,
				1.09150, 1.20004, 1.30818, 1.41613, 1.52408, 1.63159, 1.73965,
				1.84696, 1.95445, 2.06177, 2.16829
			};
			double[] loadData =
			{
				150000, 300000, 450000, 600000, 750000, 900000, 
				1050000, 1200000, 1350000, 1500000, 1650000, 1800000,
				1950000, 2100000, 2250000, 2400000, 2550000, 2700000,
				2850000, 3000000, 150000, 300000, 450000, 600000, 
				750000, 900000, 1050000, 1200000, 1350000, 1500000,
				1650000, 1800000, 1950000, 2100000, 2250000, 2400000,
				2550000, 2700000, 2850000, 3000000
			};
			NumericalVariable deflection = new NumericalVariable("deflection", deflectionData);
			NumericalVariable load = new NumericalVariable("load", loadData);

			// Now create the regression model. We supply the dependent and independent
			// variable, and the degree of the polynomial:
			PolynomialRegressionModel model = new PolynomialRegressionModel(deflection, load, 2);

			// The Compute method performs the actual regression analysis.
			model.Compute();

			// The Parameters collection contains information about the regression 
			// parameters.
			Console.WriteLine("Variable                  Value    Std.Error  t-stat  p-Value");
			foreach(Parameter parameter in model.Parameters)
			{
				// Parameter objects have the following properties:
				Console.WriteLine("{0,-19}{1,12:E4}{2,12:E2}{3,8:F2} {4,7:F4}",
					// Name, usually the name of the variable:
					parameter.Name, 
					// Estimated value of the parameter:
					parameter.Value, 
					// Standard error:
					parameter.StandardError,
					// The value of the t statistic for the hypothesis that the parameter
					// is zero.
					parameter.Statistic, 
					// Probability corresponding to the t statistic.
					parameter.PValue);
			}
			Console.WriteLine();

			// In addition to these properties, Parameter objects have a GetConfidenceInterval
			// method that returns a confidence interval at a specified confidence level.
			// Notice that individual parameters can be accessed using their numeric index.
			// Parameter 0 is the intercept, if it was included.
			Interval confidenceInterval = model.Parameters[0].GetConfidenceInterval(0.95);
			Console.WriteLine("95% confidence interval for constant term: {0:E4} - {1:E4}",
				confidenceInterval.LowerBound, confidenceInterval.UpperBound);
			Console.WriteLine();
			
			// There is also a wealth of information about the analysis available
			// through various properties of the LinearRegressionModel object:
			Console.WriteLine("Residual standard error: {0:E3}", model.StandardError);
			Console.WriteLine("R-Squared:               {0:F4}", model.RSquared);
			Console.WriteLine("Adjusted R-Squared:      {0:F4}", model.AdjustedRSquared);
			Console.WriteLine("F-statistic:             {0:F4}", model.FStatistic);
			Console.WriteLine("Corresponding p-value:   {0:E5}", model.PValue);
			Console.WriteLine();

			// Much of this data can be summarized in the form of an ANOVA table:
			Console.WriteLine(model.AnovaTable.ToString());

			Console.Write("Press any key to exit.");
			Console.ReadLine();
		}
	}
}