poor precision of matrix inverse?
3
0
Entering edit mode
Zack Liu ▴ 20
@zack-liu-4288
Last seen 9.6 years ago
Dear members, I am using "solve" to compute the inverse of a 5 by 5 matrix using R and matlab. The results are quite interesting.. It seems that R has very poor precision in terms of calculating inverse of a matrix. Can someone tell me if I should use other functions to computer matrix inverse? R Code ------------ > b [,1] [,2] [,3] [,4] [,5] [1,] 1.00000000 0.02522892 -0.09181110 0.03656846 -0.01403095 [2,] 0.02522892 1.00000000 0.12764892 0.05252468 0.05174332 [3,] -0.09181110 0.12764892 1.00000000 -0.01334807 0.02270218 [4,] 0.03656846 0.05252468 -0.01334807 1.00000000 -0.08303199 [5,] -0.01403095 0.05174332 0.02270218 -0.08303199 1.00000000 > solve(b) [,1] [,2] [,3] [,4] [,5] [1,] 1.01117403 -0.03672483 0.09683324 -0.03282862 0.01116385 [2,] -0.03672483 1.02393425 -0.13360444 -0.05881784 -0.05534769 [3,] 0.09683324 -0.13360444 1.02646994 0.01604039 -0.01369944 [4,] -0.03282862 -0.05881784 0.01604039 1.01166293 0.08621905 [5,] 0.01116385 -0.05534769 -0.01369944 0.08621905 1.01049046 > solve(b) *b [,1] [,2] [,3] [,4] [,5] [1,] 1.0111740259 -0.0009265279 -0.0088903666 -0.0012004920 -0.0001566394 [2,] -0.0009265279 1.0239342521 -0.0170544622 -0.0030893886 -0.0028638734 [3,] -0.0088903666 -0.0170544622 1.0264699443 -0.0002141082 -0.0003110073 [4,] -0.0012004920 -0.0030893886 -0.0002141082 1.0116629287 -0.0071589398 [5,] -0.0001566394 -0.0028638734 -0.0003110073 -0.0071589398 1.0104904599 Matlab Code ___________________ >> b b = 1.0000 0.0252 -0.0918 0.0366 -0.0140 0.0252 1.0000 0.1276 0.0525 0.0517 -0.0918 0.1276 1.0000 -0.0133 0.0227 0.0366 0.0525 -0.0133 1.0000 -0.0830 -0.0140 0.0517 0.0227 -0.0830 1.0000 >> b^-1 ans = 1.0112 -0.0367 0.0968 -0.0328 0.0112 -0.0367 1.0239 -0.1336 -0.0588 -0.0553 0.0968 -0.1336 1.0265 0.0160 -0.0137 -0.0328 -0.0588 0.0160 1.0117 0.0862 0.0112 -0.0553 -0.0137 0.0862 1.0105 >> b^-1 *b ans = 1.0000 -0.0000 -0.0000 -0.0000 0.0000 -0.0000 1.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 1.0000 0.0000 -0.0000 -0.0000 -0.0000 0.0000 1.0000 -0.0000 0 -0.0000 0.0000 0.0000 1.0000 [[alternative HTML version deleted]]
• 2.1k views
ADD COMMENT
0
Entering edit mode
@achilleas-pitsillides-4316
Last seen 9.6 years ago
Hey zack, it seems to me that in the example you are using entry-wise multiplication instead of matrix multiplication, i.e, you should be using solve(b) %*% b cheers, Achilleas On Mon, Nov 22, 2010 at 12:04 PM, zack liu <zack80.liu@gmail.com> wrote: > Dear members, > > I am using "solve" to compute the inverse of a 5 by 5 matrix using R and > matlab. The results are quite interesting.. It seems that R has very poor > precision in terms of calculating inverse of a matrix. > > Can someone tell me if I should use other functions to computer matrix > inverse? > > > R Code > > ------------ > > > b > [,1] [,2] [,3] [,4] [,5] > [1,] 1.00000000 0.02522892 -0.09181110 0.03656846 -0.01403095 > [2,] 0.02522892 1.00000000 0.12764892 0.05252468 0.05174332 > [3,] -0.09181110 0.12764892 1.00000000 -0.01334807 0.02270218 > [4,] 0.03656846 0.05252468 -0.01334807 1.00000000 -0.08303199 > [5,] -0.01403095 0.05174332 0.02270218 -0.08303199 1.00000000 > > solve(b) > [,1] [,2] [,3] [,4] [,5] > [1,] 1.01117403 -0.03672483 0.09683324 -0.03282862 0.01116385 > [2,] -0.03672483 1.02393425 -0.13360444 -0.05881784 -0.05534769 > [3,] 0.09683324 -0.13360444 1.02646994 0.01604039 -0.01369944 > [4,] -0.03282862 -0.05881784 0.01604039 1.01166293 0.08621905 > [5,] 0.01116385 -0.05534769 -0.01369944 0.08621905 1.01049046 > > solve(b) *b > [,1] [,2] [,3] [,4] [,5] > [1,] 1.0111740259 -0.0009265279 -0.0088903666 -0.0012004920 -0.0001566394 > [2,] -0.0009265279 1.0239342521 -0.0170544622 -0.0030893886 -0.0028638734 > [3,] -0.0088903666 -0.0170544622 1.0264699443 -0.0002141082 -0.0003110073 > [4,] -0.0012004920 -0.0030893886 -0.0002141082 1.0116629287 -0.0071589398 > [5,] -0.0001566394 -0.0028638734 -0.0003110073 -0.0071589398 1.0104904599 > > > Matlab Code > ___________________ > > >> b > > b = > > 1.0000 0.0252 -0.0918 0.0366 -0.0140 > 0.0252 1.0000 0.1276 0.0525 0.0517 > -0.0918 0.1276 1.0000 -0.0133 0.0227 > 0.0366 0.0525 -0.0133 1.0000 -0.0830 > -0.0140 0.0517 0.0227 -0.0830 1.0000 > > >> b^-1 > > ans = > > 1.0112 -0.0367 0.0968 -0.0328 0.0112 > -0.0367 1.0239 -0.1336 -0.0588 -0.0553 > 0.0968 -0.1336 1.0265 0.0160 -0.0137 > -0.0328 -0.0588 0.0160 1.0117 0.0862 > 0.0112 -0.0553 -0.0137 0.0862 1.0105 > > >> b^-1 *b > > ans = > > 1.0000 -0.0000 -0.0000 -0.0000 0.0000 > -0.0000 1.0000 -0.0000 -0.0000 -0.0000 > -0.0000 -0.0000 1.0000 0.0000 -0.0000 > -0.0000 -0.0000 0.0000 1.0000 -0.0000 > 0 -0.0000 0.0000 0.0000 1.0000 > > [[alternative HTML version deleted]] > > _______________________________________________ > Bioconductor mailing list > Bioconductor@stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: > http://news.gmane.org/gmane.science.biology.informatics.conductor > [[alternative HTML version deleted]]
ADD COMMENT
0
Entering edit mode
@misha-kapushesky-1334
Last seen 9.6 years ago
Try solve(b) %*% b. --Misha On Mon, 22 Nov 2010, zack liu wrote: > Dear members, > > I am using "solve" to compute the inverse of a 5 by 5 matrix using R and > matlab. The results are quite interesting.. It seems that R has very poor > precision in terms of calculating inverse of a matrix. > > Can someone tell me if I should use other functions to computer matrix > inverse? > > > R Code > > ------------ > >> b > [,1] [,2] [,3] [,4] [,5] > [1,] 1.00000000 0.02522892 -0.09181110 0.03656846 -0.01403095 > [2,] 0.02522892 1.00000000 0.12764892 0.05252468 0.05174332 > [3,] -0.09181110 0.12764892 1.00000000 -0.01334807 0.02270218 > [4,] 0.03656846 0.05252468 -0.01334807 1.00000000 -0.08303199 > [5,] -0.01403095 0.05174332 0.02270218 -0.08303199 1.00000000 >> solve(b) > [,1] [,2] [,3] [,4] [,5] > [1,] 1.01117403 -0.03672483 0.09683324 -0.03282862 0.01116385 > [2,] -0.03672483 1.02393425 -0.13360444 -0.05881784 -0.05534769 > [3,] 0.09683324 -0.13360444 1.02646994 0.01604039 -0.01369944 > [4,] -0.03282862 -0.05881784 0.01604039 1.01166293 0.08621905 > [5,] 0.01116385 -0.05534769 -0.01369944 0.08621905 1.01049046 >> solve(b) *b > [,1] [,2] [,3] [,4] [,5] > [1,] 1.0111740259 -0.0009265279 -0.0088903666 -0.0012004920 -0.0001566394 > [2,] -0.0009265279 1.0239342521 -0.0170544622 -0.0030893886 -0.0028638734 > [3,] -0.0088903666 -0.0170544622 1.0264699443 -0.0002141082 -0.0003110073 > [4,] -0.0012004920 -0.0030893886 -0.0002141082 1.0116629287 -0.0071589398 > [5,] -0.0001566394 -0.0028638734 -0.0003110073 -0.0071589398 1.0104904599 > > > Matlab Code > ___________________ > >>> b > > b = > > 1.0000 0.0252 -0.0918 0.0366 -0.0140 > 0.0252 1.0000 0.1276 0.0525 0.0517 > -0.0918 0.1276 1.0000 -0.0133 0.0227 > 0.0366 0.0525 -0.0133 1.0000 -0.0830 > -0.0140 0.0517 0.0227 -0.0830 1.0000 > >>> b^-1 > > ans = > > 1.0112 -0.0367 0.0968 -0.0328 0.0112 > -0.0367 1.0239 -0.1336 -0.0588 -0.0553 > 0.0968 -0.1336 1.0265 0.0160 -0.0137 > -0.0328 -0.0588 0.0160 1.0117 0.0862 > 0.0112 -0.0553 -0.0137 0.0862 1.0105 > >>> b^-1 *b > > ans = > > 1.0000 -0.0000 -0.0000 -0.0000 0.0000 > -0.0000 1.0000 -0.0000 -0.0000 -0.0000 > -0.0000 -0.0000 1.0000 0.0000 -0.0000 > -0.0000 -0.0000 0.0000 1.0000 -0.0000 > 0 -0.0000 0.0000 0.0000 1.0000 > > [[alternative HTML version deleted]] > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor >
ADD COMMENT
0
Entering edit mode
markus.boenn ▴ 50
@markusboenn-6784
Last seen 9.6 years ago
European Union
Hi Zack I think using solve(b) %*% b instead of solve(b) * b will yield a better result. Using the second command carries out a miltiplication by the same components, i.e. A * B=(A_ij*B_ij) for each i,j Best Markus zack liu wrote: > Dear members, > > I am using "solve" to compute the inverse of a 5 by 5 matrix using R and > matlab. The results are quite interesting.. It seems that R has very poor > precision in terms of calculating inverse of a matrix. > > Can someone tell me if I should use other functions to computer matrix > inverse? > > > R Code > > ------------ > > >> b >> > [,1] [,2] [,3] [,4] [,5] > [1,] 1.00000000 0.02522892 -0.09181110 0.03656846 -0.01403095 > [2,] 0.02522892 1.00000000 0.12764892 0.05252468 0.05174332 > [3,] -0.09181110 0.12764892 1.00000000 -0.01334807 0.02270218 > [4,] 0.03656846 0.05252468 -0.01334807 1.00000000 -0.08303199 > [5,] -0.01403095 0.05174332 0.02270218 -0.08303199 1.00000000 > >> solve(b) >> > [,1] [,2] [,3] [,4] [,5] > [1,] 1.01117403 -0.03672483 0.09683324 -0.03282862 0.01116385 > [2,] -0.03672483 1.02393425 -0.13360444 -0.05881784 -0.05534769 > [3,] 0.09683324 -0.13360444 1.02646994 0.01604039 -0.01369944 > [4,] -0.03282862 -0.05881784 0.01604039 1.01166293 0.08621905 > [5,] 0.01116385 -0.05534769 -0.01369944 0.08621905 1.01049046 > >> solve(b) *b >> > [,1] [,2] [,3] [,4] [,5] > [1,] 1.0111740259 -0.0009265279 -0.0088903666 -0.0012004920 -0.0001566394 > [2,] -0.0009265279 1.0239342521 -0.0170544622 -0.0030893886 -0.0028638734 > [3,] -0.0088903666 -0.0170544622 1.0264699443 -0.0002141082 -0.0003110073 > [4,] -0.0012004920 -0.0030893886 -0.0002141082 1.0116629287 -0.0071589398 > [5,] -0.0001566394 -0.0028638734 -0.0003110073 -0.0071589398 1.0104904599 > > > Matlab Code > ___________________ > > >>> b >>> > > b = > > 1.0000 0.0252 -0.0918 0.0366 -0.0140 > 0.0252 1.0000 0.1276 0.0525 0.0517 > -0.0918 0.1276 1.0000 -0.0133 0.0227 > 0.0366 0.0525 -0.0133 1.0000 -0.0830 > -0.0140 0.0517 0.0227 -0.0830 1.0000 > > >>> b^-1 >>> > > ans = > > 1.0112 -0.0367 0.0968 -0.0328 0.0112 > -0.0367 1.0239 -0.1336 -0.0588 -0.0553 > 0.0968 -0.1336 1.0265 0.0160 -0.0137 > -0.0328 -0.0588 0.0160 1.0117 0.0862 > 0.0112 -0.0553 -0.0137 0.0862 1.0105 > > >>> b^-1 *b >>> > > ans = > > 1.0000 -0.0000 -0.0000 -0.0000 0.0000 > -0.0000 1.0000 -0.0000 -0.0000 -0.0000 > -0.0000 -0.0000 1.0000 0.0000 -0.0000 > -0.0000 -0.0000 0.0000 1.0000 -0.0000 > 0 -0.0000 0.0000 0.0000 1.0000 > > [[alternative HTML version deleted]] > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor > -- Diplom-Bioinform. Markus B?nn Martin-Luther-Universit?t Halle-Wittenberg Naturwissenschaftliche Fakult?t III Institut f?r Informatik von-Seckendorff-Platz 1 06120 Halle (Saale)
ADD COMMENT

Login before adding your answer.

Traffic: 967 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6