R solve:system is exactly singular
Asked Answered
J

4

25

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

Jezabelle answered 4/7, 2011 at 13:23 Comment(0)
R
21

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.

Rexer answered 4/7, 2011 at 13:29 Comment(1)
The following page, near the bottom, helped me understand the concepts of inversion and singularity issue: linkSixpence
B
12

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.

Bois answered 4/7, 2011 at 13:29 Comment(5)
The data set contains daily returns of 26 asset returns, it should be invertible, no? I am confused why there is no problem when I have the whole data set while shorten produces error. Any ideas how to deal with it?Jezabelle
@Jezabelle Check det(Sigma): If it is zero then it is impossible to invert.Incompetent
Indeed, it is 0. What is the problem when I shorten the data set?Jezabelle
The only thing that I dont get is why there is a problem with smaller sample size, respectively how to deal with it cos I need the invert itJezabelle
@Jezabelle Either you have just been unlucky with the numbers, or you have reduced the data too much to be able to ensure linear independence. The following links may be of use: m-hikari.com/imf-2010/45-48-2010/kaimakamisIMF45-48-2010.pdf arxiv.org/abs/1010.0601Incompetent
C
6

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.

Choreodrama answered 6/8, 2014 at 10:50 Comment(0)
Y
0

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.

Yezd answered 27/10, 2021 at 18:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.