Home > Software engineering >  Converting stochastic probabilities into target numbers
Converting stochastic probabilities into target numbers

Time:12-06

Given the following stochastic transition matrix, how can I convert these probabilities to random number generator target numbers?

transmat <- structure(c(0.77, 0.561, 0.14, 0.187, 0.07, 0.063, 0, 0.063, 
            0.01, 0.125, 0.01, 0.001), dim = c(2L, 6L), 
            dimnames = list(c("0", "1"), c("0", "1", "2", "5", "8", "9")))

The aim is to output target numbers for a transition to occur in a simulation environment. This can be done manually as in the example below, which gives approximations of dice roll ranges for the sum of 3 fair six-sided dice for the first row of the transition matrix (see the probability distribution here); however, an algorithm to do this is needed.

In the provided example, state 0 will remain as state 0 with a roll of 3-12, but will transition to state 1 on a roll of 13-14, state 2 with a roll of 15-16, state 8 on a roll of 17, and state 9 on a roll of 18.

   0      1       2       5   8    9   
0 "3-12" "13-14" "15-16" "-" "17" "18"

Conceptually I am unsure of how to proceed. One thought was to (1) index the probabilities in the row and the range of random number generator results and (2) somehow compare the cumulative probability and the remaining probabilities with the probabilities of possible random numbers.

CodePudding user response:

My understanding of your problem is that you'd like to generate simulations from the probabilities in your matrix "transmat".

Apart from the literal way of performing the dice-roll simulation you describe (i.e. just generate random dice rolls using floor(runif(n=1,min=1,max=7)) and generate the next state using some kind of if statement),

You could use sample to generate realizations from the probabilities in "transmat" directly:

sample(x=as.numeric(colnames(transmat)),  
       size=10^4,
       replace=TRUE,
       prob=transmat[1,])

The key here is using the probabilities from transmat as the argument to prob in sample. A count of the simulated states using table:

   0    1    2    8    9 
7681 1413  713   98   95 

shows this is a simulation of the transition probabilities in the first row of your transition matrix.

CodePudding user response:

Could you use different colored dice (or just roll one at a time)? That would give more states, each with the same probability. For example, there are 6^3 = 216 states with three dice if they are ordered by color (or by throw order). For the first row of transmat, it could be:

c(
  "1,1,1 - 5,4,4" = 0.768518519,
  "5,4,5 - 6,3,5" = 0.143518519,
  "6,3,6 - 6,6,2" = 0.069444444,
  "      -      " = 0,
  "6,6,3 - 6,6,4" = 0.009259259,
  "6,6,5 - 6,6,6" = 0.009259259
)
#> 1,1,1 - 5,4,4 5,4,5 - 6,3,5 6,3,6 - 6,6,2      -        6,6,3 - 6,6,4 6,6,5 - 6,6,6 
#>  0.768518519   0.143518519   0.069444444   0.000000000   0.009259259   0.009259259

Each additional die/roll would increase the precision by a factor of 6.

If that's an option, I have some ideas about automating it.

  • Related