# Revision history [back]

### Central limit rule-of-thumb for Binomial approximation

Hi MIT proba OpenCourseWare reading-questions-6b first: I think maybe there is a mistake in the answer given for the problem 4: Exact: 0.0227, rule-of-thumb: 0.025 ? second : can someone tell me what is wrong in my raisonning , and how calculate the rule-of-thumb ? third: why I get an error if I uncomment the last line below ?

forget()
#############################
import matplotlib.pyplot as plt
def makeFig(pdfTable,cdfTable,textFig):
# Make a figure
fig = plt.figure()
# Make room for legend at bottom
# The axes for your lists 1-2
# Plot lines 1-3
line1 = ax1.plot([0] + pdfTable,'bo-',label='pdf')
line2 = ax1.plot([0] + cdfTable,'go-',label='cdf')
# To get lines 1-2 on the same legend, we need to
# gather all the lines together before calling legend
lines = line1+line2
labels = [l.get_label() for l in lines]
# giving loc a tuple in axes-coords. ncol=2 for 2 columns
ax1.legend(lines, labels, loc=(0,-0.4), ncol=2)
ax1.set_xlabel('--------'+ textFig)
# Display the figure
plt.show()
return
######################

n=64
kMax=40
p=0.5
kTable=[i for i in range(0,kMax+1)]
pdfTable=[]
cdfTable=[]
#A fair coin is tossed 64 times. Using Binomial Distribution to estimate the probability of getting more that 40 heads.
for k in kTable:
pdfTable.append((binomial(n,k)*p^k *(1-p)^(n-k)))
cdfTable.append(sum(pdfTable))
#pdfTableR = [ round(elem, 5) for elem in pdfTable ]
#show(pdfTableR)
cdfTableR = [ round(elem, 5) for elem in cdfTable ]
show(cdfTableR)
makeFig(pdfTable,cdfTable,'                           P more than 40 Heads On 64 toss')
pSupKmax=1-cdfTable[-1]
show('P(X > 40 Heads) On 64 toss  ')
show('Using  Binomial distribution : ',pSupKmax)

#A fair coin is tossed 64 times. Use the central limit theorem to estimate the probability of getting more that 40 heads.
################################
# for one toss
mu_1=p # Expectation for one Bernouilli
sigma_1=sqrt(p*(1-p)) # Standard deviation for one Bernouilli
# for 64 toss, sum of 64 bernoulli variables
mu_64=n*mu_1 # expectation= sum of the 64 Bernoulli Expectation
sigma_64=sqrt(n)*sigma_1 # Standard Deviation= sum of the 64 Bernoulli standard Deviation
# rule-of-thumb
show('Using  1-erf :', 1-erf( ((kMax-mu_64)/sigma_64) ) )

#############################################################
#using Central Limit Theorem using normal law integration
#############################################################

#from sage.symbolic.integration.integral import indefinite_integral
from sage.symbolic.integration.integral import definite_integral
x = var('x')
assume(x, 'real')

normalPDF = function('normalPDF')(x)
normalCDF = function('normalCDF')(x)
# normal PDF(x,mu_0,sigma_0)
normalPDF=1/(2 *sqrt(pi*sigma_0^2))*e^(-(((x-mu_64)/sigma_64 )^2)/2)
normalCDFnum=definite_integral(normalPDF,x,-infinity,kMax)
show('Using  normal law integration 1-normalCDFnum :',(1-normalCDFnum ))
# error if line below uncommented
#show('Using  normal law integration 1-normalCDFnum :',(1-normalCDFnum ).n())


### Central limit rule-of-thumb for Binomial approximation

Hi MIT proba OpenCourseWare reading-questions-6b first: I think maybe there is a mistake in the answer given for the problem 4: Exact: 0.0227, rule-of-thumb: 0.025 ? second : can someone tell me what is wrong in my raisonning , and how calculate the rule-of-thumb ? third: why I get an error if I uncomment the last line below ?

forget()
#############################
import matplotlib.pyplot as plt
def makeFig(pdfTable,cdfTable,textFig):
# Make a figure
fig = plt.figure()
# Make room for legend at bottom
# The axes for your lists 1-2
# Plot lines 1-3
line1 = ax1.plot([0] + pdfTable,'bo-',label='pdf')
line2 = ax1.plot([0] + cdfTable,'go-',label='cdf')
# To get lines 1-2 on the same legend, we need to
# gather all the lines together before calling legend
lines = line1+line2
labels = [l.get_label() for l in lines]
# giving loc a tuple in axes-coords. ncol=2 for 2 columns
ax1.legend(lines, labels, loc=(0,-0.4), ncol=2)
ax1.set_xlabel('--------'+ textFig)
# Display the figure
plt.show()
return
######################

n=64
kMax=40
p=0.5
kTable=[i for i in range(0,kMax+1)]
pdfTable=[]
cdfTable=[]
#A fair coin is tossed 64 times. Using Binomial Distribution to estimate the probability of getting more that 40 heads.
for k in kTable:
pdfTable.append((binomial(n,k)*p^k *(1-p)^(n-k)))
cdfTable.append(sum(pdfTable))
#pdfTableR = [ round(elem, 5) for elem in pdfTable ]
#show(pdfTableR)
cdfTableR = [ round(elem, 5) for elem in cdfTable ]
show(cdfTableR)
makeFig(pdfTable,cdfTable,'                           P more than 40 Heads On 64 toss')
pSupKmax=1-cdfTable[-1]
show('P(X > 40 Heads) On 64 toss  ')
show('Using  Binomial distribution : ',pSupKmax)

#A fair coin is tossed 64 times. Use the central limit theorem to estimate the probability of getting more that 40 heads.
################################
# for one toss
mu_1=p # Expectation for one Bernouilli
sigma_1=sqrt(p*(1-p)) # Standard deviation for one Bernouilli
# for 64 toss, sum of 64 bernoulli variables
mu_64=n*mu_1 # expectation= sum of the 64 Bernoulli Expectation
sigma_64=sqrt(n)*sigma_1 # Standard Deviation= sum of the 64 Bernoulli standard Deviation
# rule-of-thumb
show('Using  1-erf :', 1-erf( ((kMax-mu_64)/sigma_64) ) )

#############################################################
#using Central Limit Theorem using normal law integration
#############################################################

#from sage.symbolic.integration.integral import indefinite_integral
from sage.symbolic.integration.integral import definite_integral
x = var('x')
assume(x, 'real')

normalPDF = function('normalPDF')(x)
normalCDF = function('normalCDF')(x)
# normal PDF(x,mu_0,sigma_0)
normalPDF=1/(2 *sqrt(pi*sigma_0^2))*e^(-(((x-mu_64)/sigma_64 *sqrt(pi*sigma_64^2))*e^(-(((x-mu_64)/sigma_64 )^2)/2)
normalCDFnum=definite_integral(normalPDF,x,-infinity,kMax)
show('Using  normal law integration 1-normalCDFnum :',(1-normalCDFnum ))
# error if line below uncommented
#show('Using  normal law integration 1-normalCDFnum :',(1-normalCDFnum ).n())