1. 程式人生 > >【 MATLAB 】協方差 cov以及協方差矩陣基礎知識

【 MATLAB 】協方差 cov以及協方差矩陣基礎知識

Covariance 翻譯為協方差,因此,MATLAB裡面的函式cov也就是求協方差了。至於MATLAB語言裡面的協方差函式cov的語法是什麼樣的以及怎麼用的,我們稍後再說,這裡首先介紹下協方差相關的基礎知識點。

本文內容參考自MATLAB的幫助手冊,有的時候不得不說,資料手冊才是最好的教材,不僅對於MATLAB,這裡提供的都是原滋原味的官方內容。例如我經常去了解一些MATLAB中的相關函式,命令等,都可以通過MATLAB的資料手冊;如果我想了解一些IP核以及與之相關的知識,我可以檢視Xilinx的官方資料手冊,內容應有盡有,相比而言,如果我去借一些書籍去檢視FPGA的IP核,不僅版本陳舊,而已也有可能翻譯的有問題,讓人一知半解。

廢話就說到這裡,下面正式開始介紹。



目錄

基礎知識

協方差(Covariance):

協方差矩陣( covariance matrix):

矩陣的協方差:

方差:(這是贈送的)

MATLAB中的 cov

語法格式:

C = cov(A)

C = cov(A,B) 

C = cov(___,w)

C = cov(___,nanflag)

示例

C = cov(A) 舉例(矩陣的協方差)

cov(A,B) 舉例之兩個向量之間的協方差

cov(A,B) 舉例之兩個矩陣之間的協方差

Specify Normalization Weight

Covariance Excluding NaN




基礎知識

協方差(Covariance):

對於兩個隨機變數向量A和B,那二者之間的協方差定義為:

其中\mu _{A}表示向量A的均值,\mu_{B}表示向量B的均值。

協方差矩陣( covariance matrix):

兩個隨機變數的協方差矩陣是每個變數之間成對協方差計算的矩陣,

矩陣的協方差:

對於矩陣A,其列各自是由觀察組成的隨機變數,協方差矩陣是每個列組合之間的成對協方差計算。 換一種說法

方差:(這是贈送的)

對於由N個標量觀測組成的隨機變數向量A,方差定義為

其中u是A的均值:

一些方差定義使用歸一化因子N而不是N-1,可以通過將w設定為1來指定。在任何一種情況下,假設均值具有通常的歸一化因子N.

(注意:w是後面要說的MATLAB裡面的協方差函式的一個引數而已,在具體的MATLAB函式裡面可以通過設定w來指定歸一化因子!)



MATLAB中的 cov

語法格式:

下面逐個講解:

C = cov(A)

C = cov(A) returns the covariance.

C = cov(A)返回協方差。

  • If A is a vector of observations, C is the scalar-valued variance.

  • 如果A是一個觀測向量,那麼C是一個標量值的方差。

  • If A is a matrix whose columns represent random variables and whose rows represent observations, C is the covariance matrix with the corresponding column variances along the diagonal.

  • 如果A是矩陣,其列表示隨機變數,其行表示觀測值,則C是協方差矩陣,沿對角線具有相應的列方差。(協方差矩陣的協方差是列的協方差值)

  • C is normalized by the number of observations-1. If there is only one observation, it is normalized by 1.

  • C由觀察數-1歸一化。 如果只有一個觀察值,則將其標準化為1。

  • If A is a scalar, cov(A) returns 0. If A is an empty array, cov(A)returns NaN.

  • 如果A是標量,則cov(A)返回0.如果A是空陣列,則cov(A)返回NaN。

(你看看人家考慮的多周全!)


C = cov(A,B) 

C = cov(A,B) returns the covariance between two random variables A and B.

C = cov(A,B) 返回兩個隨機變數A和B之間協方差。

  • If A and B are vectors of observations with equal length, cov(A,B) is the 2-by-2 covariance matrix.

  • 如果A和B是同等長度的觀測向量,那麼C是一個2*2的協方差矩陣。

  • If A and B are matrices of observations, cov(A,B) treats A and B as vectors and is equivalent to cov(A(:),B(:))A and B must have equal size.

  • 如果A和B是觀察矩陣,則cov(A,B)將A和B視為向量,並且等同於cov(A(:),B(:))。 A和B必須具有相同的大小。

  • If A and B are scalars, cov(A,B) returns a 2-by-2 block of zeros. If A and B are empty arrays, cov(A,B) returns a 2-by-2 block of NaN.

  • 如果A和B是標量,則cov(A,B)返回2乘2的零塊。 如果A和B是空陣列,則cov(A,B)返回2乘2的NaN塊。


C = cov(___,w)

C = cov(___,w) specifies the normalization weight for any of the previous syntaxes. When w = 0 (default), C is normalized by the number of observations-1. When w = 1, it is normalized by the number of observations.

C = cov(___,w)指定任何先前語法的歸一化權重。 當w = 0(預設值)時,C由觀測數-1歸一化。 當w = 1時,它通過觀察次數歸一化。


C = cov(___,nanflag)

C = cov(___,nanflag) specifies a condition for omitting NaN values from the calculation for any of the previous syntaxes. For example, cov(A,'omitrows') will omit any rows of A with one or more NaN elements.

C = cov(___,nanflag)指定從任何先前語法的計算中省略NaN值的條件。 例如,cov(A,'omitrows')將省略具有一個或多個NaN元素的A的任何行。


示例

下面舉例說明重要的語法格式:

C = cov(A) 舉例(矩陣的協方差)

Create a 3-by-4 matrix and compute its covariance

A = [5 0 3 7; 1 -5 7 3; 4 9 8 10];
C = cov(A)
C = 4×4

    4.3333    8.8333   -3.0000    5.6667
    8.8333   50.3333    6.5000   24.1667
   -3.0000    6.5000    7.0000    1.0000
    5.6667   24.1667    1.0000   12.3333

Since the number of columns of A is 4, the result is a 4-by-4 matrix.

由於矩陣A有4列,表示有4個隨機變數,那麼協方差矩陣是4*4的。


cov(A,B) 舉例之兩個向量之間的協方差

1. Create two vectors and compute their 2-by-2 covariance matrix.

A = [3 6 4];
B = [7 12 -9];
cov(A,B)
ans = 2×2

    2.3333    6.8333
    6.8333  120.3333

cov(A,B) 舉例之兩個矩陣之間的協方差

2. Create two matrices of the same size and compute their 2-by-2 covariance.

A = [2 0 -9; 3 4 1];
B = [5 2 6; -4 4 9];
cov(A,B)
ans = 2×2

   22.1667   -6.9333
   -6.9333   19.4667

這相當於求A(:)和B(:)的協方差,如下驗證下:

對比下cov(A,B)發現是一致的:

Specify Normalization Weight

建立一個矩陣並計算由行數歸一化的協方差。

A = [1 3 -7; 3 9 2; -5 4 6];
C = cov(A,1)
C = 3×3

   11.5556    5.1111  -10.2222
    5.1111    6.8889    5.2222
  -10.2222    5.2222   29.5556

我覺得還是有必要比較下不歸一化的情況:

>> A = [1 3 -7; 3 9 2; -5 4 6]

A =

     1     3    -7
     3     9     2
    -5     4     6

>> C = cov(A,1)

C =

   11.5556    5.1111  -10.2222
    5.1111    6.8889    5.2222
  -10.2222    5.2222   29.5556

>> C = cov(A)

C =

   17.3333    7.6667  -15.3333
    7.6667   10.3333    7.8333
  -15.3333    7.8333   44.3333

Covariance Excluding NaN

建立矩陣並計算其協方差,排除包含NaN值的任何行。

A = [1.77 -0.005 3.98; NaN -2.95 NaN; 2.54 0.19 1.01]
A = 3×3

    1.7700   -0.0050    3.9800
       NaN   -2.9500       NaN
    2.5400    0.1900    1.0100

C = cov(A,'omitrows')
C = 3×3

    0.2964    0.0751   -1.1435
    0.0751    0.0190   -0.2896
   -1.1435   -0.2896    4.4104