Cumulative transition matrix Markov Chain simulation I have a cumulative transition matrix with probabilities for all the possible states from 1 to 5. Now the algorithm for simulating future states is following: the initial state is selected randomly, and a random value between 0 and 1 is then produced by uniform random number generator.To determine the next state in the Markov process the value of the random number is is compared with the elements of of the *i-*th row of the cumulative transition matrix determined from the preceding state. If the value of the random number is greater than the cumulative probability of the preceding state but less then or equal to the cumulative probability of the succeeding state, the succeeding state is chosen to represent the next state.

cum_trans &lt;- matrix(c(0.1686747,0.4337349,0.6265060,0.7289157,1,
              0.2765957,0.5053191,0.6648936,0.7659574,1,
              0.2518519,0.4740741,0.6518519,0.7407407,1,
              0.1911765,0.4705882,0.6617647,0.7941176,1,
              0.2096774,0.4892473,0.6827957,0.7419355,1
              ), nrow = 5, ncol = 5, byrow = TRUE)

       [,1]      [,2]      [,3]      [,4]      [,5]
 [1,] 0.1686747 0.4337349 0.6265060 0.7289157    1
 [2,] 0.2765957 0.5053191 0.6648936 0.7659574    1
 [3,] 0.2518519 0.4740741 0.6518519 0.7407407    1
 [4,] 0.1911765 0.4705882 0.6617647 0.7941176    1
 [5,] 0.2096774 0.4892473 0.6827957 0.7419355    1


state&lt;-matrix(NA, nrow=1,ncol=25)
i&lt;-sample(1:5,1)
state[1]&lt;-i
for (k in 2:25){
 rr&lt;-runif(1)
 state[k]&lt;-findInterval(rr,cum_trans[i,])+1
 i&lt;-state[k]
 }
 state


So I'm not sure if I implemented the algorithm correctly. Can someone suggest modifications or improvements.
I see nothing wrong with your implementation, in the sense it does exactly what you described in plain English, and somewhat efficiently. Here are however a few pieces of advice, mostly about improving your coding standards.


Add some spaces to your code to make it more readable. Have a look at the body of basic functions (e.g. lm) to see what clean code should look like. In particular, spaces after commas and spaces on both sides of &lt;-, =, and all binary operators.
Replace your hardcoded values (5 and 25) with variables. 5 can be derived from the number of rows in cum_trans and 25 is an input to your process. By assigning it to a variable (or a function input), and reusing that variable throughout your code, your code becomes easier to read and maintain, and more robust to changes. Imagine for example what would be required of you if you wanted to change the size of the transition matrix or wanted to have 50 iterations instead of 25.
Using a 1-row matrix is overkill and error-prone: use a vector.
Choose well how you name your variables and comment your code. Again, to make it easier to read and understand.
Learn the difference between numerics and integers. Here your states are obviously integers, yet your code stores numerics. Granted, it is not critical here, but using integers use less memory and can make your code faster. Integers are also not subject to floating point errors so they can make your code more robust in some instances.
Marginal speed improvement. The 25 random calls to runif(1) being independent, you can make a single call to runif(25) and store the results.
Think of your code in terms of inputs of outputs, then write a function.


Updated code taking some of these into account:

random.walk &lt;- function(cum_trans, length.out = 25L) {
   num.states &lt;- nrow(cum_trans)
   states  &lt;- vector(mode = "integer", length = length.out)
   # pick the initial state randomly
   states[1L] &lt;- sample(num.states, 1L)
   # jump randomly using the cum_trans matrix
   num.jumps &lt;- length.out - 1L
   randoms &lt;- runif(num.jumps)
   for (i in seq(num.jumps)) {
      current.state  &lt;- states[i]
      current.prob   &lt;- cum_trans[current.state, ]
      states[1L + i] &lt;- findInterval(randoms[i], current.prob) + 1L
   }
   return(states)
}


Next, think of the assumptions your code is making: cum_trans must be a square matrix with each row being an increasing vector of positive values ending with 1. length.out must be an integer vector of length 1. All of these assumptions can be checked at the top of the function's body; I am only implementing a few of them here:

stopifnot(nrow(cum_trans) == ncol(cum_trans),
          all(cum_trans &gt;= 0),
          is.integer(length.out),
          length(length.out) == 1L)


This way, there is no way you or your end-user can mess up with the inputs.

I hope you find this useful and it encourages you to write better code.