-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboxplot_test.go
More file actions
71 lines (67 loc) · 2.31 KB
/
boxplot_test.go
File metadata and controls
71 lines (67 loc) · 2.31 KB
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
package streamstats
import (
"fmt"
"math/rand"
"testing"
)
func TestBoxPlot(t *testing.T) {
rand.Seed(42) // for deterministic testing
N := 10000
bp := NewBoxPlot()
p := 0.5 // BoxPlots track the median
q := NewP2Quantile(p)
// add the same data to both and compare
for i := 0; i < N; i++ {
q.Add(exponentialTestData[i])
bp.Add(exponentialTestData[i])
}
if bp.Median() != q.Quantile() {
t.Errorf("Expected Median %v, got %v", q.Quantile(), bp.Median())
}
if bp.UpperQuartile() != q.UpperQuantile() {
t.Errorf("Expected UpperQuartile %v, got %v", q.UpperQuantile(), bp.UpperQuartile())
}
if bp.LowerQuartile() != q.LowerQuantile() {
t.Errorf("Expected LowerQuartile %v, got %v", q.LowerQuantile(), bp.LowerQuartile())
}
IQR := q.UpperQuantile() - q.LowerQuantile()
if bp.InterQuartileRange() != IQR {
t.Errorf("Expected InterQuartileRange %v, got %v", IQR, bp.InterQuartileRange())
}
upperWhisker := q.UpperQuantile() + 1.5*IQR
if bp.UpperWhisker() != upperWhisker {
t.Errorf("Expected upperWhisker %v, got %v", upperWhisker, bp.UpperWhisker())
}
lowerWhisker := q.LowerQuantile() - 1.5*IQR
if bp.LowerWhisker() != lowerWhisker {
t.Errorf("Expected InterQuartileRange %v, got %v", lowerWhisker, bp.LowerWhisker())
}
if bp.IsOutlier(q.Max()) == false {
t.Errorf("Expected Max %v > UpperWhisker %v to be an outlier", q.Max(), bp.UpperWhisker())
}
if bp.IsOutlier(-1000) == false { // exponential distribution is skewed so min falls within the whisker
t.Errorf("Expected Min %v < -1000 to be an outlier", q.Min())
}
midHinge := (q.UpperQuantile() + q.LowerQuantile()) / 2.0
if bp.MidHinge() != midHinge {
t.Errorf("Expected MidHinge %v, got %v", midHinge, bp.MidHinge())
}
midRange := (q.Max() + q.Min()) / 2.0
if bp.MidRange() != midRange {
t.Errorf("Expected MidRange %v, got %v", midRange, bp.MidRange())
}
triMean := (q.UpperQuantile() + 2.0*q.Quantile() + q.LowerQuantile()) / 4.0
if bp.TriMean() != triMean {
t.Errorf("Expected TriMean %v, got %v", triMean, bp.TriMean())
}
bp = NewBoxPlot()
bp.Add(0.0)
bp.Add(1.0)
bp.Add(2.0)
bp.Add(3.0)
bp.Add(4.0)
expectedString := fmt.Sprintf("Min: %0.3f LowerQuartile: %0.3f Median: %0.3f UpperQuartile: %0.3f Max: %0.3f N: %d", 0.0, 1.0, 2.0, 3.0, 4.0, 5)
if expectedString != bp.String() {
t.Errorf("Expected %s got %s", expectedString, bp)
}
}