Simulation

We will now simulate some data from a 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 Uniform(0,1) distribution, but you are invited to change the parameters.

> restart: with(plots):

> alpha:=0: beta:=1: n:=500:

> sample:=UniformS(alpha..beta, n):

Remember that when you plot histograms, the number of bins that you select can make a huge difference in the plot.

> num_bins:=20:

> a:=Histogram(sample,0..1,num_bins):

> b:=plot(UniformPDF(alpha..beta,x),x=alpha..beta,color=black):

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

[Maple Plot]

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

> theo_mean:=int(UniformPDF(alpha..beta,x)*x,x=alpha..beta);

[Maple Math]

> sim_mean:=Mean(sample);

[Maple Math]

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

[Maple Math]