Covariance Matrix [message #86847] |
Sat, 07 December 2013 04:11  |
amin farhang
Messages: 39 Registered: November 2010
|
Member |
|
|
Dear All,
I have N observed data as a vector, and I need to compute its NxN covariance matrix, but IDL correlate function just return one value as the correlation (or covariance) between two vectors and do not return a matrix.
So how can I compute NxN covariance matrix of below vector (for example):
IDL> A = [1,2,3,4,5]
Thanks in advance,
|
|
|
Re: Covariance Matrix [message #86848 is a reply to message #86847] |
Sat, 07 December 2013 05:31   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Amin Farhang writes:
> I have N observed data as a vector, and I need to compute its NxN covariance matrix, but IDL correlate function just return one value as the correlation (or covariance) between two vectors and do not return a matrix.
> So how can I compute NxN covariance matrix of below vector (for example):
>
> IDL> A = [1,2,3,4,5]
If you only have one vector, the best you can do is calculate the way
the numbers in the vector vary with respect to the mean of the vector.
In other words, you can calculate the variance.
To do a COvariance, you measure how one vector varies with respect to
one or more *other* vectors. Where is your vector B if you want to do a
covariance?
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
Re: Covariance Matrix [message #86849 is a reply to message #86847] |
Sat, 07 December 2013 06:37   |
amin farhang
Messages: 39 Registered: November 2010
|
Member |
|
|
Hi David,
Indeed there is no any B vector in my problem. In statistical notes, for computing the covariance of a vector the following formula can be used:
cov(A,A) = E[(A-EA)*(A-EA)^T]
where E means expected value, EA is the mean value of A vector and T means transpose of A vector. I write a code for this (A is a column vector):
s = size(A)
Na = s[2]
MeanValue = total(A)/float(Na)
AT = transpose(A)
A = A - MeanValue
AT = AT - MeanValue
covariance = A ## AT
for example for A=[1,2,3,4] the covariance matrix with above code become:
2.25000 0.750000 -0.750000 -2.25000
0.750000 0.250000 -0.250000 -0.750000
-0.750000 -0.250000 0.250000 0.750000
-2.25000 -0.750000 0.750000 2.25000
but the weird thing is that if we divide the covariance matrix by standard deviation of A and AT we should see the correlation matrix.
in above example std(A) = 1.29 so correlation(A) = COV(A)/(std * std):
1.74419 0.581395 -0.581395 -1.74419
0.581395 0.193798 -0.193798 -0.581395
-0.581395 -0.193798 0.193798 0.581395
-1.74419 -0.581395 0.581395 1.74419
but we know that the correlation coefficients must be between -1 and 1 (diagonal elements must be equal to 1) but you see that in addition to being 1 in diagonal, the off-diagonal are greater than 1 in some cases!!
what is happening? how can I compute correct covariance that with conversion to correlation matrix all elements be in correct range?
or is it a code in IDL for computing the correlation matrix of vector?
Thanks,
|
|
|
|
|
|
|
|
Re: Covariance Matrix [message #87936 is a reply to message #86854] |
Tue, 04 March 2014 05:44   |
haval.js
Messages: 6 Registered: March 2014
|
Junior Member |
|
|
On Sunday, 8 December 2013 19:57:00 UTC, Amin Farhang wrote:
> Dear Matthew,
>
>
>
> You are right, with one vector we couldn't have a covariance matrix, but there is a solution for this problem.
>
> For computing the covariance matrix of a vector first we should generate N-1 realization vectors then with consider all generated vectors and our own vector construct a NxN matrix, now very simply we could compute a NxN covariance matrix with CORRELATE function.
>
>
>
> Thank you for your kindly answers.
>
>
>
> Cheers,
Hi Amin,and other colleagues,
I am trying to find out the covariance matrix for one vector, could you give me some more details about how doing the calculation.
My case is related to calculated the principle eigen vector for 5by5 pixel window, which can be represented by one vector only.
Thanks in advance
Haval
|
|
|
|
Re: Covariance Matrix [message #87946 is a reply to message #87937] |
Wed, 05 March 2014 02:37   |
haval.js
Messages: 6 Registered: March 2014
|
Junior Member |
|
|
On Tuesday, 4 March 2014 14:22:43 UTC, Matthew Argall wrote:
>> My case is related to calculated the principle eigen vector for 5by5 pixel window, which can be represented by one vector only.
>
>
>
> So, you have an array with 25 elements that can be represented by a 5x5 matrix and you want to diagonalize the matrix to get the eigenvalues and eigenvectors?
>
>
>
> If this is what you are trying to do, there is an example with a 4x4 matrix on the Eigenvec help page
>
> http://exelisvis.com/docs/EIGENVEC.html
>
>
>
> I do not think you want the covariance of a single vector, but you would need to give a little more information to help further...
Dear Matthew,
I am really appreciate your answer, it helps alot.
what I am trying to do is
On Tuesday, 4 March 2014 14:22:43 UTC, Matthew Argall wrote:
>> My case is related to calculated the principle eigen vector for 5by5 pixel window, which can be represented by one vector only.
>
>
>
> So, you have an array with 25 elements that can be represented by a 5x5 matrix and you want to diagonalize the matrix to get the eigenvalues and eigenvectors?
>
>
>
> If this is what you are trying to do, there is an example with a 4x4 matrix on the Eigenvec help page
>
> http://exelisvis.com/docs/EIGENVEC.html
>
>
>
> I do not think you want the covariance of a single vector, but you would need to give a little more information to help further...
Dear Matthew,
Thank you very much for your reply, I am really appreciate it. The answer were very useful. I thought that first I have to construct vector from pixels then calculating eigen value and then eigen vector. But, according to your explanation the work become much easier.
I am trying to fuse images, my work is related to find the covariance matrix in order to find the principle eigen vector, WHICH IS corresponding to the eigenvalue of largest magnitude. Finally substitute the values in the model.
Thank you
Haval
|
|
|
Re: Covariance Matrix [message #87947 is a reply to message #87937] |
Wed, 05 March 2014 02:38   |
haval.js
Messages: 6 Registered: March 2014
|
Junior Member |
|
|
On Tuesday, 4 March 2014 14:22:43 UTC, Matthew Argall wrote:
>> My case is related to calculated the principle eigen vector for 5by5 pixel window, which can be represented by one vector only.
>
>
>
> So, you have an array with 25 elements that can be represented by a 5x5 matrix and you want to diagonalize the matrix to get the eigenvalues and eigenvectors?
>
>
>
> If this is what you are trying to do, there is an example with a 4x4 matrix on the Eigenvec help page
>
> http://exelisvis.com/docs/EIGENVEC.html
>
>
>
> I do not think you want the covariance of a single vector, but you would need to give a little more information to help further...
Dear Matthew,
Thank you very much for your reply, I am really appreciate it. The answer were very useful. I thought that first I have to construct vector from pixels then calculating eigen value and then eigen vector. But, according to your explanation the work become much easier.
I am trying to fuse images, my work is related to find the covariance matrix in order to find the principle eigen vector, WHICH IS corresponding to the eigenvalue of largest magnitude. Finally substitute the values in the model.
Thank you
Haval
|
|
|
Re: Covariance Matrix [message #87948 is a reply to message #87937] |
Wed, 05 March 2014 02:39   |
haval.js
Messages: 6 Registered: March 2014
|
Junior Member |
|
|
On Tuesday, 4 March 2014 14:22:43 UTC, Matthew Argall wrote:
>> My case is related to calculated the principle eigen vector for 5by5 pixel window, which can be represented by one vector only.
>
>
>
> So, you have an array with 25 elements that can be represented by a 5x5 matrix and you want to diagonalize the matrix to get the eigenvalues and eigenvectors?
>
>
>
> If this is what you are trying to do, there is an example with a 4x4 matrix on the Eigenvec help page
>
> http://exelisvis.com/docs/EIGENVEC.html
>
>
>
> I do not think you want the covariance of a single vector, but you would need to give a little more information to help further...
Dear Matthew,
Thank you very much for your reply, I am really appreciate it. The answer were very useful. I thought that first I have to construct vector from pixels then calculating eigen value and then eigen vector. But, according to your explanation the work become much easier.
I am trying to fuse images, my work is related to find the covariance matrix in order to find the principle eigen vector, WHICH IS corresponding to the eigenvalue of largest magnitude. Finally substitute the values in the model.
Thank you
Haval
|
|
|
Re: Covariance Matrix [message #87949 is a reply to message #87947] |
Wed, 05 March 2014 02:43   |
haval.js
Messages: 6 Registered: March 2014
|
Junior Member |
|
|
On Wednesday, 5 March 2014 10:38:52 UTC, hava...@gmail.com wrote:
> On Tuesday, 4 March 2014 14:22:43 UTC, Matthew Argall wrote:
>
>>> My case is related to calculated the principle eigen vector for 5by5 pixel window, which can be represented by one vector only.
>
>>
>
>>
>
>>
>
>> So, you have an array with 25 elements that can be represented by a 5x5 matrix and you want to diagonalize the matrix to get the eigenvalues and eigenvectors?
>
>>
>
>>
>
>>
>
>> If this is what you are trying to do, there is an example with a 4x4 matrix on the Eigenvec help page
>
>>
>
>> http://exelisvis.com/docs/EIGENVEC.html
>
>>
>
>>
>
>>
>
>> I do not think you want the covariance of a single vector, but you would need to give a little more information to help further...
>
>
>
> Dear Matthew,
>
>
>
> Thank you very much for your reply, I am really appreciate it. The answer were very useful. I thought that first I have to construct vector from pixels then calculating eigen value and then eigen vector. But, according to your explanation the work become much easier.
>
>
>
> I am trying to fuse images, my work is related to find the covariance matrix in order to find the principle eigen vector, WHICH IS corresponding to the eigenvalue of largest magnitude. Finally substitute the values in the model.
>
>
>
> Thank you
>
> Haval
To me more detailed, I have a window 5x5 which moves over the images, and at each move I have to find the principle eigen vector.
regards
Haval
|
|
|
|
|
Re: Covariance Matrix [message #92000 is a reply to message #86847] |
Mon, 28 September 2015 11:26  |
andrei.makeev
Messages: 1 Registered: September 2015
|
Junior Member |
|
|
Amin,
from your code excerpt, it looks like the way you define the
mean (expected value) of the random vector for computing
covariance is incorrect.
It is not the mean of vector's elements, but each element of
the vector is the mean of the random variable. I.e. for each
entry in the vector, you'd have to provide the corresponding
sample mean, like for element a1 it'd <a1>, for element a2,
<a2>, etc. Vector A itself, is not sufficient for calculating its
covariance, you need to have a corresponding vector of means
for each element of it.
Andrei.
On Saturday, December 7, 2013 at 7:11:41 AM UTC-5, Amin Farhang wrote:
> Dear All,
>
> I have N observed data as a vector, and I need to compute its NxN covariance matrix, but IDL correlate function just return one value as the correlation (or covariance) between two vectors and do not return a matrix.
> So how can I compute NxN covariance matrix of below vector (for example):
>
> IDL> A = [1,2,3,4,5]
>
>
> Thanks in advance,
|
|
|