2

I need to multiply the matrix estado by the matrix matriz_de transition and re-assign the variable estado to the result of the multiplication, I have to do this 1000 times, and I need to repeat the simulation 100 times.

I.e Multiply the estado matrix by the matriz_de_transicion 1000 times and see if the value of estado converges, then repeat this operation 100 times.

I have tried with for loops, but the way I re-assign the values to the variable estado doesn't work since I keep getting the same results, even if when I do it manually it seems to work.

This is my code:

    nombre_estados <- c("Estado 1","Estado 2","Estado 3")
    matriz_de_transicion <- matrix(c(0.2,0.7,0.1,
                                     0.3,0.7,0.0,
                                     0.1,0.4,0.5), 
                            byrow = T,nrow = 3, dimnames = list(nombre_estados,nombre_estados))
    estado <- matrix(c(0.2,0.7,0.1),byrow = T, nrow = 1)

    estado <- estado %*% matriz_de_transicion
    estado # 0.26     0.67     0.07
    estado <- estado %*% matriz_de_transicion
    estado # 0.26    0.679    0.061
   # repeat this 1000 times

Many thanks for the answers,how could I then add these final estados to the 100 x 3 matrix?. I created the 100 x 3 matrix:

datos <- matrix(c(0,0,0),byrow = F,ncol =  3, nrow = 100)

Then I tried to do a nested loop:

for(i in 1:100){
for(i in 1:1000){
  estado <- estado %*% matriz_de_transicion  
}
  datos[,1] <- estado[1,1]
  datos[,2] <- estado[1,2]
  datos[,3] <- estado[1,3]
  estado <- matrix(c(0.2,0.7,0.1),byrow = T, nrow = 1) # I thought this would reset the value to the initial state but I fill the 100 x 3 matrix with the same values.
}
           [,1]      [,2]       [,3]
  [1,] 0.2631579 0.6842105 0.05263158
  [2,] 0.2631579 0.6842105 0.05263158
  [3,] 0.2631579 0.6842105 0.05263158
  [4,] 0.2631579 0.6842105 0.05263158
  [5,] 0.2631579 0.6842105 0.05263158
  [6,] 0.2631579 0.6842105 0.05263158
  [7,] 0.2631579 0.6842105 0.05263158
  [8,] 0.2631579 0.6842105 0.05263158

I'm struggling to reset the value of the estado variable,for each one of the 100 simulations I need to obtain in the end, so each one of the 1:1000 loops have the same starting value for estado.

2 Answers 2

1

Maybe something like the following.

I have the function return extra information, since you ask whether the value of estado converges.

fun <- function(M, Trans, n = 1000, tol = .Machine$double.eps^0.5){
    for(i in seq_len(n)){
        Prev <- M
        M <- M %*% Trans
        if(all(abs(M - Prev) < tol)) break
    }
    list(Final = M, converge = i < n, iter = i)
}

fun(estado, matriz_de_transicion)
#$`Final`
#      Estado 1  Estado 2   Estado 3
#[1,] 0.2631579 0.6842105 0.05263159
#
#$converge
#[1] TRUE
#
#$iter
#[1] 18
Sign up to request clarification or add additional context in comments.

3 Comments

Our values are matching. Hence, I think, simple for-loop is working to fit the scenario.
@MKR Yes, but suppose the OP has to do this with different matrices. Then a function is a more convenient way to do it.
Definitely a function is a better approach. But I was a bit confused with a OP's comment that for-loop doesn't work the way values is re-assign. Hence, I thought to clarify that with an answer.
1

I'm sure just a for-loop will fit this purpose. Not sure why OP thinks calculation is not taking into effect for next calls. estado is declared at global scope anyway.

estado <- matrix(c(0.2,0.7,0.1),byrow = T, nrow = 1)
for(i in 1:1000){
  estado <- estado %*% matriz_de_transicion  
}
estado 
#  Estado 1  Estado 2   Estado 3
#[1,] 0.2631579 0.6842105 0.05263158

1 Comment

Many thanks! How could I then add these final estados to the 100 x 3 matrix? datos <- matrix(c(0,0,0),byrow = F,ncol = 3, nrow = 100) n = 100 for(i in 1:100){ for(i in 1:1000){ estado <- estado %*% matriz_de_transicion } datos[,1] <- estado[1,1] datos[,2] <- estado[1,2] datos[,3] <- estado[1,3] estado <- matrix(c(0.2,0.7,0.1),byrow = T, nrow = 1) } I'm struggling to reset the value of the estado variable,for each one of the 100 simulations I need to obtain in the end, so each one of the 1:1000 loops have the same starting value for estado.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.