forked from ematvey/gostat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchoice.go
55 lines (53 loc) · 949 Bytes
/
choice.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package stat
import (
"math"
)
func Choice_PMF(θ []float64) func(i int64) float64 {
return func(i int64) float64 {
return θ[i]
}
}
func Choice_LnPMF(θ []float64) func(i int64) float64 {
return func(i int64) float64 {
return log(θ[i])
}
}
func NextChoice(θ []float64) int64 {
u := NextUniform()
i := 0
sum := θ[0]
for ; sum < u && i < len(θ)-1; i++ {
sum += θ[i+1]
}
if u >= sum {
return int64(len(θ))
}
return int64(i)
}
func Choice(θ []float64) func() int64 {
return func() int64 {
return NextChoice(θ)
}
}
func NextLogChoice(lws []float64) int64 {
return LogChoice(lws)()
}
func LogChoice(lws []float64) func() int64 {
max := lws[0]
for _, lw := range lws[1:len(lws)] {
if lw > max {
max = lw
}
}
ws := make([]float64, len(lws))
var sum float64
for i, lw := range lws {
ws[i] = math.Exp(lw - max)
sum += ws[i]
}
norm := 1 / sum
for i := range ws {
ws[i] *= norm
}
return Choice(ws)
}