可以使用正则化(不完整)β函数I(x; a,b)来计算F分布的CDF(以及p值),例如参见MathWorld.使用I(x; a)的代码,b)从仅使用数学的blog中,p值为
1 - incompbeta(.5*df1, .5*df2, float(df1)*F/(df1*F+df2))
这是一些匹配scipy.stats.f.sf的样本值的结果:
In [57]: F, df1, df2 = 5, 20, 18
In [58]: 1 - incompbeta(.5*df1, .5*df2, float(df1)*F/(df1*F+df2))
Out[58]: 0.0005812207389501722
In [59]: st.f.sf(F, df1, df2)
Out[59]: 0.00058122073922042188
以防万一博客消失了,下面的代码:
import math
def incompbeta(a, b, x):
''' incompbeta(a,b,x) evaluates incomplete beta function, here a, b > 0 and 0 <= x <= 1. This function requires contfractbeta(a,b,x, ITMAX = 200)
(Code translated from: Numerical Recipes in C.)'''
if (x == 0):
return 0;
elif (x == 1):
return 1;
else:
lbeta = math.lgamma(a+b) - math.lgam