-
Notifications
You must be signed in to change notification settings - Fork 1
/
num.go
158 lines (136 loc) · 2.75 KB
/
num.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
package evaluation
import (
"math"
"sort"
)
var (
DefaultTrimmedMeanFraction = 0.2
DefaultDimiter = ";"
)
func TrimmedMeanInt64Slice(data []int64, frac float64) float64 {
return TrimmedMean(convertInt64SliceToFloat64Slice(data), frac)
}
func TrimmedMean(data []float64, frac float64) float64 {
// Sum returns the sum of the elements of the slice.
total := func(s []float64) float64 {
var sum float64
for _, val := range s {
sum += val
}
return sum
}
// Mean computes the weighted mean of the data set.
// sum_i {w_i * x_i} / sum_i {w_i}
// If weights is nil then all of the weights are 1. If weights is not nil, then
// len(x) must equal len(weights).
mean := func(x, weights []float64) float64 {
if weights == nil {
return total(x) / float64(len(x))
}
if len(x) != len(weights) {
panic("stat: slice length mismatch")
}
var (
sumValues float64
sumWeights float64
)
for i, w := range weights {
sumValues += w * x[i]
sumWeights += w
}
return sumValues / sumWeights
}
if frac == 0 {
frac = DefaultTrimmedMeanFraction
}
if len(data) == 0 {
return 0
}
if len(data) < 3 {
return mean(data, nil)
}
if len(data) == 3 {
sort.Float64s(data)
return data[1]
}
cnt := len(data)
sort.Float64s(data)
start := maxInt(0, floor(float64(cnt)*frac))
end := minInt(cnt-1, cnt-floor(float64(cnt)*frac))
// pp.Println("start = ", start, " end = ", end)
trimmed := data[start:end]
ret := mean(trimmed, nil)
return math.Round(ret*1000) / 1000
}
func convertInt64SliceToFloat64Slice(in []int64) []float64 {
ret := make([]float64, len(in))
for i, v := range in {
ret[i] = float64(v)
}
return ret
}
func floor(x float64) int {
return int(math.Floor(x))
}
func ceil(x float64) int {
return int(math.Ceil(x))
}
func maxInt(x, y int) int {
if x > y {
return x
}
return y
}
func minInt(x, y int) int {
if x > y {
return y
}
return x
}
func uint64Transpose(a [][]uint64) [][]uint64 {
m := len(a)
n := len(a[0])
aNew := make([][]uint64, n)
for i := 0; i < n; i++ {
aNew[i] = make([]uint64, m)
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
aNew[j][i] = a[i][j]
}
}
return aNew
}
func transpose0(a [][]float64) [][]float64 {
m := len(a)
n := len(a[0])
aNew := make([][]float64, n)
for i := 0; i < n; i++ {
aNew[i] = make([]float64, m)
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
aNew[j][i] = a[i][j]
}
}
return aNew
}
func transpose(a [][]float64) [][]float64 {
maxCols := len(a[0])
for _, r := range a {
maxCols = maxInt(maxCols, len(r))
}
r := make([][]float64, maxCols)
for x, _ := range r {
r[x] = make([]float64, len(a))
for ii := range r[x] {
r[x][ii] = -1
}
}
for y, s := range a {
for x, e := range s {
r[x][y] = e
}
}
return r
}