---
# System prepended metadata

title: 【統計學】01 機率
tags: [統計]

---

# 【統計學】01 機率

[TOC]

---

### 機率和統計的關係
<img src='https://ucarecdn.com/d6dd8e57-66be-4df3-8ad1-7b9b3de6fed9/'><br>

### Define
- **Statistical experiment (統計實驗)** : 實驗結果無法在事前得知。
- <span style="color:green">**Sample Space** ( $S$ )</span> : 所有可能結果。
- <span style="color:red">**Event**</span> : 發生的事件，是 Sample Space 的子集 (Subset)，<span style="color:red">發生事件</span>的<span style="color:blue">機率(Probability)</span> 為 <span style="color:blue">$P($<span style="color:red">$E$</span>$)$</span>。

---

## Part 1. Event and Probability
- $E$ = 發生事件 $E$
- $E'$ = **未**發生事件 $E$，$E' = \{x \mid x \in S, x \not\in E\}$

<br>

### Event 與 Event 關係
假設有 Event A 和 Event B：
- Intersection (交集) : $A \cap B = \{x \mid x \in A \text{ and }x \in B \}$
- Union (聯集) : $A \cup B = \{x \mid x \in A \text{ or } x \in B \}$
- Disjoint (互斥) : $A \cap B = \emptyset$

以是否獨立的角度：
- Independent (獨立) : $A$ 的發生與 $B$ 的發生無關，兩者不互相影響。
- Dependent (相依) : $A$ 的發生會影響 $B$ 的發生，反之亦然。
- Mutually Exclusive (互斥) : $A$ 與 $B$ 必定不會同時發生

<br>

### Prability of Event E
<span style="color:blue">Probability</span> of <span style="color:red">Event $E$</span> 是指發生<span style="color:red">事件 $E$</span> 的**所有**<span style="color:blue">機率</span>。

- ==$0 \le P(E) \le 1$、$P(\emptyset) = 0$ and $P(S) = 1$==。
- 如果所有 sample points ($N$) in $S$ 的發生機率都是「相等」的，則 $n$ 個 sample point 的發生機率為 $\frac{n}{N}$。
- 如果 Event A 和 Event B 是 ==**disjoint**，則$P(A \cap B) = 0$==，故：
    $P(A \cup B) = P(A) + P(B) - P(A \cap B) = P(A) + P(B) - 0 = P(A) + P(B)$<br>
<img src='https://miro.medium.com/v2/resize:fit:1400/1*JBJSxM3hLaQNNhfGpKNsRA.png'>
    
<br>

---

## Part 2. 條件機率 (Conditional Probability)
假設有一條件機率 $P(B \mid A)$，則代表**已知發生 Event A** 的情況下，發生 Event B 的機率 $\implies P(B \mid A) = \frac{P(A \cap B)}{P(A)}$<br><br>

==$P(A \cap B) = P(A) \cdot P(B \mid A) = P(B) \cdot P(A \mid B)$==<br>
Since **conditional probability (條件機率)** $P(B \mid A) = \frac{P(A \cap B)}{P(A)}$，故：
    $P(A \cap B) = P(A)⋅P(B∣A) = P(A) \cdot \frac{P(A \cap B)}{P(A)} = P(A \cap B)$
<br>

==$P(A \mid B) \ge P(A \cap B)$== : 因為 $P(B) < P(S)$，所以同樣的 $P(A \cap B)$ 在條件機率下除以小數後，會放大。

<img src='https://media.geeksforgeeks.org/wp-content/uploads/20211215115830/venndiag2.jpg'><br>

<br>

### Confusion Matrix
- True Positive (TP)
- True Negative (TN)
- False Positive (FP)
- False Negative (FN)
<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <img style="width:300px" src='https://images.squarespace-cdn.com/content/v1/5acbdd3a25bf024c12f4c8b4/1599600721024-AFF6NJYV5JT3A3PP4NJ2/Confusion+Matrix+Visualization.png'>

<br>

---

## Part 3. Permutation (排列) and Combination (組合)

### Permutation
排列 **n 個不同** 的物件 $\implies n \cdot (n-1) \cdot (n-2) \cdot \cdots \cdot1 = n!$

排列 **n個相同** 的物件 (例如 3顆藍球和2顆足球) $\to$ 方法類似排列：$\dfrac{(3+2)!}{3!2!}$

### Combination
從 **n 個物件** 中 **選取 r 個** 物件 $\implies \pmatrix{n\\r} = \dfrac{n!}{r!(n-r)!}$ 

<br>

---

## Part 4. Theorem of Total Probability (全事件機率)

### Partition
$E_1, E_2, \dots ,E_k$ form a partition of the sample space $S$ iff：
- <span style="color:blue">$E_i \cap E_j \ne 0 \text{ for } i \ne j$</span>
- <span style="color:blue">$E_1 \cup E_2 \cup \dots \cup E_k = S$</span>

<br>

### Theorem of Total Probability
如果 $A_1, A_2, \dots ,A_k$ form a partition of the sample space $S$, 則：

==$P(B) = P(B \cap A_1) + P(B \cap A_2) + \cdots + P(B \cap A_k) = \sum^k_{i=1} P(B \cap A_i)$==

$\therefore$ 利用全機率定理換成 $P(B \cap A_i)$ 形式，以便使用 Product Rule : <span style="color:red">$P(A \cap E_i) = P(B \mid A_i) \cdot P(A_i)$</span>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <img src='https://img.brainkart.com/imagebk37/i906kI1.jpg'><br>

<br>

---

## Part 5. Independent Event (獨立事件)

若 $P(A \mid B) = P(A) \text{ or } P(B \mid A) = P(B)$，則 $A$ 與 $B$ 為 <span style="color:red">Independent</span>。

==$P(B | A) = P(B) \iff P(A \cap B) = P(A) \cdot P(B)$==
<br>

If $A$ and $B$ are independent, then : 
- $A'$ and $B$ are independent
- $A$ and $B'$ are independent
- $A'$ and $B'$ are independent

<br>

### Proof
#### Key 
使用與結果有關的其他公式證明

#### Example
- $A'$ and $B$ are independent
    方法 : 全機率定理 $P(B)= P(B \cap A) + P(B \cap A')$

- $A'$ and $B'$ are independent
    方法 : $1 - P(A \cup B) = P(A' \cap B')$

<br>

---

## Part 6. Baye's Theorem (貝式定理)

假設有兩事件，分別為 Event $A$ and Event $B$：
- Prior Probability 事前機率 $\to P(A)、P(A')$
- Likelyhoods 概率相似度，即事件發生在不同情況下 $\to P(B \mid A)、P(B \mid A')$
- Posterior Probability 事後機率 $\to P(A \mid B)、P(A' \mid B)$

<img src='https://miro.medium.com/v2/resize:fit:1400/0*x96Ttm0fzmY6eQxH.jpg'><br><br>

$P(A \mid B) = \dfrac{P(A \cap B)}{P(B)} = \dfrac{P(B \mid A)P(A)}{P(B)} = \dfrac{P(B \mid A)P(A)}{P(B \cap A')+P(B \cap A)}$<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $= \dfrac{P(B \mid A)P(A)}{P(B \mid A')P(A')+P(B \mid A)P(A)}$<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $= \dfrac{P(A)}{P(A)+\color{red}{P(B \mid A') / P(B \mid A)}}$<br><br>

由 $\color{red}{P(B \mid A') / P(B \mid A)}$ 來決定<span style="color:blue">「事後機率」</span>與<span style="color:green">「事前機率」</span>的關係 : 
- $\frac{P(B \mid A')}{P(B \mid A)} > 1 \implies$ <span style="color:blue">事前機率</span> $>$ <span style="color:green">事後機率</span>
-  $\frac{P(B \mid A')}{P(B \mid A)} < 1 \implies$ <span style="color:blue">事前機率</span> $<$ <span style="color:green">事後機率</span>
-  $\frac{P(B \mid A')}{P(B \mid A)} = 1 \implies$ <span style="color:blue">事前機率</span> $=$ <span style="color:green">事後機率</span>
<br>

$\therefore$ ==$\color{red}{P(A\mid B) : P(A' \mid B) = P(B \mid A)\cdot P(A) : P(B \mid A') \cdot P(A')}$==<br><br>

### Example

<img src='https://i0.wp.com/ucanalytics.com/blogs/wp-content/uploads/2015/11/Bayes-Theorem-Shady-Gambler-1.jpg'><br>