chart_correlation.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package logic
  2. import (
  3. "eta_gn/eta_index_lib/models"
  4. "eta_gn/eta_index_lib/utils"
  5. "fmt"
  6. )
  7. // CalculateCorrelation 计算相关性-获取x轴和y轴
  8. func CalculateCorrelation(leadValue int, leadUnit, frequencyA, frequencyB string, dataListA, dataListB []*models.EdbInfoSearchData) (xEdbIdValue []int, yDataList []YData, err error) {
  9. xData := make([]int, 0)
  10. yData := make([]float64, 0)
  11. if leadValue == 0 {
  12. xData = append(xData, 0)
  13. }
  14. if leadValue > 0 {
  15. leadMin := 0 - leadValue
  16. xLen := 2*leadValue + 1
  17. for i := 0; i < xLen; i++ {
  18. n := leadMin + i
  19. xData = append(xData, n)
  20. }
  21. }
  22. // 计算窗口,不包含第一天
  23. //startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  24. //startDate = startDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  25. //// 2023-03-02 时间序列始终以指标B为基准, 始终是A进行平移
  26. //baseEdbInfo := edbInfoMappingB
  27. //changeEdbInfo := edbInfoMappingA
  28. // 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
  29. //baseEdbInfo := edbInfoMappingA
  30. //changeEdbInfo := edbInfoMappingB
  31. // 获取时间基准指标在时间区间内的值
  32. //aDataList := make([]*EdbDataList, 0)
  33. //switch baseEdbInfo.EdbInfoCategoryType {
  34. //case 0:
  35. // aDataList, err = GetEdbDataList(baseEdbInfo.Source, baseEdbInfo.SubSource, baseEdbInfo.EdbInfoId, startDate, endDate)
  36. //case 1:
  37. // _, aDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(baseEdbInfo.EdbInfoId, startDate, endDate, false)
  38. //default:
  39. // err = errors.New("指标base类型异常")
  40. // return
  41. //}
  42. //
  43. //// 获取变频指标所有日期的值, 插值法完善数据
  44. //bDataList := make([]*EdbDataList, 0)
  45. //switch changeEdbInfo.EdbInfoCategoryType {
  46. //case 0:
  47. // bDataList, err = GetEdbDataList(changeEdbInfo.Source, changeEdbInfo.SubSource, changeEdbInfo.EdbInfoId, "", "")
  48. //case 1:
  49. // _, bDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(changeEdbInfo.EdbInfoId, "", "", false)
  50. //default:
  51. // err = errors.New("指标change类型异常")
  52. // return
  53. //}
  54. //changeDataMap := make(map[string]float64)
  55. //newChangeDataList, e := HandleDataByLinearRegression(bDataList, changeDataMap)
  56. //if e != nil {
  57. // err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
  58. // return
  59. //}
  60. // 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
  61. baseDataList := make([]*models.EdbInfoSearchData, 0)
  62. baseDataMap := make(map[string]float64)
  63. changeDataList := make([]*models.EdbInfoSearchData, 0)
  64. changeDataMap := make(map[string]float64)
  65. // 先把低频指标升频为高频
  66. {
  67. frequencyIntMap := map[string]int{
  68. "日度": 1,
  69. "周度": 2,
  70. "旬度": 3,
  71. "月度": 4,
  72. "季度": 5,
  73. "年度": 6,
  74. }
  75. // 如果A指标是高频,那么就需要对B指标进行升频
  76. if frequencyIntMap[frequencyA] < frequencyIntMap[frequencyB] {
  77. tmpNewChangeDataList, e := models.HandleDataByLinearRegression(dataListB, changeDataMap)
  78. if e != nil {
  79. err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
  80. return
  81. }
  82. changeDataList = tmpNewChangeDataList
  83. baseDataList = dataListA
  84. for _, v := range baseDataList {
  85. baseDataMap[v.DataTime] = v.Value
  86. }
  87. } else if frequencyIntMap[frequencyA] > frequencyIntMap[frequencyB] {
  88. // 如果B指标是高频,那么就需要对A指标进行升频
  89. tmpNewChangeDataList, e := models.HandleDataByLinearRegression(dataListA, baseDataMap)
  90. if e != nil {
  91. err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
  92. return
  93. }
  94. baseDataList = tmpNewChangeDataList
  95. changeDataList = dataListB
  96. for _, v := range changeDataList {
  97. changeDataMap[v.DataTime] = v.Value
  98. }
  99. } else {
  100. baseDataList = dataListA
  101. for _, v := range baseDataList {
  102. baseDataMap[v.DataTime] = v.Value
  103. }
  104. changeDataList = dataListB
  105. for _, v := range changeDataList {
  106. changeDataMap[v.DataTime] = v.Value
  107. }
  108. }
  109. }
  110. // 计算不领先也不滞后时的相关系数
  111. baseCalculateData := make([]float64, 0)
  112. baseDataTimeArr := make([]string, 0)
  113. for i := range baseDataList {
  114. baseDataTimeArr = append(baseDataTimeArr, baseDataList[i].DataTime)
  115. baseCalculateData = append(baseCalculateData, baseDataList[i].Value)
  116. }
  117. //zeroBaseData := make([]float64, 0)
  118. //zeroCalculateData := make([]float64, 0)
  119. //for i := range baseDataTimeArr {
  120. // tmpBaseVal, ok1 := baseDataMap[baseDataTimeArr[i]]
  121. // tmpCalculateVal, ok2 := changeDataMap[baseDataTimeArr[i]]
  122. // if ok1 && ok2 {
  123. // zeroBaseData = append(zeroBaseData, tmpBaseVal)
  124. // zeroCalculateData = append(zeroCalculateData, tmpCalculateVal)
  125. // }
  126. //}
  127. //if len(zeroBaseData) != len(zeroCalculateData) {
  128. // err = fmt.Errorf("相关系数两组序列元素数不一致, %d-%d", len(baseCalculateData), len(zeroCalculateData))
  129. // return
  130. //}
  131. //zeroRatio := utils.CalculateCorrelationByIntArr(zeroBaseData, zeroCalculateData)
  132. //if leadValue == 0 {
  133. // yData = append(yData, zeroRatio)
  134. //}
  135. // 计算领先/滞后N期
  136. if leadValue > 0 {
  137. // 平移变频指标领先/滞后的日期(单位天)
  138. moveUnitDays := utils.FrequencyDaysMap[leadUnit]
  139. for i := range xData {
  140. //if xData[i] == 0 {
  141. // yData = append(yData, zeroRatio)
  142. // continue
  143. //}
  144. xCalculateData := make([]float64, 0)
  145. yCalculateData := make([]float64, 0)
  146. // 平移指定天数
  147. mDays := int(moveUnitDays) * xData[i]
  148. _, dMap := models.MoveDataDaysToNewDataList(changeDataList, mDays)
  149. // 取出对应的基准日期的值
  150. for i2 := range baseDataTimeArr {
  151. tmpDate := baseDataTimeArr[i2]
  152. if yVal, ok := dMap[tmpDate]; ok {
  153. xCalculateData = append(xCalculateData, baseCalculateData[i2])
  154. yCalculateData = append(yCalculateData, yVal)
  155. }
  156. }
  157. if len(yCalculateData) <= 0 {
  158. //err = fmt.Errorf("领先滞后相关系数两组序列元素数不一致, %d-%d", len(baseCalculateData), len(yCalculateData))
  159. //return
  160. // 领先滞后后,没有可以计算的数据了
  161. continue
  162. }
  163. // 公式计算出领先/滞后频度对应点的相关性系数
  164. ratio := utils.CalculateCorrelationByIntArr(xCalculateData, yCalculateData)
  165. yData = append(yData, ratio)
  166. }
  167. }
  168. xEdbIdValue = xData
  169. yDataList = make([]YData, 0)
  170. yDate := "0000-00-00"
  171. yDataList = append(yDataList, YData{
  172. Date: yDate,
  173. Value: yData,
  174. })
  175. return
  176. }