{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

rcode_example

# rcode_example - c(mean(new.x sqrt(var(new.x/m>[1...

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

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)
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) ) > [1] 1.00942431 0.02028024 ### Stratified sampling ind<-rbinom(m, 1, 0.5) x1<- abs(rnorm(sum(ind==1), 0, 1)) x2<- -abs(rnorm(sum(ind==0), 0, 1)) (mean(x1^2)+mean(x2^2))/2 > [1] 0.9999946 ### Importance sampling (Simulate an exp distribution with mean 2) x<-rexp(m, 1) ratio<-0.5*exp(-x/2)/exp(-x) mean(x*ratio) > [1] 1.946928...
View Full Document

{[ snackBarMessage ]}