Matlab - determinant of covariance matrix coming out as zero - matlab

I'm trying to calculate the determinant of a 171x171 covariance matrix in matlab but the determinant is coming out as zero. I know the matrix is not singular because I am able to calculate the inverse of the matrix but because the numbers in the covariance matrix are quite small (between e-02 and e-05), I think matlab rounds to zero at some point because the numbers become so small.
I tried installing the symbolic toolbox and making the covariance matrix symbolic to calculate the determinant but my computer couldn't really handle it and kept crashing. Does anyone have an idea of a workaround/ know of a way of dealing with a determinant that is close to zero, but not actually zero?

Related

MATLAB: Eig algorithm and alternatives

I am simulating a physical system, where I need to calculate the eigenvalues and vectors of a very large (~10000x10000) matrix.
So far I have used the in-built Eig algorithm in MATLAB but it is very slow for large matrices. Is there other algorithms in MATLAB that would do a better job or can I somehow improve the performance of Eig? Specifically it turns out that I only need the first ~100 eigenvectors of the matrix starting from the smallest numerical eigenvalue. Is there a way to get the algorithm to calculate only the first N eigenvectors and eigenvalues to save computation time? Of course this would only work if the eigenvectors come out sorted but they seem to do so, because of the symmetry of the Matrix I am using.
Your matrix has mostly zeros, so you should make it a sparse matrix. You'll then be able to use EIGS to calculate a smaller number of eigenvalues and eigenvectors.
http://www.mathworks.com/help/matlab/ref/eigs.html

How to find only the eigen values without finding eigen vectors in MATLAB?

I have a large matrix for which I want to find only the eigen values. I know I can use charpoly(A) and find roots, but even that is very slow (takes about a minute). Is there a fast way to find only the eigen values for the matrix.
Furthermore, my matrix happens to be a symmetric PSD covariance matrix.

how to find cholesky of a matrix?

i have n number of matrix and i want to find square root each.but my algorithm need cholesky .i am getting error that matrix is not positive definite.i converted diagonal element into real one.still I'm getting same error. is there any other way to find cholesky of a matrix?
If your matrix is a long way from being positive definite, there's nothing you can do - the Cholesky factorization is based on the assumption that it is positive definite.
Often though, a matrix is basically positive definite, but due to some small numerical issue is very slightly non-symmetric. If this is the problem you're running into, you can force it (let's say it's called x) to be symmetric by saying x = (x+x')/2.
Hope that helps!

Issues with calculating the determinant of a matrix

I am trying to calculate the determinant of the inverse of a matrix. The inverse of the matrix exists. However, when I try to calculate the determinant of the inverse, it gives me Inf value in matlab. What is the reason behind this?
Short answer: given A = inv(B), then det(A)==Inf may have two explanations:
an overflow during the numerical computation of the determinant,
one or more infinite elements in A.
In the first case your matrix is badly scaled so that det(B) may underflow and det(A) overflow. Remember that det(a*B) == a^N * det(B) where a is a scalar and B is a N times N matrix.
In the second case (i.e. nnz(A==inf)>0) matrix B may be "singular to working precision".
PS:
A matrix is nearly singular if it has a large condition number. (A small determinant has nothing to do with singularity, since the magnitude of the determinant itself is affected by scaling.).
A matrix is singular to working precision if it has a zero pivot in the Gaussian elimination: when computing the inverse, matlab has to calculate 1/0 which returns Inf.
In fact in Matlab overflow and zero-division exceptions are not caught, so that, according to IEEE 754, an Inf value is propagated.

Determinant of a positive semi definite matrix

Is it possible that the determinant of a positive semi definite matrix is equal to 0. It is coming to be zero in my case. I have a diagonal matrix with diagonal elements non zero. When I try to calculate the determinant of this matrix it is coming out to be 0. Why is it so?
This is the reason why computing the determinant is never a good idea. Yeah, I know. Your book, your teacher, or your boss told you to do so. They were probably wrong. Why? Determinants are poorly scaled beasts. Even if you compute the determinant efficiently (many algorithms fail to do even that) you don't really want a determinant most of the time.
Consider this simple positive definite matrix.
A = eye(1000);
What is the determinant? I need not even bother. It is 1. But, if you insist...
det(A)
ans =
1
OK, so that works. How about if we simply multiply that entire matrix by a small constant, 0.1 for example. What is the determinant? You might say there is no reason to bother, as we already know the determinant. It must be just det(A)*0.1^1000, so 1e-1000.
det(A*0.1)
ans =
0
What did we do wrong here? Where this failed is we forgot to remember we were working in floating point arithmetic. Since the dynamic range of a double in MATLAB goes down only to essentially
realmin
ans =
2.2250738585072e-308
then smaller numbers turn into zero - they underflow. Anyway, most of the time when we compute a determinant, we are doing so for the wrong reasons anyway. If they want you to test to see if a matrix is singular, then use rank or cond, not det.
by definition, a positive semi definite matrix may have eigenvalues equal to zero, so its determinant can therefore be zero
Now, I can't see what you mean with the sentence,
I have a diagonal matrix with diagonal elements non zero. When I try to calculate the ...
If the matrix is diagonal, and all elements in the diagonal are non-zero, the determinant should be non-zero. If you are calculating it in your computer, beware underflows.
You may consider the sum of logarithms instead of the product of the diagonal elements