如何在MATLAB中計算協方差


在本文中,我們將探討**如何使用MATLAB程式設計計算協方差**。但在那之前,讓我們先了解一下協方差的基本理論和重要性。

什麼是協方差?

**協方差**是一種統計工具,用於描述兩個或多個隨機變數之間的相關性。換句話說,協方差是一種度量,它提供關於兩個或多個變數之間關係的資訊。

協方差主要用於量化一個變數相對於另一個變數的變化。

兩個隨機變數“A”和“B”之間的協方差指定為cov(A, B),其計算方法如下:

$$\mathrm{cov(A,B)\:=\:\frac{1}{k}\displaystyle\sum\limits_{i=1}^k (a_i−E(A))(b_i−E(B))}$$

其中,E(A)和E(B)分別是隨機變數A和B的期望值或均值。

協方差的值可以是正數、負數或零,它們代表隨機變數之間不同型別的相關性或關係。

以下幾點說明了根據協方差的值,隨機變數之間關係的性質:

  • **正協方差** - 隨機變數之間的正協方差值表示,當一個變數增加時,另一個變數也傾向於增加。因此,變數之間的正協方差表示它們之間存在正線性關係。

  • **負協方差** - 負協方差值表示,當一個變數增加時,第二個變數傾向於減少。因此,負協方差表示變數之間存在負線性關係。

  • **零協方差** - 當兩個隨機變數之間的協方差值為零時,表示變數之間不存線上性關係。但這並不意味著變數之間完全沒有關係。它們可能存在非線性關係。

協方差廣泛應用於統計學、資料分析和金融領域,因為它有助於分析和理解不同隨機變數之間的關係,衡量它們的依賴性,估計風險和多樣化等等。協方差也用於機器學習領域來分析和開發資料模型。

在簡要概述了協方差之後,讓我們現在討論它在MATLAB程式設計中的實現。MATLAB提供了一個內建函式“cov()”,用於計算隨機變數之間的協方差。

以下部分描述了MATLAB中“cov()”函式的不同語法及其在MATLAB程式設計中計算協方差的應用。

計算陣列的協方差

以下語法用於計算陣列的協方差並獲得協方差矩陣

C = cov(X);

以下程式演示了此語法的實現。

示例

% MATLAB code to calculate covariance of an array
% Define an array
X = [2 4 6; 8 10 12; 14 16 18];
% Calculate covariance of the array
C = cov(X);
% Display the covariance result
disp('The covariance of the array X is:');
disp(C);

輸出

The covariance of the array X is:
   36   36   36
   36   36   36
   36   36   36

程式碼解釋

在這個MATLAB程式中,我們首先定義一個數組“X”。然後,我們使用“cov”函式計算其協方差並將結果儲存在“C”變數中。最後,我們使用“disp”函式顯示結果。

計算兩個陣列的協方差

要計算兩個陣列的協方差,我們使用“cov”函式的以下語法

C = cov(X, Y);

讓我們瞭解一下在MATLAB程式設計中此語法的實現。

示例

% MATLAB code to calculate covariance of two arrays
% Define two input arrays
X = [2 4 6; -3 5 7];
Y = [8 10 12; 2 -7 6];
% Calculate covariance of arrays
C = cov(X, Y);
% Display the covariance result
disp('The covariance of the arrays X and Y is:');
disp(C);

輸出

The covariance of the arrays X and Y is:
   15.0000   42.5000   15.0000
   -3.0000   -8.5000   -3.0000
   -3.0000   -8.5000   -3.0000

透過歸一化計算陣列的協方差

“cov”函式的以下語法用於透過用權重“W”歸一化來計算陣列的協方差

C = cov(X, W);

這裡,如果W = 1,則協方差矩陣按“輸入陣列的行數”進行歸一化;如果W = 0,則協方差矩陣按“輸入陣列的行數減1”進行歸一化。

以下MATLAB程式演示了此語法的實現。

示例

% MATLAB code to calculate covariance of an array with normalization
% Define the input array
X = [2 4 6; 3 5 7; 9 8 5];
% Calculate covariance of array with normalization by W = 1
C1 = cov(X, 1);
% Calculate covariance of array with normalization by W = 0
C2 = cov(X, 0);
% Display the covariance result
disp('The covariance of the array X with W = 1 is:');
disp(C1);
disp('The covariance of the array X with W = 0 is:');
disp(C2);

輸出

The covariance of the array X with W = 1 is:
    9.5556    5.2222   -2.0000
    5.2222    2.8889   -1.0000
   -2.0000   -1.0000    0.6667

The covariance of the array X with W = 0 is:
   14.3333    7.8333   -3.0000
    7.8333    4.3333   -1.5000
   -3.0000   -1.5000    1.0000

程式碼解釋

在這個MATLAB程式碼中,我們首先定義一個數組“X”。然後,我們透過用“W = 1”和“W = 0”對其進行歸一化來計算“X”的協方差,並將結果分別儲存在“C1”和“C2”中。最後,我們使用“disp”函式顯示協方差矩陣。

計算包含NaN值的陣列的協方差

當給定陣列包含NaN值時,我們使用“cov”函式的以下語法來計算陣列的協方差

C = cov(X, nanflag);

這裡,“nanflag”選項指定了如何在協方差計算中處理陣列的NaN值。

如果nanflag = 'includenan',則“cov”函式會在計算中考慮陣列的NaN值。

如果nanflag = 'omitrows',則“cov”函式會在計算中忽略NaN值。

以下MATLAB程式演示了使用帶“nanflag”選項的“cov”函式。

示例

% MATLAB code to calculate covariance of an array with nanflag option
% Define the input array with NaN values
X = [2 4 5; 3 5 7; NaN 7 NaN];
% Calculate covariance of array with includenan flag
C1 = cov(X, 'includenan');
% Calculate covariance of array with omitrows flag
C2 = cov(X, 'omitrows');
% Display the covariance results
disp('The covariance of the array with includenan is:');
disp(C1);
disp('The covariance of the array with omitrows is:');
disp(C2);

輸出

The covariance of the array with includenan is:
       NaN       NaN       NaN
       NaN    2.3333       NaN
       NaN       NaN       NaN

The covariance of the array with omitrows is:
    0.5000    0.5000    1.0000
    0.5000    0.5000    1.0000
    1.0000    1.0000    2.0000

程式碼解釋

在這個MATLAB程式中,我們首先定義一個包含NaN值的陣列“X”。然後,我們計算“X”的協方差,包括NaN值和忽略NaN值,並將結果分別儲存在“C1”和“C2”中。最後,我們使用“disp”函式顯示協方差矩陣。

更新於:2023年8月7日

225 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告