t分布是在正态分布和卡方分布的基础上构造的, 我们通过代码实现一下

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")

# 正态分布
N = np.random.normal(0, 1, 100000)

# 自由度为 2, 5 的卡方分布
ka_2 = []
ka_5 = []

for i in range(1000):
ka_2.append(np.sum(np.random.choice(N, 2) ** 2))
ka_5.append(np.sum(np.random.choice(N, 5) ** 2))

# 自由度为 2, 5 的t分布
t2 = []
t5 = []
for i in range(10000):
t2.append(np.random.choice(N, 1)[0] / np.sqrt(np.random.choice(ka_2, 1)/2))
t5.append(np.random.choice(N, 1)[0] / np.sqrt(np.random.choice(ka_5, 1)/5))

plt.figure(figsize=(10, 6))
sns.distplot(t2, label='2', bins=10)
sns.distplot(t5, label='5', bins=10)
sns.distplot(N, label='正态')
plt.legend()
plt.xlim(-10, 10)

如图:

t分布的构造_正态分布

 课件自由度较小的时候, 与正态分布相比: 中间小于正态分布, 两端的尾部大于正态分布的, 因为总体的面积是相等的1;

随着自由度的变大, t分布和正态分布越来越像, 当样本容量大于30 的时候如果不是特别严格可以认为两者是相等的