|
| 1 | +const errmax = 1e-15 |
| 2 | + |
| 3 | +#Compute tail of noncentral Beta distribution |
| 4 | +#Russell Lenth, Algorithm AS 226: Computing Noncentral Beta Probabilities, |
| 5 | +#Applied Statistics,Volume 36, Number 2, 1987, pages 241-244 |
| 6 | + |
| 7 | +""" |
| 8 | + ncbeta_tail(x,a,b,lambda) |
| 9 | +
|
| 10 | +Compute tail of the noncentral beta distribution. |
| 11 | +Uses the recursive relation |
| 12 | +```math |
| 13 | +I_{x}(a,b+1;0) = I_{x}(a,b;0) - \\Gamma(a+b)/\\Gamma(a+1)\\Gamma(b)x^{a}(1-x)^{b} |
| 14 | +``` |
| 15 | +and ``\\Gamma(a+1) = a\\Gamma(a)`` given in https://dlmf.nist.gov/8.17.21. |
| 16 | +""" |
| 17 | +function ncbeta_tail(a::Float64, b::Float64, lambda::Float64, x::Float64) |
| 18 | + if x <= 0.0 |
| 19 | + return 0.0 |
| 20 | + elseif x >= 1.0 |
| 21 | + return 1.0 |
| 22 | + end |
| 23 | + |
| 24 | + c = 0.5*lambda |
| 25 | + #Init series |
| 26 | + |
| 27 | + beta = logabsbeta(a,b)[1] |
| 28 | + temp = beta_inc(a,b,x)[1] |
| 29 | + gx = (beta_integrand(a,b,x,1.0-x))/a |
| 30 | + q = exp(-c) |
| 31 | + xj = 0.0 |
| 32 | + ax = q*temp |
| 33 | + sumq = 1.0 - q |
| 34 | + ans = ax |
| 35 | + |
| 36 | + while true |
| 37 | + xj += 1.0 |
| 38 | + temp -= gx |
| 39 | + gx *= x*(a+b+xj-1.0)/(a+xj) |
| 40 | + q *= c/xj |
| 41 | + sumq -= q |
| 42 | + ax = temp*q |
| 43 | + ans += ax |
| 44 | + |
| 45 | + #Check convergence |
| 46 | + errbd = abs((temp-gx)*sumq) |
| 47 | + if xj > 1000 || errbd < 1e-10 |
| 48 | + break |
| 49 | + end |
| 50 | + end |
| 51 | + return ans |
| 52 | +end |
| 53 | + |
| 54 | +""" |
| 55 | + ncbeta_poisson(a,b,lambda,x) |
| 56 | +
|
| 57 | +Compute CDF of noncentral beta if lambda >= 54 using: |
| 58 | +First ``\\lambda/2`` is calculated and the Poisson term is calculated using ``P(j-1)=j/\\lambda P(j)`` and ``P(j+1) = \\lambda/(j+1) P(j)``. |
| 59 | +Then backward recurrences are used until either the Poisson weights fall below `errmax` or `iterlo` is reached. |
| 60 | +```math |
| 61 | +I_{x}(a+j-1,b) = I_{x}(a+j,b) + \\Gamma(a+b+j-1)/\\Gamma(a+j)\\Gamma(b)x^{a+j-1}(1-x)^{b} |
| 62 | +``` |
| 63 | +Then forward recurrences are used until error bound falls below `errmax`. |
| 64 | +```math |
| 65 | +I_{x}(a+j+1,b) = I_{x}(a+j,b) - \\Gamma(a+b+j)/\\Gamma(a+j)\\Gamma(b)x^{a+j}(1-x)^{b} |
| 66 | +``` |
| 67 | +""" |
| 68 | +function ncbeta_poisson(a::Float64, b::Float64, lambda::Float64, x::Float64) |
| 69 | + c = 0.5*lambda |
| 70 | + xj = 0.0 |
| 71 | + m = round(Int, c) |
| 72 | + mr = float(m) |
| 73 | + iterlo = m - trunc(Int, 5.0*sqrt(mr)) |
| 74 | + iterhi = m + trunc(Int, 5.0*sqrt(mr)) |
| 75 | + t = -c + mr*log(c) - logabsgamma(mr + 1.0)[1] |
| 76 | + q = exp(t) |
| 77 | + r = q |
| 78 | + psum = q |
| 79 | + |
| 80 | + beta = logabsbeta(a+mr,b)[1] |
| 81 | + gx = beta_integrand(a+mr,b,x,1.0-x)/(a + mr) |
| 82 | + fx = gx |
| 83 | + temp = beta_inc(a+mr,b,x)[1] |
| 84 | + ftemp = temp |
| 85 | + xj += 1.0 |
| 86 | + |
| 87 | + sm = q*temp |
| 88 | + iter1 = m |
| 89 | + |
| 90 | + #Iterations start from M and goes downwards |
| 91 | + |
| 92 | + for iter1 = m:-1:iterlo |
| 93 | + if q < errmax |
| 94 | + break |
| 95 | + end |
| 96 | + |
| 97 | + q *= iter1/c |
| 98 | + xj += 1.0 |
| 99 | + gx *= (a + iter1)/(x*(a+b+iter1-1.0)) |
| 100 | + iter1 -= 1 |
| 101 | + temp += gx |
| 102 | + psum += q |
| 103 | + sm += q*temp |
| 104 | + end |
| 105 | + |
| 106 | + t0 = logabsgamma(a+b)[1] - logabsgamma(a+1.0)[1] - logabsgamma(b)[1] |
| 107 | + s0 = a*log(x) + b*log(1.0-x) |
| 108 | + |
| 109 | + s = 0.0 |
| 110 | + for j = 0:iter1-1 |
| 111 | + s += exp(t0+s0+j*log(x)) |
| 112 | + t1 = log(a+b+j) - log(a+j+1.0) + t0 |
| 113 | + t0 = t1 |
| 114 | + end |
| 115 | + #Compute first part of error bound |
| 116 | + |
| 117 | + errbd = (gamma_inc(float(iter1),c,0)[2])*(temp+s) |
| 118 | + q = r |
| 119 | + temp = ftemp |
| 120 | + gx = fx |
| 121 | + iter2 = m |
| 122 | + #Iterations for the higher part |
| 123 | + |
| 124 | + for iter2 = m:iterhi-1 |
| 125 | + ebd = errbd + (1.0 - psum)*temp |
| 126 | + if ebd < errmax |
| 127 | + return sm |
| 128 | + end |
| 129 | + iter2 += 1 |
| 130 | + xj += 1.0 |
| 131 | + q *= c/iter2 |
| 132 | + psum += q |
| 133 | + temp -= gx |
| 134 | + gx *= x*(a+b+iter2-1.0)/(a+iter2) |
| 135 | + sm += q*temp |
| 136 | + end |
| 137 | + return sm |
| 138 | +end |
| 139 | + |
| 140 | +#R Chattamvelli, R Shanmugam, Algorithm AS 310: Computing the Non-central Beta Distribution Function, |
| 141 | +#Applied Statistics, Volume 46, Number 1, 1997, pages 146-156 |
| 142 | + |
| 143 | +""" |
| 144 | + ncbeta(a,b,lambda,x) |
| 145 | +
|
| 146 | +Compute the CDF of the noncentral beta distribution given by |
| 147 | +```math |
| 148 | +I_{x}(a,b;\\lambda ) = \\sum_{j=0}^{\\infty}q(\\lambda/2,j)I_{x}(a+j,b;0) |
| 149 | +``` |
| 150 | +For lambda < 54 : algorithm suggested by Lenth(1987) in ncbeta_tail(a,b,lambda,x). |
| 151 | +Else for lambda >= 54 : modification in Chattamvelli(1997) in ncbeta_poisson(a,b,lambda,x) by using both forward and backward recurrences. |
| 152 | +""" |
| 153 | +function ncbeta(a::Float64, b::Float64, lambda::Float64, x::Float64) |
| 154 | + ans = x |
| 155 | + if x <= 0.0 |
| 156 | + return 0.0 |
| 157 | + elseif x >= 1.0 |
| 158 | + return 1.0 |
| 159 | + end |
| 160 | + |
| 161 | + if lambda < 54.0 |
| 162 | + return ncbeta_tail(a,b,lambda,x) |
| 163 | + else |
| 164 | + return ncbeta_poisson(a,b,lambda,x) |
| 165 | + end |
| 166 | +end |
| 167 | + |
| 168 | +""" |
| 169 | + ncF(x,v1,v2,lambda) |
| 170 | +
|
| 171 | +Compute CDF of noncentral F distribution given by: |
| 172 | +```math |
| 173 | +F(x, v1, v2; lambda) = I_{v1*x/(v1*x + v2)}(v1/2, v2/2; \\lambda) |
| 174 | +``` |
| 175 | +where ``I_{x}(a,b; lambda)`` is the noncentral beta function computed above. |
| 176 | +
|
| 177 | +Wikipedia: https://en.wikipedia.org/wiki/Noncentral_F-distribution |
| 178 | +""" |
| 179 | +function ncF(x::Float64, v1::Float64, v2::Float64, lambda::Float64) |
| 180 | + return ncbeta(v1/2, v2/2, lambda, (v1*x)/(v1*x + v2)) |
| 181 | +end |
| 182 | + |
| 183 | +function ncbeta(a::T,b::T,lambda::T,x::T) where {T<:Union{Float16,Float32}} |
| 184 | + T.(ncbeta(Float64(a),Float64(b),Float64(lambda),Float64(x))) |
| 185 | +end |
| 186 | + |
| 187 | +function ncF(x::T,v1::T,v2::T,lambda::T) where {T<:Union{Float16,Float32}} |
| 188 | + T.(ncF(Float64(x),Float64(v1),Float64(v2),Float64(lambda))) |
| 189 | +end |
| 190 | + |
| 191 | +ncbeta(a::Real,b::Real,lambda::Real,x::Real) = ncbeta(promote(float(a),float(b),float(lambda),float(x))...) |
| 192 | +ncbeta(a::T,b::T,lambda::T,x::T) where {T<:AbstractFloat} = throw(MethodError(ncbeta,(a,b,lambda,x,""))) |
| 193 | +ncF(x::Real,v1::Real,v2::Real,lambda::Real) = ncF(promote(float(x),float(v1),float(v2),float(lambda))...) |
| 194 | +ncF(x::T,v1::T,v2::T,lambda::T) where {T<:AbstractFloat} = throw(MethodError(ncF,(x,v1,v2,lambda,""))) |
| 195 | + |
0 commit comments