# 三大离散分布

ChatGPT 画的，凑活着用。

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from scipy.stats import binom


## Bernouli Distribution #

$$f(k; p) = p^k (1 - p)^{1-k} \text{ for } k \in \{0,1\}$$

p = 0.7 # p when x = 1
bernoulli_values = [0, 1]
probs = [1-p, p]
plt.bar(bernoulli_values, probs, tick_label = ['Failure (0)', 'Success (1)'])
plt.title("Bernoulli Distribution")
plt.xlabel("Random Variable X")
plt.ylabel("Probability Mass Function")
# plt.savefig("/cn/blog/2024-03-23-discrete-distributions_files/bernoulli_distribution.png")

Text(0, 0.5, 'Probability Mass Function')


## Binomial Distribution #

### 排列 Permutations #

$$_5 P_2 = \frac{5!}{(5 - 2)!} = \frac{5\times 4 \times 3 \times 2 \times 1}{3 \times 2 \times 1} = 5 \times 4 = 20$$

### 组合 Combinations #

$$_5 C_2 = {5 \choose 2} = \frac{5!}{2!(5 - 2)!} = \frac{5 \times 4 \times 3 \times 2 \times 1}{2 \times 1 \times 3 \times 2 \times 1} = 5 \times 2 = 10$$

### 抛硬币 #

$${3 \choose 2} = \frac{3!}{2! (3 - 2)!} = \frac{3 \times 2}{2 \times 1} = 3$$

$$P_k = {n \choose k}p^k (1-p)^{n - k}$$

# Parameters for the binomial distributions
n1, p1 = 20, 0.4
n2, p2 = 20, 0.7
n3, p3 = 40, 0.5

# Generate the values for the x-axis
# 知道 n1, p1 之后，我们可以画出 pmf，如果是柱状图，那么所有柱子的面积和为1.
# x = binom.ppf(p, n1, p1) 算的是当 x 到多少时，之前的柱子面积和就达到了 p
# 我们用此算出 p 为 0.01 和 0.99 时的 x 值，这样对于非常不可能的 x 值我们就不用花了
# 因为其值 (pmf) 太小了

x1 = np.arange(binom.ppf(0.01, n1, p1), binom.ppf(0.99, n1, p1))
x2 = np.arange(binom.ppf(0.01, n2, p2), binom.ppf(0.99, n2, p2))
x3 = np.arange(binom.ppf(0.01, n3, p3), binom.ppf(0.99, n3, p3))

# Calculate the probabilities for each x-value
pmf1 = binom.pmf(x1, n1, p1)
pmf2 = binom.pmf(x2, n2, p2)
pmf3 = binom.pmf(x3, n3, p3)

# Create the plot
plt.figure(figsize=(10,6))

# Plot each binomial distribution
plt.scatter(x1, pmf1, color='blue', label='p=0.4 and n=20')
plt.scatter(x2, pmf2, color='green', label='p=0.7 and n=20')
plt.scatter(x3, pmf3, color='red', label='p=0.5 and n=40')

# Labels and title
plt.ylabel('Probability Mass Function')
plt.title('Binomial Distribution Comparisons')
plt.legend()

# Show the plot
plt.show()


## Poisson Distribution 泊松分布 #

$$P_k = {60 \choose k} \left(\frac{\lambda}{60} \right)^k \left(1 - \frac{\lambda}{60} \right)^{(60 - k)}$$

from matplotlib.patches import Rectangle

# Set up the figure and axis
fig, ax = plt.subplots(figsize=(12, 1))
ax.set_xlim(0, 60)
ax.set_ylim(0, 1)

# Hide the axes
ax.axis('off')

# Draw a long rectangle to represent the line
line = Rectangle((0, 0.4), 60, 0.2, linewidth=1, edgecolor='black', facecolor='none')

# Choose two random parts to write '1' and have a dashed shadow
random_parts = np.random.choice(range(60), size=2, replace=False)

# Draw rectangles for each part and label them
for i in range(60):
# Determine the label for the part
label = '1' if i in random_parts else '0'

# Draw dashed shadow for parts with label '1'
if label == '1':
shadow = Rectangle((i, 0.4), 1, 0.2, linewidth=1, linestyle='--', edgecolor='black', facecolor='none')

# Add the label below the line
ax.text(i + 0.5, 0.1, label, ha='center', va='center', fontsize=8)

# Show the plot
plt.show()



\begin{align*} P_k &= {n \choose k} \left(\frac{\lambda}{n} \right)^k \left(1 - \frac{\lambda}{n} \right)^{(n - k)} \\ &= \frac{n!}{(n-k)! k!} \cdot \frac{\lambda^k}{n^k}\cdot \frac{\left(1 - \frac{\lambda}{n} \right)^n}{\left(1 - \frac{\lambda}{n} \right)^k} \\ \end{align*} \tag{1}

$n \to \infty$

$$\frac{n!}{(n-k)!} \to n^k$$

$$\left(1 - \frac{\lambda}{n} \right)^k \to 1$$

$$\left(1 - \frac{\lambda}{n} \right)^n \to e^{-\lambda}$$

\begin{align*} P(X = k) &= \frac{n^k}{n^k}\cdot \frac{\lambda^k}{k!} \cdot \frac{e^{-\lambda}}{1}\\ &= \frac{\lambda^k \cdot e^{-\lambda}}{k!} \end{align*} \tag{2}

### 泊松分布的用处 #

$$P_{100} = {10^5 \choose 10^2}(10^{-6})^{100} (1-10^{-6})^{10^5 - 10^2}$$

$$\lambda = 10^5 * 10^{-6} = 0.1$$

$$P(X = 100) = \frac{0.1^{100} \cdot e^{-0.1}}{100!}$$

#统计