-
Notifications
You must be signed in to change notification settings - Fork 6
/
binomial_test.go
67 lines (61 loc) · 1.99 KB
/
binomial_test.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
package main
import (
"github.com/stretchr/testify/assert"
"gonum.org/v1/gonum/stat/distuv"
"math/big"
"testing"
)
func TestRegularBinomial_Exp(t *testing.T) {
binomial := NewBinomial(5, 4, 5)
assert.Equal(t, 0, binomial.Exp(big.NewInt(4), 1).Cmp(big.NewInt(4)))
assert.Equal(t, 0, binomial.Exp(big.NewInt(4), 2).Cmp(big.NewInt(16)))
assert.Equal(t, 0, binomial.Exp(big.NewInt(4), 3).Cmp(big.NewInt(64)))
assert.Equal(t, 0, binomial.Exp(big.NewInt(5), 1).Cmp(big.NewInt(5)))
assert.Equal(t, 0, binomial.Exp(big.NewInt(5), 2).Cmp(big.NewInt(25)))
assert.Equal(t, 0, binomial.Exp(big.NewInt(5), 3).Cmp(big.NewInt(125)))
assert.Equal(t, 0, binomial.Exp(big.NewInt(2), 2).Cmp(big.NewInt(4)))
assert.Equal(t, 0, binomial.Exp(big.NewInt(2), 4).Cmp(big.NewInt(16)))
}
func TestBinomial_CDF(t *testing.T) {
binomial := NewBinomial(5, 1, 2)
b0 := big.NewRat(1, 32)
b1 := big.NewRat(6, 32)
b2 := big.NewRat(16, 32)
b3 := big.NewRat(26, 32)
b4 := big.NewRat(31, 32)
b5 := big.NewRat(32, 32)
assert.Equal(t, 0, binomial.CDF(0).Cmp(b0))
assert.Equal(t, 0, binomial.CDF(1).Cmp(b1))
assert.Equal(t, 0, binomial.CDF(2).Cmp(b2))
assert.Equal(t, 0, binomial.CDF(3).Cmp(b3))
assert.Equal(t, 0, binomial.CDF(4).Cmp(b4))
assert.Equal(t, 0, binomial.CDF(5).Cmp(b5))
binomial2 := NewBinomial(4, 1, 3)
c0 := big.NewRat(16, 81)
c1 := big.NewRat(48, 81)
c2 := big.NewRat(72, 81)
c3 := big.NewRat(80, 81)
c4 := big.NewRat(81, 81)
assert.Equal(t, 0, binomial2.CDF(0).Cmp(c0))
assert.Equal(t, 0, binomial2.CDF(1).Cmp(c1))
assert.Equal(t, 0, binomial2.CDF(2).Cmp(c2))
assert.Equal(t, 0, binomial2.CDF(3).Cmp(c3))
assert.Equal(t, 0, binomial2.CDF(4).Cmp(c4))
}
func BenchmarkBinomial_CDF(b *testing.B) {
b.ResetTimer()
binomial_1 := NewBinomial(1000, 1, 1000000)
for i := 0; i < b.N; i++ {
binomial_1.CDF(int64(i))
}
}
func BenchmarkBinomial_distuv(b *testing.B) {
b.ResetTimer()
binomial_2 := &distuv.Binomial{
N: 1000,
P: float64(1) / float64(100),
}
for i := 0; i < b.N; i++ {
binomial_2.CDF(float64(i))
}
}