123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568 |
- package models
- import (
- "github.com/shopspring/decimal"
- "hongze/hongze_edb_lib/utils"
- "time"
- )
- // GetChartPredictEdbInfoDataListByRule1 根据规则1获取预测数据
- func GetChartPredictEdbInfoDataListByRule1(edbInfoId int, dataValue float64, startDate, endDate time.Time, frequency string, predictEdbInfoData []*EdbInfoSearchData, existMap map[string]float64) (newPredictEdbInfoData []*EdbInfoSearchData) {
- newPredictEdbInfoData = predictEdbInfoData
- //获取后面的预测数据
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- predictEdbInfoData = make([]*EdbInfoSearchData, 0)
- for k, v := range dayList {
- newPredictEdbInfoData = append(newPredictEdbInfoData, &EdbInfoSearchData{
- EdbDataId: edbInfoId + 10000000000 + k,
- DataTime: v.Format(utils.FormatDate),
- Value: dataValue,
- })
- existMap[v.Format(utils.FormatDate)] = dataValue
- }
- return
- }
- // GetChartPredictEdbInfoDataListByRuleTb 根据同比值规则获取预测数据
- // 2.1 同比: 在未来某一个时间段内,给定一个固定的同比增速a,用去年同期值X乘以同比增速(1+a),得到预测值Y=X(1+a)
- // 例: 今年1-3月值,100,100,120。给定同比增速a=0.1,则明年1-3月预测值为: 100*1.1=110,100*1.1=110,120*1.1=132。
- func GetChartPredictEdbInfoDataListByRuleTb(edbInfoId int, tbValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*EdbInfoSearchData, existMap map[string]float64) (newPredictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64) {
- allDataList := make([]*EdbInfoSearchData, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- index := len(allDataList)
- //获取后面的预测数据
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- predictEdbInfoData = make([]*EdbInfoSearchData, 0)
- for k, currentDate := range dayList {
- tmpData := &EdbInfoSearchData{
- EdbDataId: edbInfoId + 10000000000 + index + k,
- DataTime: currentDate.Format(utils.FormatDate),
- //Value: dataValue,
- }
- var val float64
- var calculateStatus bool //计算结果
- //currentItem := existMap[av]
- //上一年的日期
- preDate := currentDate.AddDate(-1, 0, 0)
- preDateStr := preDate.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateStr]; ok { //上一年同期找到
- val = PredictTbzDiv(preValue, tbValue)
- calculateStatus = true
- } else {
- switch frequency {
- case "月度":
- //向上和向下,各找一个月
- nextDateDay := preDate
- preDateDay := preDate
- for i := 0; i <= 35; i++ {
- nextDateDayStr := nextDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
- val = PredictTbzDiv(preValue, tbValue)
- calculateStatus = true
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
- val = PredictTbzDiv(preValue, tbValue)
- calculateStatus = true
- break
- }
- }
- nextDateDay = nextDateDay.AddDate(0, 0, 1)
- preDateDay = preDateDay.AddDate(0, 0, -1)
- }
- case "季度", "年度":
- if preValue, ok := existMap[preDateStr]; ok { //上一年同期->下一个月找到
- val = PredictTbzDiv(preValue, tbValue)
- calculateStatus = true
- break
- }
- default:
- nextDateDay := preDate
- preDateDay := preDate
- for i := 0; i < 35; i++ {
- nextDateDayStr := nextDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
- val = PredictTbzDiv(preValue, tbValue)
- calculateStatus = true
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
- val = PredictTbzDiv(preValue, tbValue)
- calculateStatus = true
- break
- } else {
- //fmt.Println("pre not find:", preDateStr, "i:", i)
- }
- }
- nextDateDay = nextDateDay.AddDate(0, 0, 1)
- preDateDay = preDateDay.AddDate(0, 0, -1)
- }
- }
- }
- if calculateStatus {
- tmpData.Value = val
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[tmpData.DataTime] = val
- // 最大最小值
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- }
- return
- }
- // PredictTbzDiv 同比值计算
- // @params a float64 去年同期值
- // @params b float64 固定同比增速
- func PredictTbzDiv(a, b float64) (result float64) {
- if b != 0 {
- // 去年同期值
- af := decimal.NewFromFloat(a)
- // 同比增速
- bf := decimal.NewFromFloat(b)
- // 默认1
- cf := decimal.NewFromFloat(1)
- // 总增速
- val := bf.Add(cf)
- // 计算
- result, _ = val.Mul(af).RoundCeil(4).Float64()
- } else {
- result = 0
- }
- return
- }
- // GetChartPredictEdbInfoDataListByRuleTc 根据同差值规则获取预测数据
- // 2.2 同差: 在未来某一个时间段内,给定一个固定的同比增加值a,用去年同期值X加上同比增加值A,得到预测值Y=X+a
- // 例: 今年1-3月值,100,100,120。给定同比增加值a=10,则明年1-3月预测值为: 100+10=110,100+10=110,120+10=130
- func GetChartPredictEdbInfoDataListByRuleTc(edbInfoId int, tcValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*EdbInfoSearchData, existMap map[string]float64) (newPredictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64) {
- allDataList := make([]*EdbInfoSearchData, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- index := len(allDataList)
- //获取后面的预测数据
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- predictEdbInfoData = make([]*EdbInfoSearchData, 0)
- for k, currentDate := range dayList {
- tmpData := &EdbInfoSearchData{
- EdbDataId: edbInfoId + 10000000000 + index + k,
- DataTime: currentDate.Format(utils.FormatDate),
- //Value: dataValue,
- }
- var val float64
- var calculateStatus bool //计算结果
- //currentItem := existMap[av]
- //上一年的日期
- preDate := currentDate.AddDate(-1, 0, 0)
- preDateStr := preDate.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateStr]; ok { //上一年同期找到
- val = PredictTczDiv(preValue, tcValue)
- calculateStatus = true
- } else {
- switch frequency {
- case "月度":
- //向上和向下,各找一个月
- nextDateDay := preDate
- preDateDay := preDate
- for i := 0; i <= 35; i++ {
- nextDateDayStr := nextDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
- val = PredictTczDiv(preValue, tcValue)
- calculateStatus = true
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
- val = PredictTczDiv(preValue, tcValue)
- calculateStatus = true
- break
- }
- }
- nextDateDay = nextDateDay.AddDate(0, 0, 1)
- preDateDay = preDateDay.AddDate(0, 0, -1)
- }
- case "季度", "年度":
- if preValue, ok := existMap[preDateStr]; ok { //上一年同期->下一个月找到
- val = PredictTczDiv(preValue, tcValue)
- calculateStatus = true
- break
- }
- default:
- nextDateDay := preDate
- preDateDay := preDate
- for i := 0; i < 35; i++ {
- nextDateDayStr := nextDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
- val = PredictTczDiv(preValue, tcValue)
- calculateStatus = true
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
- val = PredictTczDiv(preValue, tcValue)
- calculateStatus = true
- break
- } else {
- //fmt.Println("pre not find:", preDateStr, "i:", i)
- }
- }
- nextDateDay = nextDateDay.AddDate(0, 0, 1)
- preDateDay = preDateDay.AddDate(0, 0, -1)
- }
- }
- }
- if calculateStatus {
- tmpData.Value = val
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[tmpData.DataTime] = val
- // 最大最小值
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- }
- return
- }
- // PredictTczDiv 环差值计算
- // @params a float64 上一期值
- // @params b float64 固定的环比增加值
- func PredictTczDiv(a, b float64) (result float64) {
- if b != 0 {
- // 上一期值
- af := decimal.NewFromFloat(a)
- // 固定的环比增加值
- bf := decimal.NewFromFloat(b)
- // 计算
- result, _ = af.Add(bf).RoundCeil(4).Float64()
- } else {
- result = 0
- }
- return
- }
- // GetChartPredictEdbInfoDataListByRuleHb 根据环比值规则获取预测数据
- // 环比:在未来某一个时间段内,给定一个固定的环比增速a,用上一期值X乘以环比增速(1+a),得到预测值Y=X(1+a)
- // 例: 最近1期值为100,给定环比增速a=0.2,则未来3期预测值为: 100*1.2=120,120*1.2=144,144*1.2=172.8
- func GetChartPredictEdbInfoDataListByRuleHb(edbInfoId int, hbValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*EdbInfoSearchData, existMap map[string]float64) (newPredictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64) {
- allDataList := make([]*EdbInfoSearchData, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- index := len(allDataList)
- //获取后面的预测数据
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- for k, currentDate := range dayList {
- tmpK := index + k - 1 //上1期的值
- // 环比值计算
- val := PredictHbzDiv(allDataList[tmpK].Value, hbValue)
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &EdbInfoSearchData{
- EdbDataId: edbInfoId + 10000000000 + index + k,
- DataTime: currentDateStr,
- Value: val,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
- // 最大最小值
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- // PredictHbzDiv 环比值计算
- // @params a float64 上一期值
- // @params b float64 固定的环比增速
- func PredictHbzDiv(a, b float64) (result float64) {
- if b != 0 {
- // 上一期值
- af := decimal.NewFromFloat(a)
- // 固定的环比增速
- bf := decimal.NewFromFloat(b)
- // 默认1
- cf := decimal.NewFromFloat(1)
- // 总增速
- val := bf.Add(cf)
- // 计算
- result, _ = val.Mul(af).RoundCeil(4).Float64()
- } else {
- result = 0
- }
- return
- }
- // GetChartPredictEdbInfoDataListByRuleHc 根据环差值规则获取预测数据
- // 2.4 环差:在未来某一个时间段内,给定一个固定的环比增加值a,用上一期值X加上环比增加值a,得到预测值Y=X+a
- // 例: 最近1期值为100,给定环比增加值a=10,则未来3期预测值为: 100+10=110,110+10=120,120+10=130
- func GetChartPredictEdbInfoDataListByRuleHc(edbInfoId int, hcValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*EdbInfoSearchData, existMap map[string]float64) (newPredictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64) {
- allDataList := make([]*EdbInfoSearchData, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- index := len(allDataList)
- //获取后面的预测数据
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- for k, currentDate := range dayList {
- tmpK := index + k - 1 //上1期的值
- // 环差别值计算
- val := PredictHczDiv(allDataList[tmpK].Value, hcValue)
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &EdbInfoSearchData{
- EdbDataId: edbInfoId + 10000000000 + index + k,
- DataTime: currentDateStr,
- Value: val,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
- // 最大最小值
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- // PredictHczDiv 环差值计算
- // @params a float64 上一期值
- // @params b float64 固定的环比增加值
- func PredictHczDiv(a, b float64) (result float64) {
- if b != 0 {
- // 上一期值
- af := decimal.NewFromFloat(a)
- // 固定的环比增加值
- bf := decimal.NewFromFloat(b)
- // 计算
- result, _ = af.Add(bf).RoundCeil(4).Float64()
- } else {
- result = 0
- }
- return
- }
- // GetChartPredictEdbInfoDataListByRuleNMoveMeanValue 根据N期移动均值规则获取预测数据
- // 2.5 N期移动均值:在未来某一个时间段内,下一期值等于过去N期值得平均值。
- // 例:最近3期值(N=3),为95,98,105则未来第1期值为 1/3*(95+98+105)=99.33, 未来第2期值为 1/3*(98+105+99.33)=100.78依次类推。
- func GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(edbInfoId int, nValue int, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*EdbInfoSearchData, existMap map[string]float64) (newPredictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64) {
- allDataList := make([]*EdbInfoSearchData, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- lenAllData := len(allDataList)
- if lenAllData < nValue || lenAllData <= 0 {
- return
- }
- if nValue <= 0 {
- return
- }
- // 分母
- decimalN := decimal.NewFromInt(int64(nValue))
- //获取后面的预测数据
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- for k, currentDate := range dayList {
- tmpIndex := lenAllData + k - 1 //上1期的值
- // 数据集合中的最后一个数据
- tmpDecimalVal := decimal.NewFromFloat(allDataList[tmpIndex].Value)
- for tmpK := 2; tmpK <= nValue; tmpK++ {
- tmpIndex2 := tmpIndex - tmpK //上N期的值
- tmpDecimalVal2 := decimal.NewFromFloat(allDataList[tmpIndex2].Value)
- tmpDecimalVal = tmpDecimalVal.Add(tmpDecimalVal2)
- }
- // N期移动均值计算
- val, _ := tmpDecimalVal.Div(decimalN).RoundCeil(4).Float64()
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &EdbInfoSearchData{
- EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
- DataTime: currentDateStr,
- Value: val,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
- // 最大最小值
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- // GetChartPredictEdbInfoDataListByRuleNLinearRegression 根据N期移动均值规则获取预测数据
- // 2.6N期段线性外推值:给出过去N期值所确定的线性回归方程(Y=aX+b)在未来一段时间内的推算值。回归方程虽然比较复杂,但各种编程语言应该都有现成的模块或函数,应该无需自己编写。
- // 例1:过去5期值(N=5)分别为:3,5,7,9,11(每两期值之间的时间间隔相等)。那么按照线性回归方程推算,未来三期的预测值是:13,15,17。
- //
- // 例2:过去6期值(N=6)分别为:3,3,5,7,9,11(每两期值之间的时间间隔相等)。那么按照线性回归方程推算,未来三期的预测值是:12.33,14.05,15.76。例1和例2的区别在于,多加了一期数据,导致回归方程发生改变,从而预测值不同。
- func GetChartPredictEdbInfoDataListByRuleNLinearRegression(edbInfoId int, nValue int, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*EdbInfoSearchData, existMap map[string]float64) (newPredictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64) {
- //var errMsg string
- //defer func() {
- // if errMsg != `` {
- // go alarm_msg.SendAlarmMsg("更新上海的token失败;ERR:"+err.Error(), 3)
- // }
- //}()
- allDataList := make([]*EdbInfoSearchData, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- lenAllData := len(allDataList)
- if lenAllData < nValue || lenAllData <= 0 {
- return
- }
- if nValue <= 1 {
- return
- }
- //获取后面的预测数据
- // 获取线性方程公式的a、b的值
- coordinateData := make([]Coordinate, 0)
- for tmpK := nValue; tmpK > 0; tmpK-- {
- tmpIndex2 := lenAllData - tmpK //上N期的值
- tmpCoordinate := Coordinate{
- X: float64(nValue - tmpK + 1),
- Y: allDataList[tmpIndex2].Value,
- }
- coordinateData = append(coordinateData, tmpCoordinate)
- }
- a, b := getLinearResult(coordinateData)
- //fmt.Println("a:", a, ";======b:", b)
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- for k, currentDate := range dayList {
- tmpK := nValue + k + 1
- aDecimal := decimal.NewFromFloat(a)
- xDecimal := decimal.NewFromInt(int64(tmpK))
- bDecimal := decimal.NewFromFloat(b)
- val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).RoundCeil(4).Float64()
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &EdbInfoSearchData{
- EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
- DataTime: currentDateStr,
- Value: val,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
- // 最大最小值
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- // Series is a container for a series of data
- type Series []Coordinate
- // Coordinate holds the data in a series
- type Coordinate struct {
- X, Y float64
- }
- func getLinearResult(s []Coordinate) (gradient, intercept float64) {
- if len(s) <= 1 {
- return
- }
- // Placeholder for the math to be done
- var sum [5]float64
- // Loop over data keeping index in place
- i := 0
- for ; i < len(s); i++ {
- sum[0] += s[i].X
- sum[1] += s[i].Y
- sum[2] += s[i].X * s[i].X
- sum[3] += s[i].X * s[i].Y
- sum[4] += s[i].Y * s[i].Y
- }
- // Find gradient and intercept
- f := float64(i)
- gradient = (f*sum[3] - sum[0]*sum[1]) / (f*sum[2] - sum[0]*sum[0])
- intercept = (sum[1] / f) - (gradient * sum[0] / f)
- //fmt.Println("gradient:", gradient, ";intercept:", intercept)
- // Create the new regression series
- //for j := 0; j < len(s); j++ {
- // regressions = append(regressions, Coordinate{
- // X: s[j].X,
- // Y: s[j].X*gradient + intercept,
- // })
- //}
- return
- }
|