covCovariance
Covariance can be used to measure the linear relationship between two random variables.
For the real random variables
where
If either
where
C = cov(A)C = cov(A, w)C = cov(A, nanflag)C = cov(A, w, nanflag)A should be an array of real or complex numbers with ndims(A) == 2.
If A is empty, it returns NaN.
If A is scalar, it returns 0.
If A is a vector, it returns the variance of A.
If A is a matrix, each column of A should contain observations of a random variable. Then, C is the covariance matrix where
C(i,j) is the covarance of the C(i,i) is the variance of the A.w should be either 0 or 1.
w == 0 (default), variance or covariance is normalised by length(A)-1.w == 1, variance or covariance is normalised by length(A).nanflag should be either 'includenan', 'omitrows' or 'partialrows'.
'includenan' (default): all NaN values are included in the computation of C.'omitrows': When any row of A contains NaN, the whole row of A will be removed before computing any element of C. In other words, the number of rows of A will be reduced before computing C. (See Example 2 and Example 4 below.) The output C is NaN if the removal of NaN results in an empty matrix.'partialrows': When computing covariance C(i,j), the NaN appearing in one of columns and the corresponding element in the other column will be removed. (See Example 2 and Example 4 below.) The covariance C(i,j) is NaN if the removal of NaN results in two empty columns. C = cov(A, B) C = cov(A, B, w)C = cov(A, B, nanflag)C = cov(A, B, w, nanflag)A and B should be real or complex array with identical sizes.
If A or B is empty, it returns NaN.
If A or B is a scalar, it returns 0.
If A and B are vectors, they contains observations of two random variables. The output C is a A and B.
ndims(A)>=3 or ndims(B)>=3, the matrix will be reshaped to a vector. w should be either 0 (default) or 1.
w == 0, variance or covariance is normalised by length(A)-1.w == 1, variance or covariance is normalised by length(A).nanflag should be either 'includenan', 'omitrows' or 'partialrows'.
'includenan' (default): all NaN values are included in the computation of C.'omitrows': When the A or B contains NaN, the A and B will be removed before computing any element of C. (See Example 3 below.) The output C is NaN if the removal of NaN results in empty arrays. 'partialrows': This is different from omitrows in such a way that the removal of NaN is deferred to the stage when individual elements of C is computed. (See See Example 3 below.)This example shows the covariance of
clear
x=sort(10*rand(1,100));
% y = 1 + 2x + noise.
y=1+2*x+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)
C=cov(x,y)
ans =
8.5876 16.788
16.788 33.679
This example demonstrates the difference between cov(a,'omitrows')and cov(a,'partialrows').
'omitrows' is used, NaN elements in a(:,1) and the corresponding elements in a(:,2) are removed before the computation of C1. Therefore, when C1(2,2) is computed, the shortened vector [11 12 13 14 16 17 19 20] is used although a(:,2) has no NaN elements.'partialrows' is used, the removal of NaN is performed when individual elements of C2 is computed. Therefore, the computation of C2(2,2) uses the whole vector [11 12 13 14 15 16 17 18 19 20].% Matrix of two columns
a=[
1 11;
2 12;
3 13;
4 14;
NaN 15;
6 16;
7 17;
NaN 18;
9 19;
10 20];
% Use 'omitrows'
C1=cov(a,'omitrows')
% Use 'partialrows'
C2=cov(a,'partialrows')
C1 =
10.786 10.786
10.786 10.786
C2 =
10.786 10.786
10.786 9.1667
This example demonstrates the difference of cov(a,b,'omitrows') and cov(a,b,'partialrows').
'omitrows' is used, NaN elements in a and the corresponding elements in b are removed before the computation of C1. Therefore, when C1(2,2) is computed, the shortened vector [11 12 13 14 16 17 19 20] is used although b has no NaN elements.'partialrows' is used, the removal of NaN is performed when individual elements of C2 is computed. Therefore, the computation of C2(2,2) uses the whole vector [11 12 13 14 15 16 17 18 19 20].% Vector with some NaN elements.
a=[1 2 3 4 nan 6 7 nan 9 10];
% Another vector
b=[11 12 13 14 15 16 17 18 19 20];
% Use 'omitrows'
C1=cov(a,b,'omitrows')
% Use 'partialrows'
C2=cov(a,b,'partialrows')
C1 =
10.786 10.786
10.786 10.786
C2 =
10.786 10.786
10.786 9.1667
In this example, the diagonal elements of a are all NaN.
includenan is used, all of the computed covariances are NaN.omitrows is used, all rows of a are removed, and hence a becomes empty, when obtaining the covariances. As a result, the output covariances are all NaN. paritalrows is used, only some of the rows are removed. For example, when the covariance of a(:,1) and a(:,2) are computed, only the first two rows are removed. Hence, the covariance obtained is not NaN.% 5x5 random matrix with NaN diagonal elements.
a=rand(5,5);
a(1,1)=nan;
a(2,2)=nan;
a(3,3)=nan;
a(4,4)=nan;
a(5,5)=nan;
a
% Computation includes NaN. The output contains all NaN.
cov(a)
cov(a,'includenan')
% Matrix a becomes an empty matrix,
% since all rows are removed before
% computing the variances.
cov(a,'omitrows')
% Only some elements are removed.
cov(a,'partialrows')
a = 1e-1 ×
NaN 6.0066 8.1434 6.5663 8.6598
0.4499 NaN 9.3268 6.3138 3.7444
1.0603 0.9255 NaN 6.0063 2.4672
0.9516 4.9147 2.5800 NaN 9.7731
7.2988 2.6474 8.3618 7.2957 NaN
ans =
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN
ans =
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN
ans =
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN
ans = 1e-1 ×
1.0562 -0.0680 0.4451 0.2430 0.0242
-0.0680 0.5193 -0.1295 0.0436 0.9899
0.4451 -0.1295 0.9357 -0.0181 -0.8781
0.2430 0.0436 -0.0181 0.0303 0.0850
0.0242 0.9899 -0.8781 0.0850 1.2925