-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathstats.go
58 lines (44 loc) · 1.2 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
package stats
import (
"fmt"
"math"
"slices"
)
// Mean computes the arithmetic mean of the input array.
func Mean(input []float64) (float64, error) {
if len(input) < 1 {
return math.NaN(), fmt.Errorf("input array must have at least 1 element")
}
sum := 0.0
for _, x := range input {
sum += x
}
return sum / float64(len(input)), nil
}
// StdDev computes the population standard deviation of the input array.
func StdDev(input []float64) (float64, error) {
if len(input) < 1 {
return math.NaN(), fmt.Errorf("input array must have at least 1 element")
}
mean, _ := Mean(input)
sum := 0.0
for _, x := range input {
sum += (x - mean) * (x - mean)
}
return math.Sqrt(sum / float64(len(input))), nil
}
// Median computes the median value of the input array.
func Median(input []float64) (float64, error) {
if len(input) < 1 {
return math.NaN(), fmt.Errorf("input array must have at least 1 element")
}
numElements := len(input)
// sort a copy of the input array
inputCopy := make([]float64, numElements)
copy(inputCopy, input)
slices.Sort(inputCopy)
if numElements%2 == 1 {
return inputCopy[numElements/2], nil
}
return (inputCopy[numElements/2] + inputCopy[numElements/2-1]) / 2, nil
}