-
Notifications
You must be signed in to change notification settings - Fork 2
/
stat.go
166 lines (142 loc) · 2.97 KB
/
stat.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
package gdsp
import (
"math"
"sort"
)
// Min returns the minimum value from a vector.
func Min(v Vector) float64 {
if len(v) == 0 {
return 0.0
}
minValue := math.MaxFloat64
for _, r := range v {
if r < minValue {
minValue = r
}
}
return minValue
}
// Max returns the maximum value from a vector.
func Max(v Vector) float64 {
if len(v) == 0 {
return 0.0
}
maxValue := -math.MaxFloat64
for _, r := range v {
if r > maxValue {
maxValue = r
}
}
return maxValue
}
// MinReal returns the minimum value from a slice of floats.
func MinReal(v VectorComplex) float64 {
if len(v) == 0 {
return 0.0
}
minValue := math.MaxFloat64
for _, c := range v {
if real(c) < minValue {
minValue = real(c)
}
}
return minValue
}
// MaxReal returns the maximum value from a slice of floats.
func MaxReal(v VectorComplex) float64 {
if len(v) == 0 {
return 0.0
}
maxValue := -math.MaxFloat64
for _, c := range v {
if real(c) > maxValue {
maxValue = real(c)
}
}
return maxValue
}
// MinImag returns the minimum value from a slice of floats.
func MinImag(v VectorComplex) float64 {
if len(v) == 0 {
return 0.0
}
minValue := math.MaxFloat64
for _, c := range v {
if imag(c) < minValue {
minValue = imag(c)
}
}
return minValue
}
// MaxImag returns the maximum value from a slice of floats.
func MaxImag(v VectorComplex) float64 {
if len(v) == 0 {
return 0.0
}
maxValue := -math.MaxFloat64
for _, c := range v {
if imag(c) > maxValue {
maxValue = imag(c)
}
}
return maxValue
}
// Mean returns the mean of the elements of v.
func Mean(v Vector) float64 {
return VESum(v) / float64(len(v))
}
// Median returns the median of the elements of v.
func Median(v Vector) float64 {
vc := v.Copy()
sort.Float64s(vc)
if len(v)%2 == 0 {
v1 := v[len(v)/2]
v2 := v[len(v)/2+1]
return (v1 + v2) / 2.0
}
return vc[(len(vc)-1)+1]
}
// StdDev returns the standard deviation of the elements of v.
func StdDev(v Vector) float64 {
s := 0.0
m := Mean(v)
for _, r := range v {
x := r - m
s += x * x
}
return math.Sqrt(s / float64(len(v)-1))
}
// Normalize normalizes the given vector.
func Normalize(v Vector) Vector {
nv := MakeVector(0.0, len(v))
s := StdDev(v)
m := Mean(v)
for i, r := range v {
nv[i] = (r - m) / s
}
return nv
}
// NormalizeStrict normalizes a vector using the max and min elements.
func NormalizeStrict(v Vector) (Vector, []float64) {
maxValue := Max(v)
minValue := Min(v)
difference := maxValue - minValue
nv := MakeVector(0.0, len(v))
for i, r := range v {
x := 0.0
if difference != 0.0 {
x = (r - minValue) / difference
}
nv[i] = x
}
return nv, []float64{minValue, maxValue}
}
// NormalizeStrictC normalizes a complex-valued vector.
func NormalizeStrictC(v VectorComplex) (VectorComplex, []float64) {
real := v.Real()
imag := v.Imag()
nReal, rLimits := NormalizeStrict(real)
nImag, iLimits := NormalizeStrict(imag)
vc := MakeVectorComplexFromSplit(nReal, nImag)
return vc, append(rLimits, iLimits...)
}