Ken
Ken

Reputation: 1

Getting largest eigenvalue of stochastic matrix in R and MATLAB

I' trying to get the largest eigenvalue of a fully-connected right stochastic matrix in R & MATLAB. From this link: http://en.wikipedia.org/wiki/Stochastic_matrix I understand that the largest eigenvalue will be 1. For example, we can see the eigenvalues are "1, 0" after running the following code in R:

> eigen(matrix(rep(0.5,4),ncol=2))

$values

[1] 1 0

$vectors

  [,1]      [,2]

[1,] 0.707107 -0.707107

[2,] 0.707107 0.707107

But recently, I found a very interested result if I try to get the largest eigenvalue of the following stochastic matrix:

> m = matrix(c(0.5, 0.995, 0.5, 0.005),ncol = 2 ,nrow=2);

> eigen(m)$value

[1] 1.000 -0.495

> eigen(m)$value[1] == 1

[1] FALSE

Notice that it show "FALSE". That's weird! It should be equal to 1, right? There should have some computation errors. I also tried this matrix in MATLAB and still got the same result. So far, I can only round it up to 1. Any idea about how to fix it?

Thank you,

Ken

Upvotes: 0

Views: 369

Answers (1)

mrip
mrip

Reputation: 15163

In this particular case, I get TRUE on my machine, but generally, comparing floating point values for equality is a bad idea and will be unreliable because of rounding. For example, I get:

> (0.1+0.1+0.1)/3==0.1
[1] FALSE

Floating point operations almost always involve some rounding, and so you can't expect the result of two calculations which should yield algebraically equal quantities to produce the same floating point value.

Upvotes: 1

Related Questions