# 区间估计

import matplotlib.pyplot as plt
import numpy as np

#随机生成产品质量,正态分布
x_data = [i for i in range(1,11)]
x_index = np.arange(5)   #柱的索引
weight = np.random.randn(1,10)+5
print(x_data)
weight = weight[0]
print(weight)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[5.0497961  4.36898624 5.48831912 5.0118007  3.85561999 3.55079556
3.92272509 5.31616147 6.81360384 5.3129094 ]

fix,ax = plt.subplots()
bar_width = 0.35   #定义一个数字代表每个独立柱的宽度
plt.xticks(x_index + bar_width/2, x_data)   #x轴刻度线
rects1 = ax.bar(x_data, weight, bar_width,alpha=0.4, color='b',label='legend1')
plt.show()


#根据区间估计的计算方法，首先求均值,标准差
n = len(weight)
mean = weight.sum()/n
S = 0
for i in range(0,n):
S += (weight[i] - mean)**2
S = np.sqrt(S)/n
print(mean)
print(S)

4.869071751390313
0.29197911491468465

#重量方差不清楚，所以用S估计δ，根据公式
#np.sqrt(n)(mean - miu)/S服从自由度为n-1的t分布
#查表得置信度0.95，对应的t分布值为2.776
top = mean + S*2.776/(np.sqrt(n))
low = mean - S*2.776/(np.sqrt(n))
#得到均值的区间估计为low-top


### 画图

fix,ax = plt.subplots()
bar_width = 0.35   #定义一个数字代表每个独立柱的宽度
rects1 = ax.bar(x_data, weight, bar_width,alpha=0.4, color='b')
plot2 = ax.plot([0,10],[low,low],alpha=0.5,color='r',label='estimation_low')   #线图：linestyle线性，alpha透明度，color颜色，label图例文本
plot3 = ax.plot([0,10],[top,top],alpha=0.5,color='r',label='estimation_top')   #线图：linestyle线性，alpha透明度，color颜色，label图例文本
plt.show()


09-30
12-19

01-23 9638
12-19 8314
12-02 4075
10-09 6282
01-25
08-30 1万+
07-27 122
10-18 8249