# 4-8. Negative Binomial Distribution (\*)

## 1. Negative Binomial Distribution

기하분포의 개념을 보다 일반화해서 $$r$$ 번째 성공이 발생할 때까지 시행하는 독립시행의 횟수를 $$X$$ 라 하면, 확률변수 $$X$$는 다음과 같은 음이항분포(Negative Binomial Distribution)를 따른다.&#x20;

&#x20;\[ 이를 $$X \sim NB(p, r)$$ 로 표기한다. ]

$$x$$번째 시행에서 $$r$$번째 성공이 발생하려면 그 이전의 $$(x-1)$$ 번의 시행까지 $$(r-1)$$번의 '성공'이 나와야 하고, 마지막 시행에서 '성공'이 나와야 한다.&#x20;

$$P(X=x) = P(x-1 번의 \space 시행 \space중에서 \spacer-1번의\space 성공)P(x번째 \space시행은\space  '성공')$$ \
&#x20;                     $$= \binom{x-1}{r-1} p^{r-1} (1-p)^{x-r} \times p,$$       $$x = r, r+1, r+2, ...$$&#x20;

&#x20;   &#x20;

> 음이항분포(Negative Binomial Distribution)는 성공확률이  일정한 시행에서 주어진 횟수($$r$$)의 성공이 발생할 때까지 시행한 횟수의 확률분포로 다음과 같은 확률밀도함수를 갖는다.
>
> &#x20;             $$f(x) = \binom{x-1}{r-1} p^r (1-p)^{x-r},$$         $$x = r, r+1, r+2, ...$$&#x20;

## 2. Geometric Distribution and Negative Binomial Distribution

$$X\_1, X\_2, ..., X\_n$$ 이 독립이고 동일한 기하분포(Geometric Distribution)를 따르는 확률변수일 때,  $$X = \Sigma \_{i=1}^{r}X\_i$$ 의 분포는 음이항분포(Negative Binomial Distribution)를 따른다.&#x20;

## 3. Expected Value and Variance of Negative Binomial Distribution

> $$E(X) =  E(\Sigma \_{i=1}^{r}X\_i) = r/p,$$&#x20;
>
> $$Var(X) = Var(\Sigma \_{i=1}^{r}X\_i) = r(1-p) / p^2.$$&#x20;

**EXAMPLE 27.** 성공확률이 0.4인 무한모집단에서 각각 1회, 2회, 4회의 성공을 얻을 때까지 시행하였을 때, 시행횟수의 확률분포 그래프를 작성하고, 기대값과 분산을 구하여 비교하라.

**\[ Solution ]**

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

```
library(Rstat)

# p, r, range of x
p <- 0.4; r <- c(1, 2, 4); xr <- 1:100
len <- length(r)

# dnbinom() : failures before the r-th success
fx <- list()
for (i in 1:len) fx[[i]] <- dnbinom(xr - r[i], r[i], p)

# Sum(fx) == 1 ?
sapply(fx, sum)

# Plot
mt <- paste0("Neg-Binomial(0.4,", r, ")")
win.graph(9,3); par(mfrow=c(1,3)); par(mar=c(3,4,4,2))
for (k in 1:len) plot(xr[1:30], fx[[k]][1:30], type="h", 
                      main=mt[k], ylab="f(x)", xlab="", lwd=3, col=2)

# E(X) and Var(X)
disc.mexp(xr, fx, plot=F)
```

{% endtab %}

{% tab title="Plot" %}
![](/files/-LtMoZtKzYFNdcO6R8uU)

* 기하분포처럼 오른쪽 꼬리가 길고, 왼쪽으로 치우친 형태를 나타내지만, 목표 성공횟수( $$r$$ )가 커질수록 치우침이 줄어들고 있음을 알 수 있다.
  {% endtab %}

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

```
> # E(X) and Var(X)
> disc.mexp(xr, fx, plot=F)
## E(XR) = 2.5      V(XR) = 10 - 2.5² = 3.75       D(XR) = √(3.75) = 1.936 
## E(XR) = 5        V(XR) = 32.5 - 5² = 7.5        D(XR) = √(7.5) = 2.739 
## E(XR) = 10       V(XR) = 115 - 10² = 15         D(XR) = √(15) = 3.873
```

* 기대값은 목표 성공횟수에 비례하여 증가함.
* 분산 또한 목표 성공횟수에 비례하여 증가함.
  {% endtab %}
  {% endtabs %}

**EXAMPLE 28.** 성공확률이 각각 0.1인 세 개의 프로젝트를 성공시킬 때까지 기획한 횟수 $$X$$ 에 대하여 다음을 구하시오.

1. 확률분포함수
2. 기대값과 분산
3. 10번째 시도 만에 3 개의 프로젝트를 성공시킬 확률

**\[ Solution ]**

1. $$f(x) = \binom{x-1}{3-1} (0.1)^3 (0.9)^{x-3},$$       $$x = 3, 4, 5, ...$$&#x20;
2. &#x20;$$E(X) =  r/p = 3/0.1 = 30,$$   \
   &#x20;$$Var(X) =r(1-p) / p^2 = 3 \times (0.9) / (0.1) ^ 2 = 270$$&#x20;
3. &#x20; $$f(10) = \binom{10-1}{3-1} (0.1)^3 (0.9)^{10-3}  \doteq 0.017$$&#x20;

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

```
library(Rstat)

# p, r, range of x
p <- 0.1; r <- 3; xr <- 3:250

# dnbinom() : failures before the r-th success
fx <- dnbinom(xr - r, r, p)
disc.exp(xr, fx)

# f(10)? :
xr <- 10
dnbinom(xr-r, r, p)
```

{% endtab %}

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

```
> # dnbinom() : failures before the r-th success
> fx <- dnbinom(xr - r, r, p)
> disc.exp(xr, fx)
## E(XR) = 30 
## V(XR) = 1170 - 30² = 270 
## D(XR) = √(270) = 16.432
```

{% endtab %}

{% tab title="f(10)" %}

```
> # f(10)? :
> xr <- 10
> dnbinom(xr-r, r, p)
## [1] 0.01721869
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kmis.gitbook.io/statistics/chapter-4.-discrete-random-variables/4-8.-negative-binomial-distribution.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
