forked from vdobler/chart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat.go
executable file
·159 lines (142 loc) · 2.62 KB
/
stat.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
package chart
import (
"math"
"sort"
)
// Return p percentil of pre-sorted integer data. 0 <= p <= 100.
func PercentilInt(data []int, p int) int {
n := len(data)
if n == 0 {
return 0
}
if n == 1 {
return data[0]
}
pos := float64(p) * float64(n+1) / 100
fpos := math.Floor(pos)
intPos := int(fpos)
dif := pos - fpos
if intPos < 1 {
return data[0]
}
if intPos >= n {
return data[n-1]
}
lower := data[intPos-1]
upper := data[intPos]
val := float64(lower) + dif*float64(upper-lower)
return int(math.Floor(val + 0.5))
}
// Return p percentil of pre-sorted float64 data. 0 <= p <= 100.
func percentilFloat64(data []float64, p int) float64 {
n := len(data)
if n == 0 {
return 0
}
if n == 1 {
return data[0]
}
pos := float64(p) * float64(n+1) / 100
fpos := math.Floor(pos)
intPos := int(fpos)
dif := pos - fpos
if intPos < 1 {
return data[0]
}
if intPos >= n {
return data[n-1]
}
lower := data[intPos-1]
upper := data[intPos]
val := lower + dif*(upper-lower)
return val
}
// Compute minimum, p percentil, median, average, 100-p percentil and maximum of values in data.
func SixvalInt(data []int, p int) (min, lq, med, avg, uq, max int) {
min, max = math.MaxInt32, math.MinInt32
sum, n := 0, len(data)
if n == 0 {
return
}
if n == 1 {
min = data[0]
lq = data[0]
med = data[0]
avg = data[0]
uq = data[0]
max = data[0]
return
}
for _, v := range data {
if v < min {
min = v
}
if v > max {
max = v
}
sum += v
}
avg = sum / n
sort.Ints(data)
if n%2 == 1 {
med = data[(n-1)/2]
} else {
med = (data[n/2] + data[n/2-1]) / 2
}
lq = PercentilInt(data, p)
uq = PercentilInt(data, 100-p)
return
}
// Compute minimum, p percentil, median, average, 100-p percentil and maximum of values in data.
func SixvalFloat64(data []float64, p int) (min, lq, med, avg, uq, max float64) {
n := len(data)
// Special cases 0 and 1
if n == 0 {
return
}
if n == 1 {
min = data[0]
lq = data[0]
med = data[0]
avg = data[0]
uq = data[0]
max = data[0]
return
}
// First pass (min, max, coarse average)
var sum float64
min, max = math.MaxFloat64, -math.MaxFloat64
for _, v := range data {
if v < min {
min = v
}
if v > max {
max = v
}
sum += v
}
avg = sum / float64(n)
// Second pass: Correct average
var corr float64
for _, v := range data {
corr += v - avg
}
avg += corr / float64(n)
// Median
sort.Float64s(data)
if n%2 == 1 {
med = data[(n-1)/2]
} else {
med = (data[n/2] + data[n/2-1]) / 2
}
// Percentiles
if p < 0 {
p = 0
}
if p > 100 {
p = 100
}
lq = percentilFloat64(data, p)
uq = percentilFloat64(data, 100-p)
return
}