-
Notifications
You must be signed in to change notification settings - Fork 12
/
negbinom.go
63 lines (53 loc) · 1.45 KB
/
negbinom.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
// Negative Binomial distribution
package stat
import (
"math"
. "github.com/ematvey/go-fn/fn"
)
/*
// Does not pass the test, so commented out
func NegativeBinomial_PMF(ρ float64, r int64) func(i int64) float64 {
return func(k int64) float64 {
return float64(Choose(k+r-1, r-1)) * pow(ρ, float64(r)) * pow(1-ρ, float64(k))
}
}
*/
func NegativeBinomial_PMF(ρ float64, r int64) func(k int64) float64 {
return func(k int64) float64 {
return BinomCoeff(k+r-1, k) * math.Pow(1-ρ, float64(r)) * math.Pow(ρ, float64(k))
}
}
func NegativeBinomial_PMF_At(ρ float64, r, k int64) float64 {
pmf := NegativeBinomial_PMF(ρ, r)
return pmf(k)
}
func NegativeBinomial_LnPMF(ρ float64, r int64) func(i int64) float64 {
return func(k int64) float64 {
return LnChoose(k+r-1, r-1) + log(ρ)*float64(r) + log(1-ρ)*float64(k)
}
}
//NegativeBinomial(ρ, r) => number of NextBernoulli(ρ) failures before r successes
func NextNegativeBinomial(ρ float64, r int64) int64 {
k := iZero
for r >= 0 {
i := NextBernoulli(ρ)
r -= i
k += (1 - i)
}
return k
}
func NegativeBinomial(ρ float64, r int64) func() int64 {
return func() int64 {
return NextNegativeBinomial(ρ, r)
}
}
func NegativeBinomial_CDF(ρ float64, r int64) func(k int64) float64 {
return func(k int64) float64 {
Ip := Beta_CDF_At(float64(k+1), float64(r), ρ)
return 1 - Ip
}
}
func NegativeBinomial_CDF_At(ρ float64, r, k int64) float64 {
cdf := NegativeBinomial_CDF(ρ, r)
return cdf(k)
}