ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

Perason Correlation Coefficient for a datatable

An extension method that add the possibility calculate the Pearson correlation coefficient using the names of two columns of the data table in question.

Source

namespace System.Data
{
	using Collections.Generic;

	public static class DataTableExtensions
	{
		public static double PearsonCorrelate(this DataTable aSourceTable, string aColumn1, string aColumn2)
		{
			double dblResult = 0;

			// Buffer
			double tmpX = 0;
			double tmpY = 0;

			// These will be used in the Pearson correalation formula
			double X = 0;       // Sum of X
			double Y = 0;       // Sum of Y
			double XX = 0;      // Sum of X * X
			double YY = 0;      // Sum of Y * Y
			double XY = 0;      // Sum of X * Y

			int iRowCount = aSourceTable.Rows.Count;

			foreach (DataRow row in aSourceTable.Rows)
			{
				tmpX = Convert.ToDouble(row[aColumn1]);
				tmpY = Convert.ToDouble(row[aColumn2]);

				X += tmpX;
				Y += tmpY;

				XX += tmpX * tmpX;
				YY += tmpY * tmpY;
				XY += tmpX * tmpY;
			}

			dblResult = (iRowCount * XY - X * Y) / Math.Sqrt((iRowCount * XX - X * X) * (iRowCount * YY - Y * Y));

			return dblResult;
		}
	}
}

Example

DataTable tblWeather = GetWeatherInformation();

double dblCorrelation = tblWeather.PearsonCorrelation("WindSpeed" , "Temperature");

Author: Mahmood Salamah

Submitted on: 26 aug. 2016

Language: C#

Type: System.Data.DataTable

Views: 4313