You are on page 1of 2

Multivariate Monte Carlo Integration

YIK LUN, KEI


R
2
exp(x ) |cos(x)| dx

h<-function(x){
exp(-x^2) * abs(cos(x))
}
integrate(h,-Inf,Inf)
## 1.402376 with absolute error < 0.00014

set x =

y
2



R
y
y 2

exp(( 2 ) ) cos( 2 ) |Jacobian| dy





R
y
y
1

2 exp( 2 ) cos( 2 ) dy

x<-rnorm(1e6,0,1)
sqrt(pi)*mean(abs(cos(x/sqrt(2))))
## [1] 1.40162
R 10
2
2 exp(x ) |cos(x)| dx

integrate(h,2,10)
## 0.002417228 with absolute error < 1e-04
u<-runif(1e6,2,10)
8*mean(h(u))
## [1] 0.00240397
R R
1 2
exp( 2 (x

+ (y 1)2

1
10 x(y

1)))dxdy

h <- function(x,y) { exp(-0.5 * (x^2 + (y - 1)^2 - x*(y-1)/10) ) }


x.llim <- -Inf
x.ulim <- Inf
y.llim <- -Inf
y.ulim <- Inf
integrate(function(y) {
sapply(y, function(y) {
integrate(function(x) h(x,y), x.llim, x.ulim)$value
})
}, y.llim, y.ulim)
## 6.291054 with absolute error < 0.00016

set z = y 1
2

R R 1
x2 +z 2
xz
2 exp( 2 )exp( 20 )dxdz

x<-rnorm(1e6)
y<-rnorm(1e6)
2*pi * mean(exp(x*y/20))
## [1] 6.291219
R 11 R 16
1 2
3 2 exp( 2 (x

+ (y 1)2

1
10 x(y

1)))dxdy

h <- function(x,y) { exp(-0.5 * (x^2 + (y - 1)^2 - x*(y-1)/10) ) }


x.llim <- 2
x.ulim <- 16
y.llim <- 3
y.ulim <- 11
integrate(function(y) {
sapply(y, function(y) {
integrate(function(x) h(x,y), x.llim, x.ulim)$value
})
}, y.llim, y.ulim)
## 0.004316921 with absolute error < 5.9e-08
ux<-runif(1e6,2,16)
uy<-runif(1e6,3,11)
14*8*mean(h(ux,uy))
## [1] 0.004217709

You might also like