定义
对于投骰子来说,结果是1到6。得到任何一个结果的概率是相等的,这就是均匀分布的基础。与伯努利分布不同,均匀分布的所有可能结果的n个数也是相等的。
如果变量X是均匀分布的,则密度函数可以表示为:
你可以看到,均匀分布曲线的形状是一个矩形,这也是均匀分布又称为矩形分布的原因。其中,a和b是参数。
花店每天销售的花束数量是均匀分布的,最多为40,最少为10。我们来计算一下日销售量在15到30之间的概率。
日销售量在15到30之间的概率为(30-15)*(1/(40-10)) = 0.5
同样地,日销售量大于20的概率为 = 0.667
# IMPORTS
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
import matplotlib.style as style
from IPython.core.display import HTML
# PLOTTING CONFIG
%matplotlib inline
style.use('fivethirtyeight')
plt.rcParams["figure.figsize"] = (14, 7)
plt.figure(dpi=100)
# PDF
plt.plot(np.linspace(-4, 4, 100),
stats.uniform.pdf(np.linspace(-4, 4, 100))
)
plt.fill_between(np.linspace(-4, 4, 100),
stats.uniform.pdf(np.linspace(-4, 4, 100)),
alpha=.15)
# CDF
plt.plot(np.linspace(-4, 4, 100),
stats.uniform.cdf(np.linspace(-4, 4, 100)),
)
# LEGEND
plt.text(x=-1.5, y=.7, s="pdf (normed)", rotation=65, alpha=.75, weight="bold", color="#008fd5")
plt.text(x=-.4, y=.5, s="cdf", rotation=55, alpha=.75, weight="bold", color="#fc4f30")
# TICKS
plt.tick_params(axis = 'both', which = 'major', labelsize = 18)
plt.axhline(y = 0, color = 'black', linewidth = 1.3, alpha = .7)
# TITLE
plt.text(x = -5, y = 1.25, s = "Uniform Distribution - Overview",
fontsize = 26, weight = 'bold', alpha = .75)
改变参数画出不同的均匀分布如图所示:
plt.figure(dpi=100)
# PDF loc=0, scale=1
plt.plot(np.linspace(-8, 8, 100),
stats.uniform.pdf(np.linspace(-8, 8, 100),loc=0, scale=1),
)
plt.fill_between(np.linspace(-8, 8, 100),
stats.uniform.pdf(np.linspace(-8, 8, 100),loc=0, scale=1),
alpha=.15,
)
# PDF loc=0, scale=2
plt.plot(np.linspace(-8, 8, 100),
stats.uniform.pdf(np.linspace(-8, 8, 100), loc=0, scale=2),
)
plt.fill_between(np.linspace(-8, 8, 100),
stats.uniform.pdf(np.linspace(-8, 8, 100),loc=0, scale=2),
alpha=.15,
)
# PDF loc=-3, scale=3
plt.plot(np.linspace(-8, 8, 100),
stats.uniform.pdf(np.linspace(-4, 4, 100), loc=-3, scale=3),
)
plt.fill_between(np.linspace(-8, 8, 100),
stats.uniform.pdf(np.linspace(-4, 4, 100),loc=-3, scale=3),
alpha=.15,
)
# LEGEND
plt.text(x=-1, y=.65, s="loc=0, scale=1", rotation=65, alpha=.75, weight="bold", color="#008fd5")
plt.text(x=1, y=.65, s="loc=0, scale=2", rotation=65, alpha=.75, weight="bold", color="#fc4f30")
plt.text(x=-3, y=.65, s="loc=-3, scale=3", rotation=65, alpha=.75, weight="bold", color="#e5ae38")
# TICKS
plt.tick_params(axis = 'both', which = 'major', labelsize = 18)
plt.axhline(y = 0, color = 'black', linewidth = 1.3, alpha = .7)
# TITLE,
plt.text(x = -5, y = 1.1, s = "Uniform Distribution - loc and scale",
fontsize = 26, weight = 'bold', alpha = .75)