【问题标题】:Minimum probability in RR中的最小概率
【发布时间】:2022-01-14 08:51:33
【问题描述】:

我正在尝试找出 r 中抽样生日为 0.9 (90%) 的最小人数

我正在尝试通过采样和两个 for 循环来做到这一点:

我认为我的预期结果是大约 300 人

bus = 2
#start person count at 2
count = 0
# create counter to count birthdays equal to jan first#assume no february 29th leap year
for (i in 1:400){
sims = 1000
for (i in 1:sims) {
  bday = sample(1:365, bus, replace = TRUE) #randomly sample birthdays, producing bus number of samples
  if (bday == 1) { # to see if there are two identical birthdays add to counter
    count= count + 1
  }
}
p= count/sims
print(p)
    if(p<0.9){bus = bus+1}
    if (p >= 0.9){bus = bus}
}
print(bus)

【问题讨论】:

  • 您好,不太清楚您要做什么。你能澄清一下这个问题吗?如果这是一个概率问题,它可能不需要for 循环。无论如何,如果您要使用for,则此代码通常存在许多问题(即,两个for 循环都使用i,您实际上并没有用i 指定循环中的位置(例如,bday[i]),并且您不会在 for 循环之前初始化变量。

标签: r counter simulation probability


【解决方案1】:

如果您正在搜索模拟(理论上回答这个问题会容易得多,因为它是一个二项式定律,您必须在公共汽车上找到大约 839 人),这个适合:

  birthday_searched <- sample(1:NDAYS,1)
  
  NDAYS <- 365
  sims <- 10000
  people_number <- 830:850
  probabilities <- c()
  for (i in people_number){
    save <- c()
    for (j in 1:sims) {
      bday = sample(1:NDAYS, i, replace = TRUE) #randomly sample birthdays, producing bus number of samples
      save <- c(save, birthday_searched %in% (unique(bday)))
    }
    m <- mean(save)
    probabilities <- c(probabilities,m)
  }
  names(probabilities) <- people_number
  plot(probabilities,type="l")
  abline(h=0.9, col="red")
  first_ok <- c(first_ok,names(which(probabilities>=0.9))[1])
  sprintf("Number of people in the bus for p=0.9 : %s",names(which(probabilities>=0.9))[1])

解释:公共汽车上挤满了生日随机的人。在巴士内,有一定数量的独特生日。取经典年份中的随机日期,将该日期作为公交车上某人生日的概率是 1/唯一生日数。

如果您想要更强的估计,您可以添加更多的模拟人生。

编辑:更全面的过程

【讨论】:

    猜你喜欢
    • 2021-10-13
    • 2018-10-27
    • 2022-01-05
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多