-
Notifications
You must be signed in to change notification settings - Fork 12
/
stat_npb.go
82 lines (70 loc) · 1.4 KB
/
stat_npb.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
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
package stat
import (
. "github.com/ematvey/go-fn/fn"
)
func CRP_PMF(α float64) func(x []int64) float64 {
return func(x []int64) float64 {
n := int64(len(x))
counts := make([]int64, int(α*log(float64(len(x)))))
sum := fZero
p := fOne
for i := iZero; i < n; i++ {
if x[i] >= int64(len(counts)) {
counts = copyInt64(counts, 2*int64(len(counts)))
}
if counts[x[i]] == 0 {
p *= α / (sum + α)
} else {
p *= float64(x[i]) / (sum + α)
}
counts[x[i]] += 1
sum += 1
}
return p
}
}
func CRP_LnPMF(α float64) func(x []int64) float64 {
return func(x []int64) float64 {
counts := make([]float64, len(x))
total := fZero
r := fZero
for _, c := range x {
if counts[c] == 0 {
r++
}
counts[c]++
total++
}
ll := r * log(α)
ll += LnΓ(α) - LnΓ(α+total)
for _, count := range counts {
if count != 0 {
ll += LnΓ(count)
}
}
return ll
}
}
/*
func CRP_LnPMF2(α float64) func(x []int64) float64 {
return func(x []int64) float64 {
n := int64(len(x));
counts := make([]int64, int(α*log(float64(len(x)))));
sum := fZero;
p := fZero;
for i:=iZero; i<n; i++ {
if x[i] >= int64(len(counts)) {
counts = copyInt64(counts, 2*int64(len(counts)))
}
if counts[x[i]] == 0 {
p += log(α)-log(sum+α);
} else {
p += log(float64(x[i]))-log(sum+α);
}
counts[x[i]] += 1;
sum += 1;
}
return p;
}
}
*/