Monte Carlo R 函数帮助寻找概率(来自瓮的球问题)

Monte Carlo R function help for finding probability (balls from urn problem)

我试图在 R 中使用一个简单的 Monte Carlo 采样程序来回答以下问题:一个瓮包含 10 个球。两红三白五黑。所有 10 个都一次抽取一个,不更换。求第一个和最后一个球都是黑色的概率。

我尝试了两种方法,但均无效。

这是对我来说更直观的更长的方法:

balls <- c(1:10) #Consider 1-5 black, 6-8 white, and 9-10 red.

pick.ball <- function(balls){
sample(x = balls, 1, replace = FALSE)
}

experiment <- function(n){
picks = NULL
keep <- NULL
for(j in 1:n){
   for(i in 1:10){
   picks[i] <- pick.ball(balls = balls)
   }
keep[j] <- ifelse(picks[1] == any(1:5) & picks[10] == any(1:5), 1, 0)
 }
return(length(which(keep == 1))/n)
}

这是我的第二种更简单的方法,它表明我对重复循环缺乏理解。不要打扰 运行 它——它只会永远持续下去。但如果有人能帮助我更好地理解原因,那将不胜感激!

balls <- c(1:10) #Consider 1-5 black, 6-8 white, and 9-10 red.

pick.ball <- function(balls, n){
  keep = NULL
  for(i in 1:n){
  picks <- sample(x = balls, 10, replace = FALSE)
  keep[i] <- ifelse(picks[1] == any(1:5) & picks[10] == any(1:5), 1, 0)
  repeat{
    picks
    if(length(keep) == n){
      break
      }
    }
  }
  return(which(keep == 1)/n)
}

这是我创建的循环。如果需要,您可以将其包装在一个函数中。我没有给球编号,而是使用字母。

urn <- c(rep("B", 5), rep("W", 3), rep("R", 2))

# Set the number of times you want to run the loop

nloops <- 10000


# Create an empty data frame to hold the outcomes of the simulation

m <- structure(list(first = character(),
                    last = character(),
                    is_black = integer()),
               class = "data.frame")

现在运行循环


set.seed(456)
for (j in 1:nloops) {
  b <- sample(urn, 10, replace = FALSE)
  m[j, 1:2 ] <- b[c(1, 10)] 
  m[j, 3] <- ifelse(m[j, 1] == "B" & m[j, 2] == "B", 1, 0)
}

head(m)
  first last is_black
1     B    W        0
2     B    B        1
3     B    B        1
4     R    B        0
5     B    R        0
6     R    W        0

终于有了答案:

# Proportion of cases where first and last ball drawn were black

sum(m[ , 3]) / nloops

# This was 0.22