# The function "prob.mass(x,prob)" draws the actual probability mass
# function on the top of histogram.
prob.mass = function(x,prob,...){
  for(i in 1:length(x)){
    polygon(c(x[i]-0.5,x[i]-0.5,x[i]+0.5,x[i]+0.5),
            c(0,prob[i],prob[i],0),...)
  }
}
# We can generate a sample of size 200 out of binomial
# distribution with p = 0.5 and n = 5. Here n represents
# the number of trials in one experimentation. Then draw
# 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)
print(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,lwd=2,border='red')




