rcode_example - c(mean(new.x), sqrt(var(new.x)/m) )...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
m<-1000 set.seed(1000) ### Generate m N(0,1) variables (mean is known, variance is unknown) x<-rnorm(m, 0, 1) c(mean(x^2), sqrt(var(x^2)/m)) > [1] 0.9630190 0.0418624 ### Antithetic variables c(mean(c(x, -x)^2), sqrt(var(c(x, -x)^2)/m/2) ) > [1] 0.96301899 0.02959378 ### Control variates (Suppose Y=X^4 has known expectation E(Y)=3) y<-x^4 b<-cov(x^2, y)/var(y) new.x<-x^2-b*(x^4-3)
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: c(mean(new.x), sqrt(var(new.x)/m) ) &gt; [1] 1.00942431 0.02028024 ### Stratified sampling ind&lt;-rbinom(m, 1, 0.5) x1&lt;- abs(rnorm(sum(ind==1), 0, 1)) x2&lt;- -abs(rnorm(sum(ind==0), 0, 1)) (mean(x1^2)+mean(x2^2))/2 &gt; [1] 0.9999946 ### Importance sampling (Simulate an exp distribution with mean 2) x&lt;-rexp(m, 1) ratio&lt;-0.5*exp(-x/2)/exp(-x) mean(x*ratio) &gt; [1] 1.946928...
View Full Document

Ask a homework question - tutors are online