-
Notifications
You must be signed in to change notification settings - Fork 6
/
binomial.go
209 lines (182 loc) · 4.12 KB
/
binomial.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
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
package main
import (
"math"
"math/big"
"runtime"
"sync"
"sync/atomic"
)
var (
bigZero = big.NewInt(0)
bigOne = big.NewInt(1)
ratZero = big.NewRat(0, 1)
ratOne = big.NewRat(1, 1)
)
type Binomial interface {
CDF(int64) *big.Rat
Prob(int64) *big.Rat
}
// ApproxBinomial is an approximate distribution function of Binomial distribution.
// It can only be used when τ >>> W (when p = τ / W ).
type ApproxBinomial struct {
expected *big.Int
N uint64
probCache sync.Map //map[int64]*big.Rat
}
func NewApproxBinomial(expected int64, n uint64) *ApproxBinomial {
return &ApproxBinomial{
expected: big.NewInt(expected),
N: n,
}
}
func (ab *ApproxBinomial) CDF(k int64) *big.Rat {
if k < 0 {
return ratZero
}
if uint64(k) >= ab.N {
return ratOne
}
j := int64(0)
res := new(big.Rat).SetInt64(0)
for j <= k {
res.Add(res, ab.Prob(j))
j++
}
return res
}
func (ab *ApproxBinomial) Prob(k int64) *big.Rat {
if prob, exist := ab.probCache.Load(k); exist {
return prob.(*big.Rat)
}
numer := new(big.Int).Exp(ab.expected, big.NewInt(k), nil)
denom := new(big.Int).MulRange(1, k)
e := new(big.Rat).SetFloat64(math.Pow(math.E, -float64(ab.expected.Int64())))
lval := new(big.Rat).SetFrac(numer, denom)
prob := lval.Mul(lval, e)
ab.probCache.Store(k, prob)
return prob
}
// Binomial implements the binomial distribution function.
// It's too slow!!!
type RegularBinomial struct {
N *big.Int
pn *big.Int // numerator of P
pd *big.Int // denominator of P
pnExpCache sync.Map // map[power(int64)]*big.Int
tpnExpCache sync.Map // the same with above
probCache sync.Map // map[int64]*big.Rat
cdfCache sync.Map // map[int64]*big.Rat
resDenomCache atomic.Value
}
func NewBinomial(n, pn, pd int64) *RegularBinomial {
return &RegularBinomial{
N: big.NewInt(n),
pn: big.NewInt(pn),
pd: big.NewInt(pd),
}
}
// exp computes x**y with a optimize way.
func (b *RegularBinomial) Exp(x *big.Int, y float64) *big.Int {
if y == 0 {
return bigOne
}
if y == 1 {
return x
}
var (
cache *sync.Map
res *big.Int
)
// get the corresponding cache map
if x.Cmp(b.pn) == 0 {
cache = &b.pnExpCache
} else if x.Cmp(new(big.Int).Sub(b.pd, b.pn)) == 0 {
cache = &b.tpnExpCache
}
if cache != nil {
if exp, exist := cache.Load(y); exist {
return exp.(*big.Int)
}
}
if y == 2 {
res = new(big.Int).Mul(x, x)
} else {
ly := math.Floor(math.Sqrt(float64(y)))
ry := math.Floor(y - ly)
res = new(big.Int).Mul(b.Exp(x, ly), b.Exp(x, ry))
}
if cache != nil {
cache.Store(y, res)
}
return res
}
func (b *RegularBinomial) CDF(j int64) *big.Rat {
if j < 0 {
return new(big.Rat).SetInt64(0)
}
if j >= b.N.Int64() {
return new(big.Rat).SetInt64(1)
}
if cdf, exist := b.cdfCache.Load(j); exist {
return cdf.(*big.Rat)
}
runtime.GOMAXPROCS(runtime.NumCPU())
k := int64(j - 1)
var res = new(big.Rat).SetInt64(0)
for k >= 0 {
if cdf, exist := b.cdfCache.Load(k); exist {
res = new(big.Rat).Set(cdf.(*big.Rat))
break
}
k--
}
resChan := make(chan *big.Rat, j+1)
k++
begin := k
for k <= j {
go func(i int64) {
prob := b.Prob(i)
resChan <- prob
}(k)
k++
}
k = begin
for k <= j {
select {
case rat := <-resChan:
res.Add(res, rat)
k++
}
}
close(resChan)
b.cdfCache.Store(j, res)
return res
}
func (b *RegularBinomial) Prob(k int64) *big.Rat {
if prob, exist := b.probCache.Load(k); exist {
return prob.(*big.Rat)
}
// calculate p^k * p^(n-k)
N := b.N.Int64()
lnum := b.Exp(b.pn, float64(k))
rnum := b.Exp(new(big.Int).Sub(b.pd, b.pn), float64(N-k))
resNum := new(big.Int).Mul(lnum, rnum)
mulRat := new(big.Rat).SetFrac(resNum, b.resDenom())
// calculate C(n,k)
bino := new(big.Int).Binomial(N, k)
// prob = C(n,k) * p^k * p^(n-k)
prob := new(big.Rat).Mul(new(big.Rat).SetInt(bino), mulRat)
b.probCache.Store(k, prob)
return prob
}
func (b *RegularBinomial) Probability() *big.Rat {
return new(big.Rat).SetFrac(b.pn, b.pd)
}
func (b *RegularBinomial) resDenom() *big.Int {
if res := b.resDenomCache.Load(); res != nil {
return res.(*big.Int)
}
resDenom := b.Exp(b.pd, float64(b.N.Int64()))
b.resDenomCache.Store(resDenom)
return resDenom
}