我正在尝试用一个简单的蒙特卡罗抽样程序来回答以下问题:一个骨灰盒包含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)
}这是我的第二个更简单的方法,它显示了我对repeat循环缺乏理解。不要费心运行它--它只会永远持续下去。但如果有人能帮我更好地理解原因,我将不胜感激!
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)
}发布于 2019-04-15 07:10:46
这是我创建的一个循环。如果您愿意,您可以将其包装在一个函数中。我不是给球编号,而是用字母。
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.22https://stackoverflow.com/questions/55680629
复制相似问题