covariance matrix from a random vector?
조회 수: 63(최근 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....
댓글 수: 0
채택된 답변
추가 답변(3개)
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
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
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
2021년 4월 15일
if they are zero mean random variables, can we just perform "xcorr" to the matrix??
참고 항목
범주
Find more on Matrix Indexing 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!