variance.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package stats
  2. import "math"
  3. // _variance finds the variance for both population and sample data
  4. func _variance(input Float64Data, sample int) (variance float64, err error) {
  5. if input.Len() == 0 {
  6. return math.NaN(), EmptyInput
  7. }
  8. // Sum the square of the mean subtracted from each number
  9. m, _ := Mean(input)
  10. for _, n := range input {
  11. variance += (float64(n) - m) * (float64(n) - m)
  12. }
  13. // When getting the mean of the squared differences
  14. // "sample" will allow us to know if it's a sample
  15. // or population and wether to subtract by one or not
  16. return variance / float64((input.Len() - (1 * sample))), nil
  17. }
  18. // Variance the amount of variation in the dataset
  19. func Variance(input Float64Data) (sdev float64, err error) {
  20. return PopulationVariance(input)
  21. }
  22. // PopulationVariance finds the amount of variance within a population
  23. func PopulationVariance(input Float64Data) (pvar float64, err error) {
  24. v, err := _variance(input, 0)
  25. if err != nil {
  26. return math.NaN(), err
  27. }
  28. return v, nil
  29. }
  30. // SampleVariance finds the amount of variance within a sample
  31. func SampleVariance(input Float64Data) (svar float64, err error) {
  32. v, err := _variance(input, 1)
  33. if err != nil {
  34. return math.NaN(), err
  35. }
  36. return v, nil
  37. }
  38. // Covariance is a measure of how much two sets of data change
  39. func Covariance(data1, data2 Float64Data) (float64, error) {
  40. l1 := data1.Len()
  41. l2 := data2.Len()
  42. if l1 == 0 || l2 == 0 {
  43. return math.NaN(), EmptyInput
  44. }
  45. if l1 != l2 {
  46. return math.NaN(), SizeErr
  47. }
  48. m1, _ := Mean(data1)
  49. m2, _ := Mean(data2)
  50. // Calculate sum of squares
  51. var ss float64
  52. for i := 0; i < l1; i++ {
  53. delta1 := (data1.Get(i) - m1)
  54. delta2 := (data2.Get(i) - m2)
  55. ss += (delta1*delta2 - ss) / float64(i+1)
  56. }
  57. return ss * float64(l1) / float64(l1-1), nil
  58. }
  59. // CovariancePopulation computes covariance for entire population between two variables.
  60. func CovariancePopulation(data1, data2 Float64Data) (float64, error) {
  61. l1 := data1.Len()
  62. l2 := data2.Len()
  63. if l1 == 0 || l2 == 0 {
  64. return math.NaN(), EmptyInput
  65. }
  66. if l1 != l2 {
  67. return math.NaN(), SizeErr
  68. }
  69. m1, _ := Mean(data1)
  70. m2, _ := Mean(data2)
  71. var s float64
  72. for i := 0; i < l1; i++ {
  73. delta1 := (data1.Get(i) - m1)
  74. delta2 := (data2.Get(i) - m2)
  75. s += delta1 * delta2
  76. }
  77. return s / float64(l1), nil
  78. }