使用模拟进行重要性采样的覆盖率低于预期

机器算法验证 r 模拟 指数分布 重要性抽样
2022-03-02 04:51:16

我试图回答Evaluate integral with Importance sampling method in R的问题。基本上,用户需要计算

0πf(x)dx=0π1cos(x)2+x2dx

使用指数分布作为重要性分布

q(x)=λ expλx

并找到λ这可以更好地近似积分(它是self-study)。我将问题改写为对平均值的评估μf(x)超过[0,π]: 积分就是πμ.

因此,让p(x)成为的pdfXU(0,π), 然后让Yf(X): 现在的目标是估计

μ=E[Y]=E[f(X)]=Rf(x)p(x)dx=0π1cos(x)2+x21πdx

使用重要性抽样。我在 R 中进行了模拟:

# clear the environment and set the seed for reproducibility
rm(list=ls())
gc()
graphics.off()
set.seed(1)

# function to be integrated
f <- function(x){
    1 / (cos(x)^2+x^2)
}

# importance sampling
importance.sampling <- function(lambda, f, B){
    x <- rexp(B, lambda) 
    f(x) / dexp(x, lambda)*dunif(x, 0, pi)
}

# mean value of f
mu.num <- integrate(f,0,pi)$value/pi

# initialize code
means  <- 0
sigmas <- 0
error  <- 0
CI.min <- 0
CI.max <- 0
CI.covers.parameter <- FALSE

# set a value for lambda: we will repeat importance sampling N times to verify
# coverage
N <- 100
lambda <- rep(20,N)

# set the sample size for importance sampling
B <- 10^4

# - estimate the mean value of f using importance sampling, N times
# - compute a confidence interval for the mean each time
# - CI.covers.parameter is set to TRUE if the estimated confidence 
#   interval contains the mean value computed by integrate, otherwise
# is set to FALSE
j <- 0
for(i in lambda){
    I <- importance.sampling(i, f, B)
    j <- j + 1
    mu <- mean(I)
    std <- sd(I)
    lower.CB <- mu - 1.96*std/sqrt(B)  
    upper.CB <- mu + 1.96*std/sqrt(B)  
    means[j] <- mu
    sigmas[j] <- std
    error[j] <- abs(mu-mu.num)
    CI.min[j] <- lower.CB
    CI.max[j] <- upper.CB
    CI.covers.parameter[j] <- lower.CB < mu.num & mu.num < upper.CB
}

# build a dataframe in case you want to have a look at the results for each run
df <- data.frame(lambda, means, sigmas, error, CI.min, CI.max, CI.covers.parameter)

# so, what's the coverage?
mean(CI.covers.parameter)
# [1] 0.19

该代码基本上是重要性采样的简单实现,遵循此处使用的符号。然后重复重要性采样N多次估计μ,并且每次检查 95% 区间是否覆盖实际平均值。

如您所见,对于λ=20实际覆盖率仅为 0.19。并且越来越B到值,例如106没有帮助(覆盖范围更小,0.15)。为什么会这样?

1个回答

重要性抽样对重要性分布的选择非常敏感。既然你选择了λ=20,您绘制的样本rexp的平均值为1/20有方差1/400. 这是你得到的分布

在此处输入图像描述

但是,您要评估的积分从 0 到π=3.14. 所以你想使用一个λ这给了你这样的范围。我用λ=1.

在此处输入图像描述

使用λ=1我将能够探索0到的完整积分空间π, 并且似乎只有几次平局π将被浪费。现在我重新运行你的代码,只改变λ=1.

# clear the environment and set the seed for reproducibility
rm(list=ls())
gc()
graphics.off()
set.seed(1)

# function to be integrated
f <- function(x){
  1 / (cos(x)^2+x^2)
}

# importance sampling
importance.sampling <- function(lambda, f, B){
  x <- rexp(B, lambda) 
  f(x) / dexp(x, lambda)*dunif(x, 0, pi)
}

# mean value of f
mu.num <- integrate(f,0,pi)$value/pi

# initialize code
means  <- 0
sigmas <- 0
error  <- 0
CI.min <- 0
CI.max <- 0
CI.covers.parameter <- FALSE

# set a value for lambda: we will repeat importance sampling N times to verify
# coverage
N <- 100
lambda <- rep(1,N)

# set the sample size for importance sampling
B <- 10^4

# - estimate the mean value of f using importance sampling, N times
# - compute a confidence interval for the mean each time
# - CI.covers.parameter is set to TRUE if the estimated confidence 
#   interval contains the mean value computed by integrate, otherwise
# is set to FALSE
j <- 0
for(i in lambda){
  I <- importance.sampling(i, f, B)
  j <- j + 1
  mu <- mean(I)
  std <- sd(I)
  lower.CB <- mu - 1.96*std/sqrt(B)  
  upper.CB <- mu + 1.96*std/sqrt(B)  
  means[j] <- mu
  sigmas[j] <- std
  error[j] <- abs(mu-mu.num)
  CI.min[j] <- lower.CB
  CI.max[j] <- upper.CB
  CI.covers.parameter[j] <- lower.CB < mu.num & mu.num < upper.CB
}

# build a dataframe in case you want to have a look at the results for each run
df <- data.frame(lambda, means, sigmas, error, CI.min, CI.max, CI.covers.parameter)

# so, what's the coverage?
mean(CI.covers.parameter)
#[1] .95

如果你玩λ,你会看到,如果你把它做得很小(.00001)或很大,覆盖概率会很差。

编辑 - - - -

关于一旦你离开,覆盖概率就会降低B=104B=106,这只是一个随机事件,基于您使用的事实N=100复制。覆盖概率的置信区间B=104是,

.19±1.96.19(1.19)100=.19±.0769=(.1131,.2669).

所以你不能真的说增加B=106 显着降低覆盖概率。

事实上,在你的代码中为同一个种子,改变N=100N=1000,然后与B=104,覆盖概率为 0.123,并且B=106覆盖概率是.158.

现在,0.123 附近的置信区间为

.123±1.96.123(1.123)1000=.123±.0203=(.102,.143).

因此,现在有了N=1000复制,您会发现覆盖概率显着增加。