贝叶斯法则

Python 概率基础

Alexander A. Ramírez M.

CEO @ Synergy Vision

两次抛硬币的概率

Python 概率基础

独立事件的 P(A 和 B)

$$ $$

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

Python 概率基础

依赖事件的 P(A 和 B)

$$ $$

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

Python 概率基础

依赖事件的 P(A 和 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 和 B) 等于 P(B 和 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 和 B) 等于 P(B 和 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的损坏零件占供应商1、2、3损坏零件的比例维恩图

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

Python 概率基础

贝叶斯法则的可视化(续)

供应商1的损坏零件占供应商1、2、3损坏零件的比例维恩图

$$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)

某电子零件由三家供应商 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)
print(P_V1_g_D)

随机抽到的一个损坏零件来自 V1 的概率为:

0.285714285714
Python 概率基础

让我们练习贝叶斯

Python 概率基础

Preparing Video For Download...