我试图绘制正态分布的S形累积分布函数(cdf)曲线。然而,我最终得到了一个统一的分布。我做错了什么
测试脚本
import numpy as np
from numpy.random import default_rng
from scipy.stats import norm
import matplotlib.pyplot as plt
siz = 1000
rg = default_rng( 12345 )
a = rg.random(size=siz)
rg = default_rng( 12345 )
b = norm.rvs(size=siz, random_state=rg)
c = norm.cdf(b)
print( 'a = ', a)
print( 'b = ', b)
print( 'c = ', c)
fig, ax = plt.subplots(3, 1)
acount, abins, aignored = ax[0].hist( a, bins=20, histtype='bar', label='a', color='C0' )
bcount, bbins, bignored = ax[1].hist( b, bins=20, histtype='bar', label='b', color='C1' )
ccount, cbins, cignored = ax[2].hist( c, bins=20, histtype='bar', label='c', color='C2' )
print( 'acount, abins, aignored = ', acount, abins, aignored)
print( 'bcount, bbins, bignored = ', bcount, bbins, bignored)
print( 'ccount, cbins, cignored = ', ccount, cbins, cignored)
ax[0].legend()
ax[1].legend()
ax[2].legend()
plt.show()
您正在绘制错误的值。 当你做
b = norm.rvs(size=siz, random_state=rg)
时,你得到的是从标准正态分布中独立抽取的10个随机样本,即z
值因此,它们的直方图就是你们所看到的钟形曲线
norm.cdf
返回给定z值处的cfd
值。如果需要cdf的曲线,可以从-3到3 z值均匀绘制,并在所有点上获得其cdf值。然后绘制输出概率值编辑:另一个答案给出了这种方法的代码,所以我不会再麻烦添加了
为了绘制正态分布随机变量CDF的s形结果,我不应该使用matplotlib的
hist()
函数。相反,我可以使用bar()
函数来绘制结果@Laaggan和@dumbPy回答指出,使用正则化和有序的x值是推导S形cdf曲线的方法。虽然通常这样做,但在使用随机变量时不适用。我已经将他们提到的方法的解决方案与我所做的进行了比较,以表明这两种方法给出了相同的结果。然而,我的结果(见下图)确实表明,通常获得cdf值的方法比使用随机变量产生更多的正态分布极值。排除这两个极端情况,出现的情况看起来是均匀分布的
我已经修改了我的脚本并提供了评论,以演示我是如何比较这两种方法的。我希望我的回答能对那些正在学习使用
rvs()
、pdf()
和cdf()
类函数的人有所帮助现在我不知道你的具体用途。但我认为问题在于,你正在为一些正态分布的随机数创建cdf的值。 下面您可以看到一个代码示例,该示例绘制了从-3到+3的标准法线的CDF
标准正常值的CDF
相关问题 更多 >
编程相关推荐