-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeta.go
37 lines (32 loc) · 1.13 KB
/
beta.go
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
// based on [email protected]:leesper/go_rng.git
package rand
import (
"fmt"
)
func _beta(alpha, beta float64, random GetRandomFloat64Fn) float64 {
// according to "Numerical Recipes", all distinct
// gamma variates must have different random seeds
// MK : as we use a pool to back Gamma this should ok
x := gamma(alpha, 1, random)
y := gamma(beta, 1, random)
return x / (x + y)
}
func calculateBeta(alpha, beta float64, random GetRandomFloat64Fn) float64 {
if !(alpha > 0.0) {
panic(fmt.Sprintf("Invalid parameter alpha: %.2f", alpha))
}
if !(beta > 0.0) {
panic(fmt.Sprintf("Invalid parameter beta: %.2f", beta))
}
return _beta(alpha, beta, random)
}
// Beta returns a random number of beta distribution (alpha > 0.0 and beta > 0.0)
// Uses package's built-in float64 randomization routine.
func Beta(alpha, beta float64) float64 {
return calculateBeta(alpha, beta, Float64)
}
// Beta returns a random number of beta distribution (alpha > 0.0 and beta > 0.0)
// Uses externally provided float64 randomization routine.
func BetaEx(alpha, beta float64, random GetRandomFloat64Fn) float64 {
return calculateBeta(alpha, beta, random)
}