Singular exception when it shouldn't happen, tol for inv?

x = somevector
X = hcat(ones(length(x)), x, x.^2)
inv(X'* X)

Result: SingularException(3)

somevector may be a random vector with multiple zeros but more than 2 non-zero & non-equal elements are ensured to make X to be full rank.

When I replicate this in R, it would give a similar error message, which can be resolved by tweaking the tolerance for inverse function to a smaller number. However, I am not sure if that’s where the problem is, and if it is, what’s the corresponding solution in Julia?

Any help would be much appreciated. Thanks.

If you have a nearly rank-deficient X, then you should try to avoid X' * X entirely if possible because it squares the condition number — any calculation that you do with X' * X, with any language/algorithm, is going to be much more sensitive to small errors than working directly with X.

If you are solving a least-square problem, for example, you can just use X \ b to get the fit coefficients.

More generally, you might want to work with the SVD or QR factorization of X.

4 Likes