corrcoefCorrelation coefficient
Correlation coefficient is a measure for linear relationship between two random variables. Let
where cov.
The correlation coefficient corrcoef is actually a sample statistics, which gives an approximation of the coefficient
the
If the
where
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)A should be an array of real or complex numbers, where ndims(A)==2.
If A is empty or scalar, the outputs R, P, RL and RU are all NaN.
If A is a vector, the outputs R, P, RL and RU are all equal to 1. If A contains NaN or Inf, they are all equal to NaN.
If A is a matrix, each column of A contains observations of a random variable.
R is the correlation matrix where R(i,j) is the correlation coefficient of the i-th and j-th columns for R are all equal to 1.P is a matrix having the same size as R, where P(i,j) is the R(i,j). The diagonal elements of P are all equal to 1.RL and RU are matrices having the same size as R, where RL(i,j) and RU(i,j) give the lower and upper bounds, respectively, for the R(i,j). The diagonal elements of RL and RU are equal to 1.R is complex, the outputs P, RL and RU are undefined. Therefore, there should be only one output argument.name_i is either 'alpha' or 'rows'.
When it is 'alpha', value_i should be a real number
When it is 'rows', value_i is either 'all' (default), 'complete' or 'pairwise':
all (default): NaN values are included in the computation of the output(s).complete: When any row of A contains NaN, the whole row of A will be removed before computing any element of R. In other words, the number of rows of A will be reduced before computing R. (See Example 2 below.) The output R is NaN if the removal of NaN results in an empty matrix.pairwise: When computing the correlation coefficient R(i,j), the A are examined. Any NaN appearing in one of columns and the corresponding element in the other column will be removed. (See Example 2 below.) The coefficient R(i,j) is NaN if the removal of NaN results in two empty columns.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)A and B are real or complex arrays having the same number of elements.
A and B should be vectors. Otherwise, they will be reshaped to vectors.
If A and B are empty, the outputs R, P, RL and RU are NaN.
If A and B are scalars, and
R, P, RL and RU are NaN.R, P, RL and RU are If A and B are vectors, the outputs R, P, RL and RU are
R is a
R(1,2) and R(2,1) are the correlation coefficients.P is a R is real, where
P(1,2) and P(2,1) are the R(1,2) and R(2,1), respectively.RL is a R is real, where
RL(1,2) and RL(2,1) are the lower bounds of the R(1,2) and R(2,1), respectively.RU is a R is real, where
RU(1,2) and RU(2,1) are the upper bounds of the R(1,2) and R(2,1), respectively.If the output R is complex, the outputs P, RL and RU are undefined. Therefore, there should be only one output argument.
name_i is either 'alpha' or 'rows'.
When it is 'alpha', value_i should be a real number
When it is 'rows', value_i is either 'all' (default), 'complete' or 'pairwise':
all (default): NaN values are included in the computation of the output(s).complete: When the A or B contains NaN, the A and B will be removed before computing any element of R. The output R is NaN if the removal of NaN results in empty arrays.pairwise: This is different from complete in such a way that the removal of NaN is deferred to the stage when individual elements of R is computed.This example shows the correlation coefficient of
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)
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
This example demonstrates the difference between corrcoef(a,'rows','complete') and corrcoef(a,'rows','pairwise').
'complete' is used, NaN elements in a(:,1) and the corresponding elements in a(:,2) and a(:,3) are removed before the computation of R1. Therefore, when R1(3,2) is computed, vectors of reduced length, namely, [11.2 12.8 13.0 14.1 16.3 17.4 19.1 20.9] and [21 22 23 24 26 27 29 30] are used although neither a(:,2) nor a(:,3) have NaN elements.'pairwise' is used, the removal of NaN is performed when individual elements of R2 is computed. Therefore, the computation of R2(3,2) uses all elements of a(:,2) and a(:,3).R1(3,2) and R2(3,2) are different due to different values of 'rows'.% 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')
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