Skip to content

Instantly share code, notes, and snippets.

@GuangchuangYu
Created February 29, 2012 06:40
Show Gist options
  • Save GuangchuangYu/1938586 to your computer and use it in GitHub Desktop.
Save GuangchuangYu/1938586 to your computer and use it in GitHub Desktop.
The antithetic variates method is a variance reduction technique used in Monte Carlo methods.
## Antithetic sampling reframes our estimate as a sum of negatively
## correlated random variables, using the fact that negative
## correlation reduces the variance of a sum.
## http://en.wikipedia.org/wiki/Antithetic_variates
g <- function(x) 1/(1+x)
N <- 1500
n <- 50
u1 <- matrix(runif(2 * n * N), ncol = n)
theta1 <- colMeans(g(u1))
u2 <- matrix(runif(n * N), ncol = n)
theta2 <- 0.5 * (colMeans(g(u2)) + colMeans(g(1 - u2)))
var1 <- var(theta1)
var2 <- var(theta2)
res <- data.frame(Estimate=c(mean(theta1),mean(theta2)),
Variance=c(var1,var2))
rownames(res) <- c("Classical Estimate", "Antithetic Variates")
print(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment