b:head_first_statistics:geometric_binomial_and_poisson_distributions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
b:head_first_statistics:geometric_binomial_and_poisson_distributions [2019/11/07 10:26] – [What does the Poisson distribution look like?] hkimscilb:head_first_statistics:geometric_binomial_and_poisson_distributions [2025/10/15 08:31] (current) – [e.g.,] hkimscil
Line 1: Line 1:
 ====== Geometric Binomial and Poisson Distributions ====== ====== Geometric Binomial and Poisson Distributions ======
 +정리 
 +기하분포, 이항분포, 포아송분포
 +\begin{align*}
 +\text{Geometric Distribution:  } \;\;\; \text{X} & \thicksim Geo(p) \\
 +p(X = k) & = q^{k-1} \cdot p \\
 +E\left[ X \right] & = \frac{1}{p} \\
 +V\left[ X \right] & = \frac{q}{p^2} \\
 +\\
 +\text{Binomial Distribution:  } \;\;\; \text{X} & \thicksim B(n, p) \\
 +p(X = r) & = \binom{n}{r} \cdot p^{r} \cdot q^{n-r} \\
 +E\left[ X \right] & = {n}{p} \\
 +V\left[ X \right] & = {n}{p}{q} \\
 +\\
 +\text{Poisson Distribution:  } \;\;\; \text{X} & \thicksim P( \lambda ) \\
 +P(X=r) & = e^{- \lambda} \cdot \dfrac{\lambda^{r}} {r!} \\
 +E\left[ X \right] & = \lambda \\
 +V\left[ X \right] & = \lambda \\
 +\end{align*}
 +
 ===== Geometric Distributions ===== ===== Geometric Distributions =====
  
Line 27: Line 46:
  
 | X  | P(X=x)  | Power of 0.8  | Power of 0.2  | | X  | P(X=x)  | Power of 0.8  | Power of 0.2  |
-| 1  | 0.2  | 0  | 1  | +| 1  | 0.8<sup>0</sup>0.2   | 0  | 1  | 
-| 2  | 0.8 * 0.2   | 1  | 1  |+| 2  | 0.8<sup>1</sup> * 0.2   | 1  | 1  |
 | 3  | 0.8<sup>2</sup> * 0.2   | 2  | 1  | | 3  | 0.8<sup>2</sup> * 0.2   | 2  | 1  |
 | 4  | 0.8<sup>3</sup> * 0.2   | 3  | 1  | | 4  | 0.8<sup>3</sup> * 0.2   | 3  | 1  |
Line 39: Line 58:
 This formula is called the **geometric distribution**. This formula is called the **geometric distribution**.
    
-  You run a series of independent trials.  +  You run a series of independent trials.   * (각 시행이 독립적임 = 이번 시행이 이전 시행과 상관없이 일어남) 
-  There can be either a success or failure for each trial, and the probability of success is the same for each trial. +  There can be either a success or failure for each trial, and the probability of success is the same for each trial. (성공/실패만 일어나는 상황에서, 성공하는 확률은 p로 실행햇수동안 일정) 
-  The main thing you’re interested in is how many trials are needed in +  The main thing you’re interested in is how many trials are needed in order to get the first successful outcome. (성공하면 중단하고 성공할 때까지의 확률을 분포로 봄)
-order to get the first successful outcome.+
  
 $ P(X=r) = {p \cdot q^{r-1}} $ $ P(X=r) = {p \cdot q^{r-1}} $
Line 55: Line 73:
 ## rather than p * q^(r-1) ## rather than p * q^(r-1)
 dgeom(x = 0:n, prob = p) dgeom(x = 0:n, prob = p)
-hist(dgeom(x = 0:n, prob = p))+hist(dgeom(x = 0:n, prob = p)) 
 +barplot(dgeom(x=0:n, p))
 </code> </code>
  
Line 69: Line 88:
 [29] 0.0003868563 0.0003094850  [29] 0.0003868563 0.0003094850 
  
-> hist(dgeom(x = 0:n, prob = p))</code>+hist(dgeom(x = 0:n, prob = p)) 
 +barplot(dgeom(x=0:n, p))
 </code> </code>
  
-{{:b:head_first_statistics:pasted:20191030-023820.png}} +<code> {{:b:head_first_statistics:pasted:20191030-023820.png}} </code> 
- +{{:b:head_first_statistics:pasted:20251013-080224.png}} 
-r번 시도한 후에 첫번 째 성공을 얻을 확률 +r번 시도한 , 그 이후 어디서든지 간에 성공을 얻을 확률
-첫 번째 성공을 얻을 때까지 r번 이상 시도를 해야하는 확률+
 $$ P(X > r) = q^{r} $$ $$ P(X > r) = q^{r} $$
-20번 시도 후에 어디선가 성공할 확률은?  
-20번까지는 실패하는 확률 = $q^{r} $ 
  
-{{:b:head_first_statistics:pasted:20191031-004954.png}}+예, 20번 시도 후에 어디선가 성공할 확률은? 
  
 +Solution. 
 +  * 21번째 성공 + 22번째 + 23번째 + . . . . 
 +  * 위는 구할 수 없음
 +  * 따라서
 +  * 전체 확률이 1이고 20번째까지 성공한 확률을 1에서 빼면 원하는 확률이 됨
 +  * 1 - (1번째 성공 + 2번째 성공 + . . . + 20번째 성공)
 +  * 그런데 이것은 
 +  * 20번까지는 실패하는 확률 = $q^{r} $ 과 같다
 +<code>
 +p <- .2
 +q <- 1-p
 +n <- 19
 +s <- dgeom(x = 0:n, prob = p)
 +# 20번째까지 성공할 확률을 모두 더한 확률
 +sum(s)
 +# 따라서 아래는 20번 이후 어디서든지 간에서 성공할 확률
 +1-sum(s)
 +## 혹은 (교재가 이야기하는) 20번까지 실패하는 확률
 +q^20
 +</code>
  
 +<code>
 +> p <- .2
 +> q <- 1-p
 +> n <- 19
 +> s <- dgeom(x = 0:n, prob = p)
 +> # 20번째까지 성공할 확률
 +> sum(s)
 +[1] 0.9884708
 +> # 따라서 아래는 20번 이후 어디서든지 간에서 성공할 확률
 +> 1-sum(s)
 +[1] 0.01152922
 +> ## 혹은 (교재가 이야기하는) 20번까지 실패하는 확률
 +> q^20
 +[1] 0.01152922
 +
 +</code>
 +{{:b:head_first_statistics:pasted:20191031-004954.png}}
 +
 +그렇다면 
 r 번 이전에 성공이 있을 확률은? = r 번까지의 실패할 확률의 보수 r 번 이전에 성공이 있을 확률은? = r 번까지의 실패할 확률의 보수
 $$ P(X \le r) = 1 - q^{r} $$ $$ P(X \le r) = 1 - q^{r} $$
 +
 +혹은 1번째 성공 + 2번째 성공 + . . . + r 번째 성공으로 구해도 된다
 +<code>
 +# r = 20 이라고 하면 
 +p <- .2
 +q <- 1-p
 +n <- 19
 +s <- dgeom(x = 0:n, prob = p)
 +sum(s)
 +</code>
 +
 +
 {{:b:head_first_statistics:pasted:20191031-005143.png}} {{:b:head_first_statistics:pasted:20191031-005143.png}}
  
Line 93: Line 161:
  
 ===== Expected value ===== ===== Expected value =====
-Reminding . . . +X가 성공할 확률 p를 가진 Geometric distribution을 따른다  :: $X \sim \text{Geo}(p)$ 
 + 
 +Reminding . . . [[:b/head_first_statistics/using_discrete_probability_distributions#expected_value_discrete_prob|Expected value in discrete probability]]
 $E(X) = \sum x*P(X=x)$ $E(X) = \sum x*P(X=x)$
  
-| textbook  | x  | P(X = x)  | xP(X = x)  | xP(X ≤ x): \\  $E(X) = \sum x*P(X=x)$  |+| textbook  | x  | P(X = x)  | xP(X = x)  | xP(X ≤ x): \\  $E(X) = \sum (x*P(X=x))$  |
 | r code  | trial  | ''px <- q^(trial-1)*p''  | ''npx <- trial*(q^(trial-1))*p''  |  ''plex <- cumsum(trial*(q^(trial-1))*p)''  | | r code  | trial  | ''px <- q^(trial-1)*p''  | ''npx <- trial*(q^(trial-1))*p''  |  ''plex <- cumsum(trial*(q^(trial-1))*p)''  |
 +|     | ''px''   | ''npx <- trial*px''   | ''plex <- cumsum(npx)''   |
 +|     | x번째 (trial번째) \\ 성공할 확률  | x번째의 기대치 \\ (주사위 경우처럼)  | 그 x번째까지 성공할 \\ 확률에 대한 기대값  |
 +
 +  * 우리가 작업하고 있는 채드의 슬로프 타기 예가 얼른 이해가 안된다면 아래 workout의 예를 들어 본다. 
 +
 +^ x  ^ p(x) px ^ npx.0        npx = weighted \\ probability at \\ a given spot  ^ plex.0  ^  plex  ^   ^
 +| 0  | 0.1     | 0 * 0.1   | 0.00   | 0.00  | 0.00  |   |
 +| 1  | 0.15    | 1 * 0.15  | 0.15   | 0.00 + 0.15 | 0.15  |   |
 +| 2  | 0.4     | 2 * 0.4   | 0.80   | 0.00 + 0.15 + 0.80  | 0.95  |   |
 +| 3  | 0.25    | 3 * 0.25  | 0.75   | 0.00 + 0.15 + 0.80 + 0.75  | 1.7  |   |
 +| 4  | 0.1     | 4 * 0.1   | 0.40   | 0.00 + 0.15 + 0.80 + 0.75 + 0.40  | 2.1  | = this is E(x)  |
 +
 +  * x 일주일에 내가 갈 운동횟수 (workout frequency, 0 to 4) 
 +  * px 각 횟수에 대한 probability 
 +  * npx weighted probability 
 +  * plex cumulative sum of npx (to find out the below last one)
 +  * sum of npx = 2.1 = mean of all = expected value of x = E(x)
 +  * https://www.youtube.com/watch?v=qafPcWNUiM8 참조
  
 <code> <code>
 p <- .2 p <- .2
 q <- 1-p q <- 1-p
-trial <- c(1,2,3,4,5,6,7,8)+trial <- c(1:8)
 px <- q^(trial-1)*p px <- q^(trial-1)*p
 px px
-npx <- trial*(q^(trial-1))*p+## npx <- trial*(q^(trial-1))*p 
 +## 위는 아래와 같음
 npx <- trial*px npx <- trial*px
 npx npx
-plex <- cumsum(trial*(q^(trial-1))*p)+## plex <- cumsum(trial*(q^(trial-1))*p
 +## 위는 아래와 같음 
 +plex <- cumsum(npx)
 plex plex
 sumgeod <- data.frame(trial,px,npx,plex) sumgeod <- data.frame(trial,px,npx,plex)
Line 140: Line 231:
  
 </code> </code>
 +
 +  * 아래의 예는 위의 workout 예처럼 횟수가 0-4로 정해져 있지 않고 계속 진행됨 (0-무한대)
 +  * 하지만 여기서는 100 까지로 한정 (1:100)
 +  * 각 지점에서의 probability = geometric probability = q^(trial-1)*p = px
 +  * 각 지점에서의 weighted prob = trial * px = npx 
 +  * 각 단계에서의 기대값을 구하기 위한 누적합계 cumsum(npx) = plex
 +  * 아래 그림에서 plex는 각 단계의 probability density를 더해온 값을 말한다. 
 +  * 그림이 암시하는 것처럼 오른 쪽으로 한 없이 가면서 생기는 그래프의 용적은 기대값이 된다.
 +
 +| {{:b:head_first_statistics:pasted:20251002-072502.png?400}} | 
 +| {{:b:head_first_statistics:pasted:20251002-072514.png?400}} | 
 +| {{:b:head_first_statistics:pasted:20251002-072522.png?400}} | 
  
 <code> <code>
Line 147: Line 250:
 px <- q^(trial-1)*p px <- q^(trial-1)*p
 px px
-npx <- trial*(q^(trial-1))*p+npx <- trial*px
 npx npx
-plex <- cumsum(trial*(q^(trial-1))*p)+## plex <- cumsum(trial*(q^(trial-1))*p
 +## 위는 아래와 같음 
 +plex <- cumsum(npx)
 plex plex
 sumgeod <- data.frame(trial,px,npx,plex) sumgeod <- data.frame(trial,px,npx,plex)
-round(sumgeod,3)+sumgeod  
 plot(npx, type="l") plot(npx, type="l")
 plot(plex, type="l") plot(plex, type="l")
Line 158: Line 264:
  
 <code> <code>
 +
 > p <- .2 > p <- .2
 > q <- 1-p > q <- 1-p
Line 163: Line 270:
 > px <- q^(trial-1)*p > px <- q^(trial-1)*p
 > px > px
-  [1] 2.000000e-01 1plot(plex, type="l").600000e-01 1.280000e-01 1.024000e-01 8.192000e-02 6.553600e-02 5.242880e-02 +  [1] 2.000000e-01 1.600000e-01 1.280000e-01 1.024000e-01 
-  [8] 4.194304e-02 3.355443e-02 2.684355e-02 2.147484e-02 1.717987e-02 1.374390e-02 1.099512e-02 +  [5] 8.192000e-02 6.553600e-02 5.242880e-02 4.194304e-02 
- [15] 8.796093e-03 7.036874e-03 5.629500e-03 4.503600e-03 3.602880e-03 2.882304e-03 2.305843e-03 +  [9] 3.355443e-02 2.684355e-02 2.147484e-02 1.717987e-02 
- [22] 1.844674e-03 1.475740e-03 1.180592e-03 9.444733e-04 7.555786e-04 6.044629e-04 4.835703e-04 + [13] 1.374390e-02 1.099512e-02 8.796093e-03 7.036874e-03 
- [29] 3.868563e-04 3.094850e-04 2.475880e-04 1.980704e-04 1.584563e-04 1.267651e-04 1.014120e-04 + [17] 5.629500e-03 4.503600e-03 3.602880e-03 2.882304e-03 
- [36] 8.112964e-05 6.490371e-05 5.192297e-05 4.153837e-05 3.323070e-05 2.658456e-05 2.126765e-05 + [21] 2.305843e-03 1.844674e-03 1.475740e-03 1.180592e-03 
- [43] 1.701412e-05 1.361129e-05 1.088904e-05 8.711229e-06 6.968983e-06 5.575186e-06 4.460149e-06 + [25] 9.444733e-04 7.555786e-04 6.044629e-04 4.835703e-04 
- [50] 3.568119e-06 2.854495e-06 2.283596e-06 1.826877e-06 1.461502e-06 1.169201e-06 9.353610e-07 + [29] 3.868563e-04 3.094850e-04 2.475880e-04 1.980704e-04 
- [57] 7.482888e-07 5.986311e-07 4.789049e-07 3.831239e-07 3.064991e-07 2.451993e-07 1.961594e-07 + [33] 1.584563e-04 1.267651e-04 1.014120e-04 8.112964e-05 
- [64] 1.569275e-07 1.255420e-07 1.004336e-07 8.034690e-08 6.427752e-08 5.142202e-08 4.113761e-08 + [37] 6.490371e-05 5.192297e-05 4.153837e-05 3.323070e-05 
- [71] 3.291009e-08 2.632807e-08 2.106246e-08 1.684997e-08 1.347997e-08 1.078398e-08 8.627183e-09 + [41] 2.658456e-05 2.126765e-05 1.701412e-05 1.361129e-05 
- [78] 6.901746e-09 5.521397e-09 4.417118e-09 3.533694e-09 2.826955e-09 2.261564e-09 1.809251e-09 + [45] 1.088904e-05 8.711229e-06 6.968983e-06 5.575186e-06 
- [85] 1.447401e-09 1.157921e-09 9.263367e-10 7.410694e-10 5.928555e-10 4.742844e-10 3.794275e-10 + [49] 4.460149e-06 3.568119e-06 2.854495e-06 2.283596e-06 
- [92] 3.035420e-10 2.428336e-10 1.942669e-10 1.554135e-10 1.243308e-10 9.946465e-11 7.957172e-11 + [53] 1.826877e-06 1.461502e-06 1.169201e-06 9.353610e-07 
- [99] 6.365737e-11 5.092590e-11 + [57] 7.482888e-07 5.986311e-07 4.789049e-07 3.831239e-07 
-> npx <- trial*(q^(trial-1))*p+ [61] 3.064991e-07 2.451993e-07 1.961594e-07 1.569275e-07 
 + [65] 1.255420e-07 1.004336e-07 8.034690e-08 6.427752e-08 
 + [69] 5.142202e-08 4.113761e-08 3.291009e-08 2.632807e-08 
 + [73] 2.106246e-08 1.684997e-08 1.347997e-08 1.078398e-08 
 + [77] 8.627183e-09 6.901746e-09 5.521397e-09 4.417118e-09 
 + [81] 3.533694e-09 2.826955e-09 2.261564e-09 1.809251e-09 
 + [85] 1.447401e-09 1.157921e-09 9.263367e-10 7.410694e-10 
 + [89] 5.928555e-10 4.742844e-10 3.794275e-10 3.035420e-10 
 + [93] 2.428336e-10 1.942669e-10 1.554135e-10 1.243308e-10 
 + [97] 9.946465e-11 7.957172e-11 6.365737e-11 5.092590e-11 
 +> npx <- trial*px
 > npx > npx
-  [1] 2.000000e-01 3.200000e-01 3.840000e-01 4.096000e-01 4.096000e-01 3.932160e-01 3.670016e-01 +  [1] 2.000000e-01 3.200000e-01 3.840000e-01 4.096000e-01 
-  [8] 3.355443e-01 3.019899e-01 2.684355e-01 2.362232e-01 2.061584e-01 1.786706e-01 1.539316e-01 +  [5] 4.096000e-01 3.932160e-01 3.670016e-01 3.355443e-01 
- [15] 1.319414e-01 1.125900e-01 9.570149e-02 8.106479e-02 6.845471e-02 5.764608e-02 4.842270e-02 +  [9] 3.019899e-01 2.684355e-01 2.362232e-01 2.061584e-01 
- [22] 4.058284e-02 3.394201e-02 2.833420e-02 2.361183e-02 1.964504e-02 1.632050e-02 1.353997e-02 + [13] 1.786706e-01 1.539316e-01 1.319414e-01 1.125900e-01 
- [29] 1.121883e-02 9.284550e-03 7.675228e-03 6.338253e-03 5.229059e-03 4.310012e-03 3.549422e-03 + [17] 9.570149e-02 8.106479e-02 6.845471e-02 5.764608e-02 
- [36] 2.920667e-03 2.401437e-03 1.973073e-03 1.619997e-03 1.329228e-03 1.089967e-03 8.932412e-04 + [21] 4.842270e-02 4.058284e-02 3.394201e-02 2.833420e-02 
- [43] 7.316071e-04 5.988970e-04 4.900066e-04 4.007165e-04 3.275422e-04 2.676089e-04 2.185473e-04 + [25] 2.361183e-02 1.964504e-02 1.632050e-02 1.353997e-02 
- [50] 1.784060e-04 1.455793e-04 1.187470e-04 9.682448e-05 7.892109e-05 6.430607e-05 5.238022e-05 + [29] 1.121883e-02 9.284550e-03 7.675228e-03 6.338253e-03 
- [57] 4.265246e-05 3.472060e-05 2.825539e-05 2.298743e-05 1.869645e-05 1.520236e-05 1.235804e-05 + [33] 5.229059e-03 4.310012e-03 3.549422e-03 2.920667e-03 
- [64] 1.004336e-05 8.160232e-06 6.628619e-06 5.383242e-06 4.370871e-06 3.548119e-06 2.879633e-06 + [37] 2.401437e-03 1.973073e-03 1.619997e-03 1.329228e-03 
- [71] 2.336616e-06 1.895621e-06 1.537559e-06 1.246898e-06 1.010998e-06 8.195824e-07 6.642931e-07 + [41] 1.089967e-03 8.932412e-04 7.316071e-04 5.988970e-04 
- [78] 5.383362e-07 4.361904e-07 3.533694e-07 2.862292e-07 2.318103e-07 1.877098e-07 1.519771e-07 + [45] 4.900066e-04 4.007165e-04 3.275422e-04 2.676089e-04 
- [85] 1.230291e-07 9.958120e-08 8.059129e-08 6.521410e-08 5.276414e-08 4.268560e-08 3.452790e-08 + [49] 2.185473e-04 1.784060e-04 1.455793e-04 1.187470e-04 
- [92] 2.792587e-08 2.258353e-08 1.826109e-08 1.476428e-08 1.193576e-08 9.648071e-09 7.798028e-09 + [53] 9.682448e-05 7.892109e-05 6.430607e-05 5.238022e-05 
- [99] 6.302080e-09 5.092590e-09 + [57] 4.265246e-05 3.472060e-05 2.825539e-05 2.298743e-05 
-> plex <- cumsum(trial*(q^(trial-1))*p)+ [61] 1.869645e-05 1.520236e-05 1.235804e-05 1.004336e-05 
 + [65] 8.160232e-06 6.628619e-06 5.383242e-06 4.370871e-06 
 + [69] 3.548119e-06 2.879633e-06 2.336616e-06 1.895621e-06 
 + [73] 1.537559e-06 1.246898e-06 1.010998e-06 8.195824e-07 
 + [77] 6.642931e-07 5.383362e-07 4.361904e-07 3.533694e-07 
 + [81] 2.862292e-07 2.318103e-07 1.877098e-07 1.519771e-07 
 + [85] 1.230291e-07 9.958120e-08 8.059129e-08 6.521410e-08 
 + [89] 5.276414e-08 4.268560e-08 3.452790e-08 2.792587e-08 
 + [93] 2.258353e-08 1.826109e-08 1.476428e-08 1.193576e-08 
 + [97] 9.648071e-09 7.798028e-09 6.302080e-09 5.092590e-09 
 +## plex <- cumsum(trial*(q^(trial-1))*p
 +> ## 위는 아래와 같음 
 +> plex <- cumsum(npx)
 > plex > plex
-  [1] 0.200000 0.520000 0.904000 1.313600 1.723200 2.116416 2.483418 2.818962 3.120952 3.389387 +  [1] 0.200000 0.520000 0.904000 1.313600 1.723200 2.116416 2.483418 
- [11] 3.625610 3.831769 4.010440 4.164371 4.296313 4.408903 4.504604 4.585669 4.654124 4.711770 +  [8] 2.818962 3.120952 3.389387 3.625610 3.831769 4.010440 4.164371 
- [214.760192 4.800775 4.834717 4.863051 4.886663 4.906308 4.922629 4.936169 4.947388 4.956672 + [15] 4.296313 4.408903 4.504604 4.585669 4.654124 4.711770 4.760192 
- [31] 4.964347 4.970686 4.975915 4.980225 4.983774 4.986695 4.989096 4.991069 4.992689 4.994018 + [22] 4.800775 4.834717 4.863051 4.886663 4.906308 4.922629 4.936169 
- [41] 4.995108 4.996002 4.996733 4.997332 4.997822 4.998223 4.998550 4.998818 4.999037 4.999215 + [29] 4.947388 4.956672 4.964347 4.970686 4.975915 4.980225 4.983774 
- [51] 4.999361 4.999479 4.999576 4.999655 4.999719 4.999772 4.999814 4.999849 4.999877 4.999900 + [36] 4.986695 4.989096 4.991069 4.992689 4.994018 4.995108 4.996002 
- [61] 4.999919 4.999934 4.999947 4.999957 4.999965 4.999971 4.999977 4.999981 4.999985 4.999988 + [43] 4.996733 4.997332 4.997822 4.998223 4.998550 4.998818 4.999037 
- [71] 4.999990 4.999992 4.999993 4.999995 4.999996 4.999997 4.999997 4.999998 4.999998 4.999998 + [504.999215 4.999361 4.999479 4.999576 4.999655 4.999719 4.999772 
- [81] 4.999999 4.999999 4.999999 4.999999 4.999999 5.000000 5.000000 5.000000 5.000000 5.000000 + [57] 4.999814 4.999849 4.999877 4.999900 4.999919 4.999934 4.999947 
- [91] 5.000000 5.000000 5.000000 5.000000 5.000000 5.000000 5.000000 5.000000 5.000000 5.000000+ [64] 4.999957 4.999965 4.999971 4.999977 4.999981 4.999985 4.999988 
 + [71] 4.999990 4.999992 4.999993 4.999995 4.999996 4.999997 4.999997 
 + [78] 4.999998 4.999998 4.999998 4.999999 4.999999 4.999999 4.999999 
 + [85] 4.999999 5.000000 5.000000 5.000000 5.000000 5.000000 5.000000 
 + [92] 5.000000 5.000000 5.000000 5.000000 5.000000 5.000000 5.000000 
 + [99] 5.000000 5.000000
 > sumgeod <- data.frame(trial,px,npx,plex) > sumgeod <- data.frame(trial,px,npx,plex)
-round(sumgeod,3) +> sumgeod  
-    trial    px   npx  plex +    trial           px          npx     plex 
-1       0.200 0.200 0.200 +1       2.000000e-01 2.000000e-01 0.200000 
-2       0.160 0.320 0.520 +2       1.600000e-01 3.200000e-01 0.520000 
-3       0.128 0.384 0.904 +3       1.280000e-01 3.840000e-01 0.904000 
-4       0.102 0.410 1.314 +4       1.024000e-01 4.096000e-01 1.313600 
-5       0.082 0.410 1.723 +5       8.192000e-02 4.096000e-01 1.723200 
-6       0.066 0.393 2.116 +6       6.553600e-02 3.932160e-01 2.116416 
-7       0.052 0.367 2.483 +7       5.242880e-02 3.670016e-01 2.483418 
-8       0.042 0.336 2.819 +8       4.194304e-02 3.355443e-01 2.818962 
-9       0.034 0.302 3.121 +9       3.355443e-02 3.019899e-01 3.120952 
-10     10 0.027 0.268 3.389 +10     10 2.684355e-02 2.684355e-01 3.389387 
-11     11 0.021 0.236 3.626 +11     11 2.147484e-02 2.362232e-01 3.625610 
-12     12 0.017 0.206 3.832 +12     12 1.717987e-02 2.061584e-01 3.831769 
-13     13 0.014 0.179 4.010 +13     13 1.374390e-02 1.786706e-01 4.010440 
-14     14 0.011 0.154 4.164 +14     14 1.099512e-02 1.539316e-01 4.164371 
-15     15 0.009 0.132 4.296 +15     15 8.796093e-03 1.319414e-01 4.296313 
-16     16 0.007 0.113 4.409 +16     16 7.036874e-03 1.125900e-01 4.408903 
-17     17 0.006 0.096 4.505 +17     17 5.629500e-03 9.570149e-02 4.504604 
-18     18 0.005 0.081 4.586 +18     18 4.503600e-03 8.106479e-02 4.585669 
-19     19 0.004 0.068 4.654 +19     19 3.602880e-03 6.845471e-02 4.654124 
-20     20 0.003 0.058 4.712 +20     20 2.882304e-03 5.764608e-02 4.711770 
-21     21 0.002 0.048 4.760 +21     21 2.305843e-03 4.842270e-02 4.760192 
-22     22 0.002 0.041 4.801 +22     22 1.844674e-03 4.058284e-02 4.800775 
-23     23 0.001 0.034 4.835 +23     23 1.475740e-03 3.394201e-02 4.834717 
-24     24 0.001 0.028 4.863 +24     24 1.180592e-03 2.833420e-02 4.863051 
-25     25 0.001 0.024 4.887 +25     25 9.444733e-04 2.361183e-02 4.886663 
-26     26 0.001 0.020 4.906 +26     26 7.555786e-04 1.964504e-02 4.906308 
-27     27 0.001 0.016 4.923 +27     27 6.044629e-04 1.632050e-02 4.922629 
-28     28 0.000 0.014 4.936 +28     28 4.835703e-04 1.353997e-02 4.936169 
-29     29 0.000 0.011 4.947 +29     29 3.868563e-04 1.121883e-02 4.947388 
-30     30 0.000 0.009 4.957 +30     30 3.094850e-04 9.284550e-03 4.956672 
-31     31 0.000 0.008 4.964 +31     31 2.475880e-04 7.675228e-03 4.964347 
-32     32 0.000 0.006 4.971 +32     32 1.980704e-04 6.338253e-03 4.970686 
-33     33 0.000 0.005 4.976 +33     33 1.584563e-04 5.229059e-03 4.975915 
-34     34 0.000 0.004 4.980 +34     34 1.267651e-04 4.310012e-03 4.980225 
-35     35 0.000 0.004 4.984 +35     35 1.014120e-04 3.549422e-03 4.983774 
-36     36 0.000 0.003 4.987 +36     36 8.112964e-05 2.920667e-03 4.986695 
-37     37 0.000 0.002 4.989 +37     37 6.490371e-05 2.401437e-03 4.989096 
-38     38 0.000 0.002 4.991 +38     38 5.192297e-05 1.973073e-03 4.991069 
-39     39 0.000 0.002 4.993 +39     39 4.153837e-05 1.619997e-03 4.992689 
-40     40 0.000 0.001 4.994 +40     40 3.323070e-05 1.329228e-03 4.994018 
-41     41 0.000 0.001 4.995 +41     41 2.658456e-05 1.089967e-03 4.995108 
-42     42 0.000 0.001 4.996 +42     42 2.126765e-05 8.932412e-04 4.996002 
-43     43 0.000 0.001 4.997 +43     43 1.701412e-05 7.316071e-04 4.996733 
-44     44 0.000 0.001 4.997 +44     44 1.361129e-05 5.988970e-04 4.997332 
-45     45 0.000 0.000 4.998 +45     45 1.088904e-05 4.900066e-04 4.997822 
-46     46 0.000 0.000 4.998 +46     46 8.711229e-06 4.007165e-04 4.998223 
-47     47 0.000 0.000 4.999 +47     47 6.968983e-06 3.275422e-04 4.998550 
-48     48 0.000 0.000 4.999 +48     48 5.575186e-06 2.676089e-04 4.998818 
-49     49 0.000 0.000 4.999 +49     49 4.460149e-06 2.185473e-04 4.999037 
-50     50 0.000 0.000 4.999 +50     50 3.568119e-06 1.784060e-04 4.999215 
-51     51 0.000 0.000 4.999 +51     51 2.854495e-06 1.455793e-04 4.999361 
-52     52 0.000 0.000 4.999 +52     52 2.283596e-06 1.187470e-04 4.999479 
-53     53 0.000 0.000 5.000 +53     53 1.826877e-06 9.682448e-05 4.999576 
-54     54 0.000 0.000 5.000 +54     54 1.461502e-06 7.892109e-05 4.999655 
-55     55 0.000 0.000 5.000 +55     55 1.169201e-06 6.430607e-05 4.999719 
-56     56 0.000 0.000 5.000 +56     56 9.353610e-07 5.238022e-05 4.999772 
-57     57 0.000 0.000 5.000 +57     57 7.482888e-07 4.265246e-05 4.999814 
-58     58 0.000 0.000 5.000 +58     58 5.986311e-07 3.472060e-05 4.999849 
-59     59 0.000 0.000 5.000 +59     59 4.789049e-07 2.825539e-05 4.999877 
-60     60 0.000 0.000 5.000 +60     60 3.831239e-07 2.298743e-05 4.999900 
-61     61 0.000 0.000 5.000 +61     61 3.064991e-07 1.869645e-05 4.999919 
-62     62 0.000 0.000 5.000 +62     62 2.451993e-07 1.520236e-05 4.999934 
-63     63 0.000 0.000 5.000 +63     63 1.961594e-07 1.235804e-05 4.999947 
-64     64 0.000 0.000 5.000 +64     64 1.569275e-07 1.004336e-05 4.999957 
-65     65 0.000 0.000 5.000 +65     65 1.255420e-07 8.160232e-06 4.999965 
-66     66 0.000 0.000 5.000 +66     66 1.004336e-07 6.628619e-06 4.999971 
-67     67 0.000 0.000 5.000 +67     67 8.034690e-08 5.383242e-06 4.999977 
-68     68 0.000 0.000 5.000 +68     68 6.427752e-08 4.370871e-06 4.999981 
-69     69 0.000 0.000 5.000 +69     69 5.142202e-08 3.548119e-06 4.999985 
-70     70 0.000 0.000 5.000 +70     70 4.113761e-08 2.879633e-06 4.999988 
-71     71 0.000 0.000 5.000 +71     71 3.291009e-08 2.336616e-06 4.999990 
-72     72 0.000 0.000 5.000 +72     72 2.632807e-08 1.895621e-06 4.999992 
-73     73 0.000 0.000 5.000 +73     73 2.106246e-08 1.537559e-06 4.999993 
-74     74 0.000 0.000 5.000 +74     74 1.684997e-08 1.246898e-06 4.999995 
-75     75 0.000 0.000 5.000 +75     75 1.347997e-08 1.010998e-06 4.999996 
-76     76 0.000 0.000 5.000 +76     76 1.078398e-08 8.195824e-07 4.999997 
-77     77 0.000 0.000 5.000 +77     77 8.627183e-09 6.642931e-07 4.999997 
-78     78 0.000 0.000 5.000 +78     78 6.901746e-09 5.383362e-07 4.999998 
-79     79 0.000 0.000 5.000 +79     79 5.521397e-09 4.361904e-07 4.999998 
-80     80 0.000 0.000 5.000 +80     80 4.417118e-09 3.533694e-07 4.999998 
-81     81 0.000 0.000 5.000 +81     81 3.533694e-09 2.862292e-07 4.999999 
-82     82 0.000 0.000 5.000 +82     82 2.826955e-09 2.318103e-07 4.999999 
-83     83 0.000 0.000 5.000 +83     83 2.261564e-09 1.877098e-07 4.999999 
-84     84 0.000 0.000 5.000 +84     84 1.809251e-09 1.519771e-07 4.999999 
-85     85 0.000 0.000 5.000 +85     85 1.447401e-09 1.230291e-07 4.999999 
-86     86 0.000 0.000 5.000 +86     86 1.157921e-09 9.958120e-08 5.000000 ###########  
-87     87 0.000 0.000 5.000 +87     87 9.263367e-10 8.059129e-08 5.000000 
-88     88 0.000 0.000 5.000 +88     88 7.410694e-10 6.521410e-08 5.000000 
-89     89 0.000 0.000 5.000 +89     89 5.928555e-10 5.276414e-08 5.000000 
-90     90 0.000 0.000 5.000 +90     90 4.742844e-10 4.268560e-08 5.000000 
-91     91 0.000 0.000 5.000 +91     91 3.794275e-10 3.452790e-08 5.000000 
-92     92 0.000 0.000 5.000 +92     92 3.035420e-10 2.792587e-08 5.000000 
-93     93 0.000 0.000 5.000 +93     93 2.428336e-10 2.258353e-08 5.000000 
-94     94 0.000 0.000 5.000 +94     94 1.942669e-10 1.826109e-08 5.000000 
-95     95 0.000 0.000 5.000 +95     95 1.554135e-10 1.476428e-08 5.000000 
-96     96 0.000 0.000 5.000 +96     96 1.243308e-10 1.193576e-08 5.000000 
-97     97 0.000 0.000 5.000 +97     97 9.946465e-11 9.648071e-09 5.000000 
-98     98 0.000 0.000 5.000 +98     98 7.957172e-11 7.798028e-09 5.000000 
-99     99 0.000 0.000 5.000 +99     99 6.365737e-11 6.302080e-09 5.000000 
-100   100 0.000 0.000 5.000+100   100 5.092590e-11 5.092590e-09 5.000000
 > plot(npx, type="l") > plot(npx, type="l")
 > plot(plex, type="l") > plot(plex, type="l")
 </code> </code>
 +
 +  * 기댓값이 86번째 부터는 더이상 늘지 않고 
 +  * 계산된 값을 보면 5로 수렴한다.
 +  * workout 예처럼 다섯가지의 순서가 있는 것이 아니라서 
 +  * 평균을 어떻게 나오나 보기 위해서 100까지 해 봤지만 
 +  * 86번째 이후에는 평균값이 더 늘지 않는다 (5에서)
 +  * 따라서 위의 geometric distribution에서의 기대값은 5이다.
 +
 {{:b:head_first_statistics:pasted:20191031-010726.png}} {{:b:head_first_statistics:pasted:20191031-010726.png}}
 {{:b:head_first_statistics:pasted:20191031-010811.png}} {{:b:head_first_statistics:pasted:20191031-010811.png}}
  
 +  * 그런데 이 기대값은 아래처럼 구할 수 있다. 
 +  * 위에서 $X \sim \text{Geo}(p)$ 일때, 기대값은 $E(X) = \dfrac{1}{p}$ 
 +  * 아래는 그 증명이다.
  
-$X \sim Geo(p)$ 일때, 기대값+===== Proof ===== 
 +[[:Mean and Variance of Geometric Distribution#Mean|기하분포의 기대값(expected value)]]
  
-$E(X) = \displaystyle \frac{1}{p}$ 
  
-===== Variance =====+===== Variance proof ===== 
 +[[:Mean and Variance of Geometric Distribution#Variance|기하분포의 분산 증명]] 
 +아래는 R에서의 시뮬레이션
 \begin{eqnarray*} \begin{eqnarray*}
 Var(X) & = & E((X-E(X))^{2}) \\ Var(X) & = & E((X-E(X))^{2}) \\
Line 335: Line 482:
 $E(X^{2}) = \sum {x^{2} * P(X=x)}$ $E(X^{2}) = \sum {x^{2} * P(X=x)}$
 왜냐하면, $E(X) = \sum {x * P(X=x)}$ 이므로  왜냐하면, $E(X) = \sum {x * P(X=x)}$ 이므로 
- 
  
 이를 구한 후에 여기에서 $E[X]^{2}$ 를 빼준 값이 Var(X) 이를 구한 후에 여기에서 $E[X]^{2}$ 를 빼준 값이 Var(X)
Line 341: Line 487:
  
 \begin{eqnarray*} \begin{eqnarray*}
-E(X)^{2} & = & (\frac{1}{p})^{2} \\+E(X)^{2} & = & \left(\frac{1}{p}\right)^{2} \\
 & = & (5^{2}) \\ & = & (5^{2}) \\
 & = & 25 & = & 25
Line 544: Line 690:
  
 </code> </code>
-위에서 보듯이 plex column에 해당하는 것이 E(X^2) 부분이고 이는 45가 된다.  
-E(X)^2 은 25이므로, Var(X) = 20이 된다. 
- 
 {{:b:head_first_statistics:pasted:20191031-062004.png}} {{:b:head_first_statistics:pasted:20191031-062004.png}}
 {{:b:head_first_statistics:pasted:20191031-062021.png}} {{:b:head_first_statistics:pasted:20191031-062021.png}}
  
-$Var(X) = \displaystyle \frac{q}{p^{2}}$+위에서 보듯이 plex column에 해당하는 것이 $E(X^2)$ 부분이고 이는 45가 된다 (45에 수렴한다). 또한 언급한 것처럼 $Var(X) = E(X^2) - E(X)^2$ 이고 $E(X)^2 = 25$ 이므로,  
 +\begin{align*} 
 +Var(X) & = E(X^2) - E(X)^2 \\ 
 +& = 45 - 25 \\ 
 +& = 20  
 +\end{align*}
  
 +일반적으로 
 +\begin{eqnarray*}
 +Var(X) = \displaystyle \frac{q}{p^{2}}
 +\end{eqnarray*}
 +
 +아래는 이를 R에서 계산해 본 것
 <code> <code>
 q/(p^2) q/(p^2)
Line 558: Line 712:
 </code> </code>
 ===== Sum up ===== ===== Sum up =====
-$$P(X = r) = p * q^{r-1} $$ +\begin{align} 
-$$P(X > r) = q^{r} $$ +P(X = r) = p * q^{r-1} \\ 
-$$P(X \le r) = 1 - q^{r} $$ +P(X > r) = q^{r} \\ 
-$$E(X) = \displaystyle \frac{1}{p}$$ +P(X \le r) = 1 - q^{r} \\  
-$$Var(X) = \displaystyle \frac{q}{p^{2}} $$+E(X) = \displaystyle \frac{1}{p} \\ 
 +Var(X) = \displaystyle \frac{q}{p^{2}} \\  
 +\end{align}
  
 +===== Proof of mean and variance of geometric distribution =====
 +$(4)$, $(5)$에 대한 증명은 [[:Mean and Variance of Geometric Distribution]]
 ===== e.g., ===== ===== e.g., =====
 <WRAP box> <WRAP box>
Line 581: Line 739:
 $Var(X) = \displaystyle \frac{q}{p^{2}}$ $Var(X) = \displaystyle \frac{q}{p^{2}}$
  
 +<code> 
 +> p <- .4 
 +> q <- 1-p 
 +>  
 +> p*q^(2-1) 
 +[1] 0.24 
 +> dgeom(1, p) 
 +[1] 0.24 
 +
 +> 1-q^4 
 +[1] 0.8704 
 +> dgeom(0:3, p) 
 +[1] 0.4000 0.2400 0.1440 0.0864 
 +> sum(dgeom(0:3, p)) 
 +[1] 0.8704 
 +> pgeom(3, p) 
 +[1] 0.8704 
 +
 +> q^4 
 +[1] 0.1296 
 +> 1-sum(dgeom(0:3, p)) 
 +[1] 0.1296 
 +> 1-pgeom(3, p) 
 +[1] 0.1296 
 +> pgeom(3, p, lower.tail = F) 
 +[1] 0.1296 
 +>  
 +> 1/p 
 +[1] 2.5 
 +
 +> q/p^2 
 +[1] 3.75 
 +>  
 +</code>
  
  
  
 ====== Binomial Distributions ====== ====== Binomial Distributions ======
 +
 +  - 1번의 시행에서 특정 사건 A가 발생할 확률을 p라고 하면 
 +  - n번의 (독립적인) 시행에서 사건 A가 발생할 때의 확률 분포를 
 +  - **이항확률분포**라고 한다.
 +아래를 보면
 +  * 각 한문제를 맞힐 확률은 1/4, 틀릴 확률은 3/4
 +  * 3문제를 풀면서 (3번의 시행) 각 문제를 맞힐 확률 분포를 말한다. 
 +
 {{:b:head_first_statistics:pasted:20191030-035316.png}} {{:b:head_first_statistics:pasted:20191030-035316.png}}
 {{:b:head_first_statistics:pasted:20191030-035452.png}} {{:b:head_first_statistics:pasted:20191030-035452.png}}
Line 596: Line 795:
 {{:b:head_first_statistics:pasted:20191030-040346.png}} {{:b:head_first_statistics:pasted:20191030-040346.png}}
  
-$$P(X = r) = \Huge\text{?} \cdot 0.25^{r} \cdot 0.75^{3-r}$$ +$$P(X = r) = {\huge\text{?} \cdot 0.25^{r} \cdot 0.75^{3-r}} $$ 
-$$P(X = r) = \displaytype _{3}C_{r} \cdot 0.25^{r} \cdot 0.75^{3-r}$$+$$P(X = r) = {\huge_{3}C_{r}} \cdot 0.25^{r} \cdot 0.75^{3-r}$$
  
 $_{n}C_{r}$은 n개의 사물에서 r개를 (순서없이) 고르는 방법의 수라고 할 때, 3개의 질문 중에서 한 개의 정답을 맞히는 방법은 $_{3}C_{1} = 3$ 세가지가 존재. $_{n}C_{r}$은 n개의 사물에서 r개를 (순서없이) 고르는 방법의 수라고 할 때, 3개의 질문 중에서 한 개의 정답을 맞히는 방법은 $_{3}C_{1} = 3$ 세가지가 존재.
Line 612: Line 811:
 \end{eqnarray*}  \end{eqnarray*} 
  
-$$P(X = r) = \displaytype _{n}C_{r} \cdot 0.25^{r} \cdot 0.75^{n-r}$$ +$$P(X = r) = _{n}C_{r} \cdot 0.25^{r} \cdot 0.75^{n-r}$$ 
-$$P(X = r) = \displaytype _{n}C_{r} \cdot p^{r} \cdot q^{n-r}$$+$$P(X = r) = _{n}C_{r} \cdot p^{r} \cdot q^{n-r}$$
  
-  - You’re running a series of independent trials. +  - You’re running a series of independent trials. (n번의 시행을 하게 된다) 
-  - There can be either a success or failure for each trial, and the probability of success is the same for each trial. +  - There can be either a success or failure for each trial, and the probability of success is the same for each trial. (각 시행은 성공/실패로 구분되고 성공의 확률은 (반대로 실패의 확률도) 각 시행마다 동일하다) 
-  - There are a finite number of trials. (note that this is different from that of geometric distribution)+  - There are a finite number of trials. Note that this is different from that of geometric distribution. (n번의 시행으로 한정된다. 무한대 시행이 아님)
  
 X가 n번의 시행에서 성공적인 결과를 얻는 수를 나타낸다고 할 때, r번의 성공이 있을 확률을 구하려면 아래 공식을 이용한다. X가 n번의 시행에서 성공적인 결과를 얻는 수를 나타낸다고 할 때, r번의 성공이 있을 확률을 구하려면 아래 공식을 이용한다.
  
 \begin{eqnarray*}  \begin{eqnarray*} 
-P(X = r) & = & \displaytype _{n}C_{r} \cdot p^{r} \cdot q^{n-r} \;\;\; \text{Where,} \\ +P(X = r) & = & _{n}C_{r} \cdot p^{r} \cdot q^{n-r} \;\;\; \text{Where,} \\ 
-_{n}C_{r} & = & \displaytype \frac {n!}{r!(n-r)!}+\displaystyle _{n}C_{r} & = & \displaystyle \dfrac {n!}{r!(n-r)!} \\ 
 +\text{c.f.,  } \\ 
 +\displaystyle _{n} P_{r} & = & \displaystyle \dfrac {n!} {(n-r)!} \\
 \end{eqnarray*}  \end{eqnarray*} 
 +
 +see [[:b:head_first_statistics:permutation_and_combination#what_if_horse_order_doesn_t_matter|Permutation chapter]]
 +
  
 p = 각 시행에서 성공할 확률 p = 각 시행에서 성공할 확률
Line 633: Line 837:
  
 ===== Expectation and Variance of ===== ===== Expectation and Variance of =====
 +Toss a fair coin once. What is the distribution of the number of heads?
 +  * A single trial
 +  * The trial can be one of two possible outcomes -- success and failure
 +  * P(success) = p
 +  * P(failure) = 1-p
 +
 +X = 0, 1 (failure and success)
 +$P(X=x) = p^{x}(1-p)^{1-x}$ or 
 +$P(x) = p^{x}(1-p)^{1-x}$
 +
 +참고.
 +| x     | 0          | 1  |
 +| p(x)  | q = (1-p)  | p  | 
 +
 +When x = 0 (failure), $P(X = 0) = p^{0}(1-p)^{1-0} = (1-p)$ = Probability of failure
 +When x = 1 (success), $P(X = 1) = p^{1}(1-p)^{0} = p $ = Probability of success
 +
  
-{{:b:head_first_statistics:pasted:20191104-013651.png}}+This is called Bernoulli distribution. 
 +  * Bernoulli distribution expands to binomial distribution, geometric distribution, etc. 
 +  * Binomial distribution = The distribution of number of success in n independent Bernoulli trials. 
 +  * Geometric distribution = The distribution of number of trials to get the first success in independent Bernoulli trials.
  
 $$X \sim B(1,p)$$ $$X \sim B(1,p)$$
  
 \begin{eqnarray*} \begin{eqnarray*}
-E(X) & = & \sum{n*p(x)} \\ +E(X) & = & \sum{* p(x)} \\ 
-& = & (1*p)+(0*q) \\+& = & (0*q) + (1*p) \\
 & = & p  & = & p 
 \end{eqnarray*}  \end{eqnarray*} 
Line 691: Line 915:
 n <-5 n <-5
 # combinations of 5,2 # combinations of 5,2
-c <- choose(5,2)+c <- choose(n,r
 ans1 <- c*(p^r)*(q^(n-r)) ans1 <- c*(p^r)*(q^(n-r))
-ans1+ans1    # or 
 + 
 +choose(n, r)*(p^r)*(q^(n-r)) 
 + 
 +dbinom(r, n, p) 
 +# dbinom(2, 5, 1/4) 
 </code> </code>
 +
 <code> <code>
 > p <- .25 > p <- .25
Line 701: Line 932:
 > n <-5 > n <-5
 > # combinations of 5,2 > # combinations of 5,2
-> c <- choose(5,2)+> c <- choose(n,r)
 > ans <- c*(p^r)*(q^(n-r)) > ans <- c*(p^r)*(q^(n-r))
 > ans > ans
 +[1] 0.2636719
 +>
 +> choose(n, r)*(p^r)*(q^(n-r))
 +[1] 0.2636719
 +>
 +> dbinom(r, n, p)
 [1] 0.2636719 [1] 0.2636719
  
Line 716: Line 953:
 n <-5 n <-5
 # combinations of 5,3 # combinations of 5,3
-c <- choose(5,3)+c <- choose(n,r)
 ans2 <- c*(p^r)*(q^(n-r)) ans2 <- c*(p^r)*(q^(n-r))
 ans2 ans2
 +
 +choose(n, r)*(p^r)*(q^(n-r))
 +
 +dbinom(r, n, p)
 +
 </code> </code>
 <code> <code>
Line 726: Line 968:
 > n <-5 > n <-5
 > # combinations of 5,3 > # combinations of 5,3
-> c <- choose(5,3)+> c <- choose(n,r)
 > ans2 <- c*(p^r)*(q^(n-r)) > ans2 <- c*(p^r)*(q^(n-r))
 > ans2 > ans2
 [1] 0.08789062 [1] 0.08789062
 +
 +> choose(n,r)*(p^r)*(q^(n-r))
 +[1] 0.08789062
 +
 +> dbinom(r, n, p)
 +[1] 0.08789063
 +
  
 </code> </code>
  
-Ans 3. +Ans 3. 중요 
 <code> <code>
-ans1 + ans2 +ans1 + ans2 
 +dbinom(2, 5, .25) + dbinom(3, 5, .25)  
 +dbinom(2:3, 5, .25) 
 +sum(dbinom(2:3, 5, .25)) 
 +pbinom(3, 5, .25) - pbinom(1, 5, .25)
 </code> </code>
  
-<code>> ans1 + ans2 +<code> 
 +> ans1 + ans2
 [1] 0.3515625 [1] 0.3515625
 +> dbinom(2, 5, .25) + dbinom(3, 5, .25) 
 +[1] 0.3515625
 +> dbinom(2:3, 5, .25)
 +[1] 0.26367187 0.08789063
 +> sum(dbinom(2:3, 5, .25))
 +[1] 0.3515625
 +> pbinom(3, 5, .25) - pbinom(1, 5, .25)
 +[1] 0.3515625
 +
 </code> </code>
  
Line 795: Line 1058:
 > </code> > </code>
  
-===== Another way to see E(Xand Var(X===== +Q. 한 문제를 맞힐 확률은 1/4 이다. 총 여섯 문제가 있다고 할 때, 0에서 5 문제를 맞힐 확률은? dbinom을 이용해서 구하시오. 
-==== Bernoulli Distribution ==== +<code> 
-Toss a fair coin once. What is the distribution of the number of heads? +p <- 1/4 
-  * A single trial +q <- 1-p 
-  * The trial can be one of two possible outcomes -- success and failure +n <- 6 
-  * P(success) = +pbinom(5, n, p) 
-  * P(failure1-p+1 - dbinom(6, n, p
 +sum(dbinom(0:5, n, p)) 
 +</code>  
 +<code> 
 +> p <1/4 
 +> q <- 1-p 
 +> n <6 
 +> pbinom(5, n, p) 
 +[1] 0.9997559 
 +> 1 - dbinom(6, n, p) 
 +[1] 0.9997559 
 +> sum(dbinom(0:5, n, p)) 
 +[1] 0.9997559 
 +>  
 +</code>
  
-X = 0, 1 (failure and success) +중요 . . . .  
-$P(X=x) = p^{x}(1-p)^{1-x}$+<code> 
 +# http://commres.net/wiki/mean_and_variance_of_binomial_distribution 
 +# ################################################################## 
 +# 
 +<- 1/4 
 +q <- 1 - 
 +n <- 5 
 +r <- 0 
 +all.dens <- dbinom(0:n, n, p) 
 +all.dens 
 +sum(all.dens)
  
-When x = 0 (failure)$P(X = 0) p^{0}(1-p)^{1-0} = (1-p)$ = Probability of failure +choose(5,0)*p^0*(q^(5-0)
-When x = 1 (success)$P(X = 1) p^{1}(1-p)^{0} = $ = Probability of success+choose(5,1)*p^1*(q^(5-1)) 
 +choose(5,2)*p^2*(q^(5-2)) 
 +choose(5,3)*p^3*(q^(5-3)
 +choose(5,4)*p^4*(q^(5-4)) 
 +choose(5,5)*p^5*(q^(5-5)) 
 +all.dens
  
-<WRAP box> +choose(5,0)*p^0*(q^(5-0))  
-Bernoulli distribution expands to binomial distributiongeometric distribution, etc. +  choose(5,1)*p^1*(q^(5-1))  
-Binomial distribution = The distribution of number of success in n independent Bernoulli trials. +  choose(5,2)*p^2*(q^(5-2))  
-Geometric distribution = The distribution of number of trials to get the first success in independent Bernoulli trials. +  choose(5,3)*p^3*(q^(5-3))  
-</WRAP> +  choose(5,4)*p^4*(q^(5-4))  
- +  choose(5,5)*p^5*(q^(5-5)) 
-$$P(X=xp^{x}(1-p)^{1-x}$$ +sum(all.dens
-X takes, x = 0, 1 +#  
- +(p+q)^
-\begin{eqnarray*} +# note that n = whatever, (p+q)^n = 1
-E(X& = & \sum_{x}xp(X=x\\ +
-& = & 0*p^{0}(1-p)^{1-0} + 1*p^{1}(1-p)^{1-1}  \\ +
-& = & p +
-\end{eqnarray*} +
- +
-\begin{eqnarray*} +
-Var(X& = & E((X-\mu)^{2}) \\ +
-& = & \sum_{x}(x-\mu)^2p(x)   +
-\end{eqnarray*} +
- +
-한편, +
-\begin{eqnarray*} +
-E((X-\mu)^{2}& = & E(X^2) - (E(X))^2 \\ +
-\end{eqnarray*} +
- +
-위에서  +
-\begin{eqnarray*} +
-E(X^{2}& = & \sum X^2 p(x\\ +
-& = & 0^2*p^0(1-p)^{1-0} + 1^2*p^1(1-p)^{1-1} \\ +
-& = & p +
-\end{eqnarray*} +
- +
-zero squared probability of zero occurring +
-one squared prob of one occurring  +
- +
-또한 $E(X) = p $ 임을 알고 있음 +
-\begin{eqnarray*} +
-Var(X) & = & E((X-\mu)^{2}\\ +
-& = & E(X^2- (E(X))^2 \\ +
-& = & p - p^2 \\ +
-& = & p(1-p) +
-\end{eqnarray*+
- +
-위는 First Head Statistics 에서 $X \sim (1, 0.25)$ 에서 E(X)와 Var(X)를 구한 후 (각각, p와 pq), X가 n가지가 있다고 확장하여 np와 npq를 구한 것과 같다. 즉, 교재는 Bernoulli distribution을 이야기(설명)하지 않고, 활용하여 binomial distribution의 기대값과 분산값을 구해낸 것이다. 아래는 그 나머지. +
- +
-==== Proof of E and Var ==== +
- +
-$E(U_{i}) = p$  and $Var(U_{i}p(1-p)$ or $Var(U_{i}= p \cdot q$ +
- +
-$$X = U_{1} + . . . . + U_{n}$$ +
-\begin{eqnarray*} +
-E(X) & = & E(U_{1} + . . + U_{n}\\ +
-& = & E(U_{1}) + . . . + E(U_{n}) \\ +
-& = & p + . . . + p \\ +
-& = & np +
-\end{eqnarray*} +
- +
-\begin{eqnarray*} +
-Var(X& = & Var(U_{1} + . . . + U_{n}) \\ +
-& = & Var(U_{1}) + . . . + Var(U_{n}) \\ +
-& p(1-p. . . + p(1-p\\ +
-& np(1-p) \\ +
-& = & npq  +
-\end{eqnarray*}+
  
 +</code>
  
-==== From a scratch ==== +<code> 
-\begin{eqnarray*} +> # http://commres.net/wiki/mean_and_variance_of_binomial_distribution 
-(a + b)^{m} = \sum^{m}_{y=0}{{m}\choose{y}} a^{y} b^{m-y} \\ +> # ################################################################## 
-\end{eqnarray*} +> # 
- +> p <- 1/4 
-See youtube video clip,  +> q <- 1 - p 
-{{youtube>8fqkQRjcR1M}}  +> n <- 5 
-The Binomial DistributionMathematically Deriving the Mean and Variance +> r <- 0 
 +> all.dens <- dbinom(0:n, n, p) 
 +> all.dens 
 +[1] 0.2373046875 0.3955078125 0.2636718750 0.0878906250 
 +[5] 0.0146484375 0.0009765625 
 +> sum(all.dens) 
 +[1] 1 
 +>  
 +> choose(5,0)*p^0*(q^(5-0)) 
 +[1] 0.2373047 
 +> choose(5,1)*p^1*(q^(5-1)) 
 +[1] 0.3955078 
 +choose(5,2)*p^2*(q^(5-2)) 
 +[1] 0.2636719 
 +> choose(5,3)*p^3*(q^(5-3)) 
 +[1] 0.08789062 
 +> choose(5,4)*p^4*(q^(5-4)) 
 +[1] 0.01464844 
 +choose(5,5)*p^5*(q^(5-5)) 
 +[1] 0.0009765625 
 +> all.dens 
 +[1] 0.2373046875 0.3955078125 0.2636718750 0.0878906250 
 +[5] 0.0146484375 0.0009765625 
 +>  
 +> choose(5,0)*p^0*(q^(5-0)) +  
 ++   choose(5,1)*p^1*(q^(5-1)) +  
 ++   choose(5,2)*p^2*(q^(5-2)) +  
 ++   choose(5,3)*p^3*(q^(5-3)) +  
 ++   choose(5,4)*p^4*(q^(5-4)) +  
 ++   choose(5,5)*p^5*(q^(5-5)) 
 +[1] 1 
 +> sum(all.dens) 
 +[1] 1 
 +> #  
 +> (p+q)^n 
 +[1] 1 
 +> # note that n = whatever, (p+q)^n = 1 
 +>  
 +</code> 
 +===== Proof of Binomial Expected Value and Variance ===== 
 +[[:Mean and Variance of Binomial Distribution|이항분포에서의 기댓값과 분산에 대한 수학적 증명]], Mathematical proof of Binomial Distribution Expected value and Variance
 ====== Poisson Distribution ====== ====== Poisson Distribution ======
 $$X \sim Po(\lambda)$$ $$X \sim Po(\lambda)$$
Line 908: Line 1186:
 For curiosity,  For curiosity, 
 \begin{eqnarray*} \begin{eqnarray*}
-\sum_{r=0}^{\infty} e^{- \lambda} \dfrac{\lambda^{r}} {r!} +\sum_{r=0}^{\infty} e^{- \lambda} \dfrac{\lambda^{r}} {r!}   
 +& = & e^{- \lambda} \sum_{r=0}^{\infty} \dfrac{\lambda^{r}} {r!}  \\
 & = & e^{- \lambda} \left(1 + \lambda + \dfrac{\lambda^{2}}{2!} + \dfrac{\lambda^{3}}{3!} + . . . \right) \\ & = & e^{- \lambda} \left(1 + \lambda + \dfrac{\lambda^{2}}{2!} + \dfrac{\lambda^{3}}{3!} + . . . \right) \\
 & = & e^{- \lambda}e^{\lambda} \\ & = & e^{- \lambda}e^{\lambda} \\
 & = & 1 & = & 1
 \end{eqnarray*} \end{eqnarray*}
 +
 +왜 $e^{\lambda} = \left(1 + \lambda + \dfrac{\lambda^{2}}{2!} + \dfrac{\lambda^{3}}{3!} + . . . \right)$ 인지는 [[:Taylor series]] 문서를 참조. 
 +이것이 의미하는 것은 r이 0에서 무한대로 갈 때의 확률값의 분포를 말하므로 전체 분포가 1이 됨을 의미한다. 아래 "What does the Poisson distribution look like?" 참조
  
 <code> <code>
Line 923: Line 1205:
 위의 그림은 lambda는 2, 즉 한달에 아주대학교 앞의 건널목 주변 찻길에서 교통사고가 날 횟수가 2회라고 할 때, X=3 이므로 3번 교통사고가 일어날 확률을 (P(X=3)) 묻는 문제이다. 위의 그림은 lambda는 2, 즉 한달에 아주대학교 앞의 건널목 주변 찻길에서 교통사고가 날 횟수가 2회라고 할 때, X=3 이므로 3번 교통사고가 일어날 확률을 (P(X=3)) 묻는 문제이다.
 \begin{eqnarray*} \begin{eqnarray*}
-P(X = 3) & = & \frac {e^{-2} * 2^{3}}{3!} \\+P(X = 3) & = & e^{-2} * \frac {2^{3}}{3!} \\
 & = & 0.180 & = & 0.180
 \end{eqnarray*} \end{eqnarray*}
Line 930: Line 1212:
  
 \begin{eqnarray*} \begin{eqnarray*}
-P(X=r) = e^{- \lambda} \dfrac{\lambda^{r}} {r!},\qquad = 0, 1, 2, . . .,+P(X=r) = e^{- \lambda} \dfrac{\lambda^{r}} {r!},\qquad = 0, 1, 2, . . .,
 \end{eqnarray*} \end{eqnarray*}
  
Line 951: Line 1233:
 > plot(dpois(x=1:60, lambda=30), type = "l") > plot(dpois(x=1:60, lambda=30), type = "l")
 > </code> > </code>
 +
 +위에서 언급한 
 +
 +\begin{eqnarray*}
 +\sum_{r=0}^{\infty} e^{- \lambda} \dfrac{\lambda^{r}} {r!}  
 +& = & e^{- \lambda} \sum_{r=0}^{\infty} \dfrac{\lambda^{r}} {r!}  \\
 +& = & e^{- \lambda} \left(1 + \lambda + \dfrac{\lambda^{2}}{2!} + \dfrac{\lambda^{3}}{3!} + . . . \right) \\
 +& = & e^{- \lambda}e^{\lambda} \\
 +& = & 1
 +\end{eqnarray*}
 +
 +에서 1 이란 이야기는 아래 그림의 그래프가 전체가 1이 됨을 의미함. 즉 위에서는 1부터 60까지 갔지만, 1부터 무한대로 하면 완전한 분포곡선이 되는데 이것이 1이라는 뜻 (가령 dpois(x=1:1000, lambda=30)과 같은 케이스). 
 +
 +
 [{{:b:head_first_statistics:pasted:20191107-095627.png|Figure 1. lambda=30}}] [{{:b:head_first_statistics:pasted:20191107-095627.png|Figure 1. lambda=30}}]
  
-lambda가 클 수록 좌우대칭의 종형분포를 이루고 ((Figure 1)), 작을 수로 오른 쪽으로 편향된 (skewed to the right) 혹은 양의방향으로 편향된(positively skewed) 분포를 ((Figure 2)) 이룬다.+lambda가 클 수록 좌우대칭의 종형분포를 이루고 ((Figure 1)), 작을 수록 오른 쪽으로 편향된 (skewed to the right) 혹은 양의방향으로 편향된(positively skewed) 분포를 ((Figure 2)) 이룬다.
  
 <code> <code>
Line 986: Line 1282:
  
 __1. What’s the probability of the machine not malfunctioning next week?__ __1. What’s the probability of the machine not malfunctioning next week?__
 +
 $\lambda = 3.4$ $\lambda = 3.4$
-$ malfunctioning = 0$+$\text{malfunctioning= 0$
  
 \begin{eqnarray*} \begin{eqnarray*}
-P(X=0) & = & \displaytype\frac{e^{-3.4}*3.4^{0}} {0!}  \\ +P(X=0) & = & e^{-3.4} * \frac{3.4^{0}} {0!}  \\ 
-& = & \displaytype e^{3.4} \\+& = & e^{-3.4} \\
 & = & 0.03337327  & = & 0.03337327 
 \end{eqnarray*} \end{eqnarray*}
  
 <code> <code>
 +# R 에서 계산
 > e^(-3.4) > e^(-3.4)
 +[1] 0.03337327
 +
 +# 혹은 
 +> dpois(0, 3.4)
 [1] 0.03337327 [1] 0.03337327
  
 </code> </code>
 +
 +포아송 분포를 따르는 확률에서 아무것도 일어나지 않을 때의 확률은 e<sup>-lambda </sup>가 된다. 예를 들면 119 전화가 한시간에 걸려오는 확률이 5번이라고 할 때,  지난 한 시간동안 한 건의 전화도 없을 확률은? 
 +\begin{eqnarray*}
 +P(X=0) & = & e^{-5} * \frac{5^{0}} {0!}  \\
 +& = & e^{-5} \\
 +& = & 0.006737947
 +\end{eqnarray*}
 +<code>
 +> lamba <- 5
 +> e <- exp(1)
 +> px.0 <- e^(-lamba)
 +
 +> px.0
 +[1] 0.006737947
 +
 +# or 
 +> dpois(0,5)
 +[1] 0.006737947
 +</code>
 +
 +
  
 __2. What’s the probability of the machine malfunctioning three times next week?__ __2. What’s the probability of the machine malfunctioning three times next week?__
Line 1006: Line 1329:
 x <- 3 x <- 3
 e <- exp(1) e <- exp(1)
-ans <- ((e^(-l))*l^x)/x!+ans <- ((e^(-l))*l^x)/factorial(x)
 </code> </code>
  
Line 1022: Line 1345:
 > dpois(x=3, lambda=3.4) > dpois(x=3, lambda=3.4)
 [1] 0.2186172 [1] 0.2186172
 +</code>
 +
 +마찬가지로 적어도 3번까지 고장나는 경우는 0, 1, 2, 3을 포함하므로 
 +<code>
 +> sum(dpois(c(0:3), lambda=3.4))
 +[1] 0.5583571
 +
 </code> </code>
  
Line 1031: Line 1361:
 \end{eqnarray*} \end{eqnarray*}
  
 +[[:mean and variance of Poisson distribution]]
 ===== Two Poisson distribution cases ===== ===== Two Poisson distribution cases =====
  
Line 1076: Line 1407:
 **How did Kate find the probability so quickly, and avoid the error on her calculator?** **How did Kate find the probability so quickly, and avoid the error on her calculator?**
 </WRAP> </WRAP>
 +우선 위의 문제를 binomial distribution 문제로 생각하면 답은 
 +\begin{eqnarray*}
 +P(r=15) & = & _{100}C_{15} * 0.1^{15} * 0.99^{85}\\
 +\end{eqnarray*}
 +라고 볼 수 있다. 
  
 \begin{eqnarray} \begin{eqnarray}
Line 1107: Line 1443:
 > a*b*c > a*b*c
 [1] 0.03268244 [1] 0.03268244
 +
 +</code>
 +위가 답이긴 하지만 limited calculator 로는 
 +x ~ b (n, p)이고
 +b(100, 0.1)이므로 
 +n*p = 10 = lambda 
 +따라서 Pois 분포로 보는 답은 
 +lambda = 10 일때 P(r=15)값을 구하는 문제로 
 +
 +\begin{eqnarray*}
 +P(r = 15) & = & e^{-10} * \frac {10^{15}}{15!} \\
 +& = & 0.0347180
 +\end{eqnarray*}
 +<code>
 +> dpois(x=15, lambda=10)
 +[1] 0.03471807
  
 </code> </code>
Line 1223: Line 1575:
  
 <WRAP box> <WRAP box>
-2. On average, 1 bus stops at a certain point every 15 minutes. What’s the probability that no buses will turn up in a single 15 minute interval?+2. On average, 1 bus stops at a certain point every 15 minutes. What’s the probability that __<fc #ff0000>no buses</fc>__ will turn up in a single 15 minute interval?
  
 위는 Poisson distribution 문제이므로 기대값과 분산값은 각각 lambda 값인 1 (15분마다 1대씩 버스가 온다고 한다) 위는 Poisson distribution 문제이므로 기대값과 분산값은 각각 lambda 값인 1 (15분마다 1대씩 버스가 온다고 한다)
b/head_first_statistics/geometric_binomial_and_poisson_distributions.1573089991.txt.gz · Last modified: by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki