-
Notifications
You must be signed in to change notification settings - Fork 7
/
beta_test.go
191 lines (169 loc) · 5.05 KB
/
beta_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
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
package godist
import (
"fmt"
"math/rand"
"testing"
"time"
)
type betaExample struct {
in Beta
err error
out float64
}
func Test_Beta_Imp_Distribution(t *testing.T) {
var _ Distribution = Beta{}
}
func Test_Beta_Mean(t *testing.T) {
examples := []betaExample{
betaExample{in: Beta{Alpha: 1, Beta: 1}, out: 0.5},
betaExample{in: Beta{Alpha: 2, Beta: 1}, out: 0.6666666666666666},
betaExample{in: Beta{Alpha: 0.5, Beta: 10}, out: 0.047619047619047616},
betaExample{
in: Beta{Alpha: 2.0, Beta: 0},
err: fmt.Errorf("Invalid Beta Distribution: [α = 2, β = 0]"),
},
betaExample{
in: Beta{Alpha: 0, Beta: 2.0},
err: fmt.Errorf("Invalid Beta Distribution: [α = 0, β = 2]"),
},
}
for _, ex := range examples {
actual, err := ex.in.Mean()
if ex.err != nil && (err == nil || err.Error() != ex.err.Error()) {
t.Fatalf("expected %v\n got %v\n", ex.err, err)
}
if !floatsPicoEqual(actual, ex.out) {
t.Fatalf("expected %v\n got %v\n", ex.out, actual)
}
}
}
func Test_Beta_Median(t *testing.T) {
examples := []betaExample{
betaExample{in: Beta{Alpha: 1, Beta: 0.1}, out: 0.9990234375},
betaExample{in: Beta{Alpha: 0.1, Beta: 1}, out: 0.0009765625},
betaExample{in: Beta{Alpha: 1, Beta: 1}, out: 0.5},
betaExample{in: Beta{Alpha: 2, Beta: 2}, out: 0.5},
betaExample{in: Beta{Alpha: 3, Beta: 2}, out: 0.6142724318},
betaExample{in: Beta{Alpha: 2, Beta: 3}, out: 0.3857275681},
betaExample{in: Beta{Alpha: 20, Beta: 18}, out: 0.5267857142857143},
betaExample{
in: Beta{Alpha: 0, Beta: 0},
err: fmt.Errorf("Invalid Beta Distribution: [α = 0, β = 0]"),
},
betaExample{
in: Beta{Alpha: 0.1, Beta: 0.9},
err: fmt.Errorf("Median not supported for Beta Distribution [α = 0.1, β = 0.9]"),
},
}
for _, ex := range examples {
actual, err := ex.in.Median()
if ex.err != nil && (err == nil || err.Error() != ex.err.Error()) {
t.Fatalf("expected %v\n got %v\n", ex.err, err)
}
if !floatsPicoEqual(actual, ex.out) {
t.Fatalf("expected %v\n got %v\n", ex.out, actual)
}
}
}
func Test_Beta_Mode(t *testing.T) {
examples := []betaExample{
betaExample{in: Beta{Alpha: 2, Beta: 2}, out: 0.5},
betaExample{in: Beta{Alpha: 20, Beta: 20}, out: 0.5},
betaExample{in: Beta{Alpha: 200, Beta: 20}, out: 0.9128440366972477},
betaExample{
in: Beta{Alpha: 0, Beta: 0},
err: fmt.Errorf("Invalid Beta Distribution: [α = 0, β = 0]"),
},
betaExample{
in: Beta{Alpha: 1, Beta: 2},
err: fmt.Errorf("Mode not supported for Beta Distribution [α = 1, β = 2]"),
},
}
for _, ex := range examples {
actual, err := ex.in.Mode()
if ex.err != nil && (err == nil || err.Error() != ex.err.Error()) {
t.Fatalf("expected %v\n got %v\n", ex.err, err)
}
if !floatsPicoEqual(actual, ex.out) {
t.Fatalf("expected %v\n got %v\n", ex.out, actual)
}
}
}
func Test_Beta_Variance(t *testing.T) {
examples := []betaExample{
betaExample{in: Beta{Alpha: 1, Beta: 0.1}, out: 0.03935458480913026},
betaExample{in: Beta{Alpha: 0.1, Beta: 1}, out: 0.03935458480913026},
betaExample{in: Beta{Alpha: 1, Beta: 1}, out: 0.08333333333333333},
betaExample{in: Beta{Alpha: 20, Beta: 4}, out: 0.005555555555555556},
betaExample{
in: Beta{Alpha: 0, Beta: 0},
err: fmt.Errorf("Invalid Beta Distribution: [α = 0, β = 0]"),
},
}
for _, ex := range examples {
actual, err := ex.in.Variance()
if ex.err != nil && (err == nil || err.Error() != ex.err.Error()) {
t.Fatalf("expected %v\n got %v\n", ex.err, err)
}
if !floatsPicoEqual(actual, ex.out) {
t.Fatalf("expected %v\n got %v\n", ex.out, actual)
}
}
}
// tests random variate generation for values using Jöhnk's algorithm
func Test_Beta_Float64(t *testing.T) {
inputs := []Beta{
// use Jöhnk
Beta{Alpha: 0.45, Beta: 0.45},
Beta{Alpha: 0.15, Beta: 0.15},
// use Cheng BB
Beta{Alpha: 10, Beta: 3},
Beta{Alpha: 100, Beta: 30},
Beta{Alpha: 1000, Beta: 300},
Beta{Alpha: 10, Beta: 300.25},
Beta{Alpha: 200, Beta: 3500},
// use Cheng BC
Beta{Alpha: 10, Beta: 1.0},
Beta{Alpha: 1.0, Beta: 12.0},
Beta{Alpha: 0.6, Beta: 0.6},
Beta{Alpha: 0.75, Beta: 0.75},
}
for _, b := range inputs {
actual := genBetaDist(b, 10001)
expMean, _ := b.Mean()
if !floatsCentiEqual(actual.mean, expMean) {
msg := "[Mean] expected %v got %v for %#v\n"
t.Fatalf(msg, expMean, actual.mean, b)
}
expMed, _ := b.Median()
if !floatsDeciEqual(actual.median, expMed) {
msg := "[Median] expected %v got %v for %#v\n"
t.Fatalf(msg, expMed, actual.median, b)
}
expVar, _ := b.Variance()
if !floatsDeciEqual(actual.variance, expVar) {
msg := "[Variance] expected %v got %v for %#v\n"
t.Fatalf(msg, expVar, actual.variance, b)
}
}
}
type dist struct {
mean float64
median float64
variance float64
}
func genBetaDist(b Beta, size int) dist {
rand.Seed(int64(time.Now().Nanosecond()))
var sample []float64
for i := 0; i < size; i++ {
v, _ := b.Float64()
sample = append(sample, v)
}
d := dist{}
ed := Empirical{}
ed.Add(sample...)
d.mean, _ = ed.Mean()
d.median, _ = ed.Median()
d.variance, _ = ed.Variance()
return d
}