> For the complete documentation index, see [llms.txt](https://kmis.gitbook.io/statistics/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kmis.gitbook.io/statistics/chapter-4.-discrete-random-variables/4-6.-hypergeometric-distribution.md).

# 4-6. Hypergeometric Distribution

## 1. Hypergeometric Distribution

**Hypergeometric distribution**, in [statistics](https://www.britannica.com/science/statistics), [distribution function](https://www.britannica.com/science/distribution-function) in which selections are made from two groups without replacing members of the groups. The hypergeometric distribution differs from the [binomial distribution](https://www.britannica.com/science/binomial-distribution) in the lack of replacements. Thus, it often is employed in random [sampling](https://www.britannica.com/science/sampling-statistics) for [statistical quality control](https://www.britannica.com/science/statistics/Residual-analysis#ref60723). A simple everyday example would be the random selection of members for a team from a [population](https://www.britannica.com/science/population-biology-and-anthropology) of girls and boys.

> In symbols, let the size of the population selected from be $$N$$ , with $$r$$ elements of the population belonging to one group (for convenience, called **successes**) and $$(N − r )$$ belonging to the other group (called **failures)**. Further, let the **number of samples** drawn from the population be $$n$$ , such that $$0 ≤ n ≤ N$$ . Then the probability ( $$P$$ ) that the number ( $$X$$ ) of elements drawn from the successful group is equal to some number ( $$x$$ ) is given by
>
> &#x20;                              $$P(X=x) = \frac { \binom{r}{x}\binom{N-r}{n-x} }{ \binom{N}{n}}$$       $$: X \sim HG(n, N, r)$$&#x20;
>
> using the notation of [binomial coefficients](https://www.britannica.com/science/combinatorics/Problems-of-enumeration#ref21879), or, using [factorial](https://www.britannica.com/science/factorial) notation,
>
> &#x20;                  $$P(X=x) = \frac{n!r!(N-n)!(N-r)!}{N!x!(r-x)!(n-x)!(N-r-n+x)!}$$        (hypergeometric factorial formula)

## 2. Mean and Variance

> The [mean](https://www.britannica.com/science/mean) of the hypergeometric distribution is  $$\mu = n \* P(success) = n \frac{r}{N}$$ , &#x20;
>
> and the [variance](https://www.britannica.com/topic/variance) (square of the [standard deviation](https://www.britannica.com/topic/standard-deviation)) is $$\sigma = \frac{nr(N − r)(N − n)}{N^2(N − 1)}$$ .
>
> &#x20;        $$Var(X) = np(1-p) \frac{N-n}{N-1}$$ , if $$p = \frac{r}{n}$$&#x20;

**EXAMPLE 15.** A batch of 100 piston rings is known to contain 10 defective rings. If two piston rings are drawn from the batch, write down the probabilities that:&#x20;

1. the first ring is defective;&#x20;
2. the second ring is defective given that the first one is defective.

**\[ Solution ]**

1. 10/ 100 = 1/10
2. 9/99 = 1/11

**EAXMPLE 16.** A batch of 10 rocker cover gaskets contains 4 defective gaskets. If we draw samples of size 3 without replacement, from the batch of 10, find the probability that a sample contains 2 defective gaskets. And Find the expectation and variance of samples.

**\[ Solution ]**

* $$P(X=x) = \frac { \binom{r}{x}\binom{N-r}{n-x} }{ \binom{N}{n}}$$,  $$N =10,  n =3, r=4 \space and \space x=2$$ => \
  $$P(X=2) = \frac {\_4C\_2 \* *6C\_1} {*{10}C\_3} =  \frac {6\*6}{120} = 0.3$$&#x20;

{% tabs %}
{% tab title="R Source" %}

```
library(Rstat)

# 1. Success (r), Sample Size (n)
N <- 10; n <- 3; r <- 4; x <- 0:n

# 2. Probability Distribution
fx <- dhyper(x, r, N-r, n)
fx

# 3. E(X), Var(X)
disc.exp(x, fx)

# 4. Plot
disc.exp(x, fx, plot=TRUE)
```

{% endtab %}

{% tab title="Probability Distribution" %}

```
> # 2. Probability Distribution
> fx <- dhyper(x, r, N-r, n)
> fx
## [1] 0.16666667 0.50000000 0.30000000 0.03333333
```

{% endtab %}

{% tab title="E(X) and Var(X)" %}

```
> # 3. E(X), Var(X)
> disc.exp(x, fx)        # E(X), Var(X) and Plot
## E(X) = 1.2 
## V(X) = 2 - 1.2² = 0.56 
## D(X) = √(0.56) = 0.748 
```

{% endtab %}

{% tab title="Plot" %}
![](https://2234305379-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lqmt1Wp54aKlZ5eVcaS%2F-LskeSNQbsaZAp_Byui2%2F-LsklHppA52MULKZnu9l%2Fimage.png?alt=media\&token=6c77cfa9-8b63-4745-81fa-339e8559274a)
{% endtab %}
{% endtabs %}

**EXAMPLE 17.** In the manufacture of car tyres, a particular production process is know to yield 10 tyres with defective walls in every batch of 100 tyres produced. From a production batch of 100 tyres, a sample of 4 is selected for testing to destruction. Find:&#x20;

1. the probability that the sample contains 1 defective tyre&#x20;
2. the expectation of the number of defectives in samples of size 4&#x20;
3. the variance of the number of defectives in samples of size 4.

**\[ Solution ]**

* $$P(X=x) = \frac { \binom{r}{x}\binom{N-r}{n-x} }{ \binom{N}{n}}$$,  $$N =100,  n =4, r=10 \space and \space x=1$$&#x20;

1. $$P(X=1) = \frac { *{10}C\_1 \* *{(100-10)}C*{(4-1)} } {*{100}C\_4} = \frac {10 \* 117480 }{3921225} ≈ 0.3$$&#x20;
2. $$E(X) = np = 4 \* 0.1 = 0.4$$&#x20;
3. $$V(X) = np(1-p)\frac {N-M}{N-1} = 0.4 \* 0.9 \* \frac {90}{99} ≈0.33$$&#x20;

{% tabs %}
{% tab title="R Source" %}

```
library(Rstat)

# 1. Success (r), Sample Size (n)
N <- 100; n <- 4; r <- 10; x <- 0:n

# 2. Probability Distribution
fx <- dhyper(x, r, N-r, n)
fx

# 3. E(X), Var(X) & Plot
disc.exp(x, fx, plot=TRUE)
```

{% endtab %}

{% tab title="Prob. Distribution" %}

```
> # 2. Probability Distribution
> fx <- dhyper(x, r, N-r, n)
> fx
## [1] 6.516305e-01 2.996003e-01 4.596140e-02 2.754241e-03 5.355469e-05
```

{% endtab %}

{% tab title="E(X) and Var(X)" %}

```
> disc.exp(x, fx, plot=TRUE) 
## E(X) = 0.4 
## V(X) = 0.509 - 0.4² = 0.349 
## D(X) = √(0.349) = 0.591
```

{% endtab %}

{% tab title="Plot" %}
![](https://2234305379-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lqmt1Wp54aKlZ5eVcaS%2F-Lskz4OebN1uC9KnbKL9%2F-Lsl-VHCnXqmg0k919-L%2Fimage.png?alt=media\&token=ee0e4135-2197-4bae-b9b3-fdfebbe836da)
{% endtab %}
{% endtabs %}

## 3. Using R

초기하분포의 밀도 함수, 누적분포 함수, 분위수 함수, 난수 발생을 위한 R 함수 및 모수는 아래와 같습니다.

| **구분**  |   | **초기하분포 R 함수 / 모수**                         |
| ------- | - | ------------------------------------------- |
| 밀도 함수   | d | dhyper(x, m, n, k)                          |
| 누적분포 함수 | p | phyper(q, m, n, k, lower.tail = TRUE/FALSE) |
| 분위수 함수  | q | qhyper(p, m, n, k, lower.tail = TRUE/FALSE) |
| 난수 발생   | r | rhyper(nn, m, n, k)                         |

**참고 :** 모집단이 m과 n의 개체로 구성되어 있는데 k개의 표본을 추출.            lower.tail = TRUE 이면 확률변수 x를 기준으로 왼쪽 꼬리를 의미

### &#x20;3-1. Random Number Generation & Plotting

* Random Number Generation : `rhyper(nn, m, n, k)`
* Plotting : `dhyper(x, m, n, k)`

&#x20;m=5, n=20 인 초기하분포에서 비복원으로 4개를 추출하는 것을 1000번 모의실험한 후에 도수분포표를 구해보겠습니다.

{% tabs %}
{% tab title="R Source" %}

```
# random number generation
random_hyper <- rhyper(100, m=5, n=20, k=4) ; random_hyper

# frequency table
y <- table(random_hyper)

# plotting
plot(y)
```

{% endtab %}

{% tab title="Random Number" %}

```
> random_hyper <- rhyper(1000, m=5, n=20, k=4) ; random_hyper
   [1] 1 0 1 0 1 1 1 2 0 0 1 0 1 0 0 1 0 2 0 0 1 3 2 0 1 0 0 
   ... 중
```

{% endtab %}

{% tab title="Plot" %}
![](https://2234305379-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lqmt1Wp54aKlZ5eVcaS%2F-Lr2E3ignSh6t1ldhaBr%2F-Lr2EXjSbLOeoIB2hlmM%2Fimage.png?alt=media\&token=6e0e0323-65b5-42c5-9dd8-6a591d804a2d)
{% endtab %}
{% endtabs %}

### 3-2. Probability Computation

#### 1) $$P(X = 4)$$ 확률 계산 :  dhyper(x, m, n, k)

**EXAMPLE 18.** 어떤 바리스타가 아메리카노 향 냄새를 맡아보기만 하면 "콜롬비아 원두"로 만든 것인지 아닌지를 맞출 수 있다고 주장하였다고 합니다.  그래서 그 바리스타를 데려다가 실험을 해보았습니다.  "콜롬비아 원두"로 만든 아메리카노 5잔 (m=5), 콜롬비아 원두 말고 다른 지역 원두로 만든 아메리카노 20잔 (n=20) 을 만들어 놓고 그 바리스타에게 "콜롬비아 원두"로 만든 아메리카노 5잔(k)을 골라내 보라고 시켰습니다.  이때 "콜롬비아 원두"로 만든 아메리카노를 4잔(x) 골라낼 확률은?

**\[ Solution ]**

* m : "콜롬비아 원두"로 만든 아메리카노 5잔 (원하는 결과 대상)
* n : 다른 지역 원두로 만든 아메리카노 20잔 (원하지 않는 결과 대상)
* k : 골라내는 커피 5잔 (시행횟수)
* x : 원하는 결과의 횟수 (4잔)

\=> $$P(X=4) = dhyper(x=4, m=5, n=20, k=5)$$&#x20;

{% tabs %}
{% tab title="R Source" %}

```
dhyper(x=4, m=5, n=20, k=5)
```

{% endtab %}

{% tab title="Result" %}

```
> dhyper(x=4, m=5, n=20, k=5)
[1] 0.001882176
```

{% endtab %}
{% endtabs %}

**EXAMPLE 19.**  TV를 생산하는 제조회사에서 생산한 TV 100 대 중에서 품질이 양호한 TV가 95대, 불량품이 5대가 재고창고에 들어있다고 합니다.  이 재고 창고에서 TV 10개를 비복원추출한다고 했을 때 불량품이 3개가 포함되어 있을 확률은?

**\[Solution]**

* m : 불량품의 대수 5대
* n : 양호한 TV 대수 95대
* k : 10대 비복원추출
* x : 불량품이 3대

\=> $$dhyper(3, m=5, n=95, k=10)$$&#x20;

{% tabs %}
{% tab title="R Source" %}

```
dhyper(3, m=5, n=95, k=10)
```

{% endtab %}

{% tab title="Result" %}

```
> dhyper(3, m=5, n=95, k=10)
[1] 0.006383528
```

{% endtab %}
{% endtabs %}

#### 2) $$P(X<=4)$$

* `phyper(x, m, n, k, lower.tail=TRUE)`  : lower.tail=TRUE 사용

**EXAMPLE 20.** **EXAMPLE 18**.에서 4잔 이하일 확률을 구하라.

**\[ Solution ]**

\=> $$phyper(4, m=5, n=20, k=5, lower.tail=TRUE)$$&#x20;

{% tabs %}
{% tab title="R Source" %}

```
phyper(4, m=5, n=20, k=5. lower.tail=TRUE)
```

{% endtab %}

{% tab title="Result" %}

```
> phyper(4, m=5, n=20, k=5, lower.tail=TRUE)
[1] 0.9999812
```

{% endtab %}
{% endtabs %}

* 또는 $$P(X<=4) = P(X=1) + P(X=2) +P(X=3) + P(X=4)$$&#x20;

{% tabs %}
{% tab title="R Source" %}

```
sum(dhyper(x=c(0:4), m=5, n=20, k=5)
```

{% endtab %}

{% tab title="Second Tab" %}

```
> sum(dhyper(x=c(0:4), m=5, n=20, k=5))
[1] 0.9999812
```

{% endtab %}
{% endtabs %}

#### 3-3.  특정 확률에 해당하는 분위수 구하기

`qhyper(p, m, n, k, lower.tail = TRUE/FALSE)`<br>

**EXAMPLE 21.** **EXAMPLE 18**. 에서 확률이 0.03576134가 되는 시행횟수를 구하라.

**\[ Solution ]**

{% tabs %}
{% tab title="R Source" %}

```
qhyper(p=0.03576134, m=5, n=20, k=5, lower.tail=FALSE)
```

{% endtab %}

{% tab title="x\*" %}

```
> qhyper(p=0.03576134, m=5, n=20, k=5, lower.tail=FALSE)
[1] 3
```

{% endtab %}
{% endtabs %}

**EXAMPLE 22.**  누적확률이 0.998099가 되는 시행횟수를 구하라.

**\[ Solution ]**

{% tabs %}
{% tab title="R Source" %}

```
qhyper(p=0.03576134, m=5, n=20, k=5, lower.tail=TRUE)
```

{% endtab %}

{% tab title="x\*" %}

```
> qhyper(p=0.998099, m=5, n=20, k=5, lower.tail=TRUE)
[1] 3
```

{% endtab %}
{% endtabs %}

**EXAMPLE 23.** 총 50개의 개체로 구성되며, 각각 10개, 20개, 40개의 성공개체가 잇는 세 종류의 유한모집단에서 10개의 표본을 취하였을 때, 성공개수의 확률분포를 구하여 비교하라.

**\[ Solution ]**

{% tabs %}
{% tab title="R Source" %}

```
library(Rstat)

# 1. Success (r), Sample Size (n)
N <- 50; n <- 10; r <- c(10, 25, 40); x <- 0:n

# Make empty list
fx <- list()

# Compute the Probability Distribution of each p
for (i in 1:3) fx[[i]] <- dhyper(x, r[i], N-r[i], n)
# Summation of Each fx
sapply(fx, sum)

# E(X), Var(X), and Plot using disc.mexp()
mt2 <- paste0("HG(10, 50, ", r, ")")   # Title of the plot
disc.mexp(x, fx, mt=mt2)               # E(X), Var(X) and Plot
```

{% endtab %}

{% tab title="E(X) and Var(X)" %}

```
> disc.mexp(x, fx, mt=mt2)               # E(X), Var(X) and Plot
## E(X) = 2         V(X) = 5.306 - 2² = 1.306      D(X) = √(1.306) = 1.143 
## E(X) = 5         V(X) = 27.041 - 5² = 2.041     D(X) = √(2.041) = 1.429 
## E(X) = 8         V(X) = 65.306 - 8² = 1.306     D(X) = √(1.306) = 1.143
```

{% endtab %}

{% tab title="Plot" %}
![](https://2234305379-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lqmt1Wp54aKlZ5eVcaS%2F-Lsl0ThKSNi27TsWbGIb%2F-Lsl228BfFulsvjiGL7n%2Fimage.png?alt=media\&token=7c290be2-4413-47c7-bea7-36b6a9f3e821)
{% endtab %}
{% endtabs %}

**EXAMPLE 24.** 불량률이 5%이고 1,000개의 제품으로 구성된 Lot에서 30개의 표본을 추출하였을 때 나오는 불량품의 갯수를 $$X$$ 라 할 때, 다음을 구하시오.

1. X의 확률분포함수
2. $$E(X)$$ 와 $$Var(X)$$&#x20;
3. $$P(X=3)$$&#x20;
4. $$P(X\le 3)$$&#x20;

**\[ Solution ]**

{% tabs %}
{% tab title="R Source" %}

```
library(Rstat)

# 1. Success (r), Sample Size (n)
N <- 1000; n <- 30; r <- 50; x <- 0:n

fx <- dhyper(x, r, N-r, n); fx

# E(X), Var(X), and Plot using disc.exp()
disc.exp(x, fx, plot=TRUE)      
```

{% endtab %}

{% tab title="Prob. Distribution" %}

```
> fx <- dhyper(x, r, N-r, n); fx
##  [1] 2.096813e-01 3.415005e-01 2.631628e-01 1.277323e-01 4.385615e-02 1.134096e-02 2.296361e-03
##  [8] 3.737028e-04 4.978342e-05 5.501720e-06 5.093527e-07 3.978930e-08 2.636254e-09 1.486683e-10
## [15] 7.151451e-12 2.937066e-13 1.029621e-14 3.076775e-16 7.817675e-18 1.682634e-19 3.052012e-21
## [22] 4.633387e-23 5.835330e-25 6.026622e-27 5.027505e-29 3.319749e-31 1.687139e-33 6.334441e-36
## [29] 1.646611e-38 2.632567e-41 1.939787e-44
```

* $$P(X=3)$$
* $$P(X \le3) = P(X=0) + P(X=1) + P(X=2) + P(X=3) = 0.942$$
  {% endtab %}

{% tab title="E(X) and Var(X)" %}

```
> # E(X), Var(X), and Plot using disc.exp()
> disc.exp(x, fx, plot=TRUE)      
## E(X) = 1.5 
## V(X) = 3.634 - 1.5² = 1.384 
## D(X) = √(1.384) = 1.176 
```

{% endtab %}

{% tab title="Plot" %}
![](https://2234305379-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lqmt1Wp54aKlZ5eVcaS%2F-Lsl2hG2TgesKZve57un%2F-Lsl4589LsDX7RUV3dfu%2Fimage.png?alt=media\&token=904e68ad-bbd9-44b1-9f30-027b1797be46)
{% endtab %}
{% endtabs %}

## 4. Binomial Distribution and Hypergeometric Distribution

### 4.1 Hypergeometric Distribution from Binomial Distribution

**이항 분포의 조건부 분포가 바로 초기하 분포**가 되는 것을 알 수 있습니다.

> 두 확률변수 X와 Y가 서로 독립이고, 이항분포를 따를 때, 확률변수 X+Y에 대한 X의 조건부 분포는 '초기하 분포'를 따른다.

### 4.2 Binomial Distribution from Hypergeometric Distribution

이와 반대로 **초기하 분포에 극한 (N → ∞)을 취하면 이항분포가 됩니다.**

초기하 분포의 성공확률을  $$p= \frac{r}{N}$$ 이라  할 때, 초기하 분포의 확률질량함수

$$P(X=x) = \frac { \binom{r}{x}\binom{N-r}{n-x} }{ \binom{N}{n}} = \frac { *{r}C\_x \times *{(N-r)}C*{(n-x)} } {*{N}C\_n}  = \frac { *{n}C\_x \times *{(N-n)}C*{(r-x)} } {*{N}C\_r}$$ \
&#x20;                      $$= {\_{n}C\_x} \frac{ \frac{(N-n)!}{(r-x)!((N-r)-n+x)!}}{ \frac{N!}{r!(N-r)!}  } = {\_nC\_x} \frac{r!(N-r)!(N-n)! }{N!(r-x)!((N-r)-n+x)!  }$$ \
&#x20;                      $$= {\_nC\_x} \frac{r(r-1) \cdots (r-x+1)(N-r)(N-r-1)\cdots (N-r-n+x+1) }{N(N-1)(N-2)\cdots (N-n+1)}$$&#x20;

분모, 분자를 $$N^n$$ 으로 나누면,

&#x20;              $$= {\_nC\_x} \times \frac{ \frac{r}{N}(\frac{r}{N}-\frac{1}{N}) \cdots (\frac{r}{N} - \frac{x-1}{N})(1- \frac{r}{N})(1-\frac{r}{N} -\frac{1}{N})\cdots (1- \frac{r}{N}-\frac{n-x-1}{N}) }{(1-\frac{1}{N})(1-\frac{2}{N})\cdots (1-\frac{n-1}{N})}$$&#x20;

$$p= \frac{r}{N}$$이고, $$1-p = q$$ 이므로,&#x20;

&#x20;             $$= {\_nC\_x} \times \frac{ p(p-\frac{1}{N}) \cdots (p - \frac{x-1}{N})q(q -\frac{1}{N})\cdots (q-\frac{n-x-1}{N}) }{(1-\frac{1}{N})(1-\frac{2}{N})\cdots (1-\frac{n-1}{N})}$$&#x20;

As$$N \rightarrow \infty$$ ,  $$P(X=x) \rightarrow {\_nC\_x}p^xq^{n-x}  \Rightarrow$$$$Binomial Distribution$$&#x20;

이렇게 초기하 분포에 극한을 취했을 경우 이항 분포가 됨을 보였습니다.

### 4.3 Binomial Distribution and Hypergeometric Distribution

따라서 이항 분포와 초기하 분포는 다음과 같은 관계를 가집니다.

![](https://2234305379-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lqmt1Wp54aKlZ5eVcaS%2F-Ltvzan1M9S7rw46FUvC%2F-Ltw7IMN0C9AtrXMVo-L%2Fimage.png?alt=media\&token=0265d4b8-c706-45c2-9878-91c8c786da7c)

그리고 한 가지 더 알아두셔야 할 사항은 **이항 분포는 '복원 추출'을 전제**로, **초기하 분포는 '비복원 추출'을 전제**로 한다는 것 입니다.
