自由度 df のカイ二乗分布において,x 以上の値をとる確率を与える関数です。
# カイ二乗分布の上側確率
function xxp(x, is, i, t, w, pi2)
{
pi2 = 0.398942280401432677940
if (x == 0.0) {
return 1
}
else if (is == 1) {
return gxp(sqrt(x))*2.0
}
else if (is == 2) {
return exp(-x/2.0)
}
else if ((is%2) == 0) {
w = t = 1.0
for (i = 2; i <= is-2; i += 2) {
t *= x/i
w += t
}
return exp(log(w) - x*0.5)
}
else {
t = w = sqrt(x)
for (i = 3; i <= is-2; i += 2) {
t *= x/i
w += t
}
return 2.0*(gxp(sqrt(x))+pi2*exp(log(w)-x*0.5))
}
}