predict_edb_info_rule.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. package data
  2. import (
  3. "github.com/shopspring/decimal"
  4. "hongze/hongze_chart_lib/models"
  5. "hongze/hongze_chart_lib/utils"
  6. "time"
  7. )
  8. // GetChartPredictEdbInfoDataListByRule1 根据规则1获取预测数据
  9. func GetChartPredictEdbInfoDataListByRule1(edbInfoId int, dataValue float64, startDate, endDate time.Time, frequency string, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList) {
  10. newPredictEdbInfoData = predictEdbInfoData
  11. //获取后面的预测数据
  12. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  13. predictEdbInfoData = make([]*models.EdbDataList, 0)
  14. for k, v := range dayList {
  15. newPredictEdbInfoData = append(newPredictEdbInfoData, &models.EdbDataList{
  16. EdbDataId: edbInfoId + 10000000000 + k,
  17. EdbInfoId: edbInfoId,
  18. DataTime: v.Format(utils.FormatDate),
  19. Value: dataValue,
  20. DataTimestamp: (v.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  21. })
  22. existMap[v.Format(utils.FormatDate)] = dataValue
  23. }
  24. return
  25. }
  26. // GetChartPredictEdbInfoDataListByRuleTb 根据同比值规则获取预测数据
  27. // 2.1 同比: 在未来某一个时间段内,给定一个固定的同比增速a,用去年同期值X乘以同比增速(1+a),得到预测值Y=X(1+a)
  28. // 例: 今年1-3月值,100,100,120。给定同比增速a=0.1,则明年1-3月预测值为: 100*1.1=110,100*1.1=110,120*1.1=132。
  29. func GetChartPredictEdbInfoDataListByRuleTb(edbInfoId int, tbValue float64, startDate, endDate time.Time, frequency string, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
  30. newPredictEdbInfoData = predictEdbInfoData
  31. index := len(predictEdbInfoData)
  32. //获取后面的预测数据
  33. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  34. predictEdbInfoData = make([]*models.EdbDataList, 0)
  35. for k, currentDate := range dayList {
  36. tmpData := &models.EdbDataList{
  37. EdbDataId: edbInfoId + 10000000000 + index + k,
  38. EdbInfoId: edbInfoId,
  39. DataTime: currentDate.Format(utils.FormatDate),
  40. //Value: dataValue,
  41. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  42. }
  43. var val float64
  44. var calculateStatus bool //计算结果
  45. //currentItem := existMap[av]
  46. //上一年的日期
  47. preDate := currentDate.AddDate(-1, 0, 0)
  48. preDateStr := preDate.Format(utils.FormatDate)
  49. if preValue, ok := existMap[preDateStr]; ok { //上一年同期找到
  50. val = TbzDiv(preValue, tbValue)
  51. calculateStatus = true
  52. } else {
  53. switch frequency {
  54. case "月度":
  55. //向上和向下,各找一个月
  56. nextDateDay := preDate
  57. preDateDay := preDate
  58. for i := 0; i <= 35; i++ {
  59. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  60. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  61. val = TbzDiv(preValue, tbValue)
  62. calculateStatus = true
  63. break
  64. } else {
  65. preDateDayStr := preDateDay.Format(utils.FormatDate)
  66. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  67. val = TbzDiv(preValue, tbValue)
  68. calculateStatus = true
  69. break
  70. }
  71. }
  72. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  73. preDateDay = preDateDay.AddDate(0, 0, -1)
  74. }
  75. case "季度", "年度":
  76. if preValue, ok := existMap[preDateStr]; ok { //上一年同期->下一个月找到
  77. val = TbzDiv(preValue, tbValue)
  78. calculateStatus = true
  79. break
  80. }
  81. default:
  82. nextDateDay := preDate
  83. preDateDay := preDate
  84. for i := 0; i < 35; i++ {
  85. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  86. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  87. val = TbzDiv(preValue, tbValue)
  88. calculateStatus = true
  89. break
  90. } else {
  91. preDateDayStr := preDateDay.Format(utils.FormatDate)
  92. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  93. val = TbzDiv(preValue, tbValue)
  94. calculateStatus = true
  95. break
  96. } else {
  97. //fmt.Println("pre not find:", preDateStr, "i:", i)
  98. }
  99. }
  100. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  101. preDateDay = preDateDay.AddDate(0, 0, -1)
  102. }
  103. }
  104. }
  105. if calculateStatus {
  106. tmpData.Value = val
  107. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  108. // 最大最小值
  109. if val < minValue {
  110. minValue = val
  111. }
  112. if val < maxValue {
  113. maxValue = val
  114. }
  115. }
  116. }
  117. return
  118. }
  119. // TbzDiv 同比值计算
  120. // @params a float64 去年同期值
  121. // @params b float64 固定同比增速
  122. func TbzDiv(a, b float64) (result float64) {
  123. if b != 0 {
  124. // 去年同期值
  125. af := decimal.NewFromFloat(a)
  126. // 同比增速
  127. bf := decimal.NewFromFloat(b)
  128. // 默认1
  129. cf := decimal.NewFromFloat(1)
  130. // 总增速
  131. val := bf.Add(cf)
  132. // 计算
  133. result, _ = val.Mul(af).RoundCeil(4).Float64()
  134. } else {
  135. result = 0
  136. }
  137. return
  138. }
  139. // GetChartPredictEdbInfoDataListByRuleTc 根据同差值规则获取预测数据
  140. // 2.2 同差: 在未来某一个时间段内,给定一个固定的同比增加值a,用去年同期值X加上同比增加值A,得到预测值Y=X+a
  141. // 例: 今年1-3月值,100,100,120。给定同比增加值a=10,则明年1-3月预测值为: 100+10=110,100+10=110,120+10=130
  142. func GetChartPredictEdbInfoDataListByRuleTc(edbInfoId int, tcValue float64, startDate, endDate time.Time, frequency string, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
  143. newPredictEdbInfoData = predictEdbInfoData
  144. index := len(predictEdbInfoData)
  145. //获取后面的预测数据
  146. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  147. predictEdbInfoData = make([]*models.EdbDataList, 0)
  148. for k, currentDate := range dayList {
  149. tmpData := &models.EdbDataList{
  150. EdbDataId: edbInfoId + 10000000000 + index + k,
  151. EdbInfoId: edbInfoId,
  152. DataTime: currentDate.Format(utils.FormatDate),
  153. //Value: dataValue,
  154. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  155. }
  156. var val float64
  157. var calculateStatus bool //计算结果
  158. //currentItem := existMap[av]
  159. //上一年的日期
  160. preDate := currentDate.AddDate(-1, 0, 0)
  161. preDateStr := preDate.Format(utils.FormatDate)
  162. if preValue, ok := existMap[preDateStr]; ok { //上一年同期找到
  163. val = TczDiv(preValue, tcValue)
  164. calculateStatus = true
  165. } else {
  166. switch frequency {
  167. case "月度":
  168. //向上和向下,各找一个月
  169. nextDateDay := preDate
  170. preDateDay := preDate
  171. for i := 0; i <= 35; i++ {
  172. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  173. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  174. val = TczDiv(preValue, tcValue)
  175. calculateStatus = true
  176. break
  177. } else {
  178. preDateDayStr := preDateDay.Format(utils.FormatDate)
  179. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  180. val = TczDiv(preValue, tcValue)
  181. calculateStatus = true
  182. break
  183. }
  184. }
  185. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  186. preDateDay = preDateDay.AddDate(0, 0, -1)
  187. }
  188. case "季度", "年度":
  189. if preValue, ok := existMap[preDateStr]; ok { //上一年同期->下一个月找到
  190. val = TczDiv(preValue, tcValue)
  191. calculateStatus = true
  192. break
  193. }
  194. default:
  195. nextDateDay := preDate
  196. preDateDay := preDate
  197. for i := 0; i < 35; i++ {
  198. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  199. if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  200. val = TczDiv(preValue, tcValue)
  201. calculateStatus = true
  202. break
  203. } else {
  204. preDateDayStr := preDateDay.Format(utils.FormatDate)
  205. if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  206. val = TczDiv(preValue, tcValue)
  207. calculateStatus = true
  208. break
  209. } else {
  210. //fmt.Println("pre not find:", preDateStr, "i:", i)
  211. }
  212. }
  213. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  214. preDateDay = preDateDay.AddDate(0, 0, -1)
  215. }
  216. }
  217. }
  218. if calculateStatus {
  219. tmpData.Value = val
  220. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  221. // 最大最小值
  222. if val < minValue {
  223. minValue = val
  224. }
  225. if val < maxValue {
  226. maxValue = val
  227. }
  228. }
  229. }
  230. return
  231. }
  232. // TczDiv 环差值计算
  233. // @params a float64 上一期值
  234. // @params b float64 固定的环比增加值
  235. func TczDiv(a, b float64) (result float64) {
  236. if b != 0 {
  237. // 上一期值
  238. af := decimal.NewFromFloat(a)
  239. // 固定的环比增加值
  240. bf := decimal.NewFromFloat(b)
  241. // 计算
  242. result, _ = af.Add(bf).RoundCeil(4).Float64()
  243. } else {
  244. result = 0
  245. }
  246. return
  247. }
  248. // GetChartPredictEdbInfoDataListByRuleHb 根据环比值规则获取预测数据
  249. // 环比:在未来某一个时间段内,给定一个固定的环比增速a,用上一期值X乘以环比增速(1+a),得到预测值Y=X(1+a)
  250. // 例: 最近1期值为100,给定环比增速a=0.2,则未来3期预测值为: 100*1.2=120,120*1.2=144,144*1.2=172.8
  251. func GetChartPredictEdbInfoDataListByRuleHb(edbInfoId int, hbValue float64, startDate, endDate time.Time, frequency string, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
  252. newPredictEdbInfoData = predictEdbInfoData
  253. index := len(predictEdbInfoData)
  254. //获取后面的预测数据
  255. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  256. for k, currentDate := range dayList {
  257. tmpK := index + k - 1 //上1期的值
  258. // 环比值计算
  259. val := HbzDiv(newPredictEdbInfoData[tmpK].Value, hbValue)
  260. currentDateStr := currentDate.Format(utils.FormatDate)
  261. newPredictEdbInfoData = append(newPredictEdbInfoData, &models.EdbDataList{
  262. EdbDataId: edbInfoId + 10000000000 + index + k,
  263. EdbInfoId: edbInfoId,
  264. DataTime: currentDateStr,
  265. Value: val,
  266. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  267. })
  268. existMap[currentDateStr] = val
  269. // 最大最小值
  270. if val < minValue {
  271. minValue = val
  272. }
  273. if val < maxValue {
  274. maxValue = val
  275. }
  276. }
  277. return
  278. }
  279. // HbzDiv 环比值计算
  280. // @params a float64 上一期值
  281. // @params b float64 固定的环比增速
  282. func HbzDiv(a, b float64) (result float64) {
  283. if b != 0 {
  284. // 上一期值
  285. af := decimal.NewFromFloat(a)
  286. // 固定的环比增速
  287. bf := decimal.NewFromFloat(b)
  288. // 默认1
  289. cf := decimal.NewFromFloat(1)
  290. // 总增速
  291. val := bf.Add(cf)
  292. // 计算
  293. result, _ = val.Mul(af).RoundCeil(4).Float64()
  294. } else {
  295. result = 0
  296. }
  297. return
  298. }
  299. // GetChartPredictEdbInfoDataListByRuleHc 根据环差值规则获取预测数据
  300. // 2.4 环差:在未来某一个时间段内,给定一个固定的环比增加值a,用上一期值X加上环比增加值a,得到预测值Y=X+a
  301. // 例: 最近1期值为100,给定环比增加值a=10,则未来3期预测值为: 100+10=110,110+10=120,120+10=130
  302. func GetChartPredictEdbInfoDataListByRuleHc(edbInfoId int, hcValue float64, startDate, endDate time.Time, frequency string, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
  303. newPredictEdbInfoData = predictEdbInfoData
  304. index := len(predictEdbInfoData)
  305. //获取后面的预测数据
  306. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  307. for k, currentDate := range dayList {
  308. tmpK := index + k - 1 //上1期的值
  309. // 环差别值计算
  310. val := HczDiv(newPredictEdbInfoData[tmpK].Value, hcValue)
  311. currentDateStr := currentDate.Format(utils.FormatDate)
  312. newPredictEdbInfoData = append(newPredictEdbInfoData, &models.EdbDataList{
  313. EdbDataId: edbInfoId + 10000000000 + index + k,
  314. EdbInfoId: edbInfoId,
  315. DataTime: currentDateStr,
  316. Value: val,
  317. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  318. })
  319. existMap[currentDateStr] = val
  320. // 最大最小值
  321. if val < minValue {
  322. minValue = val
  323. }
  324. if val < maxValue {
  325. maxValue = val
  326. }
  327. }
  328. return
  329. }
  330. // HczDiv 环差值计算
  331. // @params a float64 上一期值
  332. // @params b float64 固定的环比增加值
  333. func HczDiv(a, b float64) (result float64) {
  334. if b != 0 {
  335. // 上一期值
  336. af := decimal.NewFromFloat(a)
  337. // 固定的环比增加值
  338. bf := decimal.NewFromFloat(b)
  339. // 计算
  340. result, _ = af.Add(bf).RoundCeil(4).Float64()
  341. } else {
  342. result = 0
  343. }
  344. return
  345. }
  346. // GetChartPredictEdbInfoDataListByRuleNMoveMeanValue 根据N期移动均值规则获取预测数据
  347. // 2.5 N期移动均值:在未来某一个时间段内,下一期值等于过去N期值得平均值。
  348. // 例:最近3期值(N=3),为95,98,105则未来第1期值为 1/3*(95+98+105)=99.33, 未来第2期值为 1/3*(98+105+99.33)=100.78依次类推。
  349. func GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(edbInfoId int, nValue int, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
  350. allDataList := make([]*models.EdbDataList, 0)
  351. allDataList = append(allDataList, realPredictEdbInfoData...)
  352. allDataList = append(allDataList, predictEdbInfoData...)
  353. newPredictEdbInfoData = predictEdbInfoData
  354. lenAllData := len(allDataList)
  355. if lenAllData < nValue || lenAllData <= 0 {
  356. return
  357. }
  358. if nValue <= 0 {
  359. return
  360. }
  361. // 分母
  362. decimalN := decimal.NewFromInt(int64(nValue))
  363. //获取后面的预测数据
  364. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  365. for k, currentDate := range dayList {
  366. tmpIndex := lenAllData + k - 1 //上1期的值
  367. // 数据集合中的最后一个数据
  368. tmpDecimalVal := decimal.NewFromFloat(allDataList[tmpIndex].Value)
  369. for tmpK := 2; tmpK <= nValue; tmpK++ {
  370. tmpIndex2 := tmpIndex - tmpK //上N期的值
  371. tmpDecimalVal2 := decimal.NewFromFloat(allDataList[tmpIndex2].Value)
  372. tmpDecimalVal = tmpDecimalVal.Add(tmpDecimalVal2)
  373. }
  374. // N期移动均值计算
  375. val, _ := tmpDecimalVal.Div(decimalN).RoundCeil(4).Float64()
  376. currentDateStr := currentDate.Format(utils.FormatDate)
  377. tmpData := &models.EdbDataList{
  378. EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
  379. EdbInfoId: edbInfoId,
  380. DataTime: currentDateStr,
  381. Value: val,
  382. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  383. }
  384. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  385. allDataList = append(allDataList, tmpData)
  386. existMap[currentDateStr] = val
  387. // 最大最小值
  388. if val < minValue {
  389. minValue = val
  390. }
  391. if val < maxValue {
  392. maxValue = val
  393. }
  394. }
  395. return
  396. }
  397. // GetChartPredictEdbInfoDataListByRuleNLinearRegression 根据N期移动均值规则获取预测数据
  398. // 2.6N期段线性外推值:给出过去N期值所确定的线性回归方程(Y=aX+b)在未来一段时间内的推算值。回归方程虽然比较复杂,但各种编程语言应该都有现成的模块或函数,应该无需自己编写。
  399. // 例1:过去5期值(N=5)分别为:3,5,7,9,11(每两期值之间的时间间隔相等)。那么按照线性回归方程推算,未来三期的预测值是:13,15,17。
  400. //
  401. // 例2:过去6期值(N=6)分别为:3,3,5,7,9,11(每两期值之间的时间间隔相等)。那么按照线性回归方程推算,未来三期的预测值是:12.33,14.05,15.76。例1和例2的区别在于,多加了一期数据,导致回归方程发生改变,从而预测值不同。
  402. func GetChartPredictEdbInfoDataListByRuleNLinearRegression(edbInfoId int, nValue int, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
  403. //var errMsg string
  404. //defer func() {
  405. // if errMsg != `` {
  406. // go alarm_msg.SendAlarmMsg("更新上海的token失败;ERR:"+err.Error(), 3)
  407. // }
  408. //}()
  409. allDataList := make([]*models.EdbDataList, 0)
  410. allDataList = append(allDataList, realPredictEdbInfoData...)
  411. allDataList = append(allDataList, predictEdbInfoData...)
  412. newPredictEdbInfoData = predictEdbInfoData
  413. lenAllData := len(allDataList)
  414. if lenAllData < nValue || lenAllData <= 0 {
  415. return
  416. }
  417. if nValue <= 0 {
  418. return
  419. }
  420. //获取后面的预测数据
  421. // 获取线性方程公式的a、b的值
  422. coordinateData := make([]Coordinate, 0)
  423. for tmpK := nValue; tmpK > 0; tmpK-- {
  424. tmpIndex2 := lenAllData - tmpK //上N期的值
  425. tmpCoordinate := Coordinate{
  426. X: float64(nValue - tmpK + 1),
  427. Y: allDataList[tmpIndex2].Value,
  428. }
  429. coordinateData = append(coordinateData, tmpCoordinate)
  430. }
  431. a, b := getLinearResult(coordinateData)
  432. //fmt.Println("a:", a, ";======b:", b)
  433. dayList := getPredictEdbDayList(startDate, endDate, frequency)
  434. for k, currentDate := range dayList {
  435. tmpK := nValue + k + 1
  436. aDecimal := decimal.NewFromFloat(a)
  437. xDecimal := decimal.NewFromInt(int64(tmpK))
  438. bDecimal := decimal.NewFromFloat(b)
  439. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).RoundCeil(4).Float64()
  440. currentDateStr := currentDate.Format(utils.FormatDate)
  441. tmpData := &models.EdbDataList{
  442. EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
  443. EdbInfoId: edbInfoId,
  444. DataTime: currentDateStr,
  445. Value: val,
  446. DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
  447. }
  448. newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
  449. allDataList = append(allDataList, tmpData)
  450. existMap[currentDateStr] = val
  451. // 最大最小值
  452. if val < minValue {
  453. minValue = val
  454. }
  455. if val < maxValue {
  456. maxValue = val
  457. }
  458. }
  459. return
  460. }
  461. // Series is a container for a series of data
  462. type Series []Coordinate
  463. // Coordinate holds the data in a series
  464. type Coordinate struct {
  465. X, Y float64
  466. }
  467. func getLinearResult(s []Coordinate) (gradient, intercept float64) {
  468. if len(s) == 0 {
  469. return
  470. }
  471. // Placeholder for the math to be done
  472. var sum [5]float64
  473. // Loop over data keeping index in place
  474. i := 0
  475. for ; i < len(s); i++ {
  476. sum[0] += s[i].X
  477. sum[1] += s[i].Y
  478. sum[2] += s[i].X * s[i].X
  479. sum[3] += s[i].X * s[i].Y
  480. sum[4] += s[i].Y * s[i].Y
  481. }
  482. // Find gradient and intercept
  483. f := float64(i)
  484. gradient = (f*sum[3] - sum[0]*sum[1]) / (f*sum[2] - sum[0]*sum[0])
  485. intercept = (sum[1] / f) - (gradient * sum[0] / f)
  486. //fmt.Println("gradient:", gradient, ";intercept:", intercept)
  487. // Create the new regression series
  488. //for j := 0; j < len(s); j++ {
  489. // regressions = append(regressions, Coordinate{
  490. // X: s[j].X,
  491. // Y: s[j].X*gradient + intercept,
  492. // })
  493. //}
  494. return
  495. }