-
Notifications
You must be signed in to change notification settings - Fork 22
/
stats.go
85 lines (56 loc) · 1.45 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
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
package indicators
import "math"
type mfloat []float64
// Avg returns 'data' average.
func Avg(data []float64) float64 {
return Sum(data) / float64(len(data))
}
// Sum returns the sum of all elements of 'data'.
func Sum(data []float64) float64 {
var sum float64
for _, x := range data {
sum += x
}
return sum
}
// Std returns standard deviation of a slice.
func Std(slice []float64) float64 {
var result []float64
mean := Avg(slice)
for i := 0; i < len(slice); i++ {
result = append(result, math.Pow(slice[i]-mean, 2))
}
return math.Sqrt(Sum(result) / float64(len(result)))
}
// AddToAll adds a value to all slice elements.
func (slice mfloat) AddToAll(val float64) []float64 {
var addedSlice []float64
for i := 0; i < len(slice); i++ {
addedSlice = append(addedSlice, slice[i] + val)
}
return addedSlice
}
// SubSlices subtracts two slices.
func SubSlices(slice1, slice2 []float64) []float64 {
var result []float64
for i := 0; i < len(slice1); i++ {
result = append(result, slice1[i]-slice2[i])
}
return result
}
// AddSlices adds two slices.
func AddSlices(slice1, slice2 []float64) []float64 {
var result []float64
for i := 0; i < len(slice1); i++ {
result = append(result, slice1[i]+slice2[i])
}
return result
}
// DivSlice divides a slice by a float.
func DivSlice(slice []float64, n float64) []float64 {
var result []float64
for i := 0; i < len(slice); i++ {
result = append(result, slice[i]/n)
}
return result
}