Below is an expanded version of the textbook’s demonstration of stratified sampling. I noticed the textbook, presumably in a focus on brevity, often assumes you understand the intracies of the method.
For stratitified sampling, the idea appears to be to break up the integral of the function into several stratum. As you run the monte carlo method on each of the stratum, there will be higher individual variance among the stratum, which allows for more similar (or less disperse and accurate) means, and therefore less variance collectively.
M <- 20
T2 <- numeric(4)
estimates <- matrix(0, 10, 4)
colnames(estimates) <- c("mean", "mean var", "stratified mean", "stratified")
g <- function(x) {
exp(-x - log(1+x^2)) * (x > 0) * (x <1)
}
for (i in 1:10) {
estimates[i, 1] <- mean(g(runif(M)))
estimates[i, 2] <- var(g(runif(M)))
T2[1] <- mean(g(runif(M/4, 0, .25)))
T2[2] <- mean(g(runif(M/4, .25, .5)))
T2[3] <- mean(g(runif(M/4, .5, .75)))
T2[4] <- mean(g(runif(M/4, .75, 1)))
estimates[i,3] <- mean(T2)
estimates[i,4] <- var(T2)
}
estimates
## mean mean var stratified mean stratified
## [1,] 0.4151142 0.04934381 0.5326166 0.08031105
## [2,] 0.5572313 0.05471750 0.5479012 0.07319532
## [3,] 0.4960039 0.04355137 0.4976801 0.06380261
## [4,] 0.5275387 0.08895349 0.5135668 0.06953858
## [5,] 0.5950202 0.06561732 0.5151261 0.07042775
## [6,] 0.5939792 0.07081836 0.5375714 0.07959909
## [7,] 0.5429240 0.04035290 0.5137908 0.07256767
## [8,] 0.5176576 0.07071335 0.5217436 0.08021215
## [9,] 0.5426821 0.06482660 0.5425954 0.07795892
## [10,] 0.5400977 0.09187892 0.5157158 0.06094770
#We can see that each of the stratified means has higher variance than each of the regular means -- which is allowing the stratified means to be more homogenous and driving down their collective variance.
apply(estimates, 2, mean)
## mean mean var stratified mean stratified
## 0.53282488 0.06407736 0.52383079 0.07285608
apply(estimates, 2, var)
## mean mean var stratified mean stratified
## 2.658380e-03 3.081122e-04 2.481643e-04 4.689682e-05