comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Covariance Matrix
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Covariance Matrix [message #86847] Sat, 07 December 2013 04:11 Go to next message
amin farhang is currently offline  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 Go to previous messageGo to next message
David Fanning is currently offline  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 Go to previous messageGo to next message
amin farhang is currently offline  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 #86850 is a reply to message #86849] Sat, 07 December 2013 07:28 Go to previous messageGo to next message
Matthew Argall is currently offline  Matthew Argall
Messages: 286
Registered: October 2011
Senior Member
Try the Correlate function with the /COVARIANCE keyword.

http://www.exelisvis.com/docs/CORRELATE.html

Not sure what is wrong with your math. If the Correlate function does not give you what you want, I can take a closer look.
Re: Covariance Matrix [message #86851 is a reply to message #86847] Sat, 07 December 2013 09:25 Go to previous messageGo to next message
amin farhang is currently offline  amin farhang
Messages: 39
Registered: November 2010
Member
Matthew,

Thanks for your contribute. let me do some calculations with IDL correlate function:

IDL> A = [[1],[2],[3],[4]]
IDL> AT = transpose(A)
IDL> r = correlate(A,AT,/covariance)
IDL> print,r
1.66667

As you see correlate just return one value instead of a matrix. is there any way to compute the covariance (or correlation) matrix of a vector?

thanks,
Re: Covariance Matrix [message #86852 is a reply to message #86851] Sat, 07 December 2013 09:38 Go to previous messageGo to next message
Matthew Argall is currently offline  Matthew Argall
Messages: 286
Registered: October 2011
Senior Member
Ahh. I see what you are after now. Try the auto-correlation function "a_correlate" with the /COVARIANCE set. You will have to define the "lag" parameter.

Typically, LAG=[1,2,3,4,5, ..., N], where N is your sample size.

http://www.exelisvis.com/docs/A_CORRELATE.html
Re: Covariance Matrix [message #86853 is a reply to message #86851] Sat, 07 December 2013 09:46 Go to previous messageGo to next message
Matthew Argall is currently offline  Matthew Argall
Messages: 286
Registered: October 2011
Senior Member
and, by the way, If you are using a single vector, the covariance will never give you a matrix. The covariance is essientially finding the best fit line between a vector and a lagged version of itself. It does that for however many elements are in the "lag" parameter.

If you are looking for a matrix, then take this example. Say you have a vector measuring the period of three different pendulums. If the data is stored in a 3xN array, then you can call the Correlate function. It will output a 3x3 matrix giving the covariance of the three different pendulums.

Calling Correlate() with two vectors is essentially creating a best-fit line between the two vectors and outputting the correlation coefficient. You need to vary the "lag" to get a matrix.
Re: Covariance Matrix [message #86854 is a reply to message #86847] Sun, 08 December 2013 11:57 Go to previous messageGo to next message
amin farhang is currently offline  amin farhang
Messages: 39
Registered: November 2010
Member
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,
Re: Covariance Matrix [message #87936 is a reply to message #86854] Tue, 04 March 2014 05:44 Go to previous messageGo to next message
haval.js is currently offline  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 #87937 is a reply to message #87936] Tue, 04 March 2014 06:22 Go to previous messageGo to next message
Matthew Argall is currently offline  Matthew Argall
Messages: 286
Registered: October 2011
Senior Member
> 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...
Re: Covariance Matrix [message #87946 is a reply to message #87937] Wed, 05 March 2014 02:37 Go to previous messageGo to next message
haval.js is currently offline  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 Go to previous messageGo to next message
haval.js is currently offline  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 Go to previous messageGo to next message
haval.js is currently offline  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 Go to previous messageGo to next message
haval.js is currently offline  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 #87950 is a reply to message #87947] Wed, 05 March 2014 05:14 Go to previous messageGo to next message
Matthew Argall is currently offline  Matthew Argall
Messages: 286
Registered: October 2011
Senior Member
> 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.

In that case, a covariance matrix does make more sense. Your first step would be to take the covariance of your 5x5 matrix, then find the eigenvectors of that:

http://exelisvis.com/docs/CORRELATE.html
with the /COVARIANCE keyword.
Re: Covariance Matrix [message #87981 is a reply to message #87950] Mon, 10 March 2014 03:11 Go to previous messageGo to next message
haval.js is currently offline  haval.js
Messages: 6
Registered: March 2014
Junior Member
On Wednesday, 5 March 2014 13:14:31 UTC, Matthew Argall wrote:
>> 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.
>
>
>
> In that case, a covariance matrix does make more sense. Your first step would be to take the covariance of your 5x5 matrix, then find the eigenvectors of that:
>
>
>
> http://exelisvis.com/docs/CORRELATE.html
>
> with the /COVARIANCE keyword.

Dear Matthew,

Thanks for your informative replay, it would be very helpful in my research.

Regards
Haval
Re: Covariance Matrix [message #92000 is a reply to message #86847] Mon, 28 September 2015 11:26 Go to previous message
andrei.makeev is currently offline  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,
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Array -Time
Next Topic: Zooming in function graphics

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 09:13:19 PDT 2025

Total time taken to generate the page: 0.00279 seconds