【发布时间】:2019-04-04 09:34:01
【问题描述】:
我试图沿着我的中心极限数据分布获得正态分布曲线。
下面是我尝试过的实现。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
import math
# 1000 simulations of die roll
n = 10000
avg = []
for i in range(1,n):#roll dice 10 times for n times
a = np.random.randint(1,7,10)#roll dice 10 times from 1 to 6 & capturing each event
avg.append(np.average(a))#find average of those 10 times each time
plt.hist(avg[0:])
zscore = stats.zscore(avg[0:])
mu, sigma = np.mean(avg), np.std(avg)
s = np.random.normal(mu, sigma, 10000)
# Create the bins and histogram
count, bins, ignored = plt.hist(s, 20, normed=True)
# Plot the distribution curve
plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *np.exp( - (bins - mu)**2 / (2 * sigma**2)))
我得到下图,
你可以在底部看到红色的正态曲线。
谁能告诉我为什么曲线不合适?
【问题讨论】:
-
您可能需要缩放其中一个。正态分布的最大值高于 1 IIRC,你的情节上升到 2500。
-
要么将正态分布缩放到最大值(约 2700),要么使用
ax.twinx() -
你能用代码显示吗?
标签: python numpy matplotlib statistics