0% found this document useful (0 votes)
6K views8 pages

Importance Sampling

This document discusses several examples of using importance sampling and variance reduction techniques such as antithetic variates for numerical integration. It provides code to calculate integrals using standard Monte Carlo integration, antithetic variates, and importance sampling. It compares the mean and variance of estimates from these different techniques, showing that importance sampling and antithetic variates often provide estimates with lower variance than standard Monte Carlo integration.

Uploaded by

api-285777244
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6K views8 pages

Importance Sampling

This document discusses several examples of using importance sampling and variance reduction techniques such as antithetic variates for numerical integration. It provides code to calculate integrals using standard Monte Carlo integration, antithetic variates, and importance sampling. It compares the mean and variance of estimates from these different techniques, showing that importance sampling and antithetic variates often provide estimates with lower variance than standard Monte Carlo integration.

Uploaded by

api-285777244
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Importance Sampling and Variance Reduction

YIK LUN, KEI


[email protected]

Example 1: Normalizing Constants


f<-function(x){x*sqrt(2/pi)*exp(-x^2/2)}
g_tilde = function(x) exp(-2*x)
f_tilde = function(x) exp(-x^2/2)
Zg<-1/2
Zf<-sqrt(pi/2)
integrate(f_tilde,0,Inf);Zf

## 1.253314 with absolute error < 0.00012


## [1] 1.253314
n<-1e4;iter<-1e3
x<-runif(n,0,1)
mean(f(x))
## [1] 0.3156687
integrate(f,0,1)

## 0.3139431 with absolute error < 3.5e-15


h = function(x) {x}
E = function(n){
x = rexp(n,rate=2) #sampling from Exponetial dis with rate=2
w_tilde = f_tilde(x)/g_tilde(x)
weight = w_tilde/sum(w_tilde)
I_hat = sum(h(x)*weight)
Zf<-Zg*mean(f_tilde(x)/g_tilde(x))
return(c(I_hat,Zf))
}
E(1e5)
## [1] 0.7967853 1.2515515
integrate(f,0,Inf) #x>0
## 0.7978846 with absolute error < 2.3e-08

Example 2:

R 10
0 exp(2 |x

5|)dx

f<-function(x){ exp(-2*abs(x-5))}
integrate(f,0,10)

## 0.9999546 with absolute error < 2.8e-14


first<-function(n){
X <- runif(n,0,10)
Y <- 10*exp(-2*abs(X-5))#rewrite the integral
return(mean(Y))
}
original<-replicate(iter,first(n))
anti<-function(n){
x<-runif(n,0,10);y<-10-x
return( 0.5 *mean(10*exp(-2*abs(x-5))+10*exp(-2*abs(y-5))))
}
antithetic <-replicate(iter,anti(n))

0.0

0.2

0.4

f(x)

0.6

0.8

1.0

x<-runif(n,0,10)
plot(x,f(x)) # peak at x=5

6
x

10

second<-function(n){
X=rnorm(n,5,1)
Y=10*f(X)*dunif(X,0,10)/dnorm(X,mean=5,sd=1)
return(mean(Y))
}
importance<-replicate(iter,second(n))
mean(original)

## [1] 0.9985911
mean(antithetic)
## [1] 1.000879
mean(importance)

## [1] 0.9998259
var(original)

## [1] 0.0003910023
var(antithetic)
## [1] 0.0004131957
var(importance)

## [1] 3.687052e-05

Example 3:

R 2/pi
sin(x
0

cos(x))dx

h <- function(u) sin(u*cos(u))


integrate(h, 0, 2*pi)

## -1.041727 with absolute error < 3.9e-06


first<-function(n){
unif<- runif(10000, 0, 2*pi)
I<-2*pi*mean(h(unif))
return(I)
}
original<-replicate(iter,first(n))

second<-function(n){
x<- runif(10000, 0, 2*pi);y=2*pi-x
I<-0.5*2*pi*mean(h(x)+h(y))
return(I)
}
antithetic<-replicate(iter,second(n))
mean(original)

## [1] -1.041764
mean(antithetic)
## [1] -1.041069
var(original)

## [1] 0.001211512
var(antithetic)
## [1] 0.0007569473

Example 4:
g=function(x) {
integrate(g,2,5)

exp(-x^2/2)/sqrt(2*pi) }

## 0.02274985 with absolute error < 2.5e-16


first<-function(n){
unif<-runif(1e5,2,5)
I<-(5-2)*mean(g(unif))
return(I)
}
original<-replicate(iter,first(n))
second<-function(n){
x<-runif(1e5,2,5);y<-runif(n,2,5)
I<-0.5*(5-2)*mean(g(x)+g(y))
return(I)
}
antithetic<-replicate(iter,second(n))
x<-rnorm(n)
plot(x,g(x)) # peak at 0

0.4
0.3
0.2
0.0

0.1

g(x)

0
x

third<-function(n){
x<-rnorm(n,0,2)
I<-(5-2)*g(x)*dunif(x,2,5)/dnorm(x,0,2)
return(mean(I))
}
importance<-replicate(iter,third(n))
mean(original)
## [1] 0.0227513
mean(antithetic)
## [1] 0.02274533
mean(importance)
## [1] 0.02277234
var(original)
## [1] 1.477382e-08

var(antithetic)
## [1] 3.679734e-08
var(importance)

## [1] 5.304287e-07

Example 5:
g=function(x) { -23.42056+1.74564*(10^9)*exp(-x/17.004425)*sin(pi*(x+19.88618)/(7.88822*10^8)) }
integrate(g,0,52.2155)

## 2649.597 with absolute error < 2.9e-11


first<-function(n){
unif<-runif(1e5,0,52.2155)
I<-(52.2155)*mean(g(unif))
return(I)
}
original<-replicate(iter,first(n))
second<-function(n){
x<-runif(1e5,0,52.2155);y<-52.2155-x
I<-0.5*(52.2155)*mean(g(x)+g(y))
return(I)
}
antithetic<-replicate(iter,second(n))
x<-c(-1000:1000)
plot(x,g(x),xlim=c(-100,100),ylim=c(-200,200))

200
100
0
200

100

g(x)

100

50

0
x

third<-function(n){
x<-rnorm(n,0,sd=20)
I<-(52.2155)*g(x)*dunif(x,0,52.2155)/dnorm(x,0,sd=20)
return(mean(I))
}
importance<-replicate(iter,third(n))
mean(original)
## [1] 2649.641
mean(antithetic)
## [1] 2649.588
mean(importance)
## [1] 2649.37
var(original)
## [1] 36.24802

50

100

var(antithetic)
## [1] 0.453387
var(importance)
## [1] 736.6437

Example 6:
f<-function(x){x^2*0.5 * exp(-abs(x))}
integrate(f,-Inf,Inf)
## 2 with absolute error < 7.1e-05

0.00

0.10

f(x)

0.20

x<-rnorm(1e5)
plot(x,f(x))

0
x

X <- rnorm(1e5, sd=2)


Y <- (X^2) * .5 * exp(-abs(X))/dnorm(X, sd=2)
mean(Y)
## [1] 2.074182

You might also like