Section 7.2 Binomial Distribution
Consider a sequence of n independent Bernoulli trials with the likelihood of a success p on each individual trial stays constant from trial to trial with 0<p<1. If we let the variable X measure the number of successes obtained when doing a fixed number of trials n with R={0,1,...,n}, then the resulting distribution of probabilities is called a Binomial Distribution.
xxxxxxxxxx
# Binomial distribution over 0 .. n
# Probability of success on one independent trial = p must also be given
var('x')
def _(n=slider(3,50,1,3),p=slider(1/20,19/20,1/20,1/2)):
np1 = n+1
R = range(np1)
f(x) = factorial(n)/(factorial(x)*factorial(n-x))*p^x*(1-p)^(n-x)
pretty_print(html('Density Function: $f(x) =%s$'%str(latex(f(x)))))
pretty_print(html('over the space $R = %s$'%str(R)))
G = points((k,f(x=k)) for k in R)
G.show()
R = [k for k in R]
probs = [f(x=k) for k in R]
# H = histogram( R, weights = probs, align="mid", linewidth=2, edgecolor="blue", color="yellow")
# H.show()
for k in R:
pretty_print(html('$f(%s'%k+') = %s'%latex(f(x=k))+' \\approx %s$'%f(x=k).n(digits=5)))
You can of course get specific values and graph the Binomial Distribution using R as well...
xxxxxxxxxx
n <- 10
p <- 0.3
paste('Probability Function')
dbinom(0:n, n, p) # gives the probability function
paste('Distribution function')
pbinom(0:n, n, p) # gives the distribution function
paste('A random sample')
rbinom(15, n, p) # gives a random sample of 15 items from b(n,p)
x <- dbinom(0:n, size=n, prob=p)
barplot(x,names.arg=0:n, main=sprintf(paste('n=',n,' and p= ',p)))
Proof
Since successive trials are independent, then the probability of X successes occurring within n trials is given by
Theorem 7.2.2 Verification of Binomial Distribution Formula
Proof
Using the Binomial Theorem with a = p and b = 1-p yields
Utilize the interactive cell below to compute f(x) and F(x) for the Binomial distribution
xxxxxxxxxx
# Binomial calculator
def _(p=input_box(0.3,width=15),n=input_box(10,width=15)):
R = range(n+1)
f(x) = binomial(n,x)*p^x*(1-p)^(n-x)
acc = 0
for k in R:
prob = f(x=k)
acc = acc+prob
pretty_print('f(%s) = '%k,' %.8f'%prob,' and F(%s) = '%k,' %.8f'%acc)
Theorem 7.2.3 Binomial Distribution Statistics
For the Binomial Distribution
Proof
For the mean,
Using the change of variables \(k=x-1\) and \(m = n-1\) yields a binomial series
For the variance,
Using the change of variables \(k=x-2\) and \(m = n-2\) yields a binomial series
The skewness and kurtosis can be found similarly using formulas involving E[X(X-1)(X-2)] and E[X(X-1)(X-2)(X-3)]. The complete determination is performed using Sage below.
The following uses Sage to determine the general formulas for the Binomial distribution.
xxxxxxxxxx
var('x,n,p')
assume(x,'integer')
f(x) = binomial(n,x)*p^x*(1-p)^(n-x)
mu = sum(x*f,x,0,n)
M2 = sum(x^2*f,x,0,n)
M3 = sum(x^3*f,x,0,n)
M4 = sum(x^4*f,x,0,n)
pretty_print('Mean = ',mu)
v = (M2-mu^2).factor()
pretty_print('Variance = ',v)
stand = sqrt(v)
sk = ((M3 - 3*M2*mu + 2*mu^3)).factor()/stand^3
pretty_print('Skewness = ',sk)
kurt = (M4 - 4*M3*mu + 6*M2*mu^2 -3*mu^4).factor()/stand^4
pretty_print('Kurtosis = ',(kurt-3).factor(),'+3')
Flipping Coins
Suppose you flip a coin exactly 20 times. Determine the probability of getting exactly 10 heads and then determine the probability of getting 10 or fewer heads.
SolutionThis is binomial with n = 20, p = 1/2 and you are looking for f(10). With these values
Notice, the mean for this distribution is also 10 so one might expect 10 heads in general. Next, to determine the probability for 10 or fewer heads requires F(10) = f(0) + f(1) + ... + f(10). There is no "nice" formula for F but this calculation can be performed using a graphing calculator, such as the TI-84 with F(x) = binomcdf(n,p,x). In this case, F(10) = binomcdf(20,1/2,10) = 0.588.