Distribution Is All You Need – 2. Poisson Distribution

【附录】

import random
import operator as op
from functools import reduce
import numpy as np

import matplotlib.pyplot as plt
import seaborn as sns

sns.set_style("whitegrid")
custom_params = {"axes.spines.right": False, "axes.spines.top": False}
sns.set_theme(style="ticks", rc=custom_params)


####################
# 泊松分布
import math

def poisson(x, lbd):
    sam = np.exp(-lbd)
    y = []
    for k in x:
        denom = reduce(op.mul, range(1, k+1))
        res = sam * (math.pow(lbd, k) / denom)
        y.append(res)
    return y

plt.figure(dpi=150)
k = [1, 4, 10]

for lbd in k:
    x = np.arange(1, 2*k[-1]+1)
    y = poisson(x, lbd)
    plt.scatter(x, y, s=15, label=f'$\lambda={lbd}$')

plt.legend()
plt.ylabel('Probability')
plt.xlabel('k success')
plt.show()

发表评论

您的电子邮箱地址不会被公开。