1 单样本和两样本均值的t检验
(1)单样本
举例:
H
0
:
μ
=
3.1
?
H
1
:
μ
≠
3.1
H_0:\mu= 3.1 \rightleftharpoons H_1:\mu \neq 3.1
H0?:μ=3.1?H1?:μ?=3.1
import scipy.stats as stats
import numpy as np
np.random.seed(1010)
x = np.random.normal(3,1,500)
stat,p_value = stats.ttest_1samp(x,3,1)
print(stat,p_value)
(2)两样本
举例:
H
0
:
μ
1
=
μ
2
?
H
1
:
μ
≠
μ
2
H_0:\mu_ 1 = \mu_2 \rightleftharpoons H_1:\mu \neq \mu_2
H0?:μ1?=μ2??H1?:μ?=μ2?
import scipy.stats as stats
import numpy as np
np.random.seed(1010)
x = np.random.normal(3,1,500)
y = np.random.normal(3.2,1,500)
stat,p_value = stats.ttest_ind(x,y,equal_var=False)
print(stat,p_value)
2 单样本和两样本关于中位数的非参数检验
(1)单样本之Wilcoxon符号秩检验
H
0
:
M
=
3.1
?
H
1
:
M
≠
3.1
H_0:M = 3.1 \rightleftharpoons H_1:M \neq 3.1
H0?:M=3.1?H1?:M?=3.1
import scipy.stats as stats
import numpy as np
np.random.seed(1010)
x = np.random.normal(3,1,500)
stat,p_value = stats.wilcoxon(x-3.1,correction=True)
print(stat,p_value)
(2)两样本之Wilcoxon秩和检验
H
0
:
M
1
=
M
2
?
H
1
:
M
1
<
M
2
H_0:M_1 = M_2 \rightleftharpoons H_1:M_1 < M_2
H0?:M1?=M2??H1?:M1?<M2?
import scipy.stats as stats
import numpy as np
np.random.seed(1010)
x = np.random.normal(3,1,500)
y = np.random.normal(3.2,1,500)
stat,p_value = stats.mannwhitneyu(x,y,alternative='less')
print(stat,p_value)
3 拟合优度的
X
2
X^2
X2检验
(1)
X
2
X^2
X2检验
拟合优度检验是检验一组样本观测值是不是来自一个已知分布的总体。拟合优度的
x
2
x^2
x2检验的原理为:根据假设的分布可以在一些任意划分的k个范围计算出期望值
E
1
,
E
2
,
.
.
.
,
E
k
E_1,E2,...,E_k
E1?,E2,...,Ek?,而根据在这些区域所得到的观测值
O
1
,
O
2
,
.
.
.
,
O
k
O_1,O_2,...,O_k
O1?,O2?,...,Ok?可以得到检验统计量
X
2
=
~
i
=
1
k
(
O
i
?
E
i
)
2
E
i
X^2 = \sim_{i=1}^k\frac{(O_i-E_i)^2}{E_i}
X2=~i=1k?Ei?(Oi??Ei?)2?,渐渐服从
x
2
x^2
x2分布。
import scipy.stats as stats
import numpy as np
size = 5000
x= stats.poisson.rvs(10,size = size,random_state=1010)
fig = plt.figure(figsize=(12,5))
H = plt.hist(x,20,color='white','hatch='/')
E = np.diff(stats.poisson.cdf(H[1],10))*size
h_0 = H[0]
chisq,p = stats.chisquare(f_obs=h_0,f_exp=E)
print(chisq,p)
拟合优度检验还可以用Kolmogorov-Smirnov检验
stat,p_value = stats.kstest(x,lambda x:stats.poisson.cdf(x,10))
print(stat,p_value)
4 列联表的
X
2
X^2
X2检验及Fisher检验
(1)Pearson
X
2
X^2
X2检验检验
import scipy.stats as stats
import numpy as np
X =np.array([[10,20,4],[20,9,8]])
chi2,p,df,exp = stats.chi2_contingency(X)
print(p,df)
(2)似然比
X
2
X^2
X2检验
import scipy.stats as stats
import numpy as np
X =np.array([[10,20,4],[20,9,8]])
chi2,p,df,exp = stats.chi2_contingency(X,lambda_='log_likelihood')
print(p,df)
(3)Fisher
X
2
X^2
X2检验
import scipy.stats as stats
import numpy as np
X1 =np.array([[10,20],[20,9]])
oddsratio,p = stats.fisher_exact(X1)
print(oddsratio,p)
|