The hypergeometric distribution is a probability distribution that’s very similar to # binomial distribution. the binomial distribution is a very good approximation of #the hypergeometric distribution as long as you are sampling 5% or less of the # population.

K is the number of successes in the population 6

k is the number of observed successes 11

N is the population size 17

n is the number of draws 3

x_dhyper<-0:17
y_dhyper<-dhyper(x= x_dhyper,m=6,n=17-6,k=3)
y_dhyper
##  [1] 0.24264706 0.48529412 0.24264706 0.02941176 0.00000000 0.00000000
##  [7] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
## [13] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
# plot dhyper values
plot(x= x_dhyper,
     y=y_dhyper,
     type='h',
     main='Hypergeometric contaminated distribution(S=6,F=11,n=3)',
     ylab='Probability',
     xlab='# Success',
     lwd=4
     )

#Note PMF falls to 0 after X>3 , as expected.
# The prob of Prob(X=0)=0.24264,Prob(X=1)=0.485,Prob(X=2)=0.24264,Prob(X=3)=0.02941176
dhyper(x=x_dhyper,
       m=6,
       n=17-6,
       k=3
       )
##  [1] 0.24264706 0.48529412 0.24264706 0.02941176 0.00000000 0.00000000
##  [7] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
## [13] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
sum(dhyper(x=0:1,
           m=6,
           n=17-6,
           k=3
           )
    )
## [1] 0.7279412
# standard cdf
phyper(q=1,
       m=6,
       n=17-6,
       k=3,
       lower.tail=T
       )
## [1] 0.7279412
#cdf(upper tail/Complement of CDF)
1-phyper(q=1,
       m=6,
       n=17-6,
       k=3,
       lower.tail=F
       )
## [1] 0.7279412

#A town recently dismissed 6 employees in order to meet their new budget reductions. #The town had 6 employees over 50 years of age and 19 under 50. If the dismissed #employees were selected at random, what is the probability that more than 1 employee #was over 50? Round your answer to four decimal places. #N=19+6=25 #n=6 #k=6 #K=25-9=19

x_dhyper <- seq(from = 0, 
                to   = 25, 
                by   = 1
                )
y_dhyper <- dhyper(x = x_dhyper,  # success in sample / event of interest
                   m = 6,         # Success in Population
                   n = 19,        # Failure in Population
                   k = 6.         # sample size
                   )  
plot(x    = x_dhyper,
     y    = y_dhyper,
     type = 'h',
     main = 'Hypergeometric Distribution (S = 6, F = 19, n = 6)',
     ylab = 'Probability',
     xlab = '# Successes',
     lwd  = 2
     ) 

#the probability falls down to zero when x>n  in dhyper arguments.
dhyper(x = 0:25,m = 6,n = 19,k = 6)
##  [1] 1.532016e-01 3.939469e-01 3.282891e-01 1.094297e-01 1.448334e-02
##  [6] 6.437041e-04 5.646527e-06 0.000000e+00 0.000000e+00 0.000000e+00
## [11] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## [16] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## [21] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## [26] 0.000000e+00
sum(dhyper(x = 2:6,     # success in sample / event of interest
           m = 6,       # Success in Population
           n = 19,      # Failure in Population
           k = 6        # Sample Size
           ) 
    )    
## [1] 0.4528515
1 - phyper(q = 1,
           m = 6,
           n = 19,
           k = 6, 
           lower.tail = T # P[X≤x]
           )  
## [1] 0.4528515
 phyper(q = 1,
           m = 6,
           n = 19,
           k = 6, 
           lower.tail = F # P[X>x]
           )  
## [1] 0.4528515