What is covariance matrix?
The covariance matrix gives us information about the covariances between random variables. If there are two random variables X and Y, then the covariance matrix of X and Y is denoted as:
Similarly, if there are three random variables X, Y, and Z, then the covariance matrix of X, Y, and Z will be:
How to calculate covariance matrix using Python?
We can use the numpy module in Python to calculate covariance matrix between random variables.
from numpy import cov data1 = [1, 2, 3, 4, 5] data2 = [6, 7, 8, 9, 10] print(cov(data1, data2, ddof=1))
Please note that here we are calculating the sample covariance, and hence, the delta degrees of freedom or ddof is 1. As a result, (n-1) is used as the denominator. If the delta degrees of freedom or ddof is 0, then n will be used in the denominator instead of (n – 1) to calculate the covariance.
The output of the above program will be:
[[2.5 2.5] [2.5 2.5]]








































0 Comments