蒙特卡罗积分使用重要抽样给出提议函数

时间:2016-12-05 22:49:14

标签: r sampling montecarlo integral numerical-integration

给出拉普拉斯分布建议:

g(x) = 1/2*e^(-|x|)

和样本大小n = 1000,我想进行蒙特卡罗(MC)积分估算θ:

enter image description here

通过重要性抽样。最后,我想在R到达那里后计算这个MC估计值的平均值和标准差。

编辑(在以下答案后迟到)

到目前为止,这就是我的R代码:

library(VGAM)
n = 1000
x = rexp(n,0.5)
hx = mean(2*exp(-sqrt(x))*(sin(x))^2)
gx = rlaplace(n, location = 0, scale = 1)

1 个答案:

答案 0 :(得分:2)

enter image description here

现在我们可以编写一个简单的R函数来从Laplace分布中进行采样:

## `n` is sample size
rlaplace <- function (n) {
  u <- runif(n, 0, 1)
  ifelse(u < 0.5, log(2 * u), -log(2* (1 - u)))
  }

还要编写拉普拉斯分布密度函数:

g <- function (x) ifelse(x < 0, 0.5 * exp(x), 0.5 * exp(-x))

现在,你的被积函数是:

f <- function (x) {
  ifelse(x > 0, exp(-sqrt(x) - 0.5 * x) * sin(x) ^ 2, 0)
  }

现在我们使用1000个样本估算积分(set.seed用于再现性):

set.seed(0)
x <- rlaplace(1000)
mean(f(x) / g(x))
# [1] 0.2648853

还与使用正交的数值积分进行比较:

integrate(f, lower = 0, upper = Inf)
# 0.2617744 with absolute error < 1.6e-05