我正在閱讀由Joel Grus從頭開始的「數據科學」一書。我的問題特別涉及第6章,作者使用二項式隨機變量來模擬定理。關於從書籍數據科學模擬中心極限定理的問題從零開始
結果將是一個圖表,其中包含二項試驗的概率分佈和使用正態分佈的近似圖。這兩個地塊應該非常相似。書示出像這樣的圖表: Author's Chart
他提供的代碼爲:
import random
from matplotlib import pyplot as plt
from collections import Counter
def bernoulli_trial(p):
return 1 if random.random() < p else 0
def binomial(n, p):
return sum(bernoulli_trial(p) for _ in range(n))
def make_hist(p, n, num_points):
data = [binomial(n, p) for _ in range(num_points)]
histogram = Counter(data)
plt.bar([x-0.4 for x in histogram.keys()],
[v/num_points for v in histogram.values()],
0.8,
color='0.75')
mu = p * n
sigma = math.sqrt(n * p * (1-p))
# use a line chart to show the normal approximation
xs = range(min(data), max(data) + 1)
ys = [normal_cdf(i+0.5, mu, sigma) - normal_cdf(i-0.5, mu, sigma) for i in xs]
plt.plot(xs, ys)
plt.title('Binomial Distribution vs. Normal Approximation')
plt.show()
make_hist(0.75, 100, 10000)
我的問題是,在這一行: [normal_cdf(I + 0.5,μ,西格馬) - normal_cdf( i-0.5,mu,sigma)for i in xs] 作者爲什麼使用+0.5和-0.5?這是否有特定的原因?
不知道是否有人遇到過這個問題。 預先感謝您!
normal_cdf超過在'範圍(-50,50)'的載體'X/10.0'。對? – skrubber
是的。函數是:def normal_cdf(x,mu = 0,sigma = 1):return(1 + math.erf((x-mu)/math.sqrt(2)/ sigma))/ 2。 x軸爲:xs = [x/10.0(對於範圍內的x(-50,50)]。爲什麼要除以10? –
我想只是將圖表縮小到x軸-5到5之間? –