-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseries.go
423 lines (372 loc) · 11.2 KB
/
series.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright (c) 2016, Ben Morgan. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
// Package stat provides statistic functions and types.
package stat
import (
"bufio"
"bytes"
"math"
"os"
"sort"
"strconv"
)
// The Series type is a slice of float64 values.
type Series []float64
func (s *Series) Reset() { *s = make(Series, 0) }
func (s *Series) Append(f ...float64) { *s = append(*s, f...) }
func (s *Series) Append1(f float64) { *s = append(*s, f) }
// Copy returns a copy of the series s.
func (s Series) Copy() Series {
t := make(Series, len(s))
copy(t, s)
return t
}
// WriteFile writes the series to a file, where each number is on its own line.
func (s Series) WriteFile(path string) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
buf := bufio.NewWriter(f)
defer buf.Flush()
for _, f := range s {
if _, err := buf.WriteString(strconv.FormatFloat(f, 'f', -1, 64)); err != nil {
return err
}
if _, err := buf.WriteRune('\n'); err != nil {
return err
}
}
return err
}
// String returns the string representation of a series.
//
// Example:
//
// // Prints: [1 2 3 4 5]
// fmt.Println(Series{1, 2, 3, 4, 5})
//
func (s Series) String() string {
if len(s) == 0 {
return "[]"
}
var buf bytes.Buffer
buf.WriteRune('[')
buf.WriteString(strconv.FormatFloat(s[0], 'f', -1, 64))
for _, x := range s[1:] {
buf.WriteRune(' ')
buf.WriteString(strconv.FormatFloat(x, 'f', -1, 64))
}
buf.WriteRune(']')
return buf.String()
}
// The following methods are provided for ease-of-use.
// The documentation can be found with the functions of same name.
func (s Series) Len() int { return len(s) }
func (s Series) Head(n int) Series { return Head(s, n) }
func (s Series) Tail(n int) Series { return Tail(s, n) }
func (s Series) Max() float64 { return Max(s) }
func (s Series) Min() float64 { return Min(s) }
func (s Series) Mean() float64 { return Mean(s) }
func (s Series) Median() float64 { return Median(s) }
func (s Series) Var() float64 { return Var(s) }
func (s Series) VarP() float64 { return VarP(s) }
func (s Series) Std() float64 { return Std(s) }
func (s Series) StdP() float64 { return StdP(s) }
func (s Series) Skew() float64 { return Skew(s) }
func (s Series) SkewP() float64 { return SkewP(s) }
func (s Series) Autocov(lag int) float64 { return Autocov(s, lag) }
func (s Series) Autocor(lag int) float64 { return Autocor(s, lag) }
func (s Series) Cov(t Series) float64 { return Cov(s, t) }
func (s Series) CovP(t Series) float64 { return CovP(s, t) }
func (s Series) Cor(t Series) float64 { return Cor(s, t) }
func (s Series) Map(f func(float64) float64) Series { return Map(s, f) }
func (s Series) Add1(f float64) Series { return Add1(s, f) }
func (s Series) Mul1(f float64) Series { return Mul1(s, f) }
func (s Series) Sub1(f float64) Series { return Sub1(s, f) }
func (s Series) Div1(f float64) Series { return Div1(s, f) }
func (s Series) Add(t Series) Series { return Add(s, t) }
func (s Series) Mul(t Series) Series { return Mul(s, t) }
func (s Series) Sub(t Series) Series { return Sub(s, t) }
func (s Series) Div(t Series) Series { return Div(s, t) }
// Head returns the first n values from s.
//
// If n > len(s), an out-of-bounds panic will occur.
// The returned slice is a slice from s, not a new series.
func Head(s Series, n int) Series {
return s[:n]
}
// Tail returns the last n values from s.
//
// If n > len(s), an out-of-bounds panic will occur.
// The returned slice is a slice from s, not a new series.
func Tail(s Series, n int) Series {
return s[len(s)-n:]
}
// Max returns the maximum value in the series, or -∞ if the series is empty.
func Max(s Series) float64 {
if len(s) == 0 {
return math.Inf(-1)
}
m := s[0]
for _, x := range s[1:] {
if m < x {
m = x
}
}
return m
}
// Min returns the minimum value in the series, or +∞ if the series is empty.
func Min(s Series) float64 {
if len(s) == 0 {
return math.Inf(1)
}
m := s[0]
for _, x := range s[1:] {
if m > x {
m = x
}
}
return m
}
// Mean returns the empirical mean of the series s.
//
// The mean calculated here is the running mean, which ensures that
// an answer is given regardless of how long s is. The accuracy of
// the answer suffers however.
//
// If s is empty, NaN is returned.
func Mean(s Series) float64 {
if len(s) == 0 {
return math.NaN()
}
var m float64
for i, x := range s {
m += (x - m) / float64(i+1)
}
return m
}
// Median returns the median of the series s.
//
// If s has an even number of elements, the mean of the two middle
// elements is returned.
//
// If s is empty or has only one element, NaN is returned.
//
// Calculating the median requires sorting a copy of the series.
func Median(s Series) float64 {
if len(s) <= 1 {
return math.NaN()
}
n := len(s)
t := s.Copy()
sort.Float64s(t)
if n%2 == 0 {
return (t[n/2] + t[n/2-1]) / 2.0
}
return t[n/2]
}
// Var returns the sample variance of the series.
//
// If s is empty or has only one element, one cannot speak of variance,
// and NaN is returned.
func Var(s Series) float64 {
return variance(s, true)
}
// VarP returns the population variance of the series.
//
// If s is empty or has only one element, one cannot speak of variance,
// and NaN is returned.
func VarP(s Series) float64 {
return variance(s, false)
}
func variance(xs Series, sample bool) float64 {
if len(xs) <= 1 {
return math.NaN()
}
var m, s float64
for i, x := range xs {
mn := m + (x-m)/float64(i+1)
s += (x - m) * (x - mn)
m = mn
}
if sample {
return s / float64(len(xs)-1)
}
return s / float64(len(xs))
}
// Std returns the sample standard deviation of the series.
//
// If s is empty or has only one element, one cannot speak of variance,
// and NaN is returned.
func Std(s Series) float64 {
return math.Sqrt(Var(s))
}
// StdP returns the population standard deviation of the series.
//
// If s is empty or has only one element, one cannot speak of variance,
// and NaN is returned.
func StdP(s Series) float64 {
return math.Sqrt(VarP(s))
}
// Skew returns the sample skew of the series.
//
// NOTE: Not implemented yet.
func Skew(s Series) float64 {
panic("not implemented")
}
// SkewP returns the population skew of the series.
//
// NOTE: Not implemented yet.
func SkewP(s Series) float64 {
panic("not implemented")
}
// Cov returns the sample covariance of two series s and t.
//
// If the series do not have the same lengths, this function panics.
// If s is empty or has only one element, one cannot speak of variance,
// and NaN is returned.
func Cov(s, t Series) float64 {
n := len(s)
if n <= 1 {
return math.NaN()
}
u := Mul(Sub1(s, Mean(s)), Sub1(t, Mean(t)))
return Mean(u) * float64(n) / float64(n-1)
}
// CovP returns the population covariance of two series s and t.
//
// If the series do not have the same lengths, this function panics.
// If s is empty or has only one element, one cannot speak of variance,
// and NaN is returned.
func CovP(s, t Series) float64 {
n := len(s)
if n <= 1 {
return math.NaN()
}
return Mean(Mul(s, t)) - Mean(s)*Mean(t)
}
// Cor returns the sample correlation of two series s and t.
//
// If the series do not have the same lengths, this function panics.
// If s is empty or has only one element, one cannot speak of variance,
// and NaN is returned.
//
// NOTE: This is the same as the population correlation of two series,
// hence there is no CorP.
func Cor(s, t Series) float64 {
return Cov(s, t) / math.Sqrt(Var(s)*Var(t))
}
// Autocov returns the sample covariance of s with itself lag values later.
// The series s must be at least 2 longer than lag, else NaN is returned.
func Autocov(s Series, lag int) float64 {
n := len(s)
if lag > n-2 {
return math.NaN()
}
return Cov(s[:n-lag], s[lag:])
}
// Autocor returns the sample correlation of s with itself lag values later.
// The series s must be at least 2 longer than lag, else NaN is returned.
func Autocor(s Series, lag int) float64 {
n := len(s)
if lag > n-2 {
return math.NaN()
}
return Cor(s[:n-lag], s[lag:])
}
// Add1 adds f to each value in s and returns a new series.
func Add1(s Series, f float64) Series {
return Map(s, func(a float64) float64 { return a + f })
}
// Mul1 multiplies f to each value in s and returns a new series.
func Mul1(s Series, f float64) Series {
return Map(s, func(a float64) float64 { return a * f })
}
// Sub1 subtracts f from each value in s and returns a new series.
func Sub1(s Series, f float64) Series {
return Map(s, func(a float64) float64 { return a - f })
}
// Div1 divides f from each value in s and returns a new series.
func Div1(s Series, f float64) Series {
return Map(s, func(a float64) float64 { return a / f })
}
// Add returns the components of s and t added to each other.
func Add(s, t Series) Series {
return Map2(s, t, func(a, b float64) float64 { return a + b })
}
// Mul returns the components of s and t multiplied to each other.
func Mul(s, t Series) Series {
return Map2(s, t, func(a, b float64) float64 { return a * b })
}
// Sub returns the components of s subtracted by those of t.
func Sub(s, t Series) Series {
return Map2(s, t, func(a, b float64) float64 { return a - b })
}
// Div returns the components of s divided by those of t.
func Div(s, t Series) Series {
return Map2(s, t, func(a, b float64) float64 { return a / b })
}
// Resize returns s resized to have length n.
//
// If s is empty, the function panics.
// If n is less than the size of s, the first n elements of s is returned.
// If n is greater than the size of s, s is appended to s as often as necessary:
//
// Example:
//
// Resize([1 2 3], 5) -> [1 2 3 1 2]
// Resize([1 2 3 4 5], 3) -> [1 2 3]
//
func Resize(s Series, n int) Series {
m := len(s)
if n <= m {
return s[:n].Copy()
}
t := make(Series, n)
for i := 0; i < n; i++ {
t[i] = s[i%m]
}
return t
}
// Fold a series into a single value by repeatedly applying a = f(a, x).
//
// For example, to find the maximum value:
//
// Fold(s, math.Inf(-1), math.Max)
//
func Fold(s Series, a float64, f func(float64, float64) float64) float64 {
for _, x := range s {
a = f(a, x)
}
return a
}
// Apply modifies the series by replacing each value v with f(v).
func Apply(s Series, f func(float64) float64) {
for i, x := range s {
s[i] = f(x)
}
}
// Map creates a new series by applying f to each value in s.
func Map(s Series, f func(float64) float64) Series {
t := make(Series, len(s))
for i, x := range s {
t[i] = f(x)
}
return t
}
// Map creates a new series by applying f to each value in s and t.
//
// If the series lengths are not the same, the function panics.
func Map2(s, t Series, f func(a, b float64) float64) Series {
if len(s) != len(t) {
panic("series lengths must be the same")
}
u := make(Series, len(s))
for i, x := range s {
u[i] = f(x, t[i])
}
return u
}