1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
// Copyright ©2016 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
* Cephes Math Library, Release 2.3: March, 1995
* Copyright 1984, 1995 by Stephen L. Moshier
*/
package cephes
import (
"math"
"gonum.org/v1/gonum/mathext/internal/gonum"
)
const (
maxGam = 171.624376956302725
big = 4.503599627370496e15
biginv = 2.22044604925031308085e-16
)
// Incbet computes the regularized incomplete beta function.
func Incbet(aa, bb, xx float64) float64 {
if aa <= 0 || bb <= 0 {
panic(paramOutOfBounds)
}
if xx <= 0 || xx >= 1 {
if xx == 0 {
return 0
}
if xx == 1 {
return 1
}
panic(paramOutOfBounds)
}
var flag int
if bb*xx <= 1 && xx <= 0.95 {
t := pseries(aa, bb, xx)
return transformT(t, flag)
}
w := 1 - xx
// Reverse a and b if x is greater than the mean.
var a, b, xc, x float64
if xx > aa/(aa+bb) {
flag = 1
a = bb
b = aa
xc = xx
x = w
} else {
a = aa
b = bb
xc = w
x = xx
}
if flag == 1 && (b*x) <= 1.0 && x <= 0.95 {
t := pseries(a, b, x)
return transformT(t, flag)
}
// Choose expansion for better convergence.
y := x*(a+b-2.0) - (a - 1.0)
if y < 0.0 {
w = incbcf(a, b, x)
} else {
w = incbd(a, b, x) / xc
}
// Multiply w by the factor
// x^a * (1-x)^b * Γ(a+b) / (a*Γ(a)*Γ(b))
var t float64
y = a * math.Log(x)
t = b * math.Log(xc)
if (a+b) < maxGam && math.Abs(y) < maxLog && math.Abs(t) < maxLog {
t = math.Pow(xc, b)
t *= math.Pow(x, a)
t /= a
t *= w
t *= 1.0 / gonum.Beta(a, b)
return transformT(t, flag)
}
// Resort to logarithms.
y += t - gonum.Lbeta(a, b)
y += math.Log(w / a)
if y < minLog {
t = 0.0
} else {
t = math.Exp(y)
}
return transformT(t, flag)
}
func transformT(t float64, flag int) float64 {
if flag == 1 {
if t <= machEp {
t = 1.0 - machEp
} else {
t = 1.0 - t
}
}
return t
}
// incbcf returns the incomplete beta integral evaluated by a continued fraction
// expansion.
func incbcf(a, b, x float64) float64 {
var xk, pk, pkm1, pkm2, qk, qkm1, qkm2 float64
var k1, k2, k3, k4, k5, k6, k7, k8 float64
var r, t, ans, thresh float64
var n int
k1 = a
k2 = a + b
k3 = a
k4 = a + 1.0
k5 = 1.0
k6 = b - 1.0
k7 = k4
k8 = a + 2.0
pkm2 = 0.0
qkm2 = 1.0
pkm1 = 1.0
qkm1 = 1.0
ans = 1.0
r = 1.0
thresh = 3.0 * machEp
for n = 0; n <= 300; n++ {
xk = -(x * k1 * k2) / (k3 * k4)
pk = pkm1 + pkm2*xk
qk = qkm1 + qkm2*xk
pkm2 = pkm1
pkm1 = pk
qkm2 = qkm1
qkm1 = qk
xk = (x * k5 * k6) / (k7 * k8)
pk = pkm1 + pkm2*xk
qk = qkm1 + qkm2*xk
pkm2 = pkm1
pkm1 = pk
qkm2 = qkm1
qkm1 = qk
if qk != 0 {
r = pk / qk
}
if r != 0 {
t = math.Abs((ans - r) / r)
ans = r
} else {
t = 1.0
}
if t < thresh {
return ans
}
k1 += 1.0
k2 += 1.0
k3 += 2.0
k4 += 2.0
k5 += 1.0
k6 -= 1.0
k7 += 2.0
k8 += 2.0
if (math.Abs(qk) + math.Abs(pk)) > big {
pkm2 *= biginv
pkm1 *= biginv
qkm2 *= biginv
qkm1 *= biginv
}
if (math.Abs(qk) < biginv) || (math.Abs(pk) < biginv) {
pkm2 *= big
pkm1 *= big
qkm2 *= big
qkm1 *= big
}
}
return ans
}
// incbd returns the incomplete beta integral evaluated by a continued fraction
// expansion.
func incbd(a, b, x float64) float64 {
var xk, pk, pkm1, pkm2, qk, qkm1, qkm2 float64
var k1, k2, k3, k4, k5, k6, k7, k8 float64
var r, t, ans, z, thresh float64
var n int
k1 = a
k2 = b - 1.0
k3 = a
k4 = a + 1.0
k5 = 1.0
k6 = a + b
k7 = a + 1.0
k8 = a + 2.0
pkm2 = 0.0
qkm2 = 1.0
pkm1 = 1.0
qkm1 = 1.0
z = x / (1.0 - x)
ans = 1.0
r = 1.0
thresh = 3.0 * machEp
for n = 0; n <= 300; n++ {
xk = -(z * k1 * k2) / (k3 * k4)
pk = pkm1 + pkm2*xk
qk = qkm1 + qkm2*xk
pkm2 = pkm1
pkm1 = pk
qkm2 = qkm1
qkm1 = qk
xk = (z * k5 * k6) / (k7 * k8)
pk = pkm1 + pkm2*xk
qk = qkm1 + qkm2*xk
pkm2 = pkm1
pkm1 = pk
qkm2 = qkm1
qkm1 = qk
if qk != 0 {
r = pk / qk
}
if r != 0 {
t = math.Abs((ans - r) / r)
ans = r
} else {
t = 1.0
}
if t < thresh {
return ans
}
k1 += 1.0
k2 -= 1.0
k3 += 2.0
k4 += 2.0
k5 += 1.0
k6 += 1.0
k7 += 2.0
k8 += 2.0
if (math.Abs(qk) + math.Abs(pk)) > big {
pkm2 *= biginv
pkm1 *= biginv
qkm2 *= biginv
qkm1 *= biginv
}
if (math.Abs(qk) < biginv) || (math.Abs(pk) < biginv) {
pkm2 *= big
pkm1 *= big
qkm2 *= big
qkm1 *= big
}
}
return ans
}
// pseries returns the incomplete beta integral evaluated by a power series. Use
// when b*x is small and x not too close to 1.
func pseries(a, b, x float64) float64 {
var s, t, u, v, n, t1, z, ai float64
ai = 1.0 / a
u = (1.0 - b) * x
v = u / (a + 1.0)
t1 = v
t = u
n = 2.0
s = 0.0
z = machEp * ai
for math.Abs(v) > z {
u = (n - b) * x / n
t *= u
v = t / (a + n)
s += v
n += 1.0
}
s += t1
s += ai
u = a * math.Log(x)
if (a+b) < maxGam && math.Abs(u) < maxLog {
t = 1.0 / gonum.Beta(a, b)
s = s * t * math.Pow(x, a)
} else {
t = -gonum.Lbeta(a, b) + u + math.Log(s)
if t < minLog {
s = 0.0
} else {
s = math.Exp(t)
}
}
return (s)
}
|