今天我們來聊聊統計學裡面比較重要的一個定理:中心極限定理
現在有一個總體數據,如果從該總體數據中隨機抽取若干樣本,重複多次,每次抽樣得到的樣本量統計值(比如均值)與總體的統計值(比如均值)應該是差不多的,而且重複多次以後會得到多個統計值,這多個統計值會呈正態分布。還是直接來看例子吧。
import numpy as npimport pandas as pdimport seaborn as snsdata = np.random.rand(10000)sns.distplot(data)上面代碼是用來生成10000個隨機數的,並繪製分布圖。通過分布圖可以看出,這10000個隨機數基本是均等分布,也就是每個值出現的概率差不多。
現在我們從這10000個樣本中隨機抽取若干個樣本(30、50、100、500),重複抽取100次,會得到100個樣本均值,然後繪製樣本均值分布圖。
plt.figure(figsize = (9,9))plt.subplot(221)sample_mean = []for i in range(1,100): s = np.random.choice(data,size = 30).mean() sample_mean.append(s)sns.distplot(sample_mean)plt.title("size = 30")
plt.subplot(222)sample_mean = []for i in range(1,100): s = np.random.choice(data,size = 50).mean() sample_mean.append(s)sns.distplot(sample_mean)plt.title("size = 50")
plt.subplot(223)sample_mean = []for i in range(1,100): s = np.random.choice(data,size = 100).mean() sample_mean.append(s)sns.distplot(sample_mean)plt.title("size = 100")
plt.subplot(224)sample_mean = []for i in range(1,100): s = np.random.choice(data,size = 500).mean() sample_mean.append(s)sns.distplot(sample_mean)plt.title("size = 500")上面代碼是我們每次抽取的樣本量為:30、50、100、500,通過運行上面代碼可以得到每次抽取不同樣本量對應的樣本均值的分布結果:
可以看到,不同樣本量對應的均值分布均符合正態分布。以上就是關於中心極限定理的思想。這裡需要弄清楚的一點是樣本均值符合正態分布,而不是樣本本身符合正態分布哦。
那這個定理有什麼用呢?還記得我們前面一開始說過的結論嗎?就是抽樣算出來的均值會接近總體的均值,所以基於這個定理的存在,我們可以用抽樣結果的均值來估計總體的均值。比如你要統計一下北京市的平均工資,那麼你就可以從北京全部人口這個總體中隨機抽取部分樣本,抽取若干次,把這若干次的均值再求均值以後,就可以作為北京市全部人口的平均工資。
End.
來源:俊紅的數據分析之路