calculate.go 4.7 KB

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