calculate.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // CalculateCorrelationByIntArr 相关性计算
  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 CalculateCorrelationByIntArr(xArr, yArr []float64) (ratio float64) {
  49. // 序列元素数要一致
  50. xLen := float64(len(xArr))
  51. yLen := float64(len(yArr))
  52. if xLen == 0 || xLen != yLen {
  53. return
  54. }
  55. // 计算Mx和My
  56. var Xa, Ya float64
  57. for i := range xArr {
  58. Xa += xArr[i]
  59. }
  60. Mx := Xa / xLen
  61. for i := range yArr {
  62. Ya += yArr[i]
  63. }
  64. My := Ya / yLen
  65. // 计算标准偏差SDx和SDy
  66. var Xb, Yb, SDx, SDy float64
  67. for i := range xArr {
  68. Xb += (xArr[i] - Mx) * (xArr[i] - Mx)
  69. }
  70. SDx = math.Sqrt(1 / (xLen - 1) * Xb)
  71. for i := range yArr {
  72. Yb += (yArr[i] - My) * (yArr[i] - My)
  73. }
  74. SDy = math.Sqrt(1 / (yLen - 1) * Yb)
  75. // 计算相关系数
  76. var Nume, Deno float64
  77. for i := 0; i < int(xLen); i++ {
  78. Nume += (xArr[i] - Mx) * (yArr[i] - My)
  79. }
  80. Deno = (xLen - 1) * (SDx * SDy)
  81. ratio = Nume / Deno
  82. if math.IsNaN(ratio) {
  83. ratio = 0
  84. }
  85. return
  86. }
  87. // ComputeCorrelation 通过一组数据获取相关系数R
  88. // 计算步骤
  89. // 1.分别计算两个序列的平均值Mx和My
  90. // 2.分别计算两个序列的标准偏差SDx和SDy => √{1/(n-1)*SUM[(Xi-Mx)²]}
  91. // 3.计算相关系数 => SUM[(Xi-Mx)*(Yi-My)]/[(N-1)(SDx*SDy)]
  92. func ComputeCorrelation(sList []Coordinate) (r float64) {
  93. var xBar, yBar float64
  94. lenSList := len(sList)
  95. // 必须两组数据及两组以上的数据才能计算
  96. if lenSList < 2 {
  97. return
  98. }
  99. decimalX := decimal.NewFromFloat(0)
  100. decimalY := decimal.NewFromFloat(0)
  101. // 计算两组数据X、Y的平均值
  102. for _, coordinate := range sList {
  103. decimalX = decimalX.Add(decimal.NewFromFloat(coordinate.X))
  104. decimalY = decimalY.Add(decimal.NewFromFloat(coordinate.Y))
  105. }
  106. xBar, _ = decimalX.Div(decimal.NewFromInt(int64(lenSList))).Round(4).Float64()
  107. yBar, _ = decimalY.Div(decimal.NewFromInt(int64(lenSList))).Round(4).Float64()
  108. //fmt.Println(xBar)
  109. //fmt.Println(yBar)
  110. varXDeci := decimal.NewFromFloat(0)
  111. varYDeci := decimal.NewFromFloat(0)
  112. ssrDeci := decimal.NewFromFloat(0)
  113. for _, coordinate := range sList {
  114. // 分别计算X、Y的实际数据与平均值的差值
  115. diffXXbarDeci := decimal.NewFromFloat(coordinate.X).Sub(decimal.NewFromFloat(xBar))
  116. diffYYbarDeci := decimal.NewFromFloat(coordinate.Y).Sub(decimal.NewFromFloat(yBar))
  117. ssrDeci = ssrDeci.Add(diffXXbarDeci.Mul(diffYYbarDeci))
  118. //fmt.Println("i:", i, ";diffXXbar:", diffXXbarDeci.String(), ";diffYYbar:", diffYYbarDeci.String(), ";ssr:", ssrDeci.String())
  119. varXDeci = varXDeci.Add(diffXXbarDeci.Mul(diffXXbarDeci))
  120. varYDeci = varYDeci.Add(diffYYbarDeci.Mul(diffYYbarDeci))
  121. //varY += diffYYbar ** 2
  122. }
  123. //当输入的两个数组完全相同时,计算相关系数会导致除以零的操作,从而产生 NaN(Not a Number)的结果。为了避免这种情况,可以在计算相关系数之前先进行一个判断,如果两个数组的标准差为零,则相关系数应为1
  124. if varXDeci.IsZero() && varYDeci.IsZero() {
  125. r = 1
  126. return
  127. }
  128. sqrtVal, _ := varXDeci.Mul(varYDeci).Round(4).Float64()
  129. //fmt.Println("sqrtVal:", sqrtVal)
  130. sst := math.Sqrt(sqrtVal) // 平方根
  131. //fmt.Println("sst:", sst)
  132. // 如果计算出来的平方根是0,那么就直接返回,因为0不能作为除数
  133. if sst == 0 {
  134. return
  135. }
  136. r, _ = ssrDeci.Div(decimal.NewFromFloat(sst)).Round(4).Float64()
  137. return
  138. }
  139. // CalculationDecisive 通过一组数据获取决定系数R2
  140. func CalculationDecisive(sList []Coordinate) (r2 float64) {
  141. r := ComputeCorrelation(sList)
  142. r2, _ = decimal.NewFromFloat(r).Mul(decimal.NewFromFloat(r)).Round(4).Float64()
  143. return
  144. }
  145. // CalculateStandardDeviation 计算标准差
  146. func CalculateStandardDeviation(data []float64) float64 {
  147. return stat.StdDev(data, nil)
  148. }