-
Notifications
You must be signed in to change notification settings - Fork 455
/
statistics.go
200 lines (172 loc) · 4.68 KB
/
statistics.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
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package stats
import (
"math"
)
// Values presents a set of data values as an array, for the purposes of aggregation
type Values interface {
// Len returns the number of values present
Len() int
// ValueAt returns the value at the nth element
ValueAt(n int) float64
}
// MutableValues is a set of data values that can be modified
type MutableValues interface {
Values
// SetValueAt sets the value at the nth element
SetValueAt(n int, v float64)
}
// Float64Values is a simple Values implementation around a slice
type Float64Values []float64
// Len returns the number of elements in the array
func (vals Float64Values) Len() int { return len(vals) }
// ValueAt returns the value at the nth element
func (vals Float64Values) ValueAt(n int) float64 { return vals[n] }
// SetValueAt sets the value at the nth element
func (vals Float64Values) SetValueAt(n int, v float64) { vals[n] = v }
// Statistics are the computation of standard statistics (min, max, mean, count, stddev)
// over a group of values.
type Statistics struct {
Min float64
Max float64
Mean float64
Count uint
Sum float64
StdDev float64
}
// Merge merges a group of statistics
func Merge(statistics []Statistics) Statistics {
var (
count uint
min, max, mean, sum float64
)
for _, a := range statistics {
if a.Count == 0 {
continue
}
if count == 0 {
min, max = a.Min, a.Max
} else {
min, max = math.Min(min, a.Min), math.Max(max, a.Max)
}
priorCount := count
count += a.Count
sum += a.Sum
mean = ((a.Mean * float64(a.Count)) + (mean * float64(priorCount))) / float64(count)
}
if count == 0 {
return Statistics{}
}
var sum1, sum2 float64
for _, a := range statistics {
if a.Count == 0 {
continue
}
variance := a.StdDev * a.StdDev
avg := a.Mean
sum1 += float64(a.Count) * variance
sum2 += float64(a.Count) * math.Pow(avg-mean, 2)
}
variance := ((sum1 + sum2) / float64(count))
return Statistics{
Count: count,
Min: min,
Max: max,
Mean: mean,
Sum: sum,
StdDev: math.Sqrt(variance),
}
}
func calc(values Values) (uint, float64, float64, float64, float64, float64) {
count := uint(0)
sum := float64(0)
min := math.MaxFloat64
max := -math.MaxFloat64
for i := 0; i < values.Len(); i++ {
n := values.ValueAt(i)
if math.IsNaN(n) {
continue
}
count++
sum += n
min = math.Min(n, min)
max = math.Max(n, max)
}
if count == 0 {
nan := math.NaN()
return 0, nan, nan, nan, nan, nan
}
mean := float64(0)
if count > 0 {
mean = sum / float64(count)
}
stddev := float64(0)
if count > 1 {
m2 := float64(0)
for i := 0; i < values.Len(); i++ {
n := values.ValueAt(i)
if math.IsNaN(n) {
continue
}
diff := n - mean
m2 += diff * diff
}
variance := m2 / float64(count-1)
stddev = math.Sqrt(variance)
}
return count, min, max, mean, sum, stddev
}
// Calc calculates statistics for a set of values
func Calc(values Values) Statistics {
count, min, max, mean, sum, stddev := calc(values)
return Statistics{
Count: count,
Min: min,
Max: max,
Mean: mean,
Sum: sum,
StdDev: stddev,
}
}
// SingleCountStatistics returns Statistics for a single value
func SingleCountStatistics(value float64) Statistics {
return Statistics{
Count: 1,
Min: value,
Max: value,
Sum: value,
Mean: value,
StdDev: 0,
}
}
// ZeroCountStatistics returns statistics when no values are present
// (or when all values are NaNs)
func ZeroCountStatistics() Statistics {
nan := math.NaN()
return Statistics{
Count: 0,
Min: nan,
Max: nan,
Sum: nan,
Mean: nan,
StdDev: nan,
}
}