Law of Large Numbers

LLN Simulation

Simulate a large number of iid random variables. Then compute a list of the sample means (((Subscript[X, 1])+(Subscript[X, 2])+⋯+(Subscript[X, n]))/n) for n=1,2,3.... A plot shows how the sample mean converges to the theoretical mean value.

num=100000;
dist = BinomialDistribution[8,.3];
μ=Mean[dist];
vals=RandomVariate[dist,num];
means=Table[Mean[vals[[1;;n]]],{n,1,num}];
Print["Generated ",num," values from ",dist,"; individual mean is ",μ,"."]

ListPlot[means,PlotRange->{μ-0.3,μ+0.3}]

Problem 4 from class

Note that μ=(1/2) is the expected value of the sample mean. We will write a function that computes P(|((Subscript[T, n])/n)-μ|≥ϵ) for given parameters n and ϵ. For some reason the function evaluates much more quickly if we replace |((Subscript[T, n])/n)-μ|≥ϵ with (n/2)-n ϵ<(Subscript[T, n])<(n/2)+n ϵ and take the complement of the probability.

wlln[n_,ϵ_]:=1-Probability[(n/2)-n*ϵ<Tn<(n/2)+n*ϵ,Tn\[Distributed]GammaDistribution[n,(1/2)]]

Try it out:

wlln[100,0.01]

Now we will compute a list of values and make a plot.

vals=Table[wlln[n,0.01],{n,1,10000}];

ListPlot[vals,Joined->True,DataRange->{100,20000},AxesLabel->{"n","probability"}]

To find the smallest n such that P(|((Subscript[T, n])/n)-μ|≥0.01)<0.01, we will use trial and error. Using a loop and an if statement can help us find the desired value of n quickly.

Do[
val=wlln[n,0.01];
If[val<0.01,Print[{n,val}];Break[]],
{n,100,20000,100}]

Do[
val=wlln[n,0.01];
If[val<0.01,Print[{n,val}];Break[]],
{n,16500,16600}]

It looks like n=16589 is the first value of n such that P(|((Subscript[T, n])/n)-μ|≥0.01)<0.01. Let’s confirm this:

wlln[16588,0.01]

wlln[16589,0.01]