Stratified sampling attempts to reduce the variance by dividing the interval into chunks or strata - then estimating the integral for each stratum. You want to use this in situations where the means of the strata are widely dispersed, and not use them where the means of the strata are fairly equal. Here is the example shown in the book:
#number of replicates
M <- 20
#number of strata
T2 <- numeric(4)
estimates <- matrix(0,10,2)
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)))
T2[1] <- mean(g(runif(M/4,0,0.25)))
T2[2] <- mean(g(runif(M/4,0.25,0.5)))
T2[3] <- mean(g(runif(M/4,0.5,0.75)))
T2[4] <- mean(g(runif(M/4,0.75,1)))
estimates[i,2] <- mean(T2)
}
#show the means and variance
apply(estimates,2,mean)
## [1] 0.5038243 0.5233842
apply(estimates,2,var)
## [1] 0.0054250527 0.0001736827
Here is the principle shown with an integral of \(x^2 + 1\) from 0 to 1 and with 10 intervals and 100 replicates.
#number of replicates
M <- 100
#number of strata
T2 <- numeric(10)
estimates <- matrix(0,100,2)
g <- function(x){
(x^2 + 1) * (x>0)*(x<1)}
for (i in 1:100) {
estimates[i,1] <- mean(g(runif(M)))
T2[1] <- mean(g(runif(M/10,0,0.1)))
T2[2] <- mean(g(runif(M/10,0.1,0.2)))
T2[3] <- mean(g(runif(M/10,0.2,0.3)))
T2[4] <- mean(g(runif(M/10,0.3,0.4)))
T2[5] <- mean(g(runif(M/10,0.4,0.5)))
T2[6] <- mean(g(runif(M/10,0.5,0.6)))
T2[7] <- mean(g(runif(M/10,0.6,0.7)))
T2[8] <- mean(g(runif(M/10,0.7,0.8)))
T2[9] <- mean(g(runif(M/10,0.8,0.9)))
T2[10] <- mean(g(runif(M/10,0.9,1)))
estimates[i,2] <- mean(T2)
}
#show the means and variance
apply(estimates,2,mean)
## [1] 1.337399 1.332924
apply(estimates,2,var)
## [1] 9.890219e-04 1.072109e-05