Home

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:

Linear Correlation Coefficient

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

 

ApplicationApplication: 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:
     
    Linear Correlation Coefficient
    Control (Name) Text Other Properties
    Label Label   x  
    Label Label   y  
    Label Label   Values to Add:  
    TextBox ListBox txtX    
    TextBox ListBox txtY    
    Button Button btnAdd Add  
    ListView Label 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 Button btnCalculate Calculate  
    Label Label   n =  
    TextBox Text Box txtN    
    Label Label   Σx =  
    TextBox Text Box txtSumX    
    Label Label   Σx2 =  
    TextBox Text Box txtSumXSquare    
    Label Label   Σxy =  
    TextBox Text Box txtSumXY    
    Label Label   Σy =  
    TextBox Text Box txtSumY    
    Label Label   Σy2 =  
    TextBox Text Box txtSumYSquare    
    PictureBox PictureBox pbxR    
    Label Label   =  
    TextBox Text Box txtR    
    Label Label   Conclusion  
    TextBox ListBox txtConclusion    
    Button Button btnClear Clear/Reset  
    Button Button btnClose Close  
  9. Double-click an unoccupied area of the form
  10. Return to the form and double-click the Add button
  11. Return to the form
  12. Double-click the Calculate button
  13. Return to the form
  14. Double-click the Clear/Reset button
  15. Return to the form
  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());
    
                    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;
                }
            }
        }
    }
  18. To execute, press F5
  19. Enter each pair of values and click Add each time
     
    Linear Correlation Coefficient
  20. After adding the values, click Calculate
     
    Linear Correlation Coefficient
  21. Close the form and return to your programming environment

Application

 

Home Copyright © 2010-2015 FunctionX