import numpy as np
import scipy.stats as ss
import math
def interval(data,alpha):
a=np.array(data)
alpha=0.05
half_alpha=alpha/2
MEAN=a.mean()
VAR=a.var(ddof=1)
STD=a.std(ddof=1)
sigma2L=(len(data)-1)*VAR/ss.chi2(len(data)-1).isf(half_alpha)
sigma2H=(len(data)-1)*VAR/ss.chi2(len(data)-1).isf(1-half_alpha)
return [sigma2L,sigma2H],[math.sqrt(sigma2L),math.sqrt(sigma2H)]
data=[9.913,10.012,9.886,10.006,9.934,9.983,9.915,10.006,9.960,9.934,
9.938,9.870,9.956,9.926,9.957,9.941,9.895,9.897,9.903,9.903,
9.908,9.884,9.940,9.968,9.909,9.957,9.932,9.971,9.944,9.923,
9.962,9.951,9.900,9.991,9.896,9.920,9.894,9.942,9.943,9.967,
9.902,9.937,9.954,9.942,9.956,9.985,9.929,9.969,9.990,9.888,
9.988,9.948,9.929,9.892,9.976,9.890,9.952,10.005,10.001,9.926,
9.946,9.896,9.978,9.947,9.962,9.959,9.953,9.952,9.932,9.928,
9.911,9.940,9.932,9.974,9.965,9.928,9.930,9.899,9.911,9.911,
9.880,9.929,9.992,9.961,9.944,9.890,9.932,9.929,9.917,9.944,
9.967,9.923,9.936,9.948,9.954,9.933,9.926,9.965,9.956,9.961]
a=np.array(data)
alpha=0.05
half_alpha=alpha/2
MEAN=a.mean()
VAR=a.var(ddof=1)
STD=a.std(ddof=1)
T=ss.t(len(data)-1).isf(half_alpha)
CV=STD/MEAN
H1=MEAN+STD*T/math.sqrt(len(data))
L1=MEAN-STD*T/math.sqrt(len(data))
H2=MEAN+0.05*T/math.sqrt(len(data))
L2=MEAN-0.05*T/math.sqrt(len(data))
sigma2L=(len(data)-1)*VAR/ss.chi2(len(data)-1).isf(half_alpha)
sigma2H=(len(data)-1)*VAR/ss.chi2(len(data)-1).isf(1-half_alpha)
print('样本均值:',MEAN)
print('样本方差:',VAR)
print('样本标准差:',STD)
print('样本数:',len(data))
print('置信水平:%2.3f'%(1-alpha),'置信下限:%2.3f'%(half_alpha),'置信上限:%2.3f'%(1-half_alpha))
print('置信水平为%2.3f'%(1-alpha),'方差区间',(sigma2L),(sigma2H))
print('置信水平为%2.3f'%(1-alpha),'的标准差区间',math.sqrt(sigma2L),math.sqrt(sigma2H))
print('置信水平为%2.3f'%(1-alpha),'数学期望区间',L1,H1)