forum.alglib.net
http://forum.alglib.net/

Covariance Matrix Calculation (covm)
http://forum.alglib.net/viewtopic.php?f=2&t=113
Page 1 of 1

Author:  norrizuan [ Mon Nov 08, 2010 2:55 pm ]
Post subject:  Covariance Matrix Calculation (covm)

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

Author:  Sergey.Bochkanov [ Mon Nov 08, 2010 3:11 pm ]
Post subject:  Re: Covariance Matrix Calculation (covm)

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.

Author:  norrizuan [ Mon Nov 08, 2010 3:23 pm ]
Post subject:  Re: Covariance Matrix Calculation (covm)

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

Author:  norrizuan [ Mon Nov 08, 2010 3:33 pm ]
Post subject:  Re: Covariance Matrix Calculation (covm)

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

Author:  Sergey.Bochkanov [ Mon Nov 08, 2010 4:58 pm ]
Post subject:  Re: Covariance Matrix Calculation (covm)

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 :)

Author:  norrizuan [ Tue Nov 09, 2010 4:15 pm ]
Post subject:  Re: Covariance Matrix Calculation (covm)

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

Author:  ulis [ Wed Apr 30, 2014 8:05 am ]
Post subject:  Re: Covariance Matrix Calculation (covm)

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?

Author:  Sergey.Bochkanov [ Wed Apr 30, 2014 8:19 am ]
Post subject:  Re: Covariance Matrix Calculation (covm)

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.

Author:  ulis [ Wed Apr 30, 2014 8:56 am ]
Post subject:  Re: Covariance Matrix Calculation (covm)

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?

Author:  Sergey.Bochkanov [ Wed Apr 30, 2014 11:46 am ]
Post subject:  Re: Covariance Matrix Calculation (covm)

Formula you cited above clearly results in NVars*NVars matrix, because both X and Y and NVars-vectors.

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/