covariance matrix from a random vector?

97 views (last 30 days)
Given a random vector, Y=[y1,y2,...,yn]; its covariance matrix look like this:
According to this definition, How can I calculate covariance matrix in matlab? N.B. cov() function makes me confusion....

Accepted Answer

Honglei Chen
Honglei Chen on 26 Oct 2016
cov should be the function you use, what about it makes you confusing?
  3 Comments
Honglei Chen
Honglei Chen on 27 Oct 2016
The covariance works with multiple observations for the same data. For your x = [3 4 5 6]. If you pass it in as is, it is treated as one random variable with 4 observations. So if you compute the variance, then you get a scalar value of 1.6667.
From your description, it seems that you want the case where there are 4 random variables? In that case, you need more observations to get a meaningful covariance matrix. For example, if your x is of size 100x4, then you have four random variables and each variable has 100 observations. If you invoke cov() on such a matrix, you'll get the 4x4 covariance matrix. For example
cov(randn(100,4))
HTH

Sign in to comment.

More Answers (3)

Christopher Smith
Christopher Smith on 28 Jan 2018
I know this question is old but I came across this page looking for the same answer. If the cov() function in Matlab had an input element option for the expected value of the two functions then it would work but it does not. The cov() function appears to be designed for a large data set where the mean value can be determined. In your case you will need to know the expected value of each of the random variables in your vector ahead of time. Then the covariance of that vector would be calculated with nested for loops iterating through the array and computing the elementwise covariance manually. The built in function cov() does not provide any options here. This is the video link that helped me: https://m.youtube.com/watch?v=0W8hTzU1ZMM It assumes that you have a vector of random variable values (instances) where each element represents the functions to be compared with covariance and that for each element in the vector you already know the expected value. Then it is easy to take an Nx1 vector and compute the NxN covariance matrix but tedious.
  1 Comment
Anna M
Anna M on 3 Jul 2019
I am also looking for the same answer, and that video helped a lot! Thanks

Sign in to comment.


Roger Stafford
Roger Stafford on 27 Oct 2016
With just a vector, Y, you can calculate its variance but there is no significance to calculating its covariance. That would always be zero.
Covariance has a significance only with a set of vectors. Matlab’s ‘cov’ function will obtain the covariance of a matrix where the different columns are different components of random variables and the rows are different variations of those rows. Applied to your problem, the result would be a row of zeros since there is no variation (though that is not what matlab does). If you start with a single column vector the result is simply the variance which will be a scalar.

Allen Goldstein
Allen Goldstein on 3 Apr 2021
Let me see if I have this right, if I run cov(z) where z is a vector, I'll just get a vector. The covarinace matrix has the variances (cov(z1,z1)) on the diagonal and the covariances symetrically in the upper and lower triangles.
I think to get the covarinace matrix, you need to create a vector of differences bewteen each point and the mean of all points, multiply it with it's transpose, then divide by the number of points.
So if z is a vector of random variables, C will be the covariance matrix
M = ones(1,length(z))*mean(z); % vector of mean values
Zc = z - M; % distances to the mean
C = (Zc'*Zc)./length(z); % covariance matrix
  1 Comment
Kanike Sreekanth
Kanike Sreekanth on 15 Apr 2021
if they are zero mean random variables, can we just perform "xcorr" to the matrix??

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!