-
Notifications
You must be signed in to change notification settings - Fork 2
/
aggregation.go
294 lines (259 loc) · 4.6 KB
/
aggregation.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package series
import (
"github.com/WinPooh32/series/math"
)
// AggregateFunc is applied aggregation function.
type AggregateFunc func(data Data) DType
// Mean returns mean of data's values.
func Mean(data Data) DType {
var (
count int
sum DType
items = data.Values()
)
for _, v := range items {
if IsNA(v) {
continue
}
sum += v
count++
}
if count == 0 {
return math.NaN()
}
return sum / DType(count)
}
// Sum returns sum of data's values.
func Sum(data Data) DType {
var (
sum DType
count int
items = data.Values()
)
for _, v := range items {
if IsNA(v) {
continue
}
sum += v
count++
}
if count == 0 {
return math.NaN()
}
return sum
}
// Min returns minimum value.
func Min(data Data) DType {
var (
min DType = maxFloat
count int
items = data.Values()
)
for _, v := range items {
if IsNA(v) {
continue
}
if v < min {
min = v
}
count++
}
if count == 0 {
return math.NaN()
}
return min
}
// Max returns maximum value.
func Max(data Data) DType {
var (
max DType = -maxFloat
count int
items = data.Values()
)
for _, v := range items {
if IsNA(v) {
continue
}
if v > max {
max = v
}
count++
}
if count == 0 {
return math.NaN()
}
return max
}
// Median returns median value of series.
// Linear interpolation is used for odd length.
func Median(data Data) DType {
values := data.Values()
if len(values) == 0 {
return math.NaN()
}
if len(values) == 1 {
return values[0]
}
if len(values)%2 == 0 {
i := len(values) / 2
return (values[i-1] + values[i]) / 2
}
return values[len(values)/2]
}
// Argmin returns offset of the smallest value of series data.
// If the minimum is achieved in multiple locations, the first row position is returned.
func Argmin(data Data) int {
var (
min DType = maxFloat
pos int = -1
items = data.Values()
)
for i, v := range items {
if IsNA(v) {
continue
}
if v < min {
min = v
pos = i
}
}
return pos
}
// Argmax returns offset of the biggest value of series data.
// If the maximum is achieved in multiple locations, the first row position is returned.
func Argmax(data Data) int {
var (
max DType = -maxFloat
pos int = -1
items = data.Values()
)
for i, v := range items {
if IsNA(v) {
continue
}
if v > max {
max = v
pos = i
}
}
return pos
}
// Variance returns variance of values.
// Ddof - Delta Degrees of Freedom. The divisor used in calculations is N - ddof,
// where N represents the number of elements.
func Variance(data Data, mean DType, ddof int) DType {
if data.Len() == 0 || IsNA(mean) {
return math.NaN()
}
if ddof < 0 || ddof >= data.Len() {
panic("ddof must be positive value and less than data length!")
}
var (
dev DType
count int
values = data.Values()
)
for _, v := range values {
if IsNA(v) {
continue
}
d := v - mean
dev += (d * d)
count++
}
if count-ddof < 0 {
return math.NaN()
}
return dev / DType(count-ddof)
}
// Std returns standard deviation.
// Ddof - Delta Degrees of Freedom. The divisor used in calculations is N - ddof,
// where N represents the number of elements.
func Std(data Data, mean DType, ddof int) DType {
return math.Sqrt(Variance(data, mean, ddof))
}
func First(data Data) DType {
values := data.Values()
for _, v := range values {
if !IsNA(v) {
return v
}
}
return math.NaN()
}
func Last(data Data) DType {
values := data.Values()
for i := len(values) - 1; i >= 0; i-- {
v := values[i]
if !IsNA(v) {
return v
}
}
return math.NaN()
}
func Skew(data Data) DType {
count := countNotNA(data)
mean := Sum(data) / count
m2 := sumAdjustedPow2(data, mean)
m3 := sumAdjustedPow3(data, mean)
// fix floating point error.
m2 = fpZero(m2, Eps)
m3 = fpZero(m3, Eps)
if m2 == 0 || m3 == 0 {
return 0
}
if count < 3 {
return math.NaN()
}
g1 := m3 / (math.Sqrt(m2) * m2)
G1 := ((count * math.Sqrt(count-1)) * g1) / (count - 2)
return G1
}
func countNotNA(data Data) DType {
count := 0
items := data.values
for _, v := range items {
if !IsNA(v) {
count++
}
}
return DType(count)
}
func sumAdjustedPow2(data Data, mean DType) DType {
var (
sum DType
count int
items = data.Values()
)
for _, v := range items {
if IsNA(v) {
continue
}
v -= mean
sum += v * v
count++
}
if count == 0 {
return math.NaN()
}
return sum
}
func sumAdjustedPow3(data Data, mean DType) DType {
var (
sum DType
count int
items = data.Values()
)
for _, v := range items {
if IsNA(v) {
continue
}
v -= mean
sum += v * v * v
count++
}
if count == 0 {
return math.NaN()
}
return sum
}