n=7,用正太分布计算不准,要用t分布
sample=[1.5,2.9,0.9,3.9,3.2,2.1,1.9]
mean=2.34
deviation=1.04
求95置信区内期望的血压
#coding=utf-8
import math,statistics_functions
confidence_95=[[1,12.71],[2,4.303],[3,3.182],[4,2.776],[5,2.571],[6,2.447],[7,2.365],[8,2.306],[9,2.262],[10,2.228]\
,[11,2.201],[12,2.179],[13,2.160],[14,2.145],[15,2.131],[16,2.120],[17,2.110],[18,2.101],[19,2.093],[20,2.086]\
,[21,2.08],[22,2.074],[23,2.069],[24,2.064],[25,2.06],[26,2.056],[27,2.052],[28,2.154],[29,2.045],[30,2.042]]
confidence_998=[[1,318.3],[2,22.33],[3,10.21],[4,7.173],[5,5.893],\
[6,5.208],[7,4.785],[8,4.501],[9,4.297],[10,4.144],\
[11,4.025],[12,3.930],[13,3.852],[14,3.787],[15,3.733],\
[16,3.686],[17,3.646],[18,3.611],[19,3.579],[20,3.552],\
[21,3.527],[22,3.505],[23,3.485],[24,3.467],[25,3.450],\
[26,3.435],[27,3.421],[28,3.408],[29,3.396],[30,3.385]]
#样本
sample=[1.5,2.9,0.9,3.9,3.2,2.1,1.9]
#样本大小
n=len(sample)
#free自由度
free=n-1
#平均数
mean=statistics_functions.Mean(sample)
#方差
deviation=statistics_functions.Deviation(sample)
#置信度
confidence=0.95
#标准误
standard_error=deviation*1.0/math.sqrt(n)
#获取置信区
def T_Deviation(confidence,free):
if confidence==0.95:
for i in confidence_95:
if i[0]==free:
t_deviation=i[1]
if confidence==0.998:
for i in confidence_998:
if i[0]==free:
t_deviation=i[1]
return t_deviation
t_deviation=T_Deviation(confidence,free)
distribution_start=mean-standard_error*t_deviation
distribution_end=mean+standard_error*t_deviation
print "the distribution is from",distribution_start
print "to",distribution_end
原文:http://www.cnblogs.com/biopy/p/4937648.html