Python笔记-假设检验之单样本T检验
生活随笔
收集整理的這篇文章主要介紹了
Python笔记-假设检验之单样本T检验
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
概念
假設某個數(shù)據(jù)的均值為u,實際抽樣時離u越近說明假設的這個均值越合理,越遠,就說明越不合理。
這里又有個p-value的概念,代表實際抽樣的結(jié)果與假設的差異程度。值越大意味著越無差異。實際中我們會自己設置一個閾值,如0.05,當計算出來的p-value大于這個0.05時,就滿足我們的需求,這個0.05叫顯著性水平。
單樣本T檢驗:檢驗單個樣本的平均值是否等于目標值;
Python代碼
如下場景,一串數(shù)據(jù),他的增長率是否是0.1,顯著性水平為0.5,也就是p-value大于0.5說明檢驗單個樣本的平均值等于目標值;
如下代碼:
import statsmodels.api as smvalueList = [0.169747191462884, 0.165484359308337, 0.141358295556684, 0.0631967134074211, 0.101527686160212]if __name__ == '__main__':d = sm.stats.DescrStatsW(valueList)print('t檢驗= %6.4f,p-value=%6.4f, df=%s' % d.ttest_mean(0.10))pass運行截圖如下:
?其中來看下ttest_mean這個函數(shù)
def ttest_mean(self, value=0, alternative="two-sided"):"""ttest of Null hypothesis that mean is equal to value.The alternative hypothesis H1 is defined by the following- 'two-sided': H1: mean not equal to value- 'larger' : H1: mean larger than value- 'smaller' : H1: mean smaller than valueParameters----------value : float or arraythe hypothesized value for the meanalternative : strThe alternative hypothesis, H1, has to be one of the following:- 'two-sided': H1: mean not equal to value (default)- 'larger' : H1: mean larger than value- 'smaller' : H1: mean smaller than valueReturns-------tstat : floattest statisticpvalue : floatpvalue of the t-testdf : int or float"""# TODO: check direction with R, smaller=less, larger=greatertstat = (self.mean - value) / self.std_meandof = self.sum_weights - 1# TODO: use outsourcedif alternative == "two-sided":pvalue = stats.t.sf(np.abs(tstat), dof) * 2elif alternative == "larger":pvalue = stats.t.sf(tstat, dof)elif alternative == "smaller":pvalue = stats.t.cdf(tstat, dof)return tstat, pvalue, dof需要注意以下幾點:
①ttest_mean有2個參數(shù),一個是value,一般傳array進去,第二個參數(shù)有3個值,分別是:
"two-sided": 不等與value;(默認) "larger": 大于value; "smaller": 小于value;②返回值有3個參數(shù):
tstat : float :t檢驗值,越大說明越合理 pvalue : float :p-value值,和設置的顯著水平比較,證明這個傳入的參數(shù)value是否合理; df : int or float :上面的數(shù)據(jù)類型是什么float還是int,保留幾位小數(shù);總結(jié)
以上是生活随笔為你收集整理的Python笔记-假设检验之单样本T检验的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java引用数据类型可以更改类型_jav
- 下一篇: java semaphorewa_Jav