Microsoft Visual C# Statistics: The Linear Correlation Coefficient

 Introduction
 In statistics, a correlation is a value that defines a relationship between two variables so that it shows how one of the variables is tied (related) to the other.

 The linear correlation coefficient, represented as r, defines the strength of the correlation between two elements of a sample. The elements are usually represented with x and y. This almost means that a value is considered as a pair of x and y. The linear correlation coefficient is also called the Pearson product moment correlation coefficient (from Karl Pearson (1857-1936)). The formula to calculate the linear correlation coefficient is:

The factors in this equation are:

 Factor Description n The number of pairs of elements x An element of the series y The element corresponding to x in the pair Σx The sum of the x elements Σy The sum of the y elements Σxy The sum of the products of each pair Σx2 The sum of the squares of the x values (Σx)2 The square of the Σx sums Σy2 The sum of the squares of the y values (Σy)2 The square of the Σy sum

 Application: Creating the Application
1. Start Microsoft Visual Studio
2. To create a new application, on the main menu, click File -> New Project...
3. In the middle list, click Windows Forms Application
4. Change the Name to LinearCorrelationCoefficient1
5. Click OK
6. In the Solution Explorer, right-click Form1.cs and click Rename
7. Type Exercise.cs and press Enter
8. Design the form as follows:

Control (Name) Text Other Properties
Label   x
Label   y
TextBox txtX
TextBox txtY
ListView lvwValues   FullRowSelect: True
GridLines: True
View: Details
Columns
 (Name) Text TextAlign Width colIndex # 30 colX x Right 70 colY y Right 70 colXSquared x2 Right 90 colYSquared y2 Right 90 colXY xy Right 100
Button btnCalculate Calculate
Label   n =
TextBox txtN
Label   Σx =
TextBox txtSumX
Label   Σx2 =
TextBox txtSumXSquare
Label   Σxy =
TextBox txtSumXY
Label   Σy =
TextBox txtSumY
Label   Σy2 =
TextBox txtSumYSquare
PictureBox pbxR
Label   =
TextBox txtR
Label   Conclusion
TextBox txtConclusion
Button btnClear Clear/Reset
Button btnClose Close
9. Double-click an unoccupied area of the form
12. Double-click the Calculate button
14. Double-click the Clear/Reset button
16. Double-click the Close button
17. Change the file as follows:
```using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LinearCorrelationCoefficient1
{
public partial class LinearCorrelationCoefficient : Form
{
List<SamplePair> pairs;

public LinearCorrelationCoefficient()
{
InitializeComponent();
}

private void ShowPairs()
{
lvwValues.Items.Clear();

double SumX = 0.00, SumY = 0.00,
SumXSquare = 0.00, SumYSquare = 0.00,
SumXY = 0.00;

int i = 0;
foreach( SamplePair sp in pairs)
{
ListViewItem lviPair = new ListViewItem((i + 1).ToString());

i++;
}

foreach (SamplePair sp in pairs)
{
SumX += sp.x;
SumY += sp.y;
SumXSquare += (sp.x * sp.x);
SumYSquare += (sp.y * sp.y);
SumXY += (sp.x * sp.y);
}

ListViewItem lviTotals = new ListViewItem("Totals");
}

EventArgs e)
{
pairs = new List<SamplePair>();
}

private void btnAdd_Click(object sender, EventArgs e)
{
double x = 0.00, y = 0.00;

if( txtX.Text == "" )
{
MessageBox.Show("You must enter a value for x.");
return;
}

if (txtY.Text == "")
{
MessageBox.Show("You must enter a value for y.");
return;
}

try
{
x = double.Parse(txtX.Text);
}
catch (FormatException)
{
}

try
{
y = double.Parse(txtY.Text);
}
catch (FormatException)
{
}

SamplePair pair = new SamplePair(x, y);
ShowPairs();

txtX.Text = "";
txtY.Text = "";
txtX.Focus();

ShowPairs();
}

private void btnCalculate_Click(object sender, EventArgs e)
{
double SumX = 0.00, SumY = 0.00,
SumXSquare = 0.00, SumYSquare = 0.00,
SumXY = 0.00, r = 0.00;

foreach (SamplePair sp in pairs)
{
SumX += sp.x;
SumY += sp.y;
SumXSquare += (sp.x * sp.x);
SumYSquare += (sp.y * sp.y);
SumXY += (sp.x * sp.y);
}

double NTimesSumXY = pairs.Count * SumXY;
double SumXTimesSumY = SumX * SumY;
double SquareRoot1 = pairs.Count * SumXSquare - Math.Pow(SumX, 2);
double SquareRoot2 = pairs.Count * SumYSquare - Math.Pow(SumY, 2);

r = (NTimesSumXY - SumXTimesSumY) / (Math.Sqrt(SquareRoot1) *
Math.Sqrt(SquareRoot2));

txtN.Text = pairs.Count.ToString();
txtSumX.Text = SumX.ToString("F");
txtSumY.Text = SumY.ToString("F");
txtSumXSquare.Text = SumXSquare.ToString();
txtSumYSquare.Text = SumYSquare.ToString();
txtSumXY.Text = SumXY.ToString("F");

txtR.Text = r.ToString("0.00#");
}

private void btnClear_Click(object sender, EventArgs e)
{
pairs.Clear();
lvwValues.Items.Clear();

txtN.Text = "";
txtSumX.Text = "";
txtSumY.Text = "";
txtSumXSquare.Text = "";
txtSumYSquare.Text = "";
txtSumXY.Text = "";
txtR.Text = "";
}

private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}

public class SamplePair
{
public double x { get; set; }
public double y { get; set; }

public SamplePair(double X, double Y)
{
x = X;
y = Y;
}

public double xSquared
{
get
{
return x * x;
}
}

public double ySquared
{
get
{
return y * y;
}
}

public double xy
{
get
{
return x * y;
}
}
}
}```
18. To execute, press F5
19. Enter each pair of values and click Add each time

20. After adding the values, click Calculate