When analysing data it is often useful to find an equation to show the relationship in a certain set of data. The linear least squares fit tries to do exactly that.

Given a set of data points, it tries to find a straight line with the equation y = Mx + B which best fits the data, using the least squares technique, which works out the squares of the deviations of each point and then tries to minimize those.

		public static void LeastSquaresFitLinear(Pnt[] points, int numPoints, ref double M, ref double B)
		{
			//Gives best fit of data to line Y = MC + B
			double x1, y1, xy, x2, J;
			int i;

			x1 = 0.0;
			y1 = 0.0;
			xy = 0.0;
			x2 = 0.0;

			for (i = 0; i < numPoints; i++)
			{
				x1 = x1 + points[i].X;
				y1 = y1 + points[i].Y;
				xy = xy + points[i].X * points[i].Y;
				x2 = x2 + points[i].X * points[i].X;
			}

			J = ((double)numPoints * x2) - (x1 * x1);
			if (J != 0.0)
			{
				M = (((double)numPoints * xy) - (x1 * y1)) / J;
				M = Math.Floor(1.0E3 * M + 0.5) / 1.0E3;
				B = ((y1 * x2) - (x1 * xy)) / J;
				B = Math.Floor(1.0E3 * B + 0.5) / 1.0E3;
			}
			else
			{
				M = 0;
				B = 0;
			}
		}
  • Share/Bookmark

Related posts:

  1. Maths algorithms in C#: Least squares fit with a log abscissa This algorithm for the least squares fit uses a logarithmic...
  2. Maths algorithms in C#: Least squares fit with a log ordinate We have already looked at the linear least squares fit,...
  3. Maths alogrithms in C#: Least squares fit using full logs We have already looked at several other ways of doing...
  4. Maths algorithms in C#: The median The median is defined as the data point that falls...
  5. Maths algorithms in C#: The geometric mean The geometric mean is similar to the arithmetic mean in...

Related posts brought to you by Yet Another Related Posts Plugin.