# Re: [R] Singularity problem

From: Feng Li <m_at_feng.li>
Date: Wed, 16 Mar 2011 23:03:57 +0100

Thanks all of you for the very interesting discussion. I think I get it now.

Feng

On 03/16/2011 09:25 PM, rex.dwyer_at_syngenta.com wrote:
> Feng,
> Your matrix is *not* (practically) singular; its inverse is.
> The message said that the *system* was singular, not the matrix.
> Remember Cramer's Rule: xi = |Ai| / |A|
> The really, really large determinant of your matrix is going to appear in the denominator of your solutions, so, essentially, you get underflow. Try working out the entire solution with Cramer's Rule if you still don't see the problem. solve doesn't really use Cramer's Rule, but it will give you a feel for the issue.
> HTH
> Rex
>
> -----Original Message-----
> From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of Berend Hasselman
> Sent: Wednesday, March 16, 2011 1:33 PM
> To: r-help_at_r-project.org
> Subject: Re: [R] Singularity problem
>
>
> Peter Langfelder wrote:
>> On Wed, Mar 16, 2011 at 8:28 AM, Feng Li&lt;m_at_feng.li&gt; wrote:
>>> Dear R,
>>>
>>> If I have remembered correctly, a square matrix is singular if and only
>>> if
>>> its determinant is zero. I am a bit confused by the following code error.
>>> Can someone give me a hint?
>>>
>>>> a<- matrix(c(1e20,1e2,1e3,1e3),2)
>>>> det(a)
>>> [1] 1e+23
>>>> solve(a)
>>> Error in solve.default(a) :
>>> system is computationally singular: reciprocal condition number = 1e-17
>>>
>> You are right, a matrix is mathematically singular iff its determinant
>> is zero. However, this condition is useless in practice since in
>> practice one cares about the matrix being "computationally" singular,
>> i.e. so close to singular that it cannot be inverted using the
>> standard precision of real numbers. And that's what your matrix is
>> (and the error message you got says so).
>>
>> You can write your matrix as
>>
>> a = 1e20 * matrix (c(1, 1e-18, 1e-17, 1e-17), 2, 2)
>>
>> Compared to the first element, all of the other elements are nearly
>> zero, so the matrix is numerically nearly singular even though the
>> determinant is 1e23. A better measure of how numerically unstable the
>> inversion of a matrix is is the condition number which IIRC is
>> something like the largest eigenvalue divided by the smallest
>> eigenvalue.
>>
> svd(a) indicates the problem.
>
> largest singular value / smallest singular value=1e17 (condition number)
> --> reciprocal condition number=1e-17
> and the standard solve can't handle that.
>
> (pivoted) QR decomposition does help. And so does SVD.
>
> Berend
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Singularity-problem-tp3382093p3382465.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>
>
>
> message may contain confidential information. If you are not the designated recipient, please notify the sender immediately, and delete the original and any copies. Any use of the message by you is prohibited.

```--
Feng Li
Department of Statistics
Stockholm University
106 91 Stockholm, Sweden
http://feng.li/

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help