[TOC]

corrcoef

Correlation coefficient

Introduction

Correlation coefficient is a measure for linear relationship between two random variables. Let and be random variables (real or complex). Their correlation coefficient is defined as

where is the covariance, and and are the standard deviations. For a description of covariance, see the document page for the function cov.

The correlation coefficient obtained by corrcoef is actually a sample statistics, which gives an approximation of the coefficient of the population. One can use to test the null hypothesis against . The null hypothesis suggests that and are uncorrelated, whereas the alternative hypothesis suggests that they are somewhat linearly correlated. For the test statistic

the -value is given by

If the -value is less than a given significant level , the correlation is considered significant. A low -value suggests that observing the null hypothesis is unlikely. A confidence interval for is given by

where .

Usage

R = corrcoef(A)

[R, P] = corrcoef(A)

[R, P, RL, RU] = corrcoef(A)

R = corrcoef(A, name_1, value_1, name_2, value_2)

[R, P] = corrcoef(A, name_1, value_1, name_2, value_2)

[R, P, RL, RU] = corrcoef(A, name_1, value_1, name_2, value_2)

R = corrcoef(A, B)

[R, P] = corrcoef(A, B)

[R, P, RL, RU] = corrcoef(A, B)

R = corrcoef(A, B, name_1, value_1, name_2, value_2)

[R, P] = corrcoef(A, B, name_1, value_1, name_2, value_2)

[R, P, RL, RU] = corrcoef(A, B, name_1, value_1, name_2, value_2)

Examples

Example 1

This example shows the correlation coefficient of and , where with noise following normal distribution with zero mean and standard deviation of 10. The sample data show a linear relationship between and with the 95% confidence interval of as shown in the output below. The -value is about , which suggests that it is extremely unlikely that and are uncorrelated.

Input
clear
x=sort(10*rand(1,100));
% y = 1 + 2x + noise.
y=1+2*x+10*randn(size(x));
plot(x,1+2*x);
hold('on')
scatter(x,y);
hold('off')
% Covariance of x and y is C(1,2) or C(2,1)
[R,P,RL,RU]=corrcoef(x,y)
Output
All variables cleared.

R = 
 1.000000000000000   0.392400272934828
 0.392400272934828   1.000000000000000

P = 
 1.000000000000000   0.000053940195081
 0.000053940195081   1.000000000000000

RL = 
 1.000000000000000   0.212348840869820
 0.212348840869820   1.000000000000000

RU = 
 1.000000000000000   0.546683054932463
 0.546683054932463   1.000000000000000

 

Example 2

This example demonstrates the difference between corrcoef(a,'rows','complete') and corrcoef(a,'rows','pairwise').

Input
% Matrix of two columns
a=[
1.3 11.2 21;
2.5 12.8 22;
3.6 13.0 23;
4.1 14.1 24;
NaN 15.2 25;
6.1 16.3 26;
7.4 17.4 27;
NaN 18.4 28;
9.9 19.1 29;
10.1 20.9 30];
% Use 'complete'
R1=corrcoef(a,'rows','complete')
% Use 'pairwise'
R2=corrcoef(a,'rows','pairwise')
Output
R1 = 
 1.0000   0.9890   0.9962
 0.9890   1.0000   0.9955
 0.9962   0.9955   1.0000

R2 = 
 1.0000   0.9890   0.9962
 0.9890   1.0000   0.9957
 0.9962   0.9957   1.0000

References