forum.alglib.net

ALGLIB forum
It is currently Sun Dec 22, 2024 11:36 pm

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Covariance Matrix Calculation (covm)
PostPosted: Mon Nov 08, 2010 2:55 pm 
Offline

Joined: Wed Oct 20, 2010 10:12 am
Posts: 20
Hi Sergey,

Congratulation for the new release of ALBLIB library. I'm using it right now to compute covariance matrix of A matrix, 262144 x 224 matrix (262144 of observations and 224 of variables) so that the output will be in B, 224 x 224. I used the syntax alglib::covm(A, 262144, 224, B) but it gave me error, an unhandled exception of type 'System.Runtime.InteropServices.SEHException' occured ........ and the debugger stop in line 614 of your statistics.cpp.

I have tried to reduce the number of observation and variables by using 10 of them, alglib::covm(A, 10, 10, B) but still the same error occured.

Can you point my mistake, please.

Many thanks.

Rizuan


Top
 Profile  
 
 Post subject: Re: Covariance Matrix Calculation (covm)
PostPosted: Mon Nov 08, 2010 3:11 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
I guess that you've compiled ALGLIB under C++/CLI (AKA managed C++), right? ALGLIB for C++ was not tested with C++/CLI because it is not C++ nor C, but another programming language. Maybe you can try to interface with C# version of ALGLIB?

If I am wrong (i.e. it is not C++/CLI), please, post your code here and I'll try to figure out what went wrong.


Top
 Profile  
 
 Post subject: Re: Covariance Matrix Calculation (covm)
PostPosted: Mon Nov 08, 2010 3:23 pm 
Offline

Joined: Wed Oct 20, 2010 10:12 am
Posts: 20
No problem about the code. Before the code below, I have a double 3D array, hypercube[512][512][224] which I need to convert it to 2D array (real_2d_array). band = 224, row = col = 512.

alglib::real_2d_array cubetemp, covalg;

cubetemp.setlength(row*col, band);
covalg.setlength(band, band);

int c=0;
for (z = 0; z < band; z++)
{
for (x = 0; x < row; x++)
{
for (y = 0; y < col; y++)
{
cubetemp[c][z] = (double)hypercube[x][y][z];
if(c != (row*col)-1)
c++;
else
c = 0;
}
}
}

alglib::covm(cubetemp, band, band, covalg);

About the code type, I believe it is C++ but I'm not sure whether it is CLI or not. I'm trying to translate a few Matlab codes to C++ before implementing on TI processor.

Thanks in advance.

Regards,
Rizuan


Top
 Profile  
 
 Post subject: Re: Covariance Matrix Calculation (covm)
PostPosted: Mon Nov 08, 2010 3:33 pm 
Offline

Joined: Wed Oct 20, 2010 10:12 am
Posts: 20
Addition to my previos post:

I'm using ALGLIB in Visual C++ 2008. It is a CLR Console Application using .NET Framework 3.5.

Thanks.
Rizuan


Top
 Profile  
 
 Post subject: Re: Covariance Matrix Calculation (covm)
PostPosted: Mon Nov 08, 2010 4:58 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
Your code seems right. I think that the problem is that you compile it for .NET. C++ for .NET is different from usual C++ - it is another programming language with different programming model, but similar syntax. And actually you don't need C++ for NET, because Texas Composer Studio is a "usual C++" compiler without Microsoft extensions.

I suspect that automatic garbage collection interferes with ALGLIB memory management, although it is still not clear.

As for your problem.... You should switch to "usual C++". Try to create Win32 or Win64 application - not CLR application, and I think that it will work. In case of any problems feel free to post again :)


Top
 Profile  
 
 Post subject: Re: Covariance Matrix Calculation (covm)
PostPosted: Tue Nov 09, 2010 4:15 pm 
Offline

Joined: Wed Oct 20, 2010 10:12 am
Posts: 20
Hi Sergey,

You were absolutely right. My code performed faster in Win32. Your covm is working perfectly for 262144 x 224 matrix.

Many thanks.

Best regards,
Rizuan


Top
 Profile  
 
 Post subject: Re: Covariance Matrix Calculation (covm)
PostPosted: Wed Apr 30, 2014 8:05 am 
Offline

Joined: Wed Apr 30, 2014 7:53 am
Posts: 2
Hi all

I am also trying to count covariance matrix. According to Alglib docs:

OUTPUT PARAMETERS:
C - array[M,M], covariance matrix (zero if N=0 or N=1)
where M - M>0, number of variables

But as I know, covariance matrix must have size equal to number of observations.
http://en.wikipedia.org/wiki/Covariance_matrix

I need it to count Mahalanobis distance: http://en.wikipedia.org/wiki/Mahalanobis_distance

What is wrong?


Top
 Profile  
 
 Post subject: Re: Covariance Matrix Calculation (covm)
PostPosted: Wed Apr 30, 2014 8:19 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
Hello!

Covariance matrix has size equal to number of variables. That's matrix of covariances between variables, after all! :) If you describe your data as one big vector variable, then covariance matrix has size equal to dimensionality of this variable.


Top
 Profile  
 
 Post subject: Re: Covariance Matrix Calculation (covm)
PostPosted: Wed Apr 30, 2014 8:56 am 
Offline

Joined: Wed Apr 30, 2014 7:53 am
Posts: 2
Thank you for answer, Sergey.

I am confuse. In article, that I read there is one formula (really it is not Mahalanobis distance, just similar):
Image
And as you can see covariance matrix must be the same size as vector X (X=A-B). And this covariance matrix made of A and B. I guess something wrong. May be it is covariance matrix of X. Or cross-covariance matrix of A and B.

And with cross-correlation the same problem:
http://en.wikipedia.org/wiki/Cross-covariance
According to this wkipedia article cross-covariance matrix counted that way
Image
That is mean that its size Num_of_Observations_of_X x Num_of_Observation_of_Y
but in Alglib docs
C - array[M1,M2], cross-covariance matrix (zero if N=0 or N=1)
M1 - M1>0, number of variables in X
M2 - M2>0, number of variables in Y

What do you think?


Top
 Profile  
 
 Post subject: Re: Covariance Matrix Calculation (covm)
PostPosted: Wed Apr 30, 2014 11:46 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
Formula you cited above clearly results in NVars*NVars matrix, because both X and Y and NVars-vectors.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 36 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group