# Stratified Monte Carlo

Consider the integral $I=\int_{0}^{1}e^{-x}dx$.

Now consider the stratifed Monte Carlo estimate $\hat{I^{s}}$, that has $N_{st}=8$ strata. What is the variance of $\hat{I^{s}}$? What is the percent reduction in variance over the simple Monte Carlo estimate?

This follows the problem I asked and solved here: Expected Value and Variance of Monte Carlo Estimate of $\int_{0}^{1}e^{-x}dx$

True value: $\int_{0}^{1}e^{-x}dx=0.6321205588...$

Matlab code for basic Monte Carlo:

m=1000;
N = 1000;
z=zeros(1,m);
for j=1:m
U = rand(1,N);
X = exp(-U);
i=mean(X);
z(j)=i;
end
expectedvalue = mean(z)
variance = var(z)


Results: integral = 0.6382953, variance = 3.1346e-05

Stratified Monte Carlo attempt:

m=1000;
N=1000;
z=zeros(1,m);
for j=1:m
K = 8;
Ni = N/K;
for i = 1 : K
XS = exp(-((i-1+rand(1,Ni))/K));
XSB(i) = mean(XS);
SS(i) = var(XS);
end,
SST = mean(SS/N);
z(j)=mean(XSB);
end
expectedvalue=mean(z)
variance = var(z)


Results: integral = 0.6321, variance = 5.4545e-07

If anyone who knows Matlab can chime in and help out here, I'd be thankful.

-
The code is correct in its current form. –  Chris Mar 2 '13 at 12:23
When you see $10^{-29}$ on a machine with finite precision, think zero. What the first snippet calculates is a rounding error, not a variance. The random variable $U$ is never used, and the variable $x$ is never initialized.
No, it's clearly wrong -- how likely is it that you'd get a mean value that's off by $462$ standard deviations? –  joriki Mar 1 '13 at 7:34
@Chris: The means look OK, but I don't know where the expressions that you're outputting as variances are coming from. The variance in the unstratified case should be $1/1000$ times the variance of a single sample, which I believe should be about $0.08$, so that looks way off. Why aren't you calculating the variance with the standard formula, or even just using var (X)? Also the variance reduction by stratification looks way too good; I think the variance should be reduced roughly by a factor of $8$. –  joriki Mar 1 '13 at 8:54