问题描述
在昨天《纽约时报》 有一篇文章提到,该论文本质上说出现"后出现出现"的概率不是0.5(假设是一枚硬币),挑战热手"神话.我想向自己证明.
In the New York Times yesterday there was a reference to a paper essentially saying that the probability of 'heads' after a 'head' appears is not 0.5 (assuming a fair coin), challenging the "hot hand" myth. I want to prove it to myself.
因此,我正在编写一个模拟7个抛硬币的代码,并计算第一个头之后的头个数,前提是自然要有一个第一个头.
Thus, I am working on coding a simulation of 7 coin tosses, and counting the number of heads after the first head, provided, naturally, that there is a first head at all.
我想出了以下R代码行,但是我仍在获得NA值,并希望获得一些帮助:
I came up with the following lines of R code, but I'm still getting NA values, and would appreciate some help:
n <- 7 # number of tosses
p <- 0.5 # probability of heads
sims <- 100 # number of simulations
Freq_post_H <- 0 # frequency of 'head'-s after first 'head'
for(i in 1:sims){
z <- rbinom(n, 1, p)
if(sum(z==1)!=0){
y <- which(z==1)[1]
Freq_post_H[i] <- sum(z[(y+1):n])/length((y+1):n)
}else{
next()
}
Freq_post_H
}
Freq_post_H
我想念什么?
结论:在最初出现变量名称不匹配的打h之后,两个响应都解决了这个问题.答案之一是通过引入 min(y + 1,n)
来纠正初始代码中与上一次折腾(i + 1)
有关的问题,并纠正在为跳过的迭代生成 NA
的循环中,对 next
的基本误解.因此,谢谢(+1).
CONCLUSION: After the initial hiccups of mismatched variable names, both responses solve the question. One of the answers corrects problems in the initial code related to what happens with the last toss (i + 1)
by introducing min(y + 1, n)
, and corrects the basic misunderstanding of next
within a loop generating NA
for skipped iterations. So thank you (+1).
至关重要的是,第二个响应附加此结论"的原因是解决了一个更基本或概念性的问题:我们要计算以H开头的H的分数,而不是 p(H)
出现头部后剩下的抛掷次数是多少,一个公平的硬币将为0.5.
Critically, and the reason for this appended "conclusion" the second response addresses a more fundamental or conceptual problem: we want to calculate the fraction of H's that are preceded by a H, as opposed to p(H)
in whatever number of tosses remain after a head has appeared, which will be 0.5 for a fair coin.
推荐答案
这是他们在报纸上所做的模拟:
This is a simulation of what they did in the newspaper:
nsims <- 10000
k <- 4
set.seed(42)
sims <- replicate(nsims, {
x <- sample(0:1, k, TRUE)
#print(x)
sum( # sum logical values, i.e. 0/1
diff(x) == 0L & # is difference between consecutive values 0?
x[-1] == 1L ) / # and are these values heads?
sum(head(x, -1) == 1L) #divide by number of heads (without last toss)
})
mean(sims, na.rm = TRUE) #NaN cases are samples without heads, i.e. 0/0
#[1] 0.4054715
k <- 7
sims <- replicate(nsims, {
x <- sample(0:1, k, TRUE)
#print(x)
sum(diff(x) == 0L & x[-1] == 1L) / sum(head(x, -1) == 1L)
})
mean(sims, na.rm = TRUE)
#[1] 0.4289402
这篇关于模拟抛硬币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!