貝氏定理

Python 機率基礎

Alexander A. Ramírez M.

CEO @ Synergy Vision

兩次擲硬幣的機率

Python 機率基礎

獨立事件的 P(A and B)

$$ $$

$$\color{red}{P(A\ and\ B)=P(A)P(B)}$$

Python 機率基礎

相依事件的 P(A and B)

$$ $$

$$P(A\ and\ B)=P(A)P(B)$$ $$P(A\ and\ B)=\color{blue}{P(A)P(B|A)}$$

Python 機率基礎

相依事件的 P(A and B)(續)

$$ $$

$$P(A\ and\ B)=P(A)P(B)$$ $$P(A\ and\ B)=P(A)P(B|A)$$ $$P(B\ and\ A)=\color{red}{P(B)P(A|B)}$$

Python 機率基礎

P(A and B) 等於 P(B and A)

$$ $$

$$P(A\ and\ B)=P(A)P(B)$$ $$\color{gray}{P(A\ and\ B)}=\color{blue}{P(A)P(B|A)}$$ $$\color{gray}{P(B\ and\ A)}=\color{red}{P(B)P(A|B)}$$

Python 機率基礎

P(A and B) 等於 P(B and A)(續)

$$ $$

$$P(A\ and\ B)=P(A)P(B)$$ $$\color{gray}{P(A\ and\ B)}=\color{blue}{P(A)P(B|A)}$$ $$\color{gray}{P(B\ and\ A)}=\color{red}{P(B)P(A|B)}$$ $$\color{blue}{P(A)P(B|A)}=\color{gray}{P(A\ and\ B)}=\color{gray}{P(B\ and\ A)}=\color{red}{P(B)P(A|B)}$$

Python 機率基礎

貝氏關係式

$$ $$

$$P(A\ and\ B)=P(A)P(B)$$ $$\color{gray}{P(A\ and\ B)}=\color{blue}{P(A)P(B|A)}$$ $$\color{gray}{P(B\ and\ A)}=\color{red}{P(B)P(A|B)}$$ $$\color{blue}{P(A)P(B|A)}=\color{gray}{P(A\ and\ B)}=\color{gray}{P(B\ and\ A)}=\color{red}{P(B)P(A|B)}$$ $$\color{blue}{P(A)P(B|A)}=\color{red}{P(B)P(A|B)}$$

Python 機率基礎

貝氏定理

$$ $$

$$\color{blue}{P(A)P(B|A)}=\color{red}{P(B)P(A|B)}$$ $$ $$ $$\Longrightarrow \large{\color{red}{P(A|B)} = \frac{\color{blue}{P(A)P(B|A)}}{\color{red}{P(B)}}}$$

Python 機率基礎

全機率

所有供應商的瑕疵品(維恩圖)

$$P(D) = P(V_1\ and\ D) + P(V_2\ and\ D) + P(V_3\ and\ D)$$

Python 機率基礎

全機率(續)

$$ $$ $$P(D) = \color{red}{P(V_1\ and\ D)} + \color{blue}{P(V_2\ and\ D)} + \color{#9FAAC3}{P(V_3\ and\ D)}$$

$$\color{red}{P(V_1\ and\ D)} = \color{red}{P(V_1)P(D|V_1)}$$ $$\color{blue}{P(V_2\ and\ D)} = \color{blue}{P(V_2)P(D|V_2)}$$ $$\color{#9FAAC3}{P(V_3\ and\ D)} = \color{#9FAAC3}{P(V_3)P(D|V_3)}$$

$$P(D)=\color{red}{P(V_1)P(D|V_1)}+\color{blue}{P(V_2)P(D|V_2)}+\color{#9FAAC3}{P(V_3)P(D|V_3)}$$

Python 機率基礎

全機率(續)

所有供應商的瑕疵品(維恩圖)

$$P(D)=\color{red}{P(V_1)P(D|V_1)}+\color{blue}{P(V_2)P(D|V_2)}+\color{#9FAAC3}{P(V_3)P(D|V_3)}$$

Python 機率基礎

貝氏公式

$$P(A|B) = \frac{P(A)P(B|A)}{P(B)}$$

Python 機率基礎

貝氏公式(續)

  • 貝氏公式:

$$P(A|B) = \frac{P(A)P(B|A)}{P(B)}$$

  • 已知零件為瑕疵品,來自供應商 i 的機率:

$$P(V_i|D) = \frac{P(V_i)P(D|V_i)}{P(D)}$$

Python 機率基礎

貝氏公式(續)

  • 貝氏公式: $$P(A|B) = \frac{P(A)P(B|A)}{P(B)}$$

  • 已知零件為瑕疵品,來自供應商 i 的機率:

$$P(V_i|D) = \frac{P(V_i)P(D|V_i)}{P(D)}$$ $$ $$ $$P(V_1|D) = \frac{P(V_1)P(D|V_1)}{P(V_1)P(D|V_1)+P(V_2)P(D|V_2)+P(V_3)P(D|V_3)}$$

Python 機率基礎

貝氏定理的視覺化呈現

所有供應商的瑕疵品(維恩圖)

$$P(V_1|D) = \frac{P(V_1)P(D|V_1)}{P(V_1)P(D|V_1)+P(V_2)P(D|V_2)+P(V_3)P(D|V_3)}$$

Python 機率基礎

貝氏定理的視覺化呈現(續)

供應商 1 的瑕疵品(維恩圖)

$$P(V_1|D) = \frac{\color{red}{P(V_1)P(D|V_1)}}{P(V_1)P(D|V_1)+P(V_2)P(D|V_2)+P(V_3)P(D|V_3)}$$

Python 機率基礎

貝氏定理的視覺化呈現(續)

供應商 1 的瑕疵品(維恩圖)

$$P(V_1|D) = \frac{P(V_1)P(D|V_1)}{\color{red}{P(V_1)P(D|V_1)}+P(V_2)P(D|V_2)+P(V_3)P(D|V_3)}$$

Python 機率基礎

貝氏定理的視覺化呈現(續)

供應商 1 與 2 的瑕疵品(維恩圖)

$$P(V_1|D) = \frac{P(V_1)P(D|V_1)}{P(V_1)P(D|V_1)+\color{red}{P(V_2)P(D|V_2)}+P(V_3)P(D|V_3)}$$

Python 機率基礎

貝氏定理的視覺化呈現(續)

供應商 1、2 與 3 的瑕疵品(維恩圖)

$$P(V_1|D) = \frac{P(V_1)P(D|V_1)}{P(V_1)P(D|V_1)+P(V_2)P(D|V_2)+\color{red}{P(V_3)P(D|V_3)}}$$

Python 機率基礎

貝氏定理的視覺化呈現(續)

供應商 1、2 與 3 的瑕疵品(維恩圖)

$$P(V_1|D) = \frac{P(V_1)P(D|V_1)}{P(V_1)P(D|V_1)+P(V_2)P(D|V_2)+P(V_3)P(D|V_3)}$$

Python 機率基礎

貝氏定理的視覺化呈現(續)

供應商 1、2 與 3 的瑕疵品(維恩圖)

$$P(V_1|D) = \frac{P(V_1)P(D|V_1)}{P(V_1)P(D|V_1)+P(V_2)P(D|V_2)+P(V_3)P(D|V_3)}$$

Python 機率基礎

貝氏定理的視覺化呈現(續)

供應商 1 的瑕疵品/所有供應商的瑕疵品(維恩圖)

$$P(V_1|D) = \frac{\color{red}{P(V_1)P(D|V_1)}}{...}$$

Python 機率基礎

貝氏定理的視覺化呈現(續)

供應商 1 的瑕疵品/所有供應商的瑕疵品(維恩圖)

$$P(V_1|D) = \frac{\color{red}{P(V_1)P(D|V_1)}}{\color{red}{P(V_1)P(D|V_1)}+\color{blue}{P(V_2)P(D|V_2)}+\color{#9FAAC3}{P(V_3)P(D|V_3)}}$$

Python 機率基礎

工廠與零件範例(Python)

某種電子零件由 3 家供應商 V1、V2、V3 生產。

V1 產量佔一半,V2 與 V3 各佔 25%。在已由 V1 生產的前提下,零件為瑕疵品的機率為 1%;V2 為 2%,V3 為 3%。

  • 已知該零件為瑕疵品,它由 V1 生產的機率是多少?
Python 機率基礎

工廠與零件範例(Python,續)

  • 已知零件為瑕疵品,求它由 V1 生產的機率。
P_V1 = 0.5
P_V2 = 0.25
P_V3 = 0.25
P_D_g_V1 = 0.01
P_D_g_V2 = 0.02
P_D_g_V3 = 0.03
P_Damaged = P_V1 * P_D_g_V1 + P_V2 * P_D_g_V2 + P_V3 * P_D_g_V3
Python 機率基礎

工廠與零件範例(Python,續)

P_V1_g_D = (P_V1 * P_D_g_V1) / P_Damaged # P(V1|D) calculation
print(P_V1_g_D)

隨機抽到的瑕疵零件由 V1 生產的機率為:

0.285714285714
Python 機率基礎

用貝氏來練習吧

Python 機率基礎

Preparing Video For Download...