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

  1. package stats
  2. import "sort"
  3. // Average returns the average value
  4. func Average(values []float64) float64 {
  5. if len(values) == 0 {
  6. return 0
  7. }
  8. var val float64
  9. for _, point := range values {
  10. val += point
  11. }
  12. return val / float64(len(values))
  13. }
  14. // Sum returns the sum of all the given values
  15. func Sum(values []float64) float64 {
  16. var val float64
  17. for _, point := range values {
  18. val += point
  19. }
  20. return val
  21. }
  22. // Percentiles returns a map containing the asked for percentiles
  23. func Percentiles(values []float64, percentiles map[string]float64) map[string]float64 {
  24. sort.Float64s(values)
  25. results := map[string]float64{}
  26. for label, p := range percentiles {
  27. results[label] = values[int(float64(len(values))*p)]
  28. }
  29. return results
  30. }