R: monte carlo integration using selections of importance

I have an integral for estimating

      "x^(-0.5)" ; x in [0.01,1] 

for which I use MC: The theory states that an approximate PDF should be used to calculate the expected value (which almost certainly converges to the average value of the integral)

After building this integral and exponential PDF, based only on the charts, I have chosen   rexpand dexpto create a PDF file, and my code looks like this:

#Without Importance Sampling
set.seed(1909)
X <- runif(1000,0.01,1)
Y <- X^(-0.5)
c( mean(Y), var(Y) )

#Importance sampling Monte Carlo
w <- function(x) dunif(x, 0.01, 1)/dexp(x,rate=1.5)
f <- function(x) x^(-0.5)
X= rexp(1000,rate=1.5)
Y=w(X)*f(X)
c( mean(Y), var(Y) )

Can someone please confirm the correctness of my line of thought? If not, how should I approach this differently? Please explain - I understood the theory, but the implementation seems problematic for me.

For integrals that are not so simple,

1.) f (x)= [1 + sinh (2x) ln (x)] ^ - 1 PDF = g (x) ( = 0,5 SD = 5) . , , , , NaN . ( undefined, , ).

2.) f (x, y)= exp (-x ^ 4 - y ^ 4)

g (x, y) ?

+4
1

, , , . 20% rexp(1000, 1.5) 1. dexp(x, rate=1.5) [0,1]. pexp(1, rate=1.5). , :

#Importance sampling Monte Carlo
w <- function(x) dunif(x, 0.01, 1)/dexp(x,rate=1.5) * pexp(1, rate=1.5)
f <- function(x) x^(-0.5)
X <- rexp(1000,rate=1.5)
X <- X[X<=1]
Y <- w(X)*f(X)
c(mean(Y), var(Y))

. X , , NA log (X). , 0,5 . :

#Without Importance Sampling
set.seed(1909)
X <- runif(1000,0.01,1)
Y <- (1+sinh(2*X)*log(X))^(-1)
c(mean(Y), var(Y))

#Importance sampling Monte Carlo
w <- function(x) dunif(x, 0.01, 1)/dnorm(x, mean=0.5, sd=0.25) * (1-2*pnorm(0, mean=0.5, sd=0.25))
f <- function(x) (1+sinh(2*x)*log(x))^(-1)
X <- rnorm(1000, mean=0.5, sd=0.25)
Y1 <- w(X)
Y2 <- f(X)
Y <- Y1*Y2
Y <- Y[!(is.na(Y2)&Y1==0)]
c(mean(Y), var(Y))

, y. ? .

EDIT: . (1) 1. dexp(x, rate=1.5) [0,1], pexp(1, rate=1.5).

dexp01 <- function(x, rate){
  dexp(x, rate=rate)/pexp(1, rate=rate)
}

1:

integrate(dexp, 0, 1, rate=1.5)
integrate(dexp01, 0, 1, rate=1.5)

. , . [0.3,8], :

dexp0.3_8 <- function(x, rate){
  dexp(x, rate=rate)/(pexp(8, rate=rate)-pexp(0.3, rate=rate))
}
integrate(dexp0.3_8, 0.3, 8, rate=1.5)

(2) , 95% rnorm(1000, .5, .25) 0 1 ( , , ). , . - , . CrossValidated. .

+4

Source: https://habr.com/ru/post/1529142/


All Articles