def tstat(beta, var, sigma, q, N, log=False):
"""
Calculates a t-statistic and associated p-value given the estimate of beta and its standard error.
This is actually an F-test, but when only one hypothesis is being performed, it reduces to a t-test.
"""
ts = beta / np.sqrt(var * sigma)
# ts = beta / np.sqrt(sigma)
# ps = 2.0*(1.0 - stats.t.cdf(np.abs(ts), self.N-q))
# sf == survival function - this is more accurate -- could also use logsf if the precision is not good enough
if log:
ps = 2.0 + (stats.t.logsf(np.abs(ts), N - q))
else:
ps = 2.0 * (stats.t.sf(np.abs(ts), N - q))
if not len(ts) == 1 or not len(ps) == 1:
raise Exception("Something bad happened :(")
# return ts, ps
return ts.sum(), ps.sum()
评论列表
文章目录