deviations.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package util
  2. import "math"
  3. // Max .
  4. func Max(vars []int64) (maxVar int64) {
  5. for _, i := range vars {
  6. if i > maxVar {
  7. maxVar = i
  8. }
  9. }
  10. return
  11. }
  12. // Expectation .
  13. func Expectation(randomVars []float64) float64 {
  14. if len(randomVars) == 0 {
  15. return 0
  16. }
  17. var sum float64
  18. for _, rv := range randomVars {
  19. sum += rv
  20. }
  21. return sum / float64(len(randomVars))
  22. }
  23. // StdDeviation .
  24. func StdDeviation(randomVars []float64) float64 {
  25. if len(randomVars) == 0 {
  26. return 0
  27. }
  28. return math.Sqrt(Deviation(randomVars))
  29. }
  30. // Deviation .
  31. func Deviation(randomVars []float64) float64 {
  32. if len(randomVars) == 0 {
  33. return 0
  34. }
  35. var total float64
  36. expec := Expectation(randomVars)
  37. for _, rv := range randomVars {
  38. total += math.Pow(rv-expec, 2.0)
  39. }
  40. return total / float64(len(randomVars))
  41. }
  42. // Normallization .
  43. func Normallization(randomVars []int64) []float64 {
  44. if len(randomVars) == 0 {
  45. return nil
  46. }
  47. maxVal := Max(randomVars)
  48. if maxVal == 0 || maxVal == 1 {
  49. return nil
  50. }
  51. res := make([]float64, 0, len(randomVars))
  52. for _, rv := range randomVars {
  53. res = append(res, math.Log10(float64(rv))/math.Log10(float64(maxVal)))
  54. }
  55. return res
  56. }