Python中的Poission / Normal和Beta分布 - 帮助理解统计信息

时间:2018-05-03 12:29:18

标签: python numpy math statistics

我对这个python代码有一些疑问。我需要帮助才能理解输出答案。我在python中有一个简单的优秀代码来生成poission,normal和beta发行版,我想帮助理解出来的数字。

在发布我的代码之前,让我们想一个简单的场景:每分钟都有一定数量的汽车进入城市,平均到达率为63,sigma为25,

好的,这是我的代码:

import scipy as sp
import numpy as np
import matplotlib.pyplot as plt

mu, sigma = 64, 24


#normal distribution
s = np.random.normal(mu, sigma, 1000)

count, bins, ignored = plt.hist(s, 30, normed=True)

plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *
         np.exp( - (bins -mu )**2 / (2 * sigma**2)),
    linewidth=2, color='r')
plt.show()



#Poisson distribution

s = np.random.poisson(5, 10000)

import matplotlib.pyplot as plt
count, bins, ignored = plt.hist(s, 14, normed=True)
plt.show()


#Beta distribution

s = np.random.beta(mu, sigma, 1000)


count, bins, ignored = plt.hist(s, 30, normed=True)
plt.show()

以下是代码的结果:

Poisson

Normal

Beta

问题1:这些数字根据情景告诉了什么?

问题2:是否可以更改X线(泊松时为-0,15至175),(正常情况下为-3至4)和(测试时为0.55至0.85)?

问题3和问题4:我在代码内部询问过。

谢谢

2 个答案:

答案 0 :(得分:0)

就像一般性评论一样,在提问之前,你似乎没有“完成你的功课”。大多数人建议学习并熟悉主题之前提问,并且觉得这对你和那些困惑你的问题的人更好。也就是说,让我们回答您的问题,然后,请选择一本关于概率和统计数据的介绍性书籍 - 或者只是谷歌,然后开始阅读。

"Question 1: What does these numbers tell based on the scenario?"
A1:这个问题非常清楚,除了说“是”之外,它无法回答。

"Question 2: Is it possible to change the X lines(-0,15 to 175 on poission), 
(-3 to 4 on normal) and 
(0.55 to 0.85 on beta)?"

A2:当然......您可以将这些值更改为您喜欢的任何值,但这又如何推动您的努力?换句话说,你想做什么?

QUESTION 3: WHAT IS THIS NUMBER 3830? 
A3:嗯......你已经把自己放在了这里,因为你已经证明你没有RTFM。 numpy提供了大量文档,实际上here's a reference that answers your Q33830是使用您指定的均值和方差从分布中抽取的样本数。

QUESTION 4: AND WHAT ARE THESE NUMBERS? 

A4:与#3完全相同,仅在这种情况下,分发称为"Student's t",而numpy reference provides the answer:数字是分布的degrees of freedom,并且从该分布中抽取的样本数量。

答案 1 :(得分:0)

如果你想绘制pdf(Poisson将是pmf,因为它是离散分布),那么我建议使用你已经使用的scipy

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

mu, sigma = 64, 24

# normal
s = np.random.normal(mu, sigma, 1000)
x = np.linspace(norm.ppf(0.001, mu, sigma), norm.ppf(0.999, mu, sigma), 300)
plt.hist(s, 30, normed=True)
plt.plot(x, norm.pdf(x, mu, sigma), 'r', lw=2)
plt.title("Normal with mu=64, sigma=24")
plt.show()

# poisson
s = np.random.poisson(5, 10000)
x = np.arange(poisson.ppf(0.001, 5), poisson.ppf(0.999, 5))
plt.hist(s, 14, normed=True)
plt.plot(x, poisson.pmf(x, 5), 'r')
plt.title("Poisson with mu=5")
plt.show()

# beta
s = np.random.beta(mu, sigma, 1000)
x = np.linspace(beta.ppf(0.001, mu, sigma), beta.ppf(0.999, mu, sigma), 300)
plt.hist(s, 30, normed=True)
plt.plot(x, beta.pdf(x, mu, sigma), 'r')
plt.title("Beta with mu=64, sigma=24")
plt.show()

结果:

enter image description here