You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Marisa Thoma (JIRA)" <ji...@apache.org> on 2011/01/05 18:34:46 UTC

[jira] Created: (MATH-465) Incorrect matrix rank via SVD

Incorrect matrix rank via SVD
-----------------------------

                 Key: MATH-465
                 URL: https://issues.apache.org/jira/browse/MATH-465
             Project: Commons Math
          Issue Type: Bug
    Affects Versions: 2.1
         Environment: Windows XP Prof. Vs. 2002
            Reporter: Marisa Thoma


The getRank() function of SingularValueDecompositionImpl does not work properly. This problem is probably related to the numerical stability problems mentioned in [MATH-327|https://issues.apache.org/jira/browse/MATH-327] and [MATH-320|https://issues.apache.org/jira/browse/MATH-320].

Example call with the standard matrix from R (rank 2):

{code:title=TestSVDRank.java}
import org.apache.commons.math.linear.Array2DRowRealMatrix;
import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.linear.SingularValueDecomposition;
import org.apache.commons.math.linear.SingularValueDecompositionImpl;

public class TestSVDRank {
	public static void main(String[] args) {
		double[][] d = { { 1, 1, 1 }, { 0, 0, 0 }, { 1, 2, 3 } };
		RealMatrix m = new Array2DRowRealMatrix(d);
		SingularValueDecomposition svd = new SingularValueDecompositionImpl(m);
		int r = svd.getRank();
		System.out.println("Rank: "+r);
	}
}
{code} 

The rank is computed as 3. This problem also occurs for larger matrices. I discovered the problem when trying to replace the corresponding JAMA method.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (MATH-465) Incorrect matrix rank via SVD

Posted by "Phil Steitz (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/MATH-465?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Phil Steitz updated MATH-465:
-----------------------------


Thanks for reporting this.  Looks like it could as you suggest be related to MATH-327.  

> Incorrect matrix rank via SVD
> -----------------------------
>
>                 Key: MATH-465
>                 URL: https://issues.apache.org/jira/browse/MATH-465
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: 2.1
>         Environment: Windows XP Prof. Vs. 2002
>            Reporter: Marisa Thoma
>
> The getRank() function of SingularValueDecompositionImpl does not work properly. This problem is probably related to the numerical stability problems mentioned in [MATH-327|https://issues.apache.org/jira/browse/MATH-327] and [MATH-320|https://issues.apache.org/jira/browse/MATH-320].
> Example call with the standard matrix from R (rank 2):
> {code:title=TestSVDRank.java}
> import org.apache.commons.math.linear.Array2DRowRealMatrix;
> import org.apache.commons.math.linear.RealMatrix;
> import org.apache.commons.math.linear.SingularValueDecomposition;
> import org.apache.commons.math.linear.SingularValueDecompositionImpl;
> public class TestSVDRank {
> 	public static void main(String[] args) {
> 		double[][] d = { { 1, 1, 1 }, { 0, 0, 0 }, { 1, 2, 3 } };
> 		RealMatrix m = new Array2DRowRealMatrix(d);
> 		SingularValueDecomposition svd = new SingularValueDecompositionImpl(m);
> 		int r = svd.getRank();
> 		System.out.println("Rank: "+r);
> 	}
> }
> {code} 
> The rank is computed as 3. This problem also occurs for larger matrices. I discovered the problem when trying to replace the corresponding JAMA method.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] [Commented] (MATH-465) Incorrect matrix rank via SVD

Posted by "greg sterijevski (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/MATH-465?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13054046#comment-13054046 ] 

greg sterijevski commented on MATH-465:
---------------------------------------

My apologies if I am missing something, but here is what I noticed about the SVD. 

On lines 124-127 of SingularValueDecompositionImpl we have:

        for (int i = 0; i < p; i++) {
            singularValues[i] = FastMath.sqrt(FastMath.abs(singularValues[i]));
        }

This is potentially the offending line. First is the problem of negative eigenvalues. Negative variance in the principal components should probably be dealt with explicitly? Perhaps by throwing a MathException? Second, and the issue which this bug report deals with, is taking a square root of a very small number (<1) will return a larger number. If you apply the threshold test in getRank() (final double threshold = FastMath.max(m, n) * FastMath.ulp(singularValues[0]) )  prior to taking the square root, I believe this problem would be resolved. More importantly, philosophically, you test for zero variance. This is the appropriate test.

Also, rank could be precalculated in the above loop. 

> Incorrect matrix rank via SVD
> -----------------------------
>
>                 Key: MATH-465
>                 URL: https://issues.apache.org/jira/browse/MATH-465
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: 2.1
>         Environment: Windows XP Prof. Vs. 2002
>            Reporter: Marisa Thoma
>             Fix For: 3.0
>
>
> The getRank() function of SingularValueDecompositionImpl does not work properly. This problem is probably related to the numerical stability problems mentioned in [MATH-327|https://issues.apache.org/jira/browse/MATH-327] and [MATH-320|https://issues.apache.org/jira/browse/MATH-320].
> Example call with the standard matrix from R (rank 2):
> {code:title=TestSVDRank.java}
> import org.apache.commons.math.linear.Array2DRowRealMatrix;
> import org.apache.commons.math.linear.RealMatrix;
> import org.apache.commons.math.linear.SingularValueDecomposition;
> import org.apache.commons.math.linear.SingularValueDecompositionImpl;
> public class TestSVDRank {
> 	public static void main(String[] args) {
> 		double[][] d = { { 1, 1, 1 }, { 0, 0, 0 }, { 1, 2, 3 } };
> 		RealMatrix m = new Array2DRowRealMatrix(d);
> 		SingularValueDecomposition svd = new SingularValueDecompositionImpl(m);
> 		int r = svd.getRank();
> 		System.out.println("Rank: "+r);
> 	}
> }
> {code} 
> The rank is computed as 3. This problem also occurs for larger matrices. I discovered the problem when trying to replace the corresponding JAMA method.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Resolved] (MATH-465) Incorrect matrix rank via SVD

Posted by "Luc Maisonobe (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/MATH-465?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Luc Maisonobe resolved MATH-465.
--------------------------------

    Resolution: Fixed

Fixed in subversion repository as of r1148714.

This issue was fixed by changing SVD implementation according to issue MATH-611.

> Incorrect matrix rank via SVD
> -----------------------------
>
>                 Key: MATH-465
>                 URL: https://issues.apache.org/jira/browse/MATH-465
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: 2.1
>         Environment: Windows XP Prof. Vs. 2002
>            Reporter: Marisa Thoma
>             Fix For: 3.0
>
>
> The getRank() function of SingularValueDecompositionImpl does not work properly. This problem is probably related to the numerical stability problems mentioned in [MATH-327|https://issues.apache.org/jira/browse/MATH-327] and [MATH-320|https://issues.apache.org/jira/browse/MATH-320].
> Example call with the standard matrix from R (rank 2):
> {code:title=TestSVDRank.java}
> import org.apache.commons.math.linear.Array2DRowRealMatrix;
> import org.apache.commons.math.linear.RealMatrix;
> import org.apache.commons.math.linear.SingularValueDecomposition;
> import org.apache.commons.math.linear.SingularValueDecompositionImpl;
> public class TestSVDRank {
> 	public static void main(String[] args) {
> 		double[][] d = { { 1, 1, 1 }, { 0, 0, 0 }, { 1, 2, 3 } };
> 		RealMatrix m = new Array2DRowRealMatrix(d);
> 		SingularValueDecomposition svd = new SingularValueDecompositionImpl(m);
> 		int r = svd.getRank();
> 		System.out.println("Rank: "+r);
> 	}
> }
> {code} 
> The rank is computed as 3. This problem also occurs for larger matrices. I discovered the problem when trying to replace the corresponding JAMA method.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] Updated: (MATH-465) Incorrect matrix rank via SVD

Posted by "Phil Steitz (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/MATH-465?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Phil Steitz updated MATH-465:
-----------------------------

    Fix Version/s: 3.0

For now, pushing to 3.0.  If we get a fix for this and MATH-327 before 3.0 is ready, I may propose a 2.2.1 to include it.

> Incorrect matrix rank via SVD
> -----------------------------
>
>                 Key: MATH-465
>                 URL: https://issues.apache.org/jira/browse/MATH-465
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: 2.1
>         Environment: Windows XP Prof. Vs. 2002
>            Reporter: Marisa Thoma
>             Fix For: 3.0
>
>
> The getRank() function of SingularValueDecompositionImpl does not work properly. This problem is probably related to the numerical stability problems mentioned in [MATH-327|https://issues.apache.org/jira/browse/MATH-327] and [MATH-320|https://issues.apache.org/jira/browse/MATH-320].
> Example call with the standard matrix from R (rank 2):
> {code:title=TestSVDRank.java}
> import org.apache.commons.math.linear.Array2DRowRealMatrix;
> import org.apache.commons.math.linear.RealMatrix;
> import org.apache.commons.math.linear.SingularValueDecomposition;
> import org.apache.commons.math.linear.SingularValueDecompositionImpl;
> public class TestSVDRank {
> 	public static void main(String[] args) {
> 		double[][] d = { { 1, 1, 1 }, { 0, 0, 0 }, { 1, 2, 3 } };
> 		RealMatrix m = new Array2DRowRealMatrix(d);
> 		SingularValueDecomposition svd = new SingularValueDecompositionImpl(m);
> 		int r = svd.getRank();
> 		System.out.println("Rank: "+r);
> 	}
> }
> {code} 
> The rank is computed as 3. This problem also occurs for larger matrices. I discovered the problem when trying to replace the corresponding JAMA method.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.