forked from yaricom/goNEAT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
floats.go
98 lines (87 loc) · 1.59 KB
/
floats.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
package experiments
import (
"sort"
"math"
)
// Floats provides descriptive statistics on a slice of float64 values
type Floats []float64
// Min returns the smallest value in the slice
func (x Floats) Min() float64 {
if len(x) == 0 {
return 0.0
}
sort.Float64s(x)
return x[0]
}
// Max returns the greatest value in the slice
func (x Floats) Max() float64 {
if len(x) == 0 {
return 0.0
}
sort.Float64s(x)
return x[len(x)-1]
}
// Sum returns the total of the values in the slice
func (x Floats) Sum() float64 {
s := 0.0
for _, v := range x {
s += v
}
return s
}
// Mean returns the average of the values in the slice
func (x Floats) Mean() float64 {
if len(x) == 0 {
return 0.0
}
return x.Sum() / float64(len(x))
}
// Median returns the middle value in the slice
func (x Floats) Median() float64 {
sort.Float64s(x)
n := len(x)
switch {
case n == 0:
return 0.0
case n%2 == 0:
return (x[n/2-1] + x[n/2]) / 2.0
default:
return x[n/2]
}
}
func (x Floats) Q25() float64 {
if len(x) == 0 {
return 0.0
}
sort.Float64s(x)
n := len(x) / 4
return x[n]
}
func (x Floats) Q75() float64 {
if len(x) == 0 {
return 0.0
}
sort.Float64s(x)
n := len(x) * 3 / 4
return x[n]
}
// Variance returns the variance of the values in the slice
func (x Floats) Variance() float64 {
if len(x) == 0 {
return 0.0
}
m := x.Mean()
s := 0.0
for _, v := range x {
s += (v - m) * (v - m)
}
return s
}
// Stdev returns the standard deviation of the values in the slice
func (x Floats) Stdev() float64 {
if len(x) == 0 {
return 0.0
}
v := x.Variance()
return math.Sqrt(v / float64(len(x)))
}