1.3概率分布函数的图形(day4)

1.3.1 正态分布的图形

'''
正态分布的概率密度函数图形,根据不同的均值和标准差绘制曲线
'''
​
plt.figure(figsize=(8,6))
x = np.arange(-10.,10.,0.01)
y = st.norm.pdf(x,1.0,0.5)
plt.annotate("mu=1,sigma=0.5",xy=(1.0,st.norm.pdf(1.0,1.0,0.5)),
             xytext=(0.3,st.norm.pdf(1.0,1.0,0.5)+0.2),
             weight="bold",color='steelblue',fontsize=14,
             arrowprops=dict(arrowstyle="->",connectionstyle="arc3", color="steelblue"))
y1 = st.norm.pdf(x,0.0,1.0)
plt.annotate("mu=0,sigma=1",xy=(0.0,st.norm.pdf(0.0,0.0,1.0)),
             xytext=(-3.8,st.norm.pdf(0.0,0.0,1.0)+0.2),
             weight="bold",color='darkorange',fontsize=14,
             arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="darkorange"))
y2 = st.norm.pdf(x,0.5,2.0)
plt.annotate("mu=0.5,sigma=2",xy=(3.0,st.norm.pdf(3.0,0.5,2.0)),
             xytext=(3.0,st.norm.pdf(3.0,0.5,2.0)+0.2),
             weight="bold",color='g',fontsize=14,
             arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="g"))
y3 = st.norm.pdf(x,-0.5,3.0)
plt.annotate("mu=-0.5,sigma=3",xy=(-2.25,st.norm.pdf(-2.25,-0.5,3.0)),
             xytext=(-6,st.norm.pdf(-6,-0.5,3.0)+0.2),
             weight="bold",color='r',fontsize=14,
             arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="r"))
plt.plot(x,y)
plt.plot(x,y1)
plt.plot(x,y2)
plt.plot(x,y3)
plt.title("正态分布概率密度函数",size=14)
plt.xlabel("随机变量",size=14)
plt.ylabel("正态分布概率密度",size=14)
plt.ylim(-0.02,1.1)
plt.show()

plt.annotate代码解释如下:

plt.annotate("degree=1",xy=(3,st.chi2.pdf(3,1)),
             xytext=(0.7,st.chi2.pdf(3,1)-0.04),weight="bold",color='steelblue',fontsize=14,
             arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="steelblue"))

这段代码是在使用matplotlib库中的annotate()函数对概率分布函数进行标注。具体来说,它通过向图形中添加一个箭头指向特定位置并在该位置显示指定文本的方式,将一条名称为“degree=1”的直线添加到了对应的位置上。
其中,参数说明如下:

"degree=1":显示在箭头终点位置的文本。
xy=(3,st.chi2.pdf(3,1)):箭头终点的坐标。在这里,x坐标为3,y坐标为自由度为1时卡方分布的概率密度函数(PDF)在x=3处的值。
xytext=(0.7,st.chi2.pdf(3,1)-0.04):文本开始的坐标。xytext的定义方式和xy相同,只不过文本不会出现在箭头终点处,而是稍微往下偏离一些。这里的0.7和pdf值减去0.04都是根据实际效果设置的,用于让文本更加清晰地显示在图像上方。
weight="bold"、color='steelblue'、fontsize=14:文本的字体、颜色和大小等样式的设置。
arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="steelblue"):箭头的样式设置。dict()表示python中的字典类型,arrowstyle="->"表示箭头的形状为朝右的箭头,connectionstyle="arc3"表示箭头与文本之间的连接线采用圆弧形状,color="steelblue"表示箭头和连接线的颜色为铁青色。

1.3.2 𝜒2卡方分布的图形

卡方分布的概率密度函数在t分布中已经给出

'''
卡方分布不同自由度的概率密度函数图形
'''
###生成模拟数据
x = np.arange(0.,14.,0.01)
#自由度(degree=1)的卡方分布随机数概率密度函数
y = st.chi2.pdf(x,1)
###annotate函数设置指向概率密度函数曲线的箭头和文字
plt.figure(figsize=(8,6))
plt.annotate("degree=1",xy=(3,st.chi2.pdf(3,1)),
             xytext=(0.7,st.chi2.pdf(3,1)-0.04),weight="bold",color='steelblue',fontsize=14,
             arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="steelblue"))
​
#生成另一个模拟数据,下面几行代码同样功能。
y1 = st.chi2.pdf(x,2)
plt.annotate("degree=2",xy=(1.8,st.chi2.pdf(1.8,2)),
             xytext=(1.8,st.chi2.pdf(1.8,2)+0.06),weight="bold",color='darkorange',fontsize=14,
             arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="darkorange"))
y2 = st.chi2.pdf(x,4)
plt.annotate("degree=4",xy=(3,st.chi2.pdf(3,4)),
             xytext=(3,st.chi2.pdf(3,4)+0.06),weight="bold",color='g',fontsize=14,
             arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="g"))
y3 = st.chi2.pdf(x,6)
plt.annotate("degree=6",xy=(4.8,st.chi2.pdf(4.8,6)),
             xytext=(4.8,st.chi2.pdf(4.8,6)+0.06),weight="bold",color='r',fontsize=14,
             arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="r"))
plt.plot(x,y)
plt.plot(x,y1)
plt.plot(x,y2)
plt.plot(x,y3)
plt.title("卡方分布概率密度函数",size=14)
plt.xlabel("随机变量",size=14)
plt.ylabel("卡方分布概率密度",size=14)
plt.ylim(0.,0.4)
plt.show()

###卡方分布的自由度极端高的曲线,这种情况极少见。
#当自由度趋近无穷时,卡方分布渐进服从正态分布,但是存在争议。
#见:https://www.zhihu.com/question/363857851
x = np.arange(30.,90.,0.01)#随机数
y5 = st.chi2.pdf(x,60)
plt.plot(x,y5)#曲线和正态分布钟形很相似
plt.show()

1.3.3 F分布的图形

'''
F分布不同自由度的概率密度函数图形
'''
​
plt.figure(figsize=(8,6))
#生成模拟数据
x = np.arange(0.,4.,0.01)#随机数
y = st.f.pdf(x,3,20)#自由度为(3,20)的F分布随机数概率密度函数
​
#annotate函数设置指向概率密度函数曲线的箭头和文字
plt.annotate("n=3,m=20",xy=(0.3,st.f.pdf(0.3,3,20)),
             xytext=(0.3,st.f.pdf(.3,3,20)+0.3),weight="bold",color='steelblue',
             fontsize=14,
             arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="steelblue"))
#生成另一个模拟数据,下面几行代码同样功能。
y1 = st.f.pdf(x,7,20)
plt.annotate("n=7,m=20",xy=(0.6,st.f.pdf(0.6,7,20)),
             xytext=(1.2,st.f.pdf(.6,7,20)+0.2),weight="bold",color='darkorange',
             fontsize=14,
             arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="darkorange"))
y2 = st.f.pdf(x,20,20)
plt.annotate("n=20,m=20",xy=(1.5,st.f.pdf(1.5,20,20)),
             xytext=(1.6,st.f.pdf(1.5,20,20)+0.2),weight="bold",color='g',
             fontsize=14,
             arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="g"))
y3 = st.f.pdf(x,20,7)
plt.annotate("n=20,m=7",xy=(2.25,st.f.pdf(2.25,20,7)),
             xytext=(2.6,st.f.pdf(2.25,20,7)+0.2),weight="bold",color='r',
             fontsize=14,
             arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="r"))
plt.plot(x,y)
plt.plot(x,y1)
plt.plot(x,y2)
plt.plot(x,y3)
plt.title("F分布概率密度函数",size=14)
plt.xlabel("随机变量",size=14)
plt.ylabel("F分布概率密度",size=14)
plt.ylim(0.,1.1)
plt.show()

'''
F分布的分位点填充图形
'''
x = np.arange(0.,4.,0.001)
y = st.f.pdf(x,5,8)
plt.figure(figsize=(8,6))
plt.plot(x,y,label="alpha=0.1\n n=5 \n m=8")
plt.title("F(5,8)分布概率密度函数与分位点",size=14)
plt.xlabel("随机变量",size=14)
plt.ylabel("F分布概率密度",size=14)
plt.ylim(0.,0.9)
x1 = st.f.ppf(0.9,5,8)#注意:scipy的ppf函数时通过下分位来求分位数的
x2 = x[np.where(x>x1)]
#axvline函数画垂直线
plt.axvline(x=x1,ymax=st.f.pdf(x1,5,8)+0.01,ls="--",c="red")#添加垂直直线
#fill_between填充分布函数分位点的颜色,上0.1分位数对应的概率填充图
plt.fill_between(x2,y[np.where(x>x1)],0,facecolor="blue",alpha=0.3)
plt.legend(fontsize=14)
plt.show()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

封印师请假去地球钓鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值