-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboxplot.go
More file actions
67 lines (53 loc) · 2.02 KB
/
boxplot.go
File metadata and controls
67 lines (53 loc) · 2.02 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
package streamstats
import "fmt"
// BoxPlot represents a BoxPlot with interquartile range and whiskers backed by a P2-Quantile tracking the median, P=0.5
type BoxPlot struct {
P2Quantile
}
// NewBoxPlot returns a new BoxPlot
func NewBoxPlot() BoxPlot {
return BoxPlot{NewP2Quantile(0.5)}
}
// Median returns the estimated median
func (bp BoxPlot) Median() float64 {
return bp.Quantile()
}
// UpperQuartile returns the estimated upper quartile
func (bp BoxPlot) UpperQuartile() float64 {
return bp.UpperQuantile()
}
// LowerQuartile returns the estimated lower quartile
func (bp BoxPlot) LowerQuartile() float64 {
return bp.LowerQuantile()
}
// InterQuartileRange returns the estimated interquartile range
func (bp BoxPlot) InterQuartileRange() float64 {
return bp.UpperQuantile() - bp.LowerQuantile()
}
// UpperWhisker returns the estimated upper whisker, Q3 + 1.5 * IQR
func (bp BoxPlot) UpperWhisker() float64 {
return bp.UpperQuantile() + 1.5*bp.InterQuartileRange()
}
// LowerWhisker returns the estimated lower whisker, Q1 - 1.5 * IQR
func (bp BoxPlot) LowerWhisker() float64 {
return bp.LowerQuantile() - 1.5*bp.InterQuartileRange()
}
// IsOutlier returns true if the data is outside the whiskers
func (bp BoxPlot) IsOutlier(x float64) bool {
return x < bp.LowerWhisker() || x > bp.UpperWhisker()
}
// MidHinge returns the MidHinge of the data, average of upper and lower quantiles
func (bp BoxPlot) MidHinge() float64 {
return (bp.UpperQuartile() + bp.LowerQuartile()) / 2.0
}
// MidRange returns the MidRange of the data, average of max and min
func (bp BoxPlot) MidRange() float64 {
return (bp.Max() + bp.Min()) / 2.0
}
// TriMean returns the TriMean of the data, average of Median and MidHinge
func (bp BoxPlot) TriMean() float64 {
return (bp.UpperQuartile() + 2.0*bp.Median() + bp.LowerQuartile()) / 4.0
}
func (bp BoxPlot) String() string {
return fmt.Sprintf("Min: %0.3f LowerQuartile: %0.3f Median: %0.3f UpperQuartile: %0.3f Max: %0.3f N: %d", bp.Min(), bp.LowerQuartile(), bp.Median(), bp.UpperQuartile(), bp.Max(), bp.N())
}