covariance matrix from a random vector?

97 次查看(过去 30 天)
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....

采纳的回答

Honglei Chen
Honglei Chen 2016-10-26
cov should be the function you use, what about it makes you confusing?
  3 个评论
Honglei Chen
Honglei Chen 2016-10-27
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

请先登录,再进行评论。

更多回答(3 个)

Christopher Smith
Christopher Smith 2018-1-28
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 个评论
Anna M
Anna M 2019-7-3
I am also looking for the same answer, and that video helped a lot! Thanks

请先登录,再进行评论。


Roger Stafford
Roger Stafford 2016-10-27
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 2021-4-3
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 个评论
Kanike Sreekanth
Kanike Sreekanth 2021-4-15
if they are zero mean random variables, can we just perform "xcorr" to the matrix??

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by