【问题标题】:How to plot cumulative distribution function in python for six sided dice simulation?如何在 python 中为六面骰子模拟绘制累积分布函数?
【发布时间】:2016-12-12 09:02:15
【问题描述】:

我正在尝试

  1. 用模拟的骰子和结果的频率绘制直方图。
  2. 计算并绘制累积分布函数。
  3. 查找并绘制中位数。

到目前为止,这是我所拥有的:

import pylab
import random

sampleSize = 100


## Let's simulate the repeated throwing of a single six-sided die
singleDie = []
for i in range(sampleSize):
    newValue = random.randint(1,6)
    singleDie.append(newValue)

print "Results for throwing a single die", sampleSize, "times."
print "Mean of the sample =", pylab.mean(singleDie)
print "Median of the sample =", pylab.median(singleDie)
#print "Standard deviation of the sample =", pylab.std(singleDie)
print
print

pylab.hist(singleDie, bins=[0.5,1.5,2.5,3.5,4.5,5.5,6.5] )
pylab.xlabel('Value')
pylab.ylabel('Count')
pylab.savefig('singleDie.png')
pylab.show()




## What about repeatedly throwing two dice and summing them?
twoDice = []
for i in range(sampleSize):
    newValue = random.randint(1,6) + random.randint(1,6)
    twoDice.append(newValue)



print "Results for throwing two dices", sampleSize, "times."
print "Mean of the sample =", pylab.mean(twoDice)
print "Median of the sample =", pylab.median(twoDice)
#print "Standard deviation of the sample =", pylab.std(twoDice)

pylab.hist(twoDice, bins= pylab.arange(1.5,12.6,1.0))
pylab.xlabel('Value')
pylab.ylabel('Count')
pylab.savefig('twoDice.png')
pylab.show()

谁能帮助我如何绘制 cdf?

【问题讨论】:

    标签: python python-2.7 plot histogram cdf


    【解决方案1】:

    您可以直接使用直方图绘图功能来实现,例如

    import pylab
    import random
    import numpy as np
    
    sampleSize = 100
    
    
    ## Let's simulate the repeated throwing of a single six-sided die
    singleDie = []
    for i in range(sampleSize):
        newValue = random.randint(1,6)
        singleDie.append(newValue)
    
    print "Results for throwing a single die", sampleSize, "times."
    print "Mean of the sample =", pylab.mean(singleDie)
    print "Median of the sample =", pylab.median(singleDie)
    print "Standard deviation of the sample =", pylab.std(singleDie)
    
    
    pylab.hist(singleDie, bins=[0.5,1.5,2.5,3.5,4.5,5.5,6.5] )
    pylab.xlabel('Value')
    pylab.ylabel('Count')
    pylab.savefig('singleDie.png')
    pylab.show()
    
    ## What about repeatedly throwing two dice and summing them?
    twoDice = []
    for i in range(sampleSize):
        newValue = random.randint(1,6) + random.randint(1,6)
        twoDice.append(newValue)
    
    print "Results for throwing two dices", sampleSize, "times."
    print "Mean of the sample =", pylab.mean(twoDice)
    print "Median of the sample =", pylab.median(twoDice)
    #print "Standard deviation of the sample =", pylab.std(twoDice)
    
    pylab.hist(twoDice, bins= pylab.arange(1.5,12.6,1.0))
    pylab.xlabel('Value')
    pylab.ylabel('Count')
    pylab.savefig('twoDice.png')
    pylab.show()
    
    pylab.hist(twoDice, bins=pylab.arange(1.5,12.6,1.0), normed=1, histtype='step', cumulative=True)
    pylab.xlabel('Value')
    pylab.ylabel('Fraction')
    pylab.show()
    

    注意新行:

    pylab.hist(twoDice, bins=pylab.arange(1.5,12.6,1.0), normed=1, histtype='step', cumulative=True)
    

    应该直接给出 cdf 图。如果您不指定 normed=1,您将看到表示百分比的比例 (0-100),而不是通常的概率比例 (0-1)。

    还有其他方法可以做到这一点。例如:

    import pylab
    import random
    import numpy as np
    
    sampleSize = 100
    
    
    ## Let's simulate the repeated throwing of a single six-sided die
    singleDie = []
    for i in range(sampleSize):
        newValue = random.randint(1,6)
        singleDie.append(newValue)
    
    print "Results for throwing a single die", sampleSize, "times."
    print "Mean of the sample =", pylab.mean(singleDie)
    print "Median of the sample =", pylab.median(singleDie)
    print "Standard deviation of the sample =", pylab.std(singleDie)
    
    
    pylab.hist(singleDie, bins=[0.5,1.5,2.5,3.5,4.5,5.5,6.5] )
    pylab.xlabel('Value')
    pylab.ylabel('Count')
    pylab.savefig('singleDie.png')
    pylab.show()
    
    ## What about repeatedly throwing two dice and summing them?
    twoDice = []
    for i in range(sampleSize):
        newValue = random.randint(1,6) + random.randint(1,6)
        twoDice.append(newValue)
    
    print "Results for throwing two dices", sampleSize, "times."
    print "Mean of the sample =", pylab.mean(twoDice)
    print "Median of the sample =", pylab.median(twoDice)
    #print "Standard deviation of the sample =", pylab.std(twoDice)
    
    pylab.hist(twoDice, bins= pylab.arange(1.5,12.6,1.0))
    pylab.xlabel('Value')
    pylab.ylabel('Count')
    pylab.savefig('twoDice.png')
    pylab.show()
    
    twod_cdf = np.array(twoDice)
    
    X_values = np.sort(twod_cdf)
    F_values = np.array(range(sampleSize))/float(sampleSize)
    pylab.plot(X_values, F_values)
    pylab.xlabel('Value')
    pylab.ylabel('Fraction')
    pylab.show()
    

    现在请注意,我们对数组进行排序、构建函数并绘制它。

    【讨论】:

    • 非常感谢 Fedepad。我现在能够理解它了。 !
    猜你喜欢
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    • 2013-12-28
    相关资源
    最近更新 更多