[TOC]

cov

Covariance

Introduction

Covariance can be used to measure the linear relationship between two random variables.

For the real random variables and , their covariance is defined as

where and are the means of and , respectively. A positive indicates that samples of tend to lie on a line with positive slope. Whereas, a negative indicates that samples of tend to lie on a line with negative slope. It is obvious that for real and .

If either or is complex, the covariance is defined as

where denotes the complex conjugate of . Note that can be complex and .

Usage

C = cov(A)

C = cov(A, w)

C = cov(A, nanflag)

C = cov(A, w, nanflag)

C = cov(A, B)

C = cov(A, B, w)

C = cov(A, B, nanflag)

C = cov(A, B, w, nanflag)

Examples

Example 1

This example shows the covariance of and , where with noise following the standard normal distribution. The variables and show a strong linear relationship and as shown in the output below.

Input
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)
Output
ans = 
 8.5876   16.788
 16.788   33.679

 

Example 2

This example demonstrates the difference between cov(a,'omitrows')and cov(a,'partialrows').

Input
% 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')
Output
C1 = 
 10.786   10.786
 10.786   10.786

C2 = 
 10.786   10.786
 10.786   9.1667

Example 3

This example demonstrates the difference of cov(a,b,'omitrows') and cov(a,b,'partialrows').

Input
% 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')

Output
C1 = 
 10.786   10.786
 10.786   10.786

C2 = 
 10.786   10.786
 10.786   9.1667

Example 4

In this example, the diagonal elements of a are all NaN.

Input
% 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')
Output
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