Simulation
Simulation
We will now simulate some data from a negative 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 Negative Binomial( r = 4, p = 0.3) distribution, but you are more than welcome to change the parameters.
> restart: with(plots):
> r:=4: p:=0.3: numsim:=500:
> sample:=NegBinomialS(r,p,numsim):
> a:=Histogram(sample,0.5..30+0.5,30):
> b:=ProbHist(NegBinomialPDF(r,p,x),1..30,30):
> display([a,b],title="Simulated(red) vs. Theoretical(blue)");
Now let's compare the simulated mean to the theoretical mean.
> theo_mean:=r/p;
> sim_mean:=evalf(Mean(sample));
> relative_error:=(sim_mean-theo_mean)/theo_mean;