ERROR: LinearAlgebra.SingularException(5)

How should I deal with this error: ERROR: LinearAlgebra.SingularException(5)

I have the following matrix:

Xₜ⁽ˡ⁾ = [
0.997838  1.01925  1.01489   1.00797   0.997964
0.992047  1.02963  0.987861  0.990278  0.991172
]

And, I aim to calculate the following expression:

(Xₜ⁽ˡ⁾'*Xₜ⁽ˡ⁾)^-1

But it throws the error I mentioned earlier. This is real data, and it’s not synthetic. Can increasing the number of data points (by adding more rows or/and columns) solve the problem?
P.S.: Here is the result of Xₜ⁽ˡ⁾'*Xₜ⁽ˡ⁾:

julia>  Xₜ⁽ˡ⁾'*Xₜ⁽ˡ⁾
5×5 Matrix{Float64}:
 1.97984  2.03849  1.9927   1.98819  1.9791
 2.03849  2.09901  2.05156  2.04699  2.03772
 1.9927   2.05156  2.00587  2.00124  1.99196
 1.98819  2.04699  2.00124  1.99665  1.98745
 1.9791   2.03772  1.99196  1.98745  1.97835

The error is thrown for the inversion operation.

In general you shouldn’t be using the normal equations. Rather than computing (Xₜ⁽ˡ⁾'*Xₜ⁽ˡ⁾)^-1, you should probably be using a linear solve with Xₜ⁽ˡ⁾ (using a factorization like Qr if you need to do multiple solves).

It might, since the problem is with the determinant of (Xₜ⁽ˡ⁾'*Xₜ⁽ˡ⁾). I’m trying to implement an algorithm that is published in a high-ranked journal. I can not believe they have not encountered this problem, since I have used real data as well. Hence, there are two possibilities:

  1. They’ve employed the algorithm on much more data.
  2. They might somehow handled this trough a different procedure.

How should I do that?

What do you mean by “multiple solves”? It’s part of an algorithm and should be run multiple times with different inputs.

so rather than computing (Xₜ⁽ˡ⁾'*Xₜ⁽ˡ⁾)^-1*(Xₜ⁽ˡ⁾'*v), you can just compute Xₜ⁽ˡ⁾\v which will automatically compute a least-squares or minimum norm solution depending on whether you are over or under-determined.

3 Likes

Thank you so much! I will try what you’ve recommended and let you know the result.

Mr @Oscar_Smith, your solution is great. Thank you so much.