系列文章目录
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
假设随机变量符合,方差未知。
对随机变量均值的假设,符合t 分布
这里主要应用t分布的性质,得到统计量
一、例一 右边检验例子
可乐制造商为了检验可乐在储藏过程种甜度是否有损失,请专业的品尝师对可乐储藏前后的甜度 进行评分.
10位品藏师对甜度品分之差为为2.0,0.4,0.7,2.0,-0.4,2.2,-1.3,1.2,1.1,2.3
问: 甜度是否有损失
解:
step1: 假设
其中u 代表甜度损失。
step2: 计算统计量
为2.7
step3 : 设置信度为0.05,根据自由度求拒绝域
step4: 作出结论
落在拒绝域里面,甜度有损失
二、例二 左边检验例子
某种电子元器件寿命大于1000小时,随机抽样25件
测得平均寿命为950,标准差100小时。已知电子元器件符合正太分布,
试在显著水平0.05下确定这批元件是否合格。
解:
step1:
假设:
step2: 统计量
-2.5
step3 : 设置信度为0.05,根据自由度求拒绝域
step4: 拒绝原假设,产品不合格
三 双边检验例子
解:
step1 假设
step2: 计算统计量
-0.14
step3: 根据置信度0.05 得到拒绝域
step4 接受假设,两者无差异
四 代码实现
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 28 14:28:13 2021
@author: chengxf2
"""
import numpy as np
from scipy.stats import t #卡方分布
import matplotlib.pyplot as plt
from enum import Enum
class check_type(Enum):
LEFT = 1 #单边
RIGHT = 2 #右边检验
DOUBLE = 3 #双边检验
'''
显示拒绝域
,参数loc表示平均数,scale表示标准差,size是样本量
args:
t1: 分位数
'''
def Draw(df,t1,tp:check_type):
left = t.ppf(0.001,df)
right = t.ppf(0.999,df)
step = (right-left)/200
x = np.arange(left, right, step)
y = t.pdf(x,df)
plt.plot(x,y,c='g',label='t-distrubition')
print("\n t1",t1)
if tp is check_type.DOUBLE: #双边
step = abs((t1-left)/50) #因为对称性
xLeft = np.arange(left, t1,step)
yLeft = t.pdf(xLeft,df) #概率密度
xRight = np.arange(-t1, right, step)
yRight = t.pdf(xRight,df)
plt.fill_between(xLeft, yLeft, color='red', alpha=0.5) #左拒绝区域
plt.fill_between(xRight, yRight, color='red', alpha=0.5) #左拒绝区域
elif tp is check_type.LEFT: #拒绝域在左
step = abs((t1-left)/50) #因为对称性
xLeft = np.arange(left, t1,step)
yLeft = t.pdf(xLeft,df) #概率密度
plt.fill_between(xLeft, yLeft, color='red', alpha=0.5) #左拒绝区域
else:
step = abs((right-t1)/50) #因为对称性
xRight = np.arange(t1, right, step)
yRight = t.pdf(xRight,df)
plt.fill_between(xRight, yRight, color='red', alpha=0.5) #左拒绝区域
'''
step1: 作出假设
step2: 计算统计量
step3: 计算拒绝域
step4: 给出假设
args
z: 统计量
t: 拒绝域
'''
def CheckResult(z,t,tp:check_type):
if tp is check_type.DOUBLE: #双边检验
if abs(z)>abs(t):
print("\n +++拒绝假设H0 ++++")
else:
print("\n 假设H0成立")
elif tp is check_type.LEFT:
if z < t:
print("\n +++++ 拒绝假设H0 ++++++")
else:
print("\n 假设H0")
else: #右边假设
if z > t:
print("\n ++++++++=拒绝假设H0++++++")
else:
print("\n 假设H0")
'''
t检验
args
data: 数据
alpha : 置信度
tp: 检验类型
'''
def GetInfo(data,alpha, tp:check_type):
n = len(data)
s = np.std(data,ddof = 1) #样本方差
x_bar = np.mean(data)
u = 0
z =(x_bar-u)/(s/np.sqrt(n))
print("\n n: %d x_bar %5.2f s: %5.3f "%(n, x_bar, s))
print("\n step2 计算统计量 %7.2f"%z)
if tp is check_type.DOUBLE:
t0 = t.ppf(alpha/2,n-1) #分位数
elif tp is check_type.LEFT: #左分位数数
t0 = t.ppf(alpha,n-1)
else: #右分位数 #对称的
t0 = t.ppf(1-alpha, n-1)
print("---RIGHT----")
print("\n 拒绝域: %7.2f"%t0)
CheckResult(z, t0, tp)
Draw(n-1,t0, tp)
'''
可乐制造商 右边检验的例子
H0: u = 0 甜度无损失
H1: u > 0 甜度无损失
'''
def example():
data =[2.0,0.4,0.7,2.0,-0.4,2.2,-1.3,1.2,1.1,2.3]
alpha = 0.05
GetInfo(data, alpha, check_type.RIGHT)
'''
原件使用寿命的例子,左边检验的例子
H0: u>u0
H1: u<u0
'''
def example1():
u = 1000
x_bar = 950
s = 100
alpha = 0.05
n =25
df = n-1 #样本个数
z = (x_bar-u)/(s/np.sqrt(n))
t1 = t.ppf(alpha,df)
print("\n 统计量 %5.2f 拒绝域 %5.2f"%(z,t1))
Draw(df, t1, check_type.LEFT)
CheckResult(z,t1,check_type.LEFT )
def example3():
a = np.array([23, 35, 29, 42, 39, 29, 37, 34, 35 ,28])
b = np.array([26, 39, 35, 40, 38, 24, 36, 27, 41, 27])
x= a-b
GetInfo(x, 0.05, check_type.DOUBLE)
if __name__ =="__main__":
example3()