predict_edb_info_rule.go 19 KB

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