Basis van kansrekening in Python
Alexander A. Ramírez M.
CEO @ Synergy Vision

$$ $$
$$\color{red}{P(A\ and\ B)=P(A)P(B)}$$
$$ $$
$$P(A\ and\ B)=P(A)P(B)$$ $$P(A\ and\ B)=\color{blue}{P(A)P(B|A)}$$
$$ $$
$$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)}$$
$$ $$
$$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)}$$
$$ $$
$$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)}$$
$$ $$
$$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)}$$
$$ $$
$$\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)}}}$$

$$P(D) = P(V_1\ and\ D) + P(V_2\ and\ D) + P(V_3\ and\ D)$$
$$ $$ $$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)}$$

$$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)}$$
$$P(A|B) = \frac{P(A)P(B|A)}{P(B)}$$
$$P(A|B) = \frac{P(A)P(B|A)}{P(B)}$$
$$P(V_i|D) = \frac{P(V_i)P(D|V_i)}{P(D)}$$
Formule van Bayes: $$P(A|B) = \frac{P(A)P(B|A)}{P(B)}$$
De kans dat een onderdeel van leverancier i komt, gegeven dat het beschadigd is:
$$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)}$$

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

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

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

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

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

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

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

$$P(V_1|D) = \frac{\color{red}{P(V_1)P(D|V_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)}}$$
Een bepaald elektronisch onderdeel wordt gemaakt door drie leveranciers: V1, V2 en V3.
De helft komt van V1; V2 en V3 maken elk 25%. De kans op schade gegeven productie door V1 is 1%, voor V2 2% en voor V3 3%.
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
P_V1_g_D = (P_V1 * P_D_g_V1) / P_Damaged # P(V1|D) calculation
print(P_V1_g_D)
Een willekeurig gekozen beschadigd onderdeel is met kans {{4}} door V1 gemaakt.
0.285714285714
Basis van kansrekening in Python