给定建议函数,使用重要性抽样进行蒙特卡洛积分 [英] Monte Carlo integration using importance sampling given a proposal function

查看:114
本文介绍了给定建议函数,使用重要性抽样进行蒙特卡洛积分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出Laplace分配建议:

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

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

通过重要性抽样.最终,我想在到达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)

解决方案

现在我们可以编写一个简单的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

Given a Laplace Distribution proposal:

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

and sample size n = 1000, I want to Conduct the Monte Carlo (MC) integration for estimating θ:

via importance sampling. Eventually I want to calculate the mean and standard deviation of this MC estimate in R once I get there.


Edit (arrived late after the answer below)

This is what I have for my R code so far:

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)

解决方案

Now we can write a simple R function to sample from Laplace distribution:

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

Also write a function for density of Laplace distribution:

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

Now, your integrand is:

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

Now we estimate the integral using 1000 samples (set.seed for reproducibility):

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

Also compare with numerical integration using quadrature:

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

这篇关于给定建议函数,使用重要性抽样进行蒙特卡洛积分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆