首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >鱿鱼游戏第7集及模拟

鱿鱼游戏第7集及模拟
EN

Stack Overflow用户
提问于 2021-10-15 12:50:29
回答 5查看 530关注 0票数 4

昨晚我看了“鱿鱼游戏”系列电视剧的第7集。这一集在桥上有一个二项分布的游戏。

具体来说,有16名玩家和一座桥,有18副玻璃(一副纯玻璃和一副安全玻璃),.If,一位玩家碰巧选择了纯玻璃,然后玻璃无法承受播放器的重量,玻璃就断了。下一个玩家的优势是,他/她是从上一个球员的位置开始的,并且继续二项式的search.At,最后的3名球员碰巧通过了桥牌。

所以我想:就像,我口袋里有16欧元,我和p = 1/2玩正面或反面游戏。每次我在头上打赌。如果抛硬币是正面的,那么我赚0,如果是反面,我会损失1欧元。在我的口袋里,打18次头(连续与否)并留下3欧元的概率是多少?

我试图在R中模拟这个问题:

代码语言:javascript
复制
squid_bridge = function(a,n,p) {
  players = a
  while (position > 0 & position < n) {
    jump  =  sample(c(0,1),1,prob=c(1-p,p))
    position = position + jump
  }
  if (position == 0) 
    return(1) 
  else 
    return(0)
}   

n = 18
trials = 100000
a = 16
p = 1/2
set.seed(1)
simlist = replicate(trials, squid_bridge(a, n, p))

这似乎不管用。有什么帮助吗?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2021-10-15 20:45:45

下面是我认为你可以在R中建立游戏模型的方法,第一个版本和你所拥有的相似:猜对的几率是50%,如果猜对了,玩家就会推进一块瓷砖。否则不会,玩家数量减少1。如果玩家数达到0,或者他们前进到最后,游戏结束。在squid_bridge1()中显示了这一点。

代码语言:javascript
复制
squid_bridge1 <- function(players, n, prob) {
  if (players == 0 | n == 18) {
    # All players have died or we have reached the end
    return(players)
  }
  
  jump <- rbinom(1, 1, prob)
  
  if (jump == 0) {
    # Player died
    return(squid_bridge1(players - 1, n, prob))
  }

  if (jump == 1 & n < 18) {
    # Player lives and advances 1 space
    return(squid_bridge1(players, n + 1, prob))
  } 
}

然而,这并不能准确地描述游戏,因为错误的猜测会给剩下的玩家提供更多的信息。如果玩家选择错误,那么下一次猜错的概率不是50%,而是100%。然而,在这一点之后,正确猜测的概率降低到50%。这可以用另一个参数来解释,以跟踪之前猜测的正确性。

代码语言:javascript
复制
squid_bridge2 <- function(players, n, prob, previous) {
  if (players == 0 | n == 18) {
    # The game ends if there are no players or they have reached the end
    return(players)
  }
  
  if (previous == 0) {
    # The previous guess was wrong, but now the players know where to go next
    return(squid_bridge2(players, n + 1, prob, previous = 1))
  }
  
  jump <- rbinom(1, 1, prob)
  
  if (jump == 0) {
    # Player died
    return(squid_bridge2(players - 1, n, prob, previous = 0))
  }
  
  if (jump == 1 & n < 18) {
    # Move is correct. Advance 1 space
    return(squid_bridge2(players, n + 1, prob, previous = 1))
  } 
}

不过,这是个陷阱。在表演中,这并不是很简单,玩家摔倒的原因不是一个错误的猜测(被推,故意跳,等等)。我不知道这样做的概率有多大,但它很可能很低,比如说10%。

代码语言:javascript
复制
not_stupid <- function() {
  x <- runif(1, 0, 1)
  if (x <= 0.1) {
    return(FALSE)
  } else {
    return(TRUE)
  }
}

因为情绪在每次移动之前就会激增,我们将在每次移动之前测试这一点。

代码语言:javascript
复制
squid_bridge3 <- function(players, n, prob, previous) {
  if (players == 0 | n == 18) {
    # The game is over because there are no players left or they reached the end
    return(players)
  }
        
  if (previous == 0) {
    # The previous guess was wrong, but now the players know where to go next
    return(squid_bridge3(players, n + 1, prob, previous = 1))
  }
  
  if (!not_stupid()) {
    return(squid_bridge3(players - 1, n, prob, previous = 1))
  }
  
  jump <- rbinom(1, 1, prob)
  
  if (jump == 0) {
    # Player died because of either choosing wrong or a self-inflicted loss
    return(squid_bridge3(players - 1, n, prob, previous = 0))
  }
  
  if (jump == 1 & n < 18) {
    # Move is correct. Advance 1 space
    return(squid_bridge3(players, n + 1, prob, previous = 1))
  } 
}

然后运行一些模拟:

代码语言:javascript
复制
set.seed(123)
trials <- 10000
players <- 16
squid1 <- replicate(trials, squid_bridge1(players, 0, 0.5))
squid2 <- replicate(trials, squid_bridge2(players, 0, 0.5, 1))
squid3 <- replicate(trials, squid_bridge3(16, 0, 0.5, 1))

df <- tibble(squid1 = squid1,
             squid2 = squid2,
             squid3 = squid3) %>%
  pivot_longer(cols = c(squid1, squid2, squid3))

ggplot(data = df,
       aes(x = value)) +
  geom_histogram(bins = 10,
                 binwidth = 1,
                 fill = "cornflowerblue",
                 color = "black") +
  facet_wrap(~name,
             nrow = 3) +
  xlab("# of players to make it to the end") +
  scale_x_continuous(breaks = seq(0, 16, by = 1),
                     labels = seq(0, 16, by = 1))

如下所示,第一种情况严重偏左。由于玩家对每一块瓷砖基本上都是“盲目猜测”的,所以不太可能有谁能做到这一点。然而,在考虑了从错误的猜测中获得的信息后,它的平均值大约在7名玩家左右。由于另一个原因,通过添加一个随机下降的机会,分布会向左倾斜。

第一情况的

  • 平均值:1.45第二情况的
  • 平均值:第三情况的
  • 平均值: 4.99

为了回答只有3位玩家的概率问题,最后一种情况下我得到了10.8%的答案。

编辑:根据请求,下面是生成情节的代码。我还修复了存在一些命名问题的各种函数(在创建它们时,我经历了几个不同的名称)。看起来它导致了第三个函数的一个小错误,但我一直在修复它。

票数 2
EN

Stack Overflow用户

发布于 2021-11-02 13:34:55

这是一个蒙特卡罗实验,在R返回失败次数的分布。

代码语言:javascript
复制
apply(apply(matrix(rgeom(16*1e6,.5)+1,nc=16),1,cumsum)>18,1,mean)
#with details:
#rgeom(16*1e6,.5)+1 for 16 x 10⁶ geometric simulations when
#the outcome is the number of attempts till "success",
# "success" included
#,1,cumsum) for the number of steps till 16th "success"
#)>18 for counting the cases when a player manages to X the bridge
#1,mean) for finding the probability of each of the players to X

这不是二项式,而是截断负二项分布实验,因为每个玩家所做的新步骤的数目是一个几何Geom(1/2)变量,除非这18个步骤已经完成。因此,幸存者的平均人数是

代码语言:javascript
复制
sum(1-pnbinom(size=1:16,q=17:2,prob=.5))
#Explanation:
#pnbinom is the Negative Binomial cdf
#with size the number of "successes"
#q the integer at which the cdf is computed
#prob is the Negative Binomial probability parameter
#Because nbinom() is calibrated as the number of attempts
#before "success", rather than until "success", the value of
#q decreases by one for each player in the game

其值为7.000076,而不是16-18/2=7

票数 3
EN

Stack Overflow用户

发布于 2021-10-16 21:13:46

0-

代码语言:javascript
复制
##########
# Game ○ △ □ 
##########
squidd7<-function(N_Fields,N_Players,p_new_field){
  Players<-data.frame(id = 1:N_Players, alive=rep(1,N_Players),Field=0)
  
    for(i in 1:N_Players){
      while (Players[i,"alive"]==TRUE && max(Players$Field)< N_Fields) { 
        Players[i,"Field"]=Players[i,"Field"]+1  # Jump onto the next Field
        Players[i,"alive"]=rbinom(1,1,p_new_field)# Fall or repeat
      }
      Players[i+1,"Field"]=Players[i,"Field"] # next player starts where prior player died
    }
  Players<-Players[1:N_Players,] # cosmetic because i do i+1 in the prior line
  # Print me some messages
  if(!any(Players$alive>0)){
      cat("Players loose!")
    } else {
    cat(" \n After", max(Players$Field),"goal was reached! ")
    cat("Players",Players[Players$alive==1,"id"], "survive")
    }
  
  return(Players)
}


squidd7(18,16,0.5)

###########
# simulation ○ △ □
###########
results<-data.frame(matrix(0, nrow = 100, ncol = 20))
for(x in 1:ncol(results)){
     for (i in 1:nrow(results)) {
    Players<-squidd7(x+7,16,0.5)
    results[i,x]<-sum(Players$alive)
  }
}
###########
## Results ○○□□○ △ □
sdt<-apply(results,2,sd) # standart devation 
mn<-apply(results,2,mean) # ○ △ □

boxplot(results,xlab ="n Steps ",names = 8:27,ylab="N Survivors of 16 ")
points(mn,type="l")
points(sdt,type="l")

colors<-colorRampPalette(c(rgb(0,1,0,0.4),
                           rgb(1,1,0,0.4),
                           rgb(1,0,0,0.4)), alpha = TRUE)(21)


plot(density(results$X1),type="n",xlim=c(-1,17),ylim=c(0,0.30),
     main="○ △ □ ",
     sub="○ △ □ ○ △ □ ○ △ □",
     xlab="number of survivors")
for( i in 1:21){
polygon(density(results[,i]),col= colors[i])
}
legend(15,0.31,title="Steps",legend=8:28,fill=colors,border = NA,
       y.intersp = 0.5,
       cex = 0.8, text.font = 0.3)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69585019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档