Binomial Distributions
The frequency function of binomial distribution with parameters n and p is given byff = dbinom(0:n, n, p)and a random sample of size k from the distribution is generated by
sample = rbinom(k, n, p)
Here we can generate a relative frequency histogram compared with the frequency function (dashed).
n = 5 p = 0.5 x = 0:n prob = dbinom(x,n,p) prob print(cbind(0:n,prob)) size = 200 sample = rbinom(size,n,p) sample hist(sample, freq=F, breaks=seq(-0.5,n+0.5,by=1.0), ylim=c(0,max(prob)+0.1), col='green') prob.mass(x,prob,lty=2)
Programming Note. The function prob.mass() must be used as defined in subsection earlier. The option density=10 is used to give shading lines.
Sample R code. You can download binomdemo.R, and run it.
The function pbinom() is used to obtain the cumulative distribution function.
x = -1:(n+1) cdf = pbinom(x,n,p) plot(x,cdf,type='s',col='blue') lines(c(-1,n+1),c(1,1),lty=2,col='red')
Programming Note. plot(x,y,type='s') gives a plot in stair steps.
Example. A company has known that their screws is defective with probability 0.01. They sell the screws in packages of 10, and are planning a money-back guarantee: They replace it (a) if a customer find more than one defective screws, or (b) if one defective is found. For each of the money-back guarantee plans (a) and (b), what proportion of packages sold must be replaced?
n = 10 p = 0.01 1 - pbinom(1,n,p) 1 - pbinom(0,n,p)
Sample R code. You can download defective.R, and run it.
© TTU Mathematics