Microsoft Visual C# Statistics: The Linear Correlation Coefficient
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…
4 min read863 words
Introduction#
In statistical analysis, correlation describes how two variables are related, showing how changes in one correspond to changes in the other. The linear correlation coefficient, denoted as r, measures the relationship strength between two sample elements, often labeled x and y. This means data is viewed as pairs of these elements. Also known as the Pearson product moment correlation coefficient, named after Karl Pearson, the calculation uses this formula:
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 |
| Σx 2 | The sum of the squares of the x values |
| (Σx) 2 | The square of the Σx sums |
| Σy 2 | The sum of the squares of the y values |
| (Σy) 2 | The square of the Σy sum |
Hands-on Exercise: Building the Application
- Start Microsoft Visual Studio
- To create a new application, on the main menu, click File -> New Project...
- In the middle list, click Windows Forms Application
- Change the Name to LinearCorrelationCoefficient1
- Click OK
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type Exercise.cs and press Enter
- Design the form as follows: Control (Name) Text Other Properties Label x Label y Label Values to Add: TextBox txtX TextBox txtY Button btnAdd Add ListView lvwValues FullRowSelect: True GridLines: True View: Details Columns (Name) Text TextAlign Width colIndex # 30 colX x Right 70 colY y Right 70 colXSquared x 2 Right 90 colYSquared y 2 Right 90 colXY xy Right 100 Button btnCalculate Calculate Label n = TextBox txtN Label Σx = TextBox txtSumX Label Σx 2 = TextBox txtSumXSquare Label Σxy = TextBox txtSumXY Label Σy = TextBox txtSumY Label Σy 2 = TextBox txtSumYSquare PictureBox pbxR Label = TextBox txtR Label Conclusion TextBox txtConclusion Button btnClear Clear/Reset Button btnClose Close
- Double-click an unoccupied area of the form
- Return to the form and double-click the Add button
- Return to the form
- Double-click the Calculate button
- Return to the form
- Double-click the Clear/Reset button
- Return to the form
- Double-click the Close button
- 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()); lviPair.SubItems.Add(sp.x.ToString("F")); lviPair.SubItems.Add(sp.y.ToString("F")); lviPair.SubItems.Add(sp.xSquared.ToString("0.0000#")); lviPair.SubItems.Add(sp.ySquared.ToString("0.0000#")); lviPair.SubItems.Add(sp.xy.ToString("0.0000#")); lvwValues.Items.Add(lviPair); 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"); lviTotals.SubItems.Add("Σx = " + SumX.ToString("F")); lviTotals.SubItems.Add("Σy = " + SumY.ToString("F")); lviTotals.SubItems.Add("Σx² = " + SumXSquare.ToString("0.0000#")); lviTotals.SubItems.Add("Σy² = " + SumYSquare.ToString("0.0000#")); lviTotals.SubItems.Add("Σxy = " + SumXY.ToString("0.0000#")); lvwValues.Items.Add(lviTotals); } private void LinearCorrelationCoefficient_Load(object sender, 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); pairs.Add(pair); 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; } } } }
- To execute, press F5
- Enter each pair of values and click Add each time
- After adding the values, click Calculate
- Close the form and return to your programming environment
Home Copyright © 2010-2011 C# Key

