8

【丢弃】【假设检验】Python实现.

 3 years ago
source link: https://www.guofei.site/2017/10/28/hypothesistesting1.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【丢弃】【假设检验】Python实现.

2017年10月28日

Author: Guofei

文章归类: ,文章编号:


版权声明:本文作者是郭飞。转载随意,但需要标明原文链接,并通知本人
原文链接:https://www.guofei.site/2017/10/28/hypothesistesting1.html

Edit

回收原因:发现了用ipython notebook直接嵌入网页的方法,阅读更加清晰,见于这里

参见【假设检验】理论篇

数据准备

import pandas as pd
import scipy.stats as stats
df=pd.DataFrame(stats.norm.rvs(loc=0,scale=1,size=120).reshape(-1,3),columns=list('abc'))
df.loc[:,'c']=(df.loc[:,'c']>0)*1 #c这一列换成离散值

置信区间

import statsmodels.api as sm

ds = sm.stats.DescrStatsW(df)
ds.tconfint_mean(alpha=0.05)

output:

(array([-0.29225042, -0.36459267, -0.25896104]),
array([ 0.31800701, 0.2203038 , 0.42702711]))

分别是每列数的置信上界,置信下界。

均值检验

p-value指的是H0为真的概率。

方差未知

ds.ttest_mean(value=0.1)

输出的3个量分别是t-statistic, p-value, df
p-value<0.05,拒绝原假设,均值不是0.1

两独立样本

step1:方差齐次性检验

(_,df1),(_,df2)=df.groupby('c')['a']
leveneTestRes = stats.levene(df1, df2, center='median')
print('w-value=%6.4f, p-value=%6.4f' %leveneTestRes)

如果p-value比较大,无法拒绝原假设。认为方差是齐性的

step2:ttest

stats.stats.ttest_ind(df1, df2, equal_var=True)#equal_var表示方差齐次
# Or Try: sm.stats.ttest_ind(df1, df2, usevar='pooled')

您的支持将鼓励我继续创作!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK