goext/lang/mathext/statistics.go

42 lines
566 B
Go

package mathext
func Sum(v []float64) float64 {
total := float64(0)
for _, v := range v {
total += v
}
return total
}
func Mean(v []float64) float64 {
return Sum(v) / float64(len(v))
}
func Median(v []float64) float64 {
if len(v)%2 == 1 {
return v[len(v)/2]
} else {
return (v[len(v)/2-1] + v[len(v)/2]) / float64(2)
}
}
func Min(v []float64) float64 {
r := v[0]
for _, val := range v {
if val < r {
r = val
}
}
return r
}
func Max(v []float64) float64 {
r := v[0]
for _, val := range v {
if val > r {
r = val
}
}
return r
}