Solved – Matrix inverse not able to be calculated while determinant is non-zero

linear algebramatrixmatrix inverserregression

I was attempting to calculate an OLS regression in R when I saw some strange things. The inverse of a square matrix does not exist if and only if the determinants is 0. Also, the matrix must be of full rank.

So not sure how the below is possible:

> dim(X)
[1] 20000    51
> det(t(X) %*% X)
[1] 3.863823e+161 #non-zero 

> solve(t(X) %*% X)
Error in solve.default(t(X) %*% X) :
system is computationally singular: reciprocal condition number = 3.18544e-17 

Why is solve() throw an error when trying to calculate the inverse when we know the determinant is not zero? What am I missing here?

Checked that the matrix has full rank:

> qr(t(X) %*% X)$rank
[1] 51

But then just to test further I reassigned one of the X columns to the same value of another:

> X[,2] = X[,3]

Thus, two columns of the X matrix are now the same.

> qr(t(X) %*% X)$rank
[1] 50

We now can confirm the X'X matrix is not of full rank.

> det(t(X) %*% X)
[1] 1.634637e+138

But the determinant is still not equal to 0? How is this possible and what am I missing?

Best Answer

My guess is that the numbers are too big (the determinant is large) and you're running into a computational problem.

I was able to replicate your error by running:

> X <- cbind(1,exp(rexp(100,rate=1/50)))
> det(t(X) %*% X)
[1] 5.156683e+126
> solve(t(X) %*% X)
> Error in solve.default...

The problem is numerical. You might be able to solve it by making some transformation of your $X$ matrix that makes the numbers smaller but allows you to work out what $\left(X'X\right)^{-1}$ is.

Related Question