Simulation

We will now simulate some data from an discrete uniform 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 Discrete Uniform distribution on the set {1,2,3,...,20}, but you are invited to change the parameters.

> restart: with(plots):

> n:=20: numsim:=500:

> sample:=DiscUniformS(1..20,numsim):

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

> b:=plot(1/n,0..n+1,color=blue):

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

[Maple Plot]

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

> theo_mean:=n/2;

[Maple Math]

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

[Maple Math]

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

[Maple Math]