deviation.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package stats
  2. import "math"
  3. // MedianAbsoluteDeviation finds the median of the absolute deviations from the dataset median
  4. func MedianAbsoluteDeviation(input Float64Data) (mad float64, err error) {
  5. return MedianAbsoluteDeviationPopulation(input)
  6. }
  7. // MedianAbsoluteDeviationPopulation finds the median of the absolute deviations from the population median
  8. func MedianAbsoluteDeviationPopulation(input Float64Data) (mad float64, err error) {
  9. if input.Len() == 0 {
  10. return math.NaN(), EmptyInput
  11. }
  12. i := copyslice(input)
  13. m, _ := Median(i)
  14. for key, value := range i {
  15. i[key] = math.Abs(value - m)
  16. }
  17. return Median(i)
  18. }
  19. // StandardDeviation the amount of variation in the dataset
  20. func StandardDeviation(input Float64Data) (sdev float64, err error) {
  21. return StandardDeviationPopulation(input)
  22. }
  23. // StandardDeviationPopulation finds the amount of variation from the population
  24. func StandardDeviationPopulation(input Float64Data) (sdev float64, err error) {
  25. if input.Len() == 0 {
  26. return math.NaN(), EmptyInput
  27. }
  28. // Get the population variance
  29. vp, _ := PopulationVariance(input)
  30. // Return the population standard deviation
  31. return math.Pow(vp, 0.5), nil
  32. }
  33. // StandardDeviationSample finds the amount of variation from a sample
  34. func StandardDeviationSample(input Float64Data) (sdev float64, err error) {
  35. if input.Len() == 0 {
  36. return math.NaN(), EmptyInput
  37. }
  38. // Get the sample variance
  39. vs, _ := SampleVariance(input)
  40. // Return the sample standard deviation
  41. return math.Pow(vs, 0.5), nil
  42. }