Skip to content

Instantly share code, notes, and snippets.

@lucinda-lim
Last active October 30, 2020 05:22
Show Gist options
  • Save lucinda-lim/a57ca0378a35f856519e48312d54b918 to your computer and use it in GitHub Desktop.
Save lucinda-lim/a57ca0378a35f856519e48312d54b918 to your computer and use it in GitHub Desktop.
## data_a: forecast_value, data_b: pass_value
def Welch_ttest(data_a, data_b, alpha):
mean_a, mean_b = np.mean(data_a), np.mean(data_b)
# squared standard error
se_a, se_b = stats.tstd(data_a)**2.0/len(data_a), stats.tstd(data_b)**2.0/len(data_b)
#v
v_a, v_b = len(data_a)-1, len(data_b)-1
# assume unequal variance
t_stat, p_2tail = ttest_ind(data_a, data_b, equal_var=False)
# degree of freedom
degreeF = ( (se_a+se_b)**2.0 )/ ( ((se_a**2.0)/v_a) + ((se_b**2.0)/v_b) )
# critical value
cv = t.ppf(1.0-alpha, degreeF)
# calculate the p-value,one sided
p = (1.0-t.cdf(abs(t_stat), degreeF))
# h0: mean_a-mean_b<=0 ; h1: mean_a-mean_b>0
if t_stat > cv and mean_a > mean_b:
## trend
return 1
else:
## not trend
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment