-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplestats_test.go
38 lines (31 loc) · 1 KB
/
simplestats_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
package main
import (
"math/rand"
"testing"
"time"
)
func TestCalculateStandardDeviationCorrectnessWithGoRandNormal(t *testing.T) {
numOfSamples := 100000
samples := make([]float64, numOfSamples)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < len(samples); i++ {
samples[i] = r.NormFloat64()
}
epsilon := 0.01
t.Run("Must match math/rand/Normal std dev of 1", func(t *testing.T) {
lowerTargetBound := 1 - epsilon
upperTargetBound := 1 + epsilon
standardDeviation := CalculateStandardDeviation(samples)
if standardDeviation < lowerTargetBound || standardDeviation > upperTargetBound {
t.Errorf("Standard deviation found: %v, expected 1 ± %v", standardDeviation, epsilon)
}
})
t.Run("Must match math/rand/Normal mean of 0", func(t *testing.T) {
lowerTargetBound := -epsilon
upperTargetBound := epsilon
mean := CalculateMean(samples)
if mean < lowerTargetBound || mean > upperTargetBound {
t.Error("Mean found: %v, expected 0 ± %v", mean, epsilon)
}
})
}