calculate.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package utils
  2. import (
  3. "github.com/gonum/stat"
  4. "github.com/shopspring/decimal"
  5. "math"
  6. )
  7. // Series is a container for a series of data
  8. type Series []Coordinate
  9. // Coordinate holds the data in a series
  10. type Coordinate struct {
  11. X, Y float64
  12. }
  13. // GetLinearResult 生成线性方程式
  14. func GetLinearResult(s []Coordinate) (gradient, intercept float64) {
  15. if len(s) <= 1 {
  16. return
  17. }
  18. // Placeholder for the math to be done
  19. var sum [5]float64
  20. // Loop over data keeping index in place
  21. i := 0
  22. for ; i < len(s); i++ {
  23. sum[0] += s[i].X
  24. sum[1] += s[i].Y
  25. sum[2] += s[i].X * s[i].X
  26. sum[3] += s[i].X * s[i].Y
  27. sum[4] += s[i].Y * s[i].Y
  28. }
  29. // Find gradient and intercept
  30. f := float64(i)
  31. gradient = (f*sum[3] - sum[0]*sum[1]) / (f*sum[2] - sum[0]*sum[0])
  32. intercept = (sum[1] / f) - (gradient * sum[0] / f)
  33. //fmt.Println("gradient:", gradient, ";intercept:", intercept)
  34. // Create the new regression series
  35. //for j := 0; j < len(s); j++ {
  36. // regressions = append(regressions, Coordinate{
  37. // X: s[j].X,
  38. // Y: s[j].X*gradient + intercept,
  39. // })
  40. //}
  41. return
  42. }
  43. // ComputeCorrelation 通过一组数据获取相关系数R
  44. // 计算步骤
  45. // 1.分别计算两个序列的平均值Mx和My
  46. // 2.分别计算两个序列的标准偏差SDx和SDy => √{1/(n-1)*SUM[(Xi-Mx)²]}
  47. // 3.计算相关系数 => SUM[(Xi-Mx)*(Yi-My)]/[(N-1)(SDx*SDy)]
  48. func ComputeCorrelation(sList []Coordinate) (r float64) {
  49. var xBar, yBar float64
  50. lenSList := len(sList)
  51. // 必须两组数据及两组以上的数据才能计算
  52. if lenSList < 2 {
  53. return
  54. }
  55. decimalX := decimal.NewFromFloat(0)
  56. decimalY := decimal.NewFromFloat(0)
  57. // 计算两组数据X、Y的平均值
  58. for _, coordinate := range sList {
  59. decimalX = decimalX.Add(decimal.NewFromFloat(coordinate.X))
  60. decimalY = decimalY.Add(decimal.NewFromFloat(coordinate.Y))
  61. }
  62. xBar, _ = decimalX.Div(decimal.NewFromInt(int64(lenSList))).Round(4).Float64()
  63. yBar, _ = decimalY.Div(decimal.NewFromInt(int64(lenSList))).Round(4).Float64()
  64. //fmt.Println(xBar)
  65. //fmt.Println(yBar)
  66. varXDeci := decimal.NewFromFloat(0)
  67. varYDeci := decimal.NewFromFloat(0)
  68. ssrDeci := decimal.NewFromFloat(0)
  69. for _, coordinate := range sList {
  70. // 分别计算X、Y的实际数据与平均值的差值
  71. diffXXbarDeci := decimal.NewFromFloat(coordinate.X).Sub(decimal.NewFromFloat(xBar))
  72. diffYYbarDeci := decimal.NewFromFloat(coordinate.Y).Sub(decimal.NewFromFloat(yBar))
  73. ssrDeci = ssrDeci.Add(diffXXbarDeci.Mul(diffYYbarDeci))
  74. //fmt.Println("i:", i, ";diffXXbar:", diffXXbarDeci.String(), ";diffYYbar:", diffYYbarDeci.String(), ";ssr:", ssrDeci.String())
  75. varXDeci = varXDeci.Add(diffXXbarDeci.Mul(diffXXbarDeci))
  76. varYDeci = varYDeci.Add(diffYYbarDeci.Mul(diffYYbarDeci))
  77. //varY += diffYYbar ** 2
  78. }
  79. //当输入的两个数组完全相同时,计算相关系数会导致除以零的操作,从而产生 NaN(Not a Number)的结果。为了避免这种情况,可以在计算相关系数之前先进行一个判断,如果两个数组的标准差为零,则相关系数应为1
  80. if varXDeci.IsZero() && varYDeci.IsZero() {
  81. r = 1
  82. return
  83. }
  84. sqrtVal, _ := varXDeci.Mul(varYDeci).Round(4).Float64()
  85. //fmt.Println("sqrtVal:", sqrtVal)
  86. sst := math.Sqrt(sqrtVal) // 平方根
  87. //fmt.Println("sst:", sst)
  88. // 如果计算出来的平方根是0,那么就直接返回,因为0不能作为除数
  89. if sst == 0 {
  90. return
  91. }
  92. r, _ = ssrDeci.Div(decimal.NewFromFloat(sst)).Round(4).Float64()
  93. return
  94. }
  95. // CalculationDecisive 通过一组数据获取决定系数R2
  96. func CalculationDecisive(sList []Coordinate) (r2 float64) {
  97. r := ComputeCorrelation(sList)
  98. r2, _ = decimal.NewFromFloat(r).Mul(decimal.NewFromFloat(r)).Round(4).Float64()
  99. return
  100. }
  101. // CalculateStandardDeviation 计算标准差
  102. func CalculateStandardDeviation(data []float64) float64 {
  103. return stat.StdDev(data, nil)
  104. }