R语言 斜槽和阶梯模拟

slmsl1lt  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(153)

我是一个乞丐在R和尝试模拟降落伞和梯子游戏。现在我想找到或估计平均位置或移动后,单人到达第10回合,也模拟多次,以获得方差和绘制直方图。参考以下代码。请指导我。

max_turns <- 10  # number of turns to take
position <- 0  # start on the board
max_position <- 100 # number of box on board
n_sides_die <- 6 # number of sides on one die roll

                            
for (turn in 1:max_turns){      # use a for loop to simulate a number of turns
  die_roll <- sample.int(n_sides_die, 1) # simulate the values from rolls of an unbiased six-sided die
  position <- position + die_roll # compute new board position
  if ( position >= max_position ){  # break out of loop if we roll die that is greater than or equal to 60
    break
  }
}

**EXPECTING OUTPUT TO BE SOMETHING LIKE THIS**

turn_               ladder
                     or
num start roll land chute end

1     0   1    1    chute 38
2    38   4   42    NA    42
3    42   1   43    NA    43
4    43   2   45    NA    45
5    45   5   50    NA    50
6    50   3   53    NA    53
7    53   6   59    NA    59
8    59   2   61    NA    61
9    61   3   64    ladder60
10   60   3   63    NA    63
fd3cxomn

fd3cxomn1#

最好的方法是通过一个转移矩阵(在下面的代码中是P),如果你建立了一个概率矩阵,将当前的方块连接到下一个方块,那么你只需要将这个矩阵提升到10次幂,就可以找到第10圈后的分布。

## Start and end positions of the snakes and ladders
ladders.df <- data.frame(start=c(1,4,9,21,28,36,51,71,80), 
                         end=c(38,14,31,42,84,44,67,91,100)) 
chutes.df <- data.frame(start=c(98,95,93,87,64,62,56,49,47,16), 
                        end=c(78,75,73,24,60,19,53,11,26,6)) 
# We only need a vector of start squares and a vector of end squares
to <- c(chutes.df$end, ladders.df$end)
from <- c(chutes.df$start, ladders.df$start)

# This function takes the square you land on then applies:
# the reflection if more than 100
# the snakes and ladders
position <- function(x){
  if(x>100) x=100-(x-100)
  if(x %in% from) x <- to[which(from==x)]
  x
}



# Now set up the transition matrix. The probability of each dice roll number is 1/6

# Initialise a zero matrix
# I'm adding a position 101 which will be the starting 'zero' position.
P = matrix(0,nrow=101,ncol=101)
# Add 1/6 probability to every possible transition
for(i in 1:99){
  for(j in 1:6){
    P[i,position(i+j)]=P[i,position(i+j)]+1/6
  }
} 
# 100 is an absorbing state
P[100,100]=1
# Setting the rules for the first turn
for(j in 1:6) P[101,position(j)] <- P[101,position(j)]+1/6

# Distribution of positions after 1 turn from square 0 
# (row 101 are the positions starting from square 0):

P[101,]

# Distribution of positions after 2 turns from square 1:

(P %*% P)[101,]

# Distribution of positions after 10 turns from square 1 (with plot):

P2 <- P
for(i in 1:20) P2 <- P2 %*% P

P2[101,] |> plot(type="o")

现在假设您要模拟一条10个转弯的路径。
利用第10圈的位置具有由P[x,]给出的概率的多项式分布的事实,其中x是第9圈的位置(等等)。

# Start at square 0
pos <- 101

# Turn 1 comes from a multinomial distribution with probabilities P[pos[101],]
pos[2] <- which(rmultinom(1,1,prob = P[pos[1],] )==1)

# We can add more turns (say 10)

# Start at square 0 (matrix row 101)
pos <- 101
for(i in 2:11){
  pos[i] <- which(rmultinom(1,1,prob = P[pos[i-1],] )==1)
}

# Look at the path (without the starting 0):
pos[-1]

 [1]  2 14 20 42 43 45 67 69 70 74

最后,绘制十圈后到达位置的直方图:

# 1 turn transition matrix
P2 <- P
# Add 9 more turns to get the 10 turn transition matrix
for(i in 1:9) P2 <- P2 %*% P

# Sample 10000 times from the distribution starting from 0 after 10 turns:
sims <- sample(1:100,10000,TRUE,prob = P2[101,1:100])

# Plot histogram
hist(sims, breaks=100)

相关问题