/
floats.go
88 lines (76 loc) · 1.81 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
package experiment
import (
"gonum.org/v1/gonum/floats"
"gonum.org/v1/gonum/stat"
"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 math.NaN()
}
return floats.Min(x)
}
// Max returns the greatest value in the slice
func (x Floats) Max() float64 {
if len(x) == 0 {
return math.NaN()
}
return floats.Max(x)
}
// Sum returns the total of the values in the slice
func (x Floats) Sum() float64 {
return floats.Sum(x)
}
// Mean returns the average of the values in the slice
func (x Floats) Mean() float64 {
if len(x) == 0 {
return math.NaN()
}
return stat.Mean(x, nil)
}
// MeanVariance returns the sample mean and unbiased variance of the values in the slice
func (x Floats) MeanVariance() []float64 {
if len(x) == 0 {
return []float64{math.NaN(), math.NaN()}
}
m, v := stat.MeanVariance(x, nil)
return []float64{m, v}
}
// Median returns the middle value in the slice (50% quantile)
func (x Floats) Median() float64 {
if len(x) == 0 {
return math.NaN()
}
return stat.Quantile(0.5, stat.Empirical, x, nil)
}
// Q25 is the 25% quantile
func (x Floats) Q25() float64 {
if len(x) == 0 {
return math.NaN()
}
return stat.Quantile(0.25, stat.Empirical, x, nil)
}
// Q75 is the 75% quantile
func (x Floats) Q75() float64 {
if len(x) == 0 {
return math.NaN()
}
return stat.Quantile(0.75, stat.Empirical, x, nil)
}
// Variance returns the variance of the values in the slice
func (x Floats) Variance() float64 {
if len(x) == 0 {
return math.NaN()
}
return stat.Variance(x, nil)
}
// StdDev returns the standard deviation of the values in the slice
func (x Floats) StdDev() float64 {
if len(x) == 0 {
return math.NaN()
}
return stat.StdDev(x, nil)
}