The Rayleigh density [156, (18.76)] is: \[f(x) = \frac{x}{\sigma^2}e^\frac{-x^2}{2\sigma^2}, x \geq 0, \sigma \geq 0\] Implement a function to generate samples from a Rayleigh(σ) distribution, using antithetic variables. What is the percent reduction in variance of X+X′ compared with \[\frac{X_1 + X_2}{2}\] for independent X1, X2?
library(extraDistr) #Thanks, Keith!
MC.Rayleigh <- function(x, R = 10000, antithetic = TRUE) {
u <- runif(R/2)
if(!antithetic) v <- runif(R/2) else
v <- 1 - u
u <- c(u , v)
cdf <- numeric(length(x))
for (i in 1:length(x)){
g <- x
g <- rrayleigh(u, x[i])
cdf[i] <- mean(g) / sqrt(2 * pi) + 0.5
}
cdf
}
x <- seq(.1, 2.5, length=5)
Phi <- pnorm(x)
set.seed(123)
MC1 <- MC.Rayleigh(x, anti = FALSE)
set.seed(123)
MC2 <- MC.Rayleigh(x)
print(round(rbind(x, MC1, MC2, Phi), 5))
## [,1] [,2] [,3] [,4] [,5]
## x 0.10000 0.70000 1.30000 1.90000 2.50000
## MC1 0.55028 0.85039 1.15649 1.44974 1.74358
## MC2 0.55007 0.85095 1.15597 1.45520 1.74989
## Phi 0.53983 0.75804 0.90320 0.97128 0.99379