forked from aunum/gold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
60 lines (53 loc) · 1.28 KB
/
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
package num
import (
"github.com/aunum/log"
"github.com/chewxy/math32"
)
// MinMaxNorm is min-max normalization.
func MinMaxNorm(x, min, max float32) float32 {
if x < min || x > max || min >= max {
log.Fatalf("parameters for min-max norm not valid")
}
return (x - min) / (max - min)
}
// MeanNorm is mean normalization.
func MeanNorm(x, min, max, average float32) float32 {
if x < min || x > max || min >= max {
log.Fatalf("parameters for mean norm not valid")
}
return (x - average) / (max - min)
}
// ZNorm uses z-score normalization.
func ZNorm(x, mean, stdDev float32) float32 {
return (x - mean) / stdDev
}
// Mean of the values.
func Mean(vals []float32) float32 {
n := float32(len(vals))
var sum float32
for _, val := range vals {
sum += val
}
return sum / n
}
// Variance is the average distance from the mean.
func Variance(vals []float32) float32 {
mu := Mean(vals)
var distance float32
for _, val := range vals {
diff := val - mu
distance += math32.Pow(diff, 2)
}
return distance / float32((len(vals) - 1))
}
// StdDev returns the standard deviation of x.
func StdDev(x []float32) float32 {
mu := Mean(x)
var distance float32
for _, z := range x {
diff := z - mu
p := math32.Pow(diff, 2)
distance += p
}
return math32.Sqrt(distance / float32(len(x)))
}