【问题标题】:How to get a sigmodal CDF curve use scipy.stats.norm.cdf and matplotlib?如何使用 scipy.stats.norm.cdf 和 matplotlib 获得 sigmodal CDF 曲线?
【发布时间】:2020-08-02 14:14:49
【问题描述】:

我正在尝试绘制正态分布的 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()

【问题讨论】:

  • 您可以尝试计算随机值的累积和np.cumsum。如果应用于 b,这应该是 sigmoid-like,但未归一化

标签: python numpy matplotlib scipy


【解决方案1】:

现在我不知道您的特定应用程序。但我认为问题在于您正在为许多正态分布的随机数创建 cdf 的值。 下面你可以看到一个代码示例,它绘制了从 -3 到 +3 的标准法线的 CDF

import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt

x = np.arange(-3, 3, 0.1)
c = norm.cdf(x)

plt.plot(x, c)
plt.show()

标准法线的CDF

【讨论】:

    【解决方案2】:

    要绘制正态分布随机变量的 CDF 的 sigmoidal 结果,我不应该使用 matplotlib 的 hist() 函数。相反,我可以使用bar() 函数来绘制我的结果。

    @Laaggan@dumbPy 的回答指出,使用正则化和有序的 x 值是推导 sigmoidal cdf 曲线的方法。虽然通常这样做,但在使用随机变量时不适用。我已经将他们提到的方法的解决方案与我所做的进行了比较,以表明两种方法都给出了相同的结果。但是,我的结果(见下图)确实表明,与使用随机变量相比,获取 cdf 值的常用方法会产生更多的正态分布极值。排除两个极端后,出现的事件似乎是均匀分布的。

    我已经修改了我的脚本并提供了 cmets 来演示我如何比较这两种方法。我希望我的回答能对正在学习使用scipy.stats.norm 类的rvs()pdf()cdf() 函数的其他人有所帮助。

    import numpy as np
    from numpy.random import default_rng
    from scipy.stats import norm
    import matplotlib.pyplot as plt
    
    mu = 0
    sigma = 1
    samples = 1000
    
    rg = default_rng( 12345 )
    a = rg.random(size=samples) #Get a  uniform distribution of numbers in the range of 0 to 1.
    print( 'a = ', a)
    
    # Get pdf and cdf values using normal random variates. 
    rg = default_rng( 12345 ) #Recreate Bit Generator to ensure a same starting point  
    b_pdf = norm.rvs( loc=mu, scale=sigma, size=samples, random_state=rg ) #Get pdf of normal distribution(mu=0, sigma=1 gives -3.26 to +3.26).
    b_cdf = norm.cdf( b_pdf, loc=mu, scale=sigma ) #get cdf of normal distribution using pdf values (always gives between 0 to 1).
    print( 'b_pdf = ', b_pdf)
    print( 'b_cdf = ', b_cdf)
    
    #To check b is normally distributed. Using the ordered x (commonly practiced): 
    c_x = np.linspace( mu - 3.26*sigma, mu + 3.26*sigma, samples )
    c_pdf = norm.pdf( c_x, loc=mu, scale=sigma )
    c_cdf = norm.cdf( c_x, loc=mu, scale=sigma  )
    print( 'c_x = ', c_x )
    print( 'c_pdf = ', c_pdf )
    print( 'c_cdf = ', c_cdf )
    
    
    fig, ax = plt.subplots(3, 1)
    bins=np.linspace( 0, 1, num=10 )
    acount, abins, aignored = ax[0].hist( a, bins=50, histtype='bar', label='a', color='C0', alpha=0.2, density=True  )
    bcount, bbins, bignored = ax[0].hist( b_cdf, bins=50, histtype='bar', label='b_cdf', color='C1', alpha=0.2, density=True )
    ccount, cbins, cignored = ax[0].hist( c_cdf, bins=50, histtype='bar', label='c_cdf', color='C2', alpha=0.2, density=True )
    
    bcount, bbins, bignored = ax[1].hist( b_pdf, bins=20, histtype='bar', label='b_pdf', color='C1', alpha=0.4, density=True  )
    cpdf_line = ax[1].plot(c_x, c_pdf, label='c_pdf', color='C2')
    
    bpdf_bar = ax[2].bar( b_pdf, b_cdf, label='b_cdf', color='C1', alpha=0.4, width=0.01)
    ccdf_line = ax[2].plot(c_x, c_cdf, label='c_cdf', 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(loc='upper left')
    ax[1].legend(loc='upper left')
    ax[2].legend(loc='upper left')
    plt.show()
    

    【讨论】:

      【解决方案3】:

      您正在绘制错误的值。 当您执行b = norm.rvs(size=siz, random_state=rg) 时,您得到的是从标准正态分布中独立抽取的 10 个随机样本,即z

      因此,它们的直方图就是您所看到的钟形曲线。

      norm.cdf 在给定的 z 值处返回 cfd 值。如果你想要 cdf 的 S 曲线,你可以从 -3 到 3 z 值均匀绘制,并在所有点上得到它们的 cdf 值。然后绘制输出概率值。

      编辑:另一个答案给出了这种方法的代码,所以我不会再添加了。

      【讨论】:

        猜你喜欢
        • 2019-06-21
        • 2020-01-21
        • 1970-01-01
        • 2015-10-24
        • 2020-05-16
        • 1970-01-01
        • 2016-08-04
        • 2019-11-23
        • 2017-02-05
        相关资源
        最近更新 更多