Simulation

We will now simulate some data from an Gamma 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 Gamma(2,3) distribution, but you are invited to change the parameters.

> restart: with(plots):

> alpha:=2: beta:=3: n:=500:

> sample:=GammaS(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:=26:

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

> b:=plot(GammaPDF(alpha,beta,x),x=0..26,color=black):

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

[Maple Plot]

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

> theo_mean:=alpha*beta;

[Maple Math]

> sim_mean:=Mean(sample);

[Maple Math]

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

[Maple Math]