# 超幾何分布 (Hypergeometric distribution)

(1)
\begin{align} f(x) = \frac{{r \choose x} {N-r \choose{n-x}} }{N \choose n} \end{align}
• 意義：N 個球中有白球有 r 個，黑球 N-r 個，取出 n 個球，其中有 x 個白球的機率; (取後不放回)
• R 函數： hyper(m,n,k) = p(x) = choose(m, x) choose(n, k-x) / choose(m+n, k)
(2)
\begin{align} P(X=x; m, n, k) = \frac{{m \choose x} {n \choose{k-x}} }{{m+n} \choose k} \end{align}

# 特性

(3)
\begin{eqnarray} 1. && E[X] = k (\frac{m}{m+n}) \\ 2. && Var(X) = k (\frac{n}{m+n}) (\frac{m}{m+n}) (\frac{m+n-k}{m+n-1}) \end{eqnarray}

# 動差生成函數

(4)
$$m_x(t) = ???$$

# R 程式範例一

m=10; n=5; k=8
x=seq(0,10)
plot(x, dhyper(x, m, n, k), type='h', main='dhyper(m=10,n=5,k=8)', xlab='x')


# R 程式範例二

m <- 10; n <- 7; k <- 8
x <- 0:(k+1)
rbind(phyper(x, m, n, k), dhyper(x, m, n, k))
all(phyper(x, m, n, k) == cumsum(dhyper(x, m, n, k)))# FALSE
## but error is very small:
signif(phyper(x, m, n, k) - cumsum(dhyper(x, m, n, k)), digits=3)


> m <- 10; n <- 7; k <- 8
> x <- 0:(k+1)
> rbind(phyper(x, m, n, k), dhyper(x, m, n, k))
[,1]         [,2]       [,3]     [,4]      [,5]      [,6]      [,7]
[1,]    0 0.0004113534 0.01336898 0.117030 0.4193747 0.7821884 0.9635952
[2,]    0 0.0004113534 0.01295763 0.103661 0.3023447 0.3628137 0.1814068
[,8]       [,9] [,10]
[1,] 0.99814891 1.00000000     1
[2,] 0.03455368 0.00185109     0
> all(phyper(x, m, n, k) == cumsum(dhyper(x, m, n, k)))# FALSE
[1] FALSE
> ## but error is very small:
> signif(phyper(x, m, n, k) - cumsum(dhyper(x, m, n, k)), digits=3)
[1]  0.00e+00  0.00e+00  1.73e-18  0.00e+00 -5.55e-17  1.11e-16  2.22e-16
[8]  2.22e-16  2.22e-16  2.22e-16
>