Return-Path: X-Original-To: apmail-commons-dev-archive@www.apache.org Delivered-To: apmail-commons-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id C4F7610505 for ; Sun, 16 Feb 2014 01:03:02 +0000 (UTC) Received: (qmail 99568 invoked by uid 500); 16 Feb 2014 01:02:59 -0000 Delivered-To: apmail-commons-dev-archive@commons.apache.org Received: (qmail 99399 invoked by uid 500); 16 Feb 2014 01:02:58 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 99390 invoked by uid 99); 16 Feb 2014 01:02:58 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 16 Feb 2014 01:02:58 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=HTML_MESSAGE,LOTS_OF_MONEY,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of ted.dunning@gmail.com designates 209.85.213.51 as permitted sender) Received: from [209.85.213.51] (HELO mail-yh0-f51.google.com) (209.85.213.51) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 16 Feb 2014 01:02:51 +0000 Received: by mail-yh0-f51.google.com with SMTP id t59so13029062yho.38 for ; Sat, 15 Feb 2014 17:02:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=H2rMGiJaGB3cPjr+bY3Dh0LXSJse66gcXtIVKKuUH5U=; b=VxP1rL/iFM700Ky+C4v2kaiSQ9dPd5toBisR0BsOjb+4PH+iVYVEAKxv0K3xr5XiMz U1hFTCWuzrc6dSP+w0XMrirvK2VyNlW+1DZ1r1pTaz/h6NkkyYOfsLgkpj2fWXf4O7kD W4FU55GlyC6Wm3j2vgvffLh8LMwQ08DyPhAdfHYJ9tgmSbxf/avynhRnmiM/JnmTj0ms Eegv8s+DXkGct0xXY7LG/Eck8l19kl8vUSF9KtXOmiadHKlRiR9JaumfV1ebYAHwMh1W 9s+wEo7bb8Yg6m2tGuP1PPibxfaSAAwyLlZXSX81uUWayt/sRiWhqzhV5M3wdNKTm9n7 MFPQ== X-Received: by 10.236.106.99 with SMTP id l63mr6776308yhg.81.1392512550144; Sat, 15 Feb 2014 17:02:30 -0800 (PST) MIME-Version: 1.0 Received: by 10.170.223.87 with HTTP; Sat, 15 Feb 2014 17:02:00 -0800 (PST) In-Reply-To: <003301cf2a46$c42fd4a0$4c8f7de0$@gmail.com> References: <000601cf29f2$b75aa830$260ff890$@gmail.com> <003301cf2a46$c42fd4a0$4c8f7de0$@gmail.com> From: Ted Dunning Date: Sat, 15 Feb 2014 17:02:00 -0800 Message-ID: Subject: Re: [math] trouble with SingularValueDecomposition To: Commons Developers List Content-Type: multipart/alternative; boundary=089e0158a972e5ad3a04f27b97ef X-Virus-Checked: Checked by ClamAV on apache.org --089e0158a972e5ad3a04f27b97ef Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Note that the only reason that the order is unconstrained is because the two corresponding singular values are equal. Strictly speaking, for equal singular values, any unitary transformation of the corresponding singular vectors are also valid singular vectors. On Sat, Feb 15, 2014 at 4:09 AM, Patrick Meyer wrote: > Thanks Ted. As I mentioned my knowledge of SVD is limited, and I was not > aware that it is OK to have a different order of the first two columns in > the results (or the conditions under which the order doesn't matter). I a= m > trying to track down a bug in some code and that=E2=80=99s what led me to= the SVD. > I guess I need to keep looking for the real bug. > > For completeness, my results R were the same as you reported. My results > from CM are shown below and if you swap the first and second column, the > results agree with R. > > U: > 0.9940594018965339 0.06774763124429131 -0.08518312016997649 > 0.10615872136916754 -0.7761401247896214 0.6215599991704858 > 0.02400481989869077 0.6269104921377042 0.778721390144956 > > V: > 0.9963653125425972 0.0 -0.08518312016997495 > 0.0531395658155507 -0.7815621241949481 0.6215599991704865 > 0.06657590034559915 0.6238274168581248 0.7787213901449556 > > > > -----Original Message----- > From: Ted Dunning [mailto:ted.dunning@gmail.com] > Sent: Saturday, February 15, 2014 2:17 AM > To: Commons Developers List > Subject: Re: [math] trouble with SingularValueDecomposition > > For what its worth, I tested the Mahout SVD which shares code lineage wit= h > the Commons Math implementation. > > The results I got were: > > > > > > > > > > > > > > > > > > > > > > > > > > *sum(abs(m - u * s * v')) =3D 4.31946146e-16S =3D 1.002319690998 > > 1.002319690998 1.000000000000 > > U =3D 0.994059401897 0.067747631244 > > -0.085183120170 0.106158721369 -0.776140124790 0.621559999170 > > 0.024004819899 0.626910492138 0.778721390145 V =3D 0.996365312543 > > 0.000000000000 -0.085183120170 0.053139565816 -0.781562124195 > > 0.621559999170 0.066575900346 0.623827416858 0.778721390145* > > > Note that the residue of the reconstruction is excellently small. This > indicates that the result is correct. > > > If you compare these to the R results, > > > > > > > > > > > > > > > > > > > > > > > > *[1] 1.0023196909980066 1.0023196909980066 1.0000000000000000$u > > [,1] [,2] [,3][1,] > > 0.067747631244291326 -0.994059401896534967 0.085183120169970525 [2,] > > -0.776140124789635122 -0.106158721369163295 -0.621559999170469113[3,] > > 0.626910492137687125 -0.024004819898688426 -0.778721390144969994$v > > [,1] [,2] [,3] [1,] > > 0.00000000000000000 -0.996365312542597747 0.085183120169970497[2,] > > -0.78156212419496163 -0.053139565815546450 -0.621559999170469668[3,] > > 0.62382741685810772 -0.066575900345596822 -0.778721390144969550* > > > These are identical to the previous results except that the first two > singular values are equal which means that the order of the corresponding > left and right singular vectors are different and there are sign changes = in > the singular vectors. > > My guess is that you will get the same results in Apache Commons Math. > > > > On Fri, Feb 14, 2014 at 6:07 PM, Patrick Meyer wrote= : > > > Hi, > > > > > > > > I am using the SingularValueDecomposition class with a matrix but it > > gives me a different result than R. My knowledge of SVD is limited, so > > any advice is welcomed. > > > > > > > > Here's the method in Java > > > > > > > > public void svdTest(){ > > > > > > > > double[][] x =3D { > > > > {1.0, -0.053071807862720116, 0.04236086650321309}, > > > > {0.05307180786272012, 1.0, 0.0058054424137053435}, > > > > {-0.04236086650321309, -0.005805442413705342, 1.0} > > > > }; > > > > > > > > RealMatrix X =3D new Array2DRowRealMatrix(x); > > > > > > > > SingularValueDecomposition svd =3D new > > SingularValueDecomposition(X); > > > > > > > > RealMatrix U =3D svd.getU(); > > > > for(int i=3D0;i > > > for(int j=3D0;j > > > System.out.print(U.getEntry(i,j) + " "); > > > > } > > > > System.out.println(); > > > > } > > > > > > > > System.out.println(); > > > > System.out.println(); > > > > RealMatrix V =3D svd.getV(); > > > > for(int i=3D0;i > > > for(int j=3D0;j > > > System.out.print(V.getEntry(i,j) + " "); > > > > } > > > > System.out.println(); > > > > } > > > > > > > > > > > > } > > > > > > > > > > > > And here's the function in R. > > > > > > > > x<-matrix(c( > > > > 1.0, -0.053071807862720116, 0.04236086650321309, > > > > 0.05307180786272012, 1.0, 0.0058054424137053435, > > > > -0.04236086650321309, -0.005805442413705342, 1.0), > > > > nrow=3D3, byrow=3DTRUE) > > > > svd(x) > > > > > > > > Does anyone know why I am getting different results for U and V? I am > > using commons math 3.1. > > > > > > > > Thanks, > > > > Patrick > > > > > > > > > > > > > > > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org > For additional commands, e-mail: dev-help@commons.apache.org > > --089e0158a972e5ad3a04f27b97ef--