假设检验2_t分布的应用

系列文章目录

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 一、右边检验例子
  • 二、左边检验例子
  • 三   双边检验例子
  • CODE实现


前言

          假设随机变量符合N(u,\sigma^2),方差未知。

对随机变量均值的假设,符合t 分布 

\frac{X}{\sqrt{Y/n}}

 这里主要应用t分布的性质,得到统计量

z=\frac{\bar{x}-u}{s/\sqrt{n}} \sim t(n-1)


一、例一 右边检验例子

        可乐制造商为了检验可乐在储藏过程种甜度是否有损失,请专业的品尝师对可乐储藏前后的甜度 进行评分.
    10位品藏师对甜度品分之差为为2.0,0.4,0.7,2.0,-0.4,2.2,-1.3,1.2,1.1,2.3
问: 甜度是否有损失

   解:

      step1: 假设

                H_0: u=0 , H_1: u>0

                 其中u 代表甜度损失。

      step2: 计算统计量

                  z=\frac{\bar{x}-u}{s/\sqrt{n}}     为2.7

     step3 : 设置信度为0.05,根据自由度求拒绝域

            w=\begin{Bmatrix} 1.83,& \infty \end{Bmatrix}

      step4: 作出结论

             落在拒绝域里面,甜度有损失

             

         

                

二、例二 左边检验例子

   

    某种电子元器件寿命大于1000小时,随机抽样25件
测得平均寿命为950,标准差100小时。已知电子元器件符合正太分布,
试在显著水平0.05下确定这批元件是否合格。

    解:

     step1:

            u_0=1000

         假设:   H_0: u>u_0, H_1: u<u_0

    step2: 统计量

        z=\frac{\bar{x}-u}{s/\sqrt{n}}   -2.5

   

step3 : 设置信度为0.05,根据自由度求拒绝域

            W=\begin{Bmatrix} -\infty & -1.71 \end{Bmatrix}

step4:  拒绝原假设,产品不合格

    


三 双边检验例子

    

 

 

    解:

     step1 假设H_0:u=0, H_1: u \neq 0

     step2:   计算统计量

       z=\frac{\bar{x}-u}{s/\sqrt{n}}

      -0.14

     step3: 根据置信度0.05 得到拒绝域

      W =\begin{Bmatrix} -\infty & -2.26 \end{Bmatrix}

   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()

     

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值