R solve:system is exactly singular
I am solving simple optimization problem. The data set has 26 columns and over 3000 rows. The source code looks like
Means <- colMeans(Returns)
Sigma <- cov(Returns)
invSigma1 <- solve(Sigma)
And everything works perfect- but then I want to do the same for shorter period (only 261 rows) a开发者_JS百科nd the solve function writes the following error:
solve(Sigma)
Error in solve.default(Sigma) :
Lapack routine dgesv: system is exactly singular
Its weird because when I do the same with some random numbers:
Returns<-matrix(runif(6786,-1,1), nrow=261)
Means <- colMeans(Returns)
Sigma <- cov(Returns)
invSigma <- solve(Sigma)
no error occurs at all. Could someone explain me where could be the problem and how to treat it. Thank you very much, Alex
Using solve
with a single parameter is a request to invert a matrix. The error message is telling you that your matrix is singular and cannot be inverted.
I guess your code uses somewhere in the second case a singular matrix (i.e. not invertible), and the solve function needs to invert it. This has nothing to do with the size but with the fact that some of your vectors are (probably) colinear.
Lapack is a Linear Algebra package which is used by R (actually it's used everywhere) underneath solve()
, dgesv spits this kind of error when the matrix you passed as a parameter is singular.
As an addendum: dgesv performs LU decomposition, which, when using your matrix, forces a division by 0, since this is ill-defined, it throws this error. This only happens when matrix is singular or when it's singular on your machine (due to approximation you can have a really small number be considered 0)
I'd suggest you check its determinant if the matrix you're using contains mostly integers and is not big. If it's big, then take a look at this link.
I can understand your question. The problem is that your matrix is perpendicular. You can see your first number and the last number of your matrix is same.
精彩评论