Платформа ЦРНП "Мирокод" для разработки проектов
https://git.mirocod.ru
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
740 B
35 lines
740 B
package stats |
|
|
|
import "sort" |
|
|
|
// Average returns the average value |
|
func Average(values []float64) float64 { |
|
if len(values) == 0 { |
|
return 0 |
|
} |
|
|
|
var val float64 |
|
for _, point := range values { |
|
val += point |
|
} |
|
return val / float64(len(values)) |
|
} |
|
|
|
// Sum returns the sum of all the given values |
|
func Sum(values []float64) float64 { |
|
var val float64 |
|
for _, point := range values { |
|
val += point |
|
} |
|
return val |
|
} |
|
|
|
// Percentiles returns a map containing the asked for percentiles |
|
func Percentiles(values []float64, percentiles map[string]float64) map[string]float64 { |
|
sort.Float64s(values) |
|
results := map[string]float64{} |
|
for label, p := range percentiles { |
|
results[label] = values[int(float64(len(values))*p)] |
|
} |
|
return results |
|
}
|
|
|