Simulation

We will now simulate some data from a Binomial distribution in order to compare the sampling distribution to the theoretical distribution. The next bit of code will simulate a random sample of size 500 from a Binomial(10,0.5) distribution, but you are invited to change the parameters.

> restart: with(plots):

> n:=10: p:=0.5: numsim:=500:

> sample:=BinomialS(n,p,numsim):

> a:=Histogram(sample,-0.5..n+0.5,n+1):

> b:=ProbHist(BinomialPDF(n,p,x),0..n,n+1):

> display([a,b],title="Simulated vs. Theoretical");

[Maple Plot]

Now let's compare the simulated mean to the theoretical mean.

> theo_mean:=n*p;

[Maple Math]

> sim_mean:=evalf(Mean(sample));

[Maple Math]

> relative_error:=(sim_mean-theo_mean)/theo_mean;

[Maple Math]

>