I've been given a matrix:
P <- matrix(c(0, 0, 0, 0.5, 0, 0.5, 0.1, 0.1, 0, 0.4, 0, 0.4, 0, 0.2, 0.2, 0.3, 0, 0.3, 0, 0, 0.3, 0.5, 0, 0.2, 0, 0, 0, 0.4, 0.6, 0, 0, 0, 0, 0, 0.4, 0.6), nrow = 6, ncol = 6, byrow = TRUE)
Using the functions, mpow
, rows_equal
, matrices_equal
. I want to find when P^n
converges, in other words what n is, when all the rows are equal in the matrix and when P^n = P^(n+1)
.
By just looking at the functions i
have managed to deduce that around n=19-21
the matrix will converge.
Although, I want to find the right n using a loop. Here under are the functions mpow
, rows_equal
and matrices_equal
. I know they can be written differently but please keep them as they are.
mpow <- function(P, n, d=4) {
if (n == 0) diag(nrow(P)))
else if (n== 1) P
else P %*% mpow(P, n - 1))
}
rows_equal <- function(P, d = 4) {
P_new <- trunc(P * 10^d)
for (k in 2:nrow(P_new)) {
if (!all(P_new[1, ] == P_new[k, ])) {
return(FALSE)}
}
return(TRUE)
}
matrices_equal <- function(A, B, d = 4) {
A_new <- trunc(A * 10^d)
B_new <-trunc(B * 10^d)
if (all(A_new == B_new)) TRUE else FALSE
}
Now, to write the loop, we should do it something along the lines of:
First creating a function like so:
when_converged <- function(P) {...}
and
for (n in 1:50)
To try for when t.ex n = 50.
Although i don't know how to write the code correctly to do so, can anyone help me with that?
Thank you for reading my question.
Actually, a much better way is to do this:
Then when you call the function:
The function
stydis
, essentially continuously does:until convergence of
P
is reached. Convergence is numerically determined by the L1 norm of discrepancy matrix:The L1 norm is the maximum, absolute value of all matrix elements. When the L1 norm drops below
1e-16
, convergence occurs.As you can see, convergence takes 71 steps. Now, we can obtain faster "convergence" by controlling
tol
(tolerance):But if you check:
Only the first 4 digits are the same, as you put
tol = 1e-4
.A floating point number has a maximum of 16 digits, so I would suggest you use
tol = 1e-16
for reliable convergence test.