我正在使用接受-拒绝方法进行 beta 分布,g(x) = 1, 0 ≤ x ≤ 1。函数为:f(x) = 100x^3(1-x)^2。
我想创建一个算法来从这个密度函数生成数据。
如何在 k = 1000 次重复 (n=1000) 时估计 P(0 ≤ X ≤ 0.8)?我如何在 R 中解决这个问题?
我已经有了:
beta.rejection <- function(f, c, g, n) {
naccepts <- 0
result.sample <- rep(NA, n)
while (naccepts < n) {
y <- runif(1, 0, 0.8)
u <- runif(1, 0, 0.8)
if ( u <= f(y) / (c*g(y)) ) {
naccepts <- naccepts + 1
result.sample[n.accepts] = y
}
}
result.sample
}
f <- function(x) 100*(x^3)*(1-x)^2
g <- function(x) x/x
c <- 2
result <- beta.rejection(f, c, g, 10000)
for (i in 1:1000){ # k = 1000 reps
result[i] <- result[i] / n
}
print(mean(result))