-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
series_stats.go
113 lines (83 loc) · 1.91 KB
/
series_stats.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
// Copyright 2018-20 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package dataframe
import (
"context"
)
// Mean returns the mean. All non-nil values are ignored.
func (s *SeriesFloat64) Mean(ctx context.Context) (float64, error) {
sum, err := s.Sum(ctx)
if err != nil {
return 0, err
}
count := len(s.Values) - s.nilCount
if count == 0 {
return sum, nil
}
return sum / float64(count), nil
}
// Sum returns the sum of all non-nil values. If all values are nil, a NaN is returned.
// If opposing infinites are found, a NaN is also returned
func (s *SeriesFloat64) Sum(ctx context.Context) (float64, error) {
count := len(s.Values)
var posinfs int
var neginfs int
if count > 0 && count == s.nilCount {
// All values are nil
return nan(), nil
}
var sum float64
for _, v := range s.Values {
if err := ctx.Err(); err != nil {
return 0, err
}
if isNaN(v) {
continue
} else if isInf(v, 1) {
posinfs++
sum = sum + v
if neginfs > 0 {
return nan(), nil
}
} else if isInf(v, -1) {
neginfs++
sum = sum + v
if posinfs > 0 {
return nan(), nil
}
} else {
sum = sum + v
}
}
return float64(sum), nil
}
// Mean returns the mean. All non-nil values are ignored.
func (s *SeriesInt64) Mean(ctx context.Context) (float64, error) {
sum, err := s.Sum(ctx)
if err != nil {
return 0, err
}
count := len(s.values) - s.nilCount
if count == 0 {
return sum, nil
}
return sum / float64(count), nil
}
// Sum returns the sum of all non-nil values. If all values are nil, a
// NaN is returned.
func (s *SeriesInt64) Sum(ctx context.Context) (float64, error) {
count := len(s.values)
if count > 0 && count == s.nilCount {
// All values are nil
return nan(), nil
}
var sum int64
for _, v := range s.values {
if err := ctx.Err(); err != nil {
return 0, err
}
if v != nil {
sum = sum + *v
}
}
return float64(sum), nil
}