[R] using SVD to get an inverse matrix of covariance matrix
Dear R-users, I have one question about using SVD to get an inverse matrix of covariance matrix Sometimes I met many singular values d are close to 0: look this example $d [1] 4.178853e+00 2.722005e+00 2.139863e+00 1.867628e+00 1.588967e+00 [6] 1.401554e+00 1.256964e+00 1.185750e+00 1.060692e+00 9.932592e-01 [11] 9.412768e-01 8.530497e-01 8.211395e-01 8.077817e-01 7.706618e-01 [16] 7.007119e-01 6.237449e-01 5.709922e-01 5.550645e-01 5.062633e-01 [21] 4.792278e-01 4.222183e-01 3.660419e-01 3.293667e-01 3.026312e-01 [26] 2.942821e-01 2.811098e-01 2.626359e-01 2.199134e-01 1.943776e-01 [31] 1.712359e-01 1.561616e-01 1.359116e-01 1.280704e-01 1.099847e-01 [36] 1.013633e-01 9.622151e-02 8.396722e-02 7.083654e-02 6.755967e-02 [41] 5.392306e-02 3.807169e-02 2.942905e-02 2.726249e-02 4.555067e-16 [46] 3.095299e-16 2.918951e-16 2.672369e-16 2.336190e-16 2.239488e-16 [51] 2.089471e-16 1.970283e-16 1.863823e-16 1.775903e-16 1.698164e-16 [56] 1.594850e-16 1.500927e-16 1.469157e-16 1.406057e-16 1.366468e-16 [61] 1.319553e-16 1.252144e-16 1.193341e-16 1.142526e-16 1.064905e-16 [66] 1.040117e-16 1.005124e-16 9.310727e-17 8.995158e-17 8.529797e-17 [71] 8.204344e-17 7.759612e-17 7.478445e-17 7.225679e-17 6.709050e-17 [76] 5.996665e-17 5.830386e-17 5.687619e-17 5.121094e-17 4.848857e-17 [81] 4.549679e-17 4.307547e-17 3.830520e-17 3.450571e-17 3.312035e-17 [86] 3.260300e-17 2.399392e-17 2.141970e-17 1.996962e-17 1.881993e-17 [91] 1.567323e-17 1.062695e-17 6.730278e-18 2.118570e-18 4.991002e-19 Since the inverse matrix = u * inverse(d) * v', If I calculate inverse d based on formula : 1/d, then most values of inverse matrix will be huge. This must be not a good way. MOre special case, if a single value is 0, then we can not calculate inverse d based on 1/d. Therefore, my question is how I can calculate inverse d (that is inverse diag(d) more efficiently??? Thanks ping __ Post your free ad now! http://personals.yahoo.ca __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] using SVD to get an inverse matrix of covariance matrix
If some of the eigenvalues of a square matrix are (close to) zero, then it's inverse does not exist. However, you can always calculate it's generalized inverse ginv(). library(MASS) help(ginv) It'll allow you to specify a tol argument: tol: A relative tolerance to detect zero singular values. Hope that helps, Jerome On July 11, 2003 08:49 am, ge yreyt wrote: Content-Length: 2154 Status: R X-Status: N Dear R-users, I have one question about using SVD to get an inverse matrix of covariance matrix Sometimes I met many singular values d are close to 0: look this example snip Since the inverse matrix = u * inverse(d) * v', If I calculate inverse d based on formula : 1/d, then most values of inverse matrix will be huge. This must be not a good way. MOre special case, if a single value is 0, then we can not calculate inverse d based on 1/d. Therefore, my question is how I can calculate inverse d (that is inverse diag(d) more efficiently??? Thanks ping __ Post your free ad now! http://personals.yahoo.ca __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] using SVD to get an inverse matrix of covariance matrix
More presicely, if M is singular, then M%*%x = b will have multiple solutions only if b is in the subspace spanned by columns of M. Example: M - array(1:2, dim=c(2,2)) (svdM - svd(M)) $d [1] 3.162278 0.00 $u [,1] [,2] [1,] -0.4472136 -0.8944272 [2,] -0.8944272 0.4472136 $v [,1] [,2] [1,] -0.7071068 -0.7071068 [2,] -0.7071068 0.7071068 This M is not symmetrical and so cannot be a covariance matrix, but you can get the same effect with a symmetrical matrix. I'm using this example because it is the simplest thing that comes to mind to illustrate the point. By the definition of the singular value decomposition, M = svdM$u %*% diag(svdM$d) %*% t(svdM$v). Since svdM$d[2] == 0, M%*%x = svdM$u[, 1]*svdM$d[1]*t(svdM$v[,1])%*%x = x1 * svdM$u[,1], where x1 = svdM$d[1]*t(svdM$v[,1])%*%x Thus, if b is proportional to svdM$u[,1], the system has a solution. That solution will be proportional to svdM$v[,1] + x2*svdM$v[,2], for any arbitrary value of x2. hope this helps. spencer graves Thomas Lumley wrote: On Fri, 11 Jul 2003, ge yreyt wrote: Dear R-users, I have one question about using SVD to get an inverse matrix of covariance matrix Sometimes I met many singular values d are close to 0: look this example snip most values of inverse matrix will be huge. This must be not a good way. MOre special case, if a single value is 0, then we can not calculate inverse d based on 1/d. Therefore, my question is how I can calculate inverse d (that is inverse diag(d) more efficiently??? If singular values are zero the matrix doesn't have an inverse: that is, the equation Mx=b will have multiple solutions for any given b. It is possible to get a pseudoinverse, a matrix M that picks out one of the solutions. One way to do this is to set the diagonal to 1/d where d is not (nearly) zero and to 0 when d is (nearly) zero. One place to find a discussion of this is `Matrix Computations' by Golub and van Loan. -thomas __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help