本文整理汇总了Python中scipy.stats.ttest_1samp方法的典型用法代码示例。如果您正苦于以下问题:Python stats.ttest_1samp方法的具体用法?Python stats.ttest_1samp怎么用?Python stats.ttest_1samp使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.stats
的用法示例。
在下文中一共展示了stats.ttest_1samp方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mcfdr
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import ttest_1samp [as 别名]
def mcfdr(nrepl=100, nobs=50, ntests=10, ntrue=6, mu=0.5, alpha=0.05, rho=0.):
'''MonteCarlo to test fdrcorrection
'''
nfalse = ntests - ntrue
locs = np.array([0.]*ntrue + [mu]*(ntests - ntrue))
results = []
for i in range(nrepl):
#rvs = locs + stats.norm.rvs(size=(nobs, ntests))
rvs = locs + randmvn(rho, size=(nobs, ntests))
tt, tpval = stats.ttest_1samp(rvs, 0)
res = fdrcorrection_bak(np.abs(tpval), alpha=alpha, method='i')
res0 = fdrcorrection0(np.abs(tpval), alpha=alpha)
#res and res0 give the same results
results.append([np.sum(res[:ntrue]), np.sum(res[ntrue:])] +
[np.sum(res0[:ntrue]), np.sum(res0[ntrue:])] +
res.tolist() +
np.sort(tpval).tolist() +
[np.sum(tpval[:ntrue]<alpha),
np.sum(tpval[ntrue:]<alpha)] +
[np.sum(tpval[:ntrue]<alpha/ntests),
np.sum(tpval[ntrue:]<alpha/ntests)])
return np.array(results)
示例2: test_onesample
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import ttest_1samp [as 别名]
def test_onesample(self):
t, p = stats.ttest_1samp(self.X1, 0)
assert_array_almost_equal(t, self.T1_0)
assert_array_almost_equal(p, self.P1_0)
t, p = stats.ttest_1samp(self.X2, 0)
assert_array_almost_equal(t, self.T2_0)
assert_array_almost_equal(p, self.P2_0)
t, p = stats.ttest_1samp(self.X1, 1)
assert_array_almost_equal(t, self.T1_1)
assert_array_almost_equal(p, self.P1_1)
t, p = stats.ttest_1samp(self.X1, 2)
assert_array_almost_equal(t, self.T1_2)
assert_array_almost_equal(p, self.P1_2)
示例3: compute_expected_treatment_likelihood_ratio
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import ttest_1samp [as 别名]
def compute_expected_treatment_likelihood_ratio(df, term_counts, word_index, do_shuffle=False):
word_occurence = term_counts[:,word_index].toarray()
mask_word = (word_occurence == 1).flatten()
p_ind = np.arange(term_counts.shape[0])
n = p_ind[mask_word].shape[0]
if do_shuffle:
np.random.shuffle(p_ind)
rand_indices = p_ind[:mask_word.shape[0]]
df_word_occurs = df[df.post_index.isin(rand_indices)]
else:
df_word_occurs = df[df.post_index.isin(p_ind[mask_word])]
model_p_z = train_treatment_classifier(df, term_counts, word_index, occurence=None)
prob_t_given_z = predict_treatment_prob_from_model(model_p_z, df_word_occurs)
model_st_word = train_treatment_classifier(df_word_occurs, term_counts, word_index,occurence=None)
prob_st_word = predict_treatment_prob_from_model(model_st_word, df_word_occurs)
log_ratio = np.log(prob_st_word/prob_t_given_z)
print("Mean (and std err.) of ratio:", log_ratio.mean(), log_ratio.std()/np.sqrt(n))
print("N = ", n)
return ttest_1samp(log_ratio, 0.0)
示例4: compute_treatment_likelihood_ratio
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import ttest_1samp [as 别名]
def compute_treatment_likelihood_ratio(df, term_counts, word_index):
null_model = train_treatment_classifier(df, term_counts, word_index, occurence=None)
model_st_word = train_treatment_classifier(df, term_counts, word_index)
model_st_no_word = train_treatment_classifier(df, term_counts, word_index, occurence=0)
word_occurence = term_counts[:,word_index].toarray()
mask_word = (word_occurence == 1).flatten()
mask_no_word = (word_occurence == 0).flatten()
p_ind = np.arange(term_counts.shape[0])
word_occurs = df[df.post_index.isin(p_ind[mask_word])]
word_no_occurs = df[df.post_index.isin(p_ind[mask_no_word])]
null_given_word = predict_treatment_prob_from_model(null_model, word_occurs)
null_given_no_word = predict_treatment_prob_from_model(null_model, word_no_occurs)
p_t_given_word = predict_treatment_prob_from_model(model_st_word, word_occurs)
p_t_given_no_word = predict_treatment_prob_from_model(model_st_no_word, word_no_occurs)
ratio_word = np.log(null_given_word / p_t_given_word)
ratio_no_word = np.log(null_given_no_word / p_t_given_no_word)
ratio = np.hstack([ratio_word, ratio_no_word])
print("Mean and std of log likelihood ratios", ratio.mean(), ratio.std())
# return -2*ratio.sum(), chi2.pdf(-2*ratio.sum(), df=2)
return ttest_1samp(ratio, 0.0)
示例5: likelihood_ratio_hypothesis_test
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import ttest_1samp [as 别名]
def likelihood_ratio_hypothesis_test(df, term_counts, word_index):
treated = df[df.treatment==1]
control = df[df.treatment==0]
null_model = train_classifier(df, term_counts, word_index, treat_index=None)
model_st_treated = train_classifier(df, term_counts, word_index)
model_st_not_treated = train_classifier(df, term_counts, word_index, treat_index=0)
null_st_treated = compute_word_occur_prob(null_model, treated)
null_st_not_treated = compute_word_occur_prob(null_model, control)
prob_st_treated = compute_word_occur_prob(model_st_treated, treated)
prob_st_not_treated = compute_word_occur_prob(model_st_not_treated)
ratio_treated = np.log(null_st_treated / prob_st_treated)
ratio_control = np.log(null_st_not_treated / prob_st_not_treated)
ratios = np.hstack([ratio_treated,ratio_control])
print("Mean and std. of log likelihood ratios:", ratios.mean(), ratios.std())
return ttest_1samp(ratios, 0.0)
# statistic = -2 * ratios.sum()
# p_value = chi2.pdf(statistic, df=2)
# return statistic, p_value
示例6: plot_information_table
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import ttest_1samp [as 别名]
def plot_information_table(ic_data):
"""
IC 统计量
"""
ic_summary_table = pd.DataFrame()
ic_summary_table["IC Mean"] = ic_data.mean()
ic_summary_table["IC std."] = ic_data.std()
ic_summary_table["Risk-Adjusted IC (IR)"] = ic_data.mean() / ic_data.std()
t_stat, p_value = stats.ttest_1samp(ic_data, 0)
ic_summary_table["t-stat (IC)"] = t_stat
ic_summary_table["p-value (IC)"] = p_value
ic_summary_table["IC Skew"] = stats.skew(ic_data)
ic_summary_table["IC Kurtosis"] = stats.kurtosis(ic_data)
print("Information Analysis")
plotting_utils.print_table(ic_summary_table.apply(lambda x: x.round(3)).T)
示例7: test_vs_nonmasked
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import ttest_1samp [as 别名]
def test_vs_nonmasked(self):
np.random.seed(1234567)
outcome = np.random.randn(20, 4) + [0, 0, 1, 2]
# 1-D inputs
res1 = stats.ttest_1samp(outcome[:, 0], 1)
res2 = mstats.ttest_1samp(outcome[:, 0], 1)
assert_allclose(res1, res2)
# 2-D inputs
res1 = stats.ttest_1samp(outcome[:, 0], outcome[:, 1], axis=None)
res2 = mstats.ttest_1samp(outcome[:, 0], outcome[:, 1], axis=None)
assert_allclose(res1, res2)
res1 = stats.ttest_1samp(outcome[:, :2], outcome[:, 2:], axis=0)
res2 = mstats.ttest_1samp(outcome[:, :2], outcome[:, 2:], axis=0)
assert_allclose(res1, res2)
# Check default is axis=0
res3 = mstats.ttest_1samp(outcome[:, :2], outcome[:, 2:])
assert_allclose(res2, res3)
示例8: main
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import ttest_1samp [as 别名]
def main():
[(_ignore_stat, first), (_ignore_stat, second)] = load_stats(sys.argv[1:])
# Attempt to increase robustness by dropping the outlying 10% of values.
first = trim(first, 0.1)
second = trim(second, 0.1)
fmean = stats.mean(first)
smean = stats.mean(second)
p = ttest_1samp(second, fmean)[1]
if p >= 0.95:
# rejected the null hypothesis
print(sys.argv[1], 'mean of', fmean, 'differs from', sys.argv[2], 'mean of', smean, '(%2.0f%%)' % (p * 100,))
else:
# failed to reject the null hypothesis
print('cannot prove means (%s, %s) differ (%2.0f%%)' % (fmean, smean, p * 100,))
示例9: test_weightstats_2
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import ttest_1samp [as 别名]
def test_weightstats_2(self):
x1, x2 = self.x1, self.x2
w1, w2 = self.w1, self.w2
d1 = DescrStatsW(x1)
d1w = DescrStatsW(x1, weights=w1)
d2w = DescrStatsW(x2, weights=w2)
x1r = d1w.asrepeats()
x2r = d2w.asrepeats()
# print 'random weights'
# print ttest_ind(x1, x2, weights=(w1, w2))
# print stats.ttest_ind(x1r, x2r)
assert_almost_equal(ttest_ind(x1, x2, weights=(w1, w2))[:2],
stats.ttest_ind(x1r, x2r), 14)
# not the same as new version with random weights/replication
# assert x1r.shape[0] == d1w.sum_weights
# assert x2r.shape[0] == d2w.sum_weights
assert_almost_equal(x2r.mean(0), d2w.mean, 14)
assert_almost_equal(x2r.var(), d2w.var, 14)
assert_almost_equal(x2r.std(), d2w.std, 14)
# note: the following is for 1d
assert_almost_equal(np.cov(x2r, bias=1), d2w.cov, 14)
# assert_almost_equal(np.corrcoef(np.x2r), d2w.corrcoef, 19)
# TODO: exception in corrcoef (scalar case)
# one-sample tests
# print d1.ttest_mean(3)
# print stats.ttest_1samp(x1, 3)
# print d1w.ttest_mean(3)
# print stats.ttest_1samp(x1r, 3)
assert_almost_equal(d1.ttest_mean(3)[:2], stats.ttest_1samp(x1, 3), 11)
assert_almost_equal(d1w.ttest_mean(3)[:2],
stats.ttest_1samp(x1r, 3), 11)
示例10: test_weightstats_3
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import ttest_1samp [as 别名]
def test_weightstats_3(self):
x1_2d, x2_2d = self.x1_2d, self.x2_2d
w1, w2 = self.w1, self.w2
d1w_2d = DescrStatsW(x1_2d, weights=w1)
d2w_2d = DescrStatsW(x2_2d, weights=w2)
x1r_2d = d1w_2d.asrepeats()
x2r_2d = d2w_2d.asrepeats()
assert_almost_equal(x2r_2d.mean(0), d2w_2d.mean, 14)
assert_almost_equal(x2r_2d.var(0), d2w_2d.var, 14)
assert_almost_equal(x2r_2d.std(0), d2w_2d.std, 14)
assert_almost_equal(np.cov(x2r_2d.T, bias=1), d2w_2d.cov, 14)
assert_almost_equal(np.corrcoef(x2r_2d.T), d2w_2d.corrcoef, 14)
# print d1w_2d.ttest_mean(3)
# #scipy.stats.ttest is also vectorized
# print stats.ttest_1samp(x1r_2d, 3)
t, p, d = d1w_2d.ttest_mean(3)
assert_almost_equal([t, p], stats.ttest_1samp(x1r_2d, 3), 11)
# print [stats.ttest_1samp(xi, 3) for xi in x1r_2d.T]
cm = CompareMeans(d1w_2d, d2w_2d)
ressm = cm.ttest_ind()
resss = stats.ttest_ind(x1r_2d, x2r_2d)
assert_almost_equal(ressm[:2], resss, 14)
# doesn't work for 2d, levene doesn't use weights
# cm = CompareMeans(d1w_2d, d2w_2d)
# ressm = cm.test_equal_var()
# resss = stats.levene(x1r_2d, x2r_2d)
# assert_almost_equal(ressm[:2], resss, 14)
示例11: test_ttest
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import ttest_1samp [as 别名]
def test_ttest(self):
x1r = self.x1r
d1w = self.d1w
assert_almost_equal(d1w.ttest_mean(3)[:2],
stats.ttest_1samp(x1r, 3), 11)
# def
# assert_almost_equal(ttest_ind(x1, x2, weights=(w1, w2))[:2],
# stats.ttest_ind(x1r, x2r), 14)
示例12: linear_harvey_collier
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import ttest_1samp [as 别名]
def linear_harvey_collier(res):
'''Harvey Collier test for linearity
The Null hypothesis is that the regression is correctly modeled as linear.
Parameters
----------
res : Result instance
Returns
-------
tvalue : float
test statistic, based on ttest_1sample
pvalue : float
pvalue of the test
Notes
-----
TODO: add sort_by option
This test is a t-test that the mean of the recursive ols residuals is zero.
Calculating the recursive residuals might take some time for large samples.
'''
#I think this has different ddof than
#B.H. Baltagi, Econometrics, 2011, chapter 8
#but it matches Gretl and R:lmtest, pvalue at decimal=13
rr = recursive_olsresiduals(res, skip=3, alpha=0.95)
from scipy import stats
return stats.ttest_1samp(rr[3][3:], 0)
示例13: ttest
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import ttest_1samp [as 别名]
def ttest(_arg1, _arg2):
"""
T-Test is a statistical hypothesis test that is used to compare
two sample means or a sample’s mean against a known population mean.
For more information on the function and how to use it please refer
to tabpy-tools.md
"""
# one sample test with mean
if len(_arg2) == 1:
test_stat, p_value = stats.ttest_1samp(_arg1, _arg2)
return p_value
# two sample t-test where _arg1 is numeric and _arg2 is a binary factor
elif len(set(_arg2)) == 2:
# each sample in _arg1 needs to have a corresponding classification
# in _arg2
if not (len(_arg1) == len(_arg2)):
raise ValueError
class1, class2 = set(_arg2)
sample1 = []
sample2 = []
for i in range(len(_arg1)):
if _arg2[i] == class1:
sample1.append(_arg1[i])
else:
sample2.append(_arg1[i])
test_stat, p_value = stats.ttest_ind(sample1, sample2, equal_var=False)
return p_value
# arg1 is a sample and arg2 is a sample
else:
test_stat, p_value = stats.ttest_ind(_arg1, _arg2, equal_var=False)
return p_value
示例14: test_ttest_1samp_new
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import ttest_1samp [as 别名]
def test_ttest_1samp_new():
n1, n2, n3 = (10,15,20)
rvn1 = stats.norm.rvs(loc=5,scale=10,size=(n1,n2,n3))
# check multidimensional array and correct axis handling
# deterministic rvn1 and rvn2 would be better as in test_ttest_rel
t1,p1 = stats.ttest_1samp(rvn1[:,:,:], np.ones((n2,n3)),axis=0)
t2,p2 = stats.ttest_1samp(rvn1[:,:,:], 1,axis=0)
t3,p3 = stats.ttest_1samp(rvn1[:,0,0], 1)
assert_array_almost_equal(t1,t2, decimal=14)
assert_almost_equal(t1[0,0],t3, decimal=14)
assert_equal(t1.shape, (n2,n3))
t1,p1 = stats.ttest_1samp(rvn1[:,:,:], np.ones((n1,n3)),axis=1)
t2,p2 = stats.ttest_1samp(rvn1[:,:,:], 1,axis=1)
t3,p3 = stats.ttest_1samp(rvn1[0,:,0], 1)
assert_array_almost_equal(t1,t2, decimal=14)
assert_almost_equal(t1[0,0],t3, decimal=14)
assert_equal(t1.shape, (n1,n3))
t1,p1 = stats.ttest_1samp(rvn1[:,:,:], np.ones((n1,n2)),axis=2)
t2,p2 = stats.ttest_1samp(rvn1[:,:,:], 1,axis=2)
t3,p3 = stats.ttest_1samp(rvn1[0,0,:], 1)
assert_array_almost_equal(t1,t2, decimal=14)
assert_almost_equal(t1[0,0],t3, decimal=14)
assert_equal(t1.shape, (n1,n2))
olderr = np.seterr(all='ignore')
try:
# test zero division problem
t,p = stats.ttest_1samp([0,0,0], 1)
assert_equal((np.abs(t),p), (np.inf, 0))
assert_equal(stats.ttest_1samp([0,0,0], 0), (np.nan, np.nan))
# check that nan in input array result in nan output
anan = np.array([[1,np.nan],[-1,1]])
assert_equal(stats.ttest_1samp(anan, 0),([0, np.nan], [1,np.nan]))
finally:
np.seterr(**olderr)
示例15: agreement_t_test
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import ttest_1samp [as 别名]
def agreement_t_test(a, b):
""" Check agreement based on means of two samples, using the t-statistic. """
means1 = np.nanmean(a, 0)
t1, p1 = stats.ttest_1samp(b, means1)
# t2, p2 = stats.ttest_1samp(a, means2)
# select agreeing featurs (p <= 0.05)
return p1 < 0.05