plot(x    = 0:20, 
     y    = dbinom(x    = 0:20, 
                   size = 20, 
                   prob = .5
     ), 
     type = 'h',
     main = 'Binomial Distribution (n=20, p=0.5)',
     ylab = 'Probability',
     xlab = '# Successes',
     lwd  = 3
)

dbinom(x=0:20, size=20, prob=.5)
##  [1] 9.536743e-07 1.907349e-05 1.811981e-04 1.087189e-03 4.620552e-03
##  [6] 1.478577e-02 3.696442e-02 7.392883e-02 1.201344e-01 1.601791e-01
## [11] 1.761971e-01 1.601791e-01 1.201344e-01 7.392883e-02 3.696442e-02
## [16] 1.478577e-02 4.620552e-03 1.087189e-03 1.811981e-04 1.907349e-05
## [21] 9.536743e-07
sum(dbinom(x=0:20, size=20, prob=.5))
## [1] 1
round(x= sum(dbinom(x=9:12, size=20, prob=.5)), digits=4)
## [1] 0.6167
dbinom(x=9:12, size=20, prob=.5)
## [1] 0.1601791 0.1761971 0.1601791 0.1201344
sum(dbinom(x=9:12,size=20, prob=.5))
## [1] 0.6166897
#CDF Approach pbinom

pbinom(q=12, size=20, prob=.5,lower.tail = TRUE)-pbinom(q=8, size=20, prob=.5,lower.tail = TRUE)
## [1] 0.6166897
pbinom(q=12, size=20, prob=.5,lower.tail = FALSE)-pbinom(q=8, size=20, prob=.5,lower.tail = FALSE)
## [1] -0.6166897
#A quality control inspector has drawn a sample of 13 light bulbs from a recent  
#production lot.  Suppose 20% of the bulbs in the lot are defective.  What is the  
#probability that less than 6 but more than 3 bulbs from the sample are defective?   
  
#Sample size =13
#defective bulb % =20
#Probability >3 and <6
#Binomial Distribution
plot(x    = 0:13, 
     y    = dbinom(x    =0:13, 
                   size = 13, 
                   prob = .2
     ), 
     type = 'h',
     main = 'Binomial Distribution size n=13)',
     ylab = 'Probability',
     xlab = '# Defective Successes',
     lwd  = 3
)

x<-4:5
#sum(dbinom(x,13,.20))
round(x= sum(dbinom(x, size=13, prob=.2)), digits=4)
## [1] 0.2226
#The auto parts department of an automotive dealership sends out a mean of 4.2 special 
#orders daily.  What is the probability that, for any day, the number of special orders sent out 
#will be no more than 3?  
??poisson
## starting httpd help server ... done
##
#P(X<=3)=P(X=0)+P(X=1)+P(X=2),+P(X=3)\
#/lambda=4.2
#P(X=less than or equal to 3)



range=0:15

plot(x=range,
     y=dpois(x=range,lambda = 4.2),
     main = 'Poisson Distribution (lambda = 4.2)',
     ylab = 'Probability',
     xlab = '# Successes',
     type = 'h',
     lwd  = 3
     )

sum(dpois(x=0.3,lambda = 4.2))
## Warning in dpois(x = 0.3, lambda = 4.2): non-integer x = 0.300000
## [1] 0
#Note: I am getting error Warning message:
#In dpois(x = 0.3, lambda = 4.2) : non-integer x = 0.300000

round(dpois( x=range,lambda=4.2),digits = 4)
##  [1] 0.0150 0.0630 0.1323 0.1852 0.1944 0.1633 0.1143 0.0686 0.0360 0.0168
## [11] 0.0071 0.0027 0.0009 0.0003 0.0001 0.0000
ppois(q=3,lambda=4.2,lower.tail = F)
## [1] 0.6045966
1-ppois(q=3,lambda=4.2,lower.tail = F)
## [1] 0.3954034