我正试图解决以下问题:
设Z_n是n个标准正常观测的最大值。估计n应该是多少,使P(Z_n>4)=0.25
我已经尝试了下面的代码,并且我知道答案是关于n=9000的,因为它大约返回0.25。我应该修改我的代码,以便n是输出,而不是输入。
n=9000
x1 <- sapply(1:n, function(i){max(rnorm(n=n,0,1))})
length(x1[x1>4])/length(x1)我怎么能这么做?
谢谢你的帮助!
发布于 2020-04-19 03:22:57
你可以选择合适的范围然后做二进制搜索。请记住,结果将取决于样品和RNG种子的数量。
Zn <- function(n) {
max(rnorm(n))
}
Sample <- function(N, n) {
set.seed(312345) # sample same sequence of numbers
x <- replicate(N, Zn(n))
sum( x > 4.0 )/N
}
P <- 0.25
BinarySearch <- function(n_start, n_end, N) {
lo <- n_start
hi <- n_end
s_lo <- Sample(N, lo)
s_hi <- Sample(N, hi)
if (s_lo > P)
return(list(-1, 0.0, 0.0)) # wrong low end of interval
if (s_hi < P)
return(list(-2, 0.0, 0.0)) # wrong high end of interval
while (hi-lo > 1) {
me <- (hi+lo) %/% 2
s_me <- Sample(N, me)
if (s_me >= P)
hi <- me
else
lo <- me
cat("hi = ", hi, "lo = ", lo, "S = ", s_me, "\n")
}
list(hi, Sample(N, hi-1), Sample(N, hi))
}
q <- BinarySearch(9000, 10000, 100000) # range [9000...10000] with 100K points sampled
print(q[1]) # n at which we have P(Zn(n)>4)>=0.25
print(q[2]) # P(Zn(n-1)>4)
print(q[3]) # P(Zn(n)>4)因此,我
9089
0.24984
0.25015这看起来很合理。虽然很慢..。
https://stackoverflow.com/questions/61290213
复制相似问题