-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_utils.go
executable file
·200 lines (181 loc) · 5.88 KB
/
test_utils.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
package prob
import (
"testing"
"math"
"fmt"
"math/rand"
"time"
)
const (
numSamples = 1000000
defaultEpsilon = 0.01
)
type inOut struct {
in float64
out float64
}
// Epsilon is option. Will default to defaultEpsilon value.
type sampleValues struct {
mean float64
variance float64
epsilon float64
}
// Distribution test value struct.
type distributionTest struct {
dist Distribution
mean float64
variance float64
stdDev float64
relStdDev float64
skewness float64
kurtosis float64
pdf []inOut
cdf []inOut
}
// Run tests on distribtion examples.
func testValues(examples []distributionTest) error {
rand.Seed(time.Now().UTC().UnixNano())
for _, example := range examples {
// Test mean.
mean := example.dist.Mean()
if !floatsPicoEqual(mean, example.mean) {
if !checkInf(mean, example.mean) && !checkNaN(mean, example.mean) {
return fmt.Errorf("\nMean:\n Expected: %f\n Got: %f\n", example.mean, mean)
}
}
// Test variance.
variance := example.dist.Variance()
if !floatsPicoEqual(variance, example.variance) {
if !checkInf(variance, example.variance) && !checkNaN(variance, example.variance) {
return fmt.Errorf("\nVariance:\n Expected: %f\n Got: %f\n", example.variance, variance)
}
}
// Test standard deviation.
stdDev := example.dist.StdDev()
if !floatsPicoEqual(stdDev, example.stdDev) {
if !checkInf(stdDev, example.stdDev) && !checkNaN(stdDev, example.stdDev) {
return fmt.Errorf("\nStdDev:\n Expected: %f\n Got: %f\n", example.stdDev, stdDev)
}
}
// Test relative standard deviation.
relStdDev := example.dist.RelStdDev()
if !floatsPicoEqual(relStdDev, example.relStdDev) {
if !checkInf(relStdDev, example.relStdDev) && !checkNaN(relStdDev, example.relStdDev) {
return fmt.Errorf("\nRelStdDev:\n Expected: %f\n Got: %f\n", example.relStdDev, relStdDev)
}
}
// Test skewness.
skewness := example.dist.Skewness()
if !floatsPicoEqual(skewness, example.skewness) {
if !checkInf(skewness, example.skewness) && !checkNaN(skewness, example.skewness) {
return fmt.Errorf("\nSkewness:\n Expected: %f\n Got: %f\n", example.skewness, skewness)
}
}
// Test excess kurtosis.
kurtosis := example.dist.Kurtosis()
if !floatsPicoEqual(kurtosis, example.kurtosis) {
if !checkInf(kurtosis, example.kurtosis) && !checkNaN(kurtosis, example.kurtosis) {
return fmt.Errorf("\nKurtosis:\n Expected: %f\n Got: %f\n", example.kurtosis, kurtosis)
}
}
// Test pdf values.
for _, pdf := range example.pdf {
out := example.dist.Pdf(pdf.in)
if !floatsPicoEqual(out, pdf.out) {
return fmt.Errorf("\nPdf of %f:\n Expected: %f\n Got: %f\n", pdf.in, pdf.out, out)
}
}
// Test cdf values.
for _, cdf := range example.cdf {
out := example.dist.Cdf(cdf.in)
if !floatsPicoEqual(out, cdf.out) {
return fmt.Errorf("\nCdf of %f:\n Expected: %f\n Got: %f\n", cdf.in, cdf.out, out)
}
}
}
return nil
}
func testSamples(dist Distribution) error {
// Generate samples.
samples := Sample(dist, numSamples)
if len(samples) != numSamples {
return fmt.Errorf("\nCould not generate samples.")
}
// Test sample average against expected value if it exists.
sampleMean := averageFloats(samples)
actualMean := dist.Mean()
if !math.IsInf(actualMean,0) && !math.IsNaN(actualMean) {
if !floatsEqual(actualMean, sampleMean, defaultEpsilon) {
return fmt.Errorf("\nSample average:\n Expected: %f\n Got: %f\n", actualMean, sampleMean)
}
}
// Test sample variance against expected variance if it exists.
sampleVar := varianceFloats(samples, sampleMean)
actualVar := dist.Variance()
if !math.IsInf(actualVar,0) && !math.IsNaN(actualVar) {
if !floatsEqual(actualVar, sampleVar, defaultEpsilon * 10) {
return fmt.Errorf("\nSample variance:\n Expected: %f\n Got: %f\n", actualVar, sampleVar)
}
}
return nil
}
// floatsEqual determines if two values are within epsilon of each other.
func floatsEqual(f1, f2, epsilon float64) bool {
return math.Abs(f1-f2) < epsilon
}
// floatsIntegerEqual determines if two values are within 10^0 of each other.
func floatsIntegerEqual(f1, f2 float64) bool {
return math.Abs(f1-f2) < 1
}
// floatsDeciEqual determines if two values are within 10^-1 of each other.
func floatsDeciEqual(f1, f2 float64) bool {
return math.Abs(f1-f2) < 0.1
}
// floatsCentiEqual determines if two values are within 10^-2 of each other.
func floatsCentiEqual(f1, f2 float64) bool {
return math.Abs(f1-f2) < 0.01
}
// floatsMilliEqual determines if two values are within 10^-3 of each other.
func floatsMilliEqual(f1, f2 float64) bool {
return math.Abs(f1-f2) < 0.001
}
// floatsNanoEqual determines if two values are within 10^-9 of each other.
func floatsNanoEqual(f1, f2 float64) bool {
return math.Abs(f1-f2) < 0.000000001
}
// floatsPicoEqual determines if two values are within 10^-12 of each other.
func floatsPicoEqual(f1, f2 float64) bool {
return math.Abs(f1-f2) < 0.000000000001
}
func checkInf(f1, f2 float64) bool {
if math.IsInf(f1,0) || math.IsInf(f2,0) {
return math.IsInf(f1,0) && math.IsInf(f2,0)
}
return false
}
func checkNaN(f1, f2 float64) bool {
if math.IsNaN(f1) || math.IsNaN(f2) {
return math.IsNaN(f1) && math.IsNaN(f2)
}
return false
}
func averageFloats(values []float64) float64 {
var total float64
for _, value := range values {
total += value
}
return total / float64(len(values))
}
func varianceFloats(values []float64, mean float64) float64 {
var total, diff float64
for _, value := range values {
diff = value - mean
total += diff * diff
}
return total / (float64(len(values)) - 1)
}
func runBenchmark(b *testing.B, dist Distribution) {
for n := 0; n <= b.N; n++ {
dist.Random()
}
}