How to Calculate a Correlation
Can one statistic measure both the strength and direction of a linear relationship between two variables? Sure! Statisticians use the correlation coefficient to measure the strength and direction of the linear relationship between two numerical variables X and Y. The correlation coefficient for a sample of data is denoted by r.
Although the street definition of correlation applies to any two items that are related (such as gender and political affiliation), statisticians use this term only in the context of two numerical variables. The formal term for correlation is the correlation coefficient. Many different correlation measures have been created; the one used in this case is called the Pearson correlation coefficient.
The formula for the correlation (r) is
where n is the number of pairs of data;
are the sample means of all the xvalues and all the yvalues, respectively; and s_{x} and s_{y} are the sample standard deviations of all the x and yvalues, respectively.
You can use the following steps to calculate the correlation, r, from a data set:

Find the mean of all the xvalues

Find the standard deviation of all the xvalues (call it s_{x}) and the standard deviation of all the yvalues (call it s_{y}).
For example, to find s_{x}, you would use the following equation:

For each of the n pairs (x, y) in the data set, take

Add up the n results from Step 3.

Divide the sum by s_{x} ∗ s_{y}.

Divide the result by n – 1, where n is the number of (x, y) pairs. (It’s the same as multiplying by 1 over n – 1.)
This gives you the correlation, r.
For example, suppose you have the data set (3, 2), (3, 3), and (6, 4). You calculate the correlation coefficient r via the following steps. (Note that for this data the xvalues are 3, 3, 6, and the yvalues are 2, 3, 4.)

Calculating the mean of the x and y values, you get

The standard deviations are s_{x} = 1.73 and s_{y} = 1.00.

The n = 3 differences found in Step 2 multiplied together are: (3 – 4)(2 – 3) = (– 1)( – 1) = +1; (3 – 4)(3 – 3) = (– 1)(0) = 0; (6 – 4)(4 – 3) = (2)(1) = +2.

Adding the n = 3 Step 3 results, you get 1 + 0 + 2 = 3.

Dividing by s_{x} ∗ s_{y} gives you 3 / (1.73 ∗ 1.00) = 3 / 1.73 = 1.73. (It’s just a coincidence that the result from Step 5 is also 1.73.)

Now divide the Step 5 result by 3 – 1 (which is 2), and you get the correlation r = 0.87.