123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908 |
- package data
- import (
- "encoding/json"
- "errors"
- "eta/eta_mobile/models/data_manage"
- "eta/eta_mobile/utils"
- "fmt"
- "github.com/nosixtools/solarlunar"
- "github.com/shopspring/decimal"
- "math"
- "strings"
- "time"
- )
- type RuleParams struct {
- EdbInfoId int
- DayList []time.Time
- PredictEdbInfoData []*data_manage.EdbDataList
- RealPredictEdbInfoData []*data_manage.EdbDataList
- ExistMap map[string]float64
- Value string
- }
- type RuleCalculate interface {
- Calculate(params RuleParams) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error)
- }
- // GetChartPredictEdbInfoDataListByRule1 根据规则1获取预测数据
- func GetChartPredictEdbInfoDataListByRule1(edbInfoId int, dataValue float64, dayList []time.Time, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList) {
- newPredictEdbInfoData = predictEdbInfoData
- //获取后面的预测数据
- predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
- for k, v := range dayList {
- newPredictEdbInfoData = append(newPredictEdbInfoData, &data_manage.EdbDataList{
- EdbDataId: edbInfoId + 100000 + k,
- EdbInfoId: edbInfoId,
- DataTime: v.Format(utils.FormatDate),
- Value: dataValue,
- DataTimestamp: v.UnixNano() / 1e6,
- })
- 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, dayList []time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64) {
- allDataList := make([]*data_manage.EdbDataList, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- index := len(allDataList)
- //获取后面的预测数据
- predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
- for k, currentDate := range dayList {
- tmpData := &data_manage.EdbDataList{
- EdbDataId: edbInfoId + 100000 + index + k,
- EdbInfoId: edbInfoId,
- DataTime: currentDate.Format(utils.FormatDate),
- //Value: dataValue,
- DataTimestamp: currentDate.UnixNano() / 1e6,
- }
- 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 = TbzDivMul(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 = TbzDivMul(preValue, tbValue)
- calculateStatus = true
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
- val = TbzDivMul(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 = TbzDivMul(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 = TbzDivMul(preValue, tbValue)
- calculateStatus = true
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
- val = TbzDivMul(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
- }
- // TbzDivMul 同比值计算
- // @params a float64 去年同期值
- // @params b float64 固定同比增速
- func TbzDivMul(a, b float64) (result float64) {
- // 去年同期值
- af := decimal.NewFromFloat(a)
- // 同比增速
- bf := decimal.NewFromFloat(b)
- // 默认1
- cf := decimal.NewFromFloat(1)
- // 总增速
- val := bf.Add(cf)
- // 计算
- result, _ = val.Mul(af).RoundCeil(4).Float64()
- 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, dayList []time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64) {
- allDataList := make([]*data_manage.EdbDataList, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- index := len(allDataList)
- //获取后面的预测数据
- predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
- for k, currentDate := range dayList {
- tmpData := &data_manage.EdbDataList{
- EdbDataId: edbInfoId + 100000 + index + k,
- EdbInfoId: edbInfoId,
- DataTime: currentDate.Format(utils.FormatDate),
- //Value: dataValue,
- DataTimestamp: currentDate.UnixNano() / 1e6,
- }
- 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 = TczDiv(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 = TczDiv(preValue, tcValue)
- calculateStatus = true
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
- val = TczDiv(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 = TczDiv(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 = TczDiv(preValue, tcValue)
- calculateStatus = true
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
- val = TczDiv(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
- }
- // TczDiv 环差值计算
- // @params a float64 上一期值
- // @params b float64 固定的环比增加值
- func TczDiv(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, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64) {
- allDataList := make([]*data_manage.EdbDataList, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- index := len(allDataList)
- //获取后面的预测数据
- for k, currentDate := range dayList {
- tmpK := index + k - 1 //上1期的值
- // 环比值计算
- val := HbzDiv(allDataList[tmpK].Value, hbValue)
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &data_manage.EdbDataList{
- EdbDataId: edbInfoId + 100000 + index + k,
- EdbInfoId: edbInfoId,
- DataTime: currentDateStr,
- Value: val,
- DataTimestamp: currentDate.UnixNano() / 1e6,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
- // 最大最小值
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- // HbzDiv 环比值计算
- // @params a float64 上一期值
- // @params b float64 固定的环比增速
- func HbzDiv(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, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64) {
- allDataList := make([]*data_manage.EdbDataList, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- index := len(allDataList)
- //获取后面的预测数据
- for k, currentDate := range dayList {
- tmpK := index + k - 1 //上1期的值
- // 环差别值计算
- val := HczDiv(allDataList[tmpK].Value, hcValue)
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &data_manage.EdbDataList{
- EdbDataId: edbInfoId + 100000 + index + k,
- EdbInfoId: edbInfoId,
- DataTime: currentDateStr,
- Value: val,
- DataTimestamp: currentDate.UnixNano() / 1e6,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
- // 最大最小值
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- // HczDiv 环差值计算
- // @params a float64 上一期值
- // @params b float64 固定的环比增加值
- func HczDiv(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, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64) {
- allDataList := make([]*data_manage.EdbDataList, 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))
- //获取后面的预测数据
- 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 := &data_manage.EdbDataList{
- EdbDataId: edbInfoId + 100000 + lenAllData + k,
- EdbInfoId: edbInfoId,
- DataTime: currentDateStr,
- Value: val,
- DataTimestamp: currentDate.UnixNano() / 1e6,
- }
- 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, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error) {
- allDataList := make([]*data_manage.EdbDataList, 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([]utils.Coordinate, 0)
- for tmpK := nValue; tmpK > 0; tmpK-- {
- tmpIndex2 := lenAllData - tmpK //上N期的值
- tmpCoordinate := utils.Coordinate{
- X: float64(nValue - tmpK + 1),
- Y: allDataList[tmpIndex2].Value,
- }
- coordinateData = append(coordinateData, tmpCoordinate)
- }
- a, b := utils.GetLinearResult(coordinateData)
- if math.IsNaN(a) || math.IsNaN(b) {
- err = errors.New("线性方程公式生成失败")
- return
- }
- //fmt.Println("a:", a, ";======b:", b)
- 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 := &data_manage.EdbDataList{
- EdbDataId: edbInfoId + 100000 + lenAllData + k,
- EdbInfoId: edbInfoId,
- DataTime: currentDateStr,
- Value: val,
- DataTimestamp: currentDate.UnixNano() / 1e6,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
- // 最大最小值
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- // GetChartPredictEdbInfoDataListByRuleTrendsHC 根据动态环比增加值的计算规则获取预测数据
- //
- // 研究员有对预测指标进行动态环差计算的需求,即预测指标使用环差规则进行预测时,环比增加值不是固定值,而是由几个预测指标计算得出的动态变化的值;
- // 需求说明:
- // 1、增加“动态环差”预测规则;
- // 2、环比增加值在弹窗设置;
- // 3、动态环差预测举例:
- // 指标A实际最新数据为2022-10-27(100);
- // 预测指标B预测数据为2022-10-28(240)、2022-10-29(300);
- // 预测指标C预测数据为2022-10-28(260)、2022-10-29(310);
- // 计算公式为B-C;
- // 则指标A至2022-10-29的预测值为2022-10-28(100+(240-260)=80)、2022-10-29(80+(300-310)=90);
- // 注:动态环比增加值的计算遵从计算指标的计算规则,即用于计算的指标若有部分指标缺少部分日期数据,则这部分日期数据不做计算,为空;若动态环比增加值某一天为空,则往前追溯最近一期有值的环比增加值作为该天的数值参与计算;
- func GetChartPredictEdbInfoDataListByRuleTrendsHC(edbInfoId int, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, hcDataMap, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64) {
- allDataList := make([]*data_manage.EdbDataList, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- lenAllData := len(allDataList)
- if lenAllData <= 0 {
- return
- }
- for k, currentDate := range dayList {
- // 最近一条数据
- tmpLenAllDataList := len(allDataList)
- lastValue := allDataList[tmpLenAllDataList-1].Value
- // 动态环差值数据
- currentDateStr := currentDate.Format(utils.FormatDate)
- hcVal, ok := hcDataMap[currentDateStr]
- if !ok {
- continue
- }
- lastValueDecimal := decimal.NewFromFloat(lastValue)
- hcValDecimal := decimal.NewFromFloat(hcVal)
- val, _ := lastValueDecimal.Add(hcValDecimal).RoundCeil(4).Float64()
- tmpData := &data_manage.EdbDataList{
- EdbDataId: edbInfoId + 100000 + lenAllData + k,
- EdbInfoId: edbInfoId,
- DataTime: currentDateStr,
- Value: val,
- DataTimestamp: currentDate.UnixNano() / 1e6,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
- // 最大最小值
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- // GetChartPredictEdbInfoDataListByRuleFinalValueHc 根据 给定终值后插值 规则获取预测数据
- //
- // 项目背景:
- // 假设螺纹产量在2023年1月1号的预测值是255万吨,从当下到2023年1月1号,螺纹产量将会线性变化,那么每一期的螺纹产量是多少?
- // 算法:从当下(2022/10/28)到2023/1/1号,一共65天,从当前值(305.02)到255,差值-50.02,
- // 则每日环差为-50.02/65=-0.7695。因为数据点是周度频率,每周环差为,-0.3849*7=-5.3868。
- // 从以上计算过程可看出,“给定终值后差值”的算法,是在“环差”算法的基础上,做的一个改动。即这个”环差值”=【(终值-最新值)/终值与最新值得日期差】*数据频率
- // 需求说明:
- // 1、增加一个预测规则,名为“给定终值后插值”,给定预测截止日期和预测终值,计算最新数据日期至预测截止日期的时间差T,计算最新数据和预测终值的数据差S,数据频率与指标频度有关,日度=1,周度=7,旬度=10,月度=30,季度=90,年度=365,环差值=S/T*频率,预测数值=前一天数值+环差值;
- // 2、最新数据值和日期改动后,需重新计算环差值和预测数值;
- func GetChartPredictEdbInfoDataListByRuleFinalValueHc(edbInfoId int, finalValue float64, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64) {
- allDataList := make([]*data_manage.EdbDataList, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- index := len(allDataList)
- //获取后面的预测日期
- lenDay := len(dayList)
- if lenDay <= 0 {
- return
- }
- var hcValue float64
- lastValueDeciamal := decimal.NewFromFloat(allDataList[index-1].Value) // 实际数据的最后一个值
- finalValueDeciamal := decimal.NewFromFloat(finalValue) // 给定的终止数据
- dayDecimal := decimal.NewFromInt(int64(lenDay)) // 需要作为分母的期数
- hcValue, _ = finalValueDeciamal.Sub(lastValueDeciamal).Div(dayDecimal).Float64() // 计算出来的环差值
- //获取后面的预测数据
- predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
- lastK := lenDay - 1 // 最后的日期
- for k, currentDate := range dayList {
- tmpK := index + k - 1 //上1期的值
- var val float64
- // 环差别值计算
- if k == lastK { //如果是最后一天,那么就用最终值,否则就计算
- val = finalValue
- } else {
- val = HczDiv(allDataList[tmpK].Value, hcValue)
- }
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &data_manage.EdbDataList{
- EdbDataId: edbInfoId + 100000 + index + k,
- EdbInfoId: edbInfoId,
- DataTime: currentDateStr,
- Value: val,
- DataTimestamp: currentDate.UnixNano() / 1e6,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
- // 最大最小值
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- // SeasonConf 季节性规则的配置
- type SeasonConf struct {
- Calendar string `description:"公历、农历"`
- YearType int `description:"选择方式,1:连续N年;2:指定年份"`
- NValue int `description:"连续N年"`
- YearList []int `description:"指定年份列表"`
- }
- // GetChartPredictEdbInfoDataListByRuleSeason 根据 季节性 规则获取预测数据
- //
- // ETA预测规则:季节性
- // 已知选定指标A最近更新日期: 2022-12-6 200
- // 设置预测截止日期2023-01-06
- // 1、选择过去N年,N=3
- // 则过去N年为2021、2020、2019
- // 指标A日期 实际值 指标A日期
- // 2019/12/5 150 2019/12/6
- // 2020/12/5 180 2020/12/6
- // 2021/12/5 210 2021/12/6
- // 2019/12/31 200 2020/1/1
- // 2020/12/31 210 2021/1/1
- // 2021/12/31 250 2022/1/1
- //
- // 计算12.7预测值,求过去N年环差均值=[(100-150)+(160-180)+(250-210)]/3=-10
- // 则12.7预测值=12.6值+过去N年环差均值=200-10=190
- // 以此类推...
- //
- // 计算2023.1.2预测值,求过去N年环差均值=[(300-200)+(220-210)+(260-250)]/3=40
- // 则2023.1.2预测值=2023.1.1值+过去N年环差均值
- func GetChartPredictEdbInfoDataListByRuleSeason(edbInfoId int, configValue string, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error) {
- // 获取配置的年份列表
- yearList, seasonConf, err := getYearListBySeasonConf(configValue)
- if err != nil {
- return
- }
- calendar := seasonConf.Calendar
- allDataList := make([]*data_manage.EdbDataList, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- // 插值法数据处理
- handleDataMap := make(map[string]float64)
- err = handleDataByLinearRegression(allDataList, handleDataMap)
- if err != nil {
- return
- }
- // 获取每个年份的日期数据需要平移的天数
- moveDayMap := make(map[int]int, 0) // 每个年份的春节公历
- {
- if calendar == "公历" {
- for _, year := range yearList {
- moveDayMap[year] = 0 //公历就不平移了
- }
- } else {
- currentDay := time.Now()
- if currentDay.Month() >= 11 { //如果大于等于11月份,那么用的是下一年的春节
- currentDay = currentDay.AddDate(1, 0, 0)
- }
- currentYear := currentDay.Year()
- currentYearCjnl := fmt.Sprintf("%d-01-01", currentYear) //当年的春节农历
- currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当年的春节公历
- currentYearCjglTime, tmpErr := time.ParseInLocation(utils.FormatDate, currentYearCjgl, time.Local)
- if tmpErr != nil {
- err = errors.New("当前春节公历日期转换失败:" + tmpErr.Error())
- return
- }
- // 指定的年份
- for _, year := range yearList {
- tmpYearCjnl := fmt.Sprintf("%d-01-01", year) //指定年的春节农历
- tmpYearCjgl := solarlunar.LunarToSolar(tmpYearCjnl, false) //指定年的春节公历
- //moveDayList = append(moveDayList, 0) //公历就不平移了
- tmpYearCjglTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpYearCjgl, time.Local)
- if tmpErr != nil {
- err = errors.New(fmt.Sprintf("%d公历日期转换失败:%s", year, tmpErr.Error()))
- return
- }
- tmpCurrentYearCjglTime := currentYearCjglTime.AddDate(year-currentYear, 0, 0)
- moveDay := utils.GetTimeSubDay(tmpYearCjglTime, tmpCurrentYearCjglTime)
- moveDayMap[year] = moveDay //公历平移
- }
- }
- }
- index := len(allDataList)
- //获取后面的预测数据
- predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
- for k, currentDate := range dayList {
- // 如果遇到闰二月,如2.29,去掉该天数据
- if strings.Contains(currentDate.Format(utils.FormatDate), "02-29") {
- continue
- }
- tmpHistoryVal := decimal.NewFromFloat(0) //往期的差值总和
- tmpHistoryValNum := 0 // 往期差值计算的数量
- tmpLenAllDataList := len(allDataList)
- tmpK := tmpLenAllDataList - 1 //上1期数据的下标
- lastDayData := allDataList[tmpK] // 上1期的数据
- lastDayStr := lastDayData.DataTime
- lastDayVal := lastDayData.Value
- lastDay, tmpErr := time.ParseInLocation(utils.FormatDate, lastDayStr, time.Local)
- if tmpErr != nil {
- err = errors.New("获取上期日期转换失败:" + tmpErr.Error())
- }
- for _, year := range yearList {
- moveDay := moveDayMap[year] //需要移动的天数
- var tmpHistoryCurrentVal, tmpHistoryLastVal float64
- var isFindHistoryCurrent, isFindHistoryLast bool //是否找到前几年的数据
- //前几年当日的日期
- tmpHistoryCurrentDate := currentDate.AddDate(year-currentDate.Year(), 0, -moveDay)
- for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
- tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, i)
- if val, ok := handleDataMap[tmpDate.Format(utils.FormatDate)]; ok {
- tmpHistoryCurrentVal = val
- isFindHistoryCurrent = true
- break
- } else {
- tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, -i)
- if val, ok := handleDataMap[tmpDate.Format(utils.FormatDate)]; ok {
- tmpHistoryCurrentVal = val
- isFindHistoryCurrent = true
- break
- }
- }
- }
- //前几年上一期的日期
- tmpHistoryLastDate := lastDay.AddDate(year-lastDay.Year(), 0, -moveDay)
- for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
- tmpDate := tmpHistoryLastDate.AddDate(0, 0, i)
- if val, ok := handleDataMap[tmpDate.Format(utils.FormatDate)]; ok {
- tmpHistoryLastVal = val
- isFindHistoryLast = true
- break
- } else {
- tmpDate := tmpHistoryLastDate.AddDate(0, 0, -i)
- if val, ok := handleDataMap[tmpDate.Format(utils.FormatDate)]; ok {
- tmpHistoryLastVal = val
- isFindHistoryLast = true
- break
- }
- }
- }
- // 如果两个日期对应的数据都找到了,那么计算两期的差值
- if isFindHistoryCurrent && isFindHistoryLast {
- af := decimal.NewFromFloat(tmpHistoryCurrentVal)
- bf := decimal.NewFromFloat(tmpHistoryLastVal)
- tmpHistoryVal = tmpHistoryVal.Add(af.Sub(bf))
- tmpHistoryValNum++
- }
- }
- //计算的差值与选择的年份数量不一致,那么当前日期不计算
- if tmpHistoryValNum != len(yearList) {
- continue
- }
- lastDayValDec := decimal.NewFromFloat(lastDayVal)
- val, _ := tmpHistoryVal.Div(decimal.NewFromInt(int64(tmpHistoryValNum))).Add(lastDayValDec).RoundCeil(4).Float64()
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &data_manage.EdbDataList{
- EdbDataId: edbInfoId + 100000 + index + k,
- EdbInfoId: edbInfoId,
- DataTime: currentDateStr,
- Value: val,
- DataTimestamp: currentDate.UnixNano() / 1e6,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
- // 继续使用插值法补充新预测日期的数据之间的值
- err = handleDataByLinearRegression([]*data_manage.EdbDataList{
- lastDayData, tmpData,
- }, handleDataMap)
- if err != nil {
- return
- }
- // 最大最小值
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- // MoveAverageConf 移动平均同比规则的配置
- type MoveAverageConf struct {
- Year int `description:"指定年份"`
- NValue int `description:"N期的数据"`
- }
- // GetChartPredictEdbInfoDataListByRuleMoveAverageTb 根据 移动平均同比 规则获取预测数据
- //
- // ETA预测规则:季节性
- // 2、选择指定N年,N=3
- // 指定N年为2012、2015、2018
- // 指标A日期 实际值 指标A日期 实际值
- // 2012/12/5 150 2012/12/6 130
- // 2015/12/5 180 2015/12/6 150
- // 2018/12/5 210 2018/12/6 260
- // 2012/12/31 200 2013/1/1 200
- // 2015/12/31 210 2016/1/1 250
- // 2018/12/31 250 2019/1/1 270
- // 计算12.7预测值,求过去N年环差均值=[(130-150)+(150-180)+(290-210)]/3=10
- // 则12.7预测值=12.6值+过去N年环差均值=200+10=210
- // 以此类推...
- // 计算2023.1.2预测值,求过去N年环差均值=[(200-200)+(250-210)+(270-250)]/3=16.67
- // 则2023.1.2预测值=2023.1.1值+过去N年环差均值
- func GetChartPredictEdbInfoDataListByRuleMoveAverageTb(edbInfoId int, nValue, year int, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error) {
- allDataList := make([]*data_manage.EdbDataList, 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))
- // 需要减去的年份
- subYear := year - dayList[0].Year()
- //获取后面的预测数据
- for k, currentDate := range dayList {
- tmpLenAllDataList := len(allDataList)
- tmpIndex := tmpLenAllDataList - 1 //上1期数据的下标
- averageDateList := make([]string, 0) //计算平均数的日期
- // 数据集合中的最后一个数据
- tmpDecimalVal := decimal.NewFromFloat(allDataList[tmpIndex].Value)
- averageDateList = append(averageDateList, allDataList[tmpIndex].DataTime)
- for tmpK := 1; tmpK < nValue; tmpK++ {
- tmpIndex2 := tmpIndex - tmpK //上N期的值
- tmpDecimalVal2 := decimal.NewFromFloat(allDataList[tmpIndex2].Value)
- tmpDecimalVal = tmpDecimalVal.Add(tmpDecimalVal2)
- averageDateList = append(averageDateList, allDataList[tmpIndex2].DataTime)
- }
- // 最近的N期平均值
- tmpAverageVal := tmpDecimalVal.Div(decimalN)
- var tmpHistoryCurrentVal float64 // 前几年当日的数据值
- var isFindHistoryCurrent, isFindHistoryLast bool //是否找到前几年的数据
- tmpHistoryDecimalVal := decimal.NewFromFloat(0) //前几年N期数据总值
- {
- // 前几年N期汇总期数
- tmpHistoryValNum := 0
- {
- //前几年当日的日期
- tmpHistoryCurrentDate := currentDate.AddDate(subYear, 0, 0)
- for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
- tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, i)
- if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
- tmpHistoryCurrentVal = val
- isFindHistoryCurrent = true
- break
- } else {
- tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, -i)
- if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
- tmpHistoryCurrentVal = val
- isFindHistoryCurrent = true
- break
- }
- }
- }
- }
- for _, averageDate := range averageDateList {
- lastDay, tmpErr := time.ParseInLocation(utils.FormatDate, averageDate, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //前几年上一期的日期
- tmpHistoryLastDate := lastDay.AddDate(subYear, 0, 0)
- for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
- tmpDate := tmpHistoryLastDate.AddDate(0, 0, i)
- if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
- tmpDecimalVal2 := decimal.NewFromFloat(val)
- tmpHistoryDecimalVal = tmpHistoryDecimalVal.Add(tmpDecimalVal2)
- tmpHistoryValNum++
- break
- } else {
- tmpDate := tmpHistoryLastDate.AddDate(0, 0, -i)
- if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
- tmpDecimalVal2 := decimal.NewFromFloat(val)
- tmpHistoryDecimalVal = tmpHistoryDecimalVal.Add(tmpDecimalVal2)
- tmpHistoryValNum++
- break
- }
- }
- }
- }
- // 汇总期数与配置的N期数量一致
- if tmpHistoryValNum == nValue {
- isFindHistoryLast = true
- }
- }
- // 如果没有找到前几年的汇总数据,或者没有找到前几年当日的数据,那么退出当前循环,进入下一循环
- if !isFindHistoryLast || !isFindHistoryCurrent {
- continue
- }
- // 计算最近N期平均值
- tmpHistoryAverageVal := tmpHistoryDecimalVal.Div(decimalN)
- // 计算最近N期同比值
- tbVal := tmpAverageVal.Div(tmpHistoryAverageVal)
- // 预测值结果 = 同比年份同期值(tmpHistoryCurrentVal的值)* 同比值(tbVal的值)
- val, _ := decimal.NewFromFloat(tmpHistoryCurrentVal).Mul(tbVal).RoundCeil(4).Float64()
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &data_manage.EdbDataList{
- EdbDataId: edbInfoId + 100000 + lenAllData + k,
- EdbInfoId: edbInfoId,
- DataTime: currentDateStr,
- Value: val,
- DataTimestamp: currentDate.UnixNano() / 1e6,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
- // 最大最小值
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- // GetChartPredictEdbInfoDataListByRuleTbzscz 根据 同比增速差值 规则获取预测数据
- // 同比增速差值计算方式:
- // 1、首先计算出所选指标实际最新日期值的同比增速:(本期数值-同期数值)÷同期数值*100%
- // 2、根据预测截止日期的同比增速终值、最新日期值的同比增速、与最新日期距离截止日期的期数,计算出到截止日期为止的每一期的同比增速。(等差规则计算每一期的同比增速,结合去年同期值,计算出每一期的同比预测值)。公差=(末项-首项)÷(n-1),an=a1+(n-1)d,(n为正整数,n大于等于2)
- // 3、根据去年同期值和未来每一期的同比增速值,求出同比预测值,同比预测值=同期值*(1+同比增速)
- // 同比增速差值:计算最新数据的同比增速((本期数值-同期数值)÷同期数值*100%),结合同比增速终值与期数,计算每一期同比增速,进而求出同比预测值。
- //
- // 例:如上图所示指标,(1)最新日期值2022-12-31 141175 ,结合同期值,计算同比增速;
- // (2)同比增速终值,若为50%, 预测日期为2023-03-31,则根据(1)中的同比增速值与同比增速终值,计算出中间两期的同比增速;
- // (3)求出每一期的预测同比值,预测同比值=同期值*(1+同比增速)
- func GetChartPredictEdbInfoDataListByRuleTbzscz(edbInfoId int, tbEndValue float64, dayList []time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64) {
- allDataList := make([]*data_manage.EdbDataList, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- index := len(allDataList)
- // 获取近期数据的同比值
- if index <= 0 {
- return
- }
- lastData := allDataList[index-1]
- lastDayTime, _ := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
- var lastTb decimal.Decimal // 计算最新数据与上一期的数据同比值
- {
- //上一年的日期
- preDate := lastDayTime.AddDate(-1, 0, 0)
- preDateStr := preDate.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateStr]; ok { //上一年同期找到
- lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
- } 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 { //上一年同期->下一个月找到
- lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
- lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
- break
- }
- }
- nextDateDay = nextDateDay.AddDate(0, 0, 1)
- preDateDay = preDateDay.AddDate(0, 0, -1)
- }
- case "季度", "年度":
- if preValue, ok := existMap[preDateStr]; ok { //上一年同期->下一个月找到
- lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
- break
- }
- default:
- nextDateDay := preDate
- preDateDay := preDate
- for i := 0; i < 35; i++ {
- nextDateDayStr := nextDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
- lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
- lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
- break
- } else {
- //fmt.Println("pre not find:", preDateStr, "i:", i)
- }
- }
- nextDateDay = nextDateDay.AddDate(0, 0, 1)
- preDateDay = preDateDay.AddDate(0, 0, -1)
- }
- }
- }
- }
- //获取后面的预测数据
- lenDay := len(dayList)
- tbEndValueDecimal := decimal.NewFromFloat(tbEndValue)
- avgTbVal := tbEndValueDecimal.Sub(lastTb).Div(decimal.NewFromInt(int64(lenDay)))
- predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
- for k, currentDate := range dayList {
- var tbValue decimal.Decimal
- if k == lenDay-1 { // 如果是最后的日期了,那么就用终值去计算
- tbValue = tbEndValueDecimal.Add(decimal.NewFromInt(1))
- } else { // 最近数据的同比值 + (平均增值乘以当前期数)
- tbValue = lastTb.Add(avgTbVal.Mul(decimal.NewFromInt(int64(k + 1)))).Add(decimal.NewFromInt(1))
- }
- tmpData := &data_manage.EdbDataList{
- EdbDataId: edbInfoId + 100000 + index + k,
- EdbInfoId: edbInfoId,
- DataTime: currentDate.Format(utils.FormatDate),
- //Value: dataValue,
- DataTimestamp: currentDate.UnixNano() / 1e6,
- }
- 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, _ = decimal.NewFromFloat(preValue).Mul(tbValue).RoundCeil(4).Float64()
- 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, _ = decimal.NewFromFloat(preValue).Mul(tbValue).RoundCeil(4).Float64()
- calculateStatus = true
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
- val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).RoundCeil(4).Float64()
- calculateStatus = true
- break
- }
- }
- nextDateDay = nextDateDay.AddDate(0, 0, 1)
- preDateDay = preDateDay.AddDate(0, 0, -1)
- }
- case "季度", "年度":
- if preValue, ok := existMap[preDateStr]; ok { //上一年同期->下一个月找到
- val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).RoundCeil(4).Float64()
- 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, _ = decimal.NewFromFloat(preValue).Mul(tbValue).RoundCeil(4).Float64()
- calculateStatus = true
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok { //上一年同期->上一个月找到
- val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).RoundCeil(4).Float64()
- 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
- }
- // RuleLineNhConf 一元线性拟合规则的配置
- type RuleLineNhConf struct {
- StartDate string `description:"开始日期"`
- EndDate string `description:"结束日期"`
- MoveDay int `description:"移动天数"`
- EdbInfoId int `description:"指标id"`
- }
- // GetChartPredictEdbInfoDataListByRuleLineNh 根据 一元线性拟合 的计算规则获取预测数据
- //
- // 选择被预测的指标B(作为自变量,非预测指标),选择指标A(作为因变量,可以是基础指标和预测指标)
- // 2、选择拟合时间段,起始日期至今或指定时间段,选择至今,在计算时截止到指标B的最新日期
- // 3、设定A领先B时间(天),正整数、负整数、0
- // 4、调用拟合残差的数据预处理和算法,给出拟合方程Y=aX+b的系数a,b
- // 5、指标A代入拟合方程得到拟合预测指标B',拟合预测指标使用指标B的频度,在指标B的实际值后面连接拟合预测指标B'对应日期的预测值
- //
- // 注:选择预测截止日期,若所选日期 ≤ 指标A设置领先后的日期序列,则预测指标日期最新日期有值(在指标B'的有值范围内);若所选日期 > 指标A设置领先后的日期序列,则预测指标只到指标A领先后的日期序列(超出指标B'的有值范围,最多到指标B'的最新值);指标A、B更新后,更新预测指标
- func GetChartPredictEdbInfoDataListByRuleLineNh(edbInfoId int, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, newNhccDataMap, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error) {
- allDataList := make([]*data_manage.EdbDataList, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- lenAllData := len(allDataList)
- if lenAllData <= 0 {
- return
- }
- for k, currentDate := range dayList {
- // 动态拟合残差值数据
- currentDateStr := currentDate.Format(utils.FormatDate)
- val, ok := newNhccDataMap[currentDateStr]
- if !ok {
- continue
- }
- tmpData := &data_manage.EdbDataList{
- EdbDataId: edbInfoId + 100000 + lenAllData + k,
- EdbInfoId: edbInfoId,
- DataTime: currentDateStr,
- Value: val,
- DataTimestamp: currentDate.UnixNano() / 1e6,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
- // 最大最小值
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- // getCalculateNhccData 获取计算出来的 拟合残差 数据
- func getCalculateNhccData(secondDataList []*data_manage.EdbDataList, ruleConf RuleLineNhConf) (newBDataMap map[string]float64, err error) {
- firstEdbInfoId := ruleConf.EdbInfoId
- moveDay := ruleConf.MoveDay
- startDate, _ := time.ParseInLocation(utils.FormatDate, ruleConf.StartDate, time.Local)
- endDate, _ := time.ParseInLocation(utils.FormatDate, ruleConf.EndDate, time.Local)
- //查询当前指标现有的数据
- edbInfo, err := data_manage.GetEdbInfoById(firstEdbInfoId)
- if err != nil {
- return
- }
- //第一个指标
- aDataList := make([]data_manage.EdbDataList, 0)
- aDataMap := make(map[string]float64)
- {
- //第一个指标的数据列表
- var firstDataList []*data_manage.EdbDataList
- switch edbInfo.EdbInfoType {
- case 0:
- firstDataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
- case 1:
- _, firstDataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
- default:
- err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
- }
- if err != nil {
- return
- }
- aDataList, aDataMap = handleNhccData(firstDataList, moveDay)
- }
- //第二个指标
- bDataList := make([]data_manage.EdbDataList, 0)
- bDataMap := make(map[string]float64)
- {
- bDataList, bDataMap = handleNhccData(secondDataList, 0)
- }
- if len(aDataList) <= 0 {
- err = errors.New("指标A没有数据")
- return
- }
- if len(bDataList) <= 0 {
- err = errors.New("指标B没有数据")
- return
- }
- // 拟合残差计算的结束日期判断
- {
- endAData := aDataList[len(aDataList)-1]
- tmpEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, endAData.DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- // 如果A指标的最新数据日期早于拟合残差的结束日期,那么就用A指标的最新数据日期
- if tmpEndDate.Before(endDate) {
- endDate = tmpEndDate
- }
- endBData := bDataList[len(bDataList)-1]
- tmpEndDate, tmpErr = time.ParseInLocation(utils.FormatDate, endBData.DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- // 如果B指标的最新数据日期早于拟合残差的结束日期,那么就用A指标的最新数据日期
- if tmpEndDate.Before(endDate) {
- endDate = tmpEndDate
- }
- }
- // 计算线性方程公式
- var a, b float64
- {
- coordinateData := make([]utils.Coordinate, 0)
- for i := startDate; i.Before(endDate) || i.Equal(endDate); i = i.AddDate(0, 0, 1) {
- dateStr := i.Format(utils.FormatDate)
- xValue, ok := aDataMap[dateStr]
- if !ok {
- err = errors.New("指标A日期:" + dateStr + "数据异常,导致计算线性方程公式失败")
- return
- }
- yValue, ok := bDataMap[dateStr]
- if !ok {
- err = errors.New("指标B日期:" + dateStr + "数据异常,导致计算线性方程公式失败")
- return
- }
- tmpCoordinate := utils.Coordinate{
- X: xValue,
- Y: yValue,
- }
- coordinateData = append(coordinateData, tmpCoordinate)
- }
- a, b = utils.GetLinearResult(coordinateData)
- }
- if math.IsNaN(a) || math.IsNaN(b) {
- err = errors.New("线性方程公式生成失败")
- return
- }
- //fmt.Println("a:", a, ";======b:", b)
- //计算B’
- newBDataMap = make(map[string]float64)
- {
- //B’=aA+b
- aDecimal := decimal.NewFromFloat(a)
- bDecimal := decimal.NewFromFloat(b)
- for _, aData := range aDataList {
- xDecimal := decimal.NewFromFloat(aData.Value)
- val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).RoundCeil(4).Float64()
- newBDataMap[aData.DataTime] = val
- }
- }
- return
- }
- // handleNhccData 处理拟合残差需要的数据
- func handleNhccData(dataList []*data_manage.EdbDataList, moveDay int) (newDataList []data_manage.EdbDataList, dateDataMap map[string]float64) {
- dateMap := make(map[time.Time]float64)
- var minDate, maxDate time.Time
- dateDataMap = make(map[string]float64)
- for _, v := range dataList {
- currDate, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- if minDate.IsZero() || currDate.Before(minDate) {
- minDate = currDate
- }
- if maxDate.IsZero() || currDate.After(maxDate) {
- maxDate = currDate
- }
- dateMap[currDate] = v.Value
- }
- // 处理领先、滞后数据
- newDateMap := make(map[time.Time]float64)
- for currDate, value := range dateMap {
- newDate := currDate.AddDate(0, 0, moveDay)
- newDateMap[newDate] = value
- }
- minDate = minDate.AddDate(0, 0, moveDay)
- maxDate = maxDate.AddDate(0, 0, moveDay)
- // 开始平移天数
- dayNum := utils.GetTimeSubDay(minDate, maxDate)
- for i := 0; i <= dayNum; i++ {
- currDate := minDate.AddDate(0, 0, i)
- tmpValue, ok := newDateMap[currDate]
- if !ok {
- // 万一没有数据,那么就过滤当次循环
- if len(newDataList) <= 0 {
- continue
- }
- //找不到数据,那么就用前面的数据吧
- tmpValue = newDataList[len(newDataList)-1].Value
- }
- tmpData := data_manage.EdbDataList{
- //EdbDataId: 0,
- DataTime: currDate.Format(utils.FormatDate),
- Value: tmpValue,
- }
- dateDataMap[tmpData.DataTime] = tmpData.Value
- newDataList = append(newDataList, tmpData)
- }
- return
- }
- // GetChartPredictEdbInfoDataListByRuleNAnnualAverage 根据 N年均值 规则获取预测数据
- // ETA预测规则:N年均值:过去N年同期均值。过去N年可以连续或者不连续,指标数据均用线性插值补全为日度数据后计算;
- func GetChartPredictEdbInfoDataListByRuleNAnnualAverage(edbInfoId int, configValue string, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error) {
- // 获取配置的年份列表
- yearList, _, err := getYearListBySeasonConf(configValue)
- if err != nil {
- return
- }
- allDataList := make([]*data_manage.EdbDataList, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- // 插值法数据处理
- handleDataMap := make(map[string]float64)
- err = handleDataByLinearRegression(allDataList, handleDataMap)
- if err != nil {
- return
- }
- index := len(allDataList)
- //获取后面的预测数据
- predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
- for k, currentDate := range dayList {
- // 如果遇到闰二月,如2.29,去掉该天数据
- if strings.Contains(currentDate.Format(utils.FormatDate), "02-29") {
- continue
- }
- tmpK := len(allDataList) - 1 //上1期数据的下标
- lastDayData := allDataList[tmpK] // 上1期的数据
- tmpHistoryVal := decimal.NewFromFloat(0) //往期的差值总和
- tmpHistoryValNum := 0 // 往期差值计算的数量
- for _, year := range yearList {
- //前几年当日的日期
- tmpHistoryCurrentDate := currentDate.AddDate(year-currentDate.Year(), 0, 0)
- if val, ok := handleDataMap[tmpHistoryCurrentDate.Format(utils.FormatDate)]; ok {
- tmpHistoryVal = tmpHistoryVal.Add(decimal.NewFromFloat(val))
- tmpHistoryValNum++
- }
- }
- //计算的差值与选择的年份数量不一致,那么当前日期不计算
- if tmpHistoryValNum != len(yearList) {
- continue
- }
- val, _ := tmpHistoryVal.Div(decimal.NewFromInt(int64(tmpHistoryValNum))).Round(4).Float64()
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &data_manage.EdbDataList{
- EdbDataId: edbInfoId + 100000 + index + k,
- EdbInfoId: edbInfoId,
- DataTime: currentDateStr,
- Value: val,
- DataTimestamp: currentDate.UnixNano() / 1e6,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
- // 继续使用插值法补充新预测日期的数据之间的值
- err = handleDataByLinearRegression([]*data_manage.EdbDataList{
- lastDayData, tmpData,
- }, handleDataMap)
- if err != nil {
- return
- }
- // 最大最小值
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- // AnnualValueInversionConf 年度值倒推规则
- type AnnualValueInversionConf struct {
- Value float64 `description:"年度值"`
- Type int `description:"分配方式,1:均值法;2:同比法"`
- Year int `description:"同比年份"`
- YearList []int `description:"指定年份列表"`
- }
- // GetChartPredictEdbInfoDataListByRuleAnnualValueInversion 根据 年度值倒推 规则获取预测数据
- // 预测指标-年度值倒推
- // 1、年度值倒推,选择同比法,支持选择多个年份(当前只可选择一个年份)。选择多个年份时,计算多个年份的余额平均,和同期平均。
- // 2、年度值倒推,同比法的算法优化:旬度,月度,季度,半年度的算法,同原先算法。
- // 日度、周度值算法更新(假设指标实际值最新日期月2024/3/1):
- // 1、设定年度值
- // 2、计算余额:年度值-年初至今累计值
- // 3、年初至今累计值计算方法:用后置填充变频成连续自然日日度数据。计算1/1至指标最新日期(2024/3/3/1)的累计值。
- // 4、计算同比年份全年累计值,年初至指标最新值同期(2023/3/1)累计值,两者相减得到同比年份同期余额,再取平均值,作为最终的同期余额
- // 5、用今年余额/去年同期余额得到同比增速。
- // 6、每一期预测值,为同比年份的同期值,乘以(1+同比)。去年同期,用变频后的序列对应。
- // 7、如果选择的同比年份是多个。则计算多个年份的平均余额。今年余额/平均余额=同比增速。同比基数为多个年份的同期平均值
- func GetChartPredictEdbInfoDataListByRuleAnnualValueInversion(edbInfoId int, configValue string, dayList []time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*data_manage.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error) {
- if frequency == "年度" {
- err = errors.New("当前指标频度是年度,不允许配置年度值倒推")
- return
- }
- // 获取配置
- var annualValueInversionConf AnnualValueInversionConf
- err = json.Unmarshal([]byte(configValue), &annualValueInversionConf)
- if err != nil {
- err = errors.New("年度值倒推配置信息异常:" + err.Error())
- return
- }
- allDataList := make([]*data_manage.EdbDataList, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- index := len(allDataList)
- // 没有数据,直接返回
- if index <= 0 {
- return
- }
- // 配置的年度值
- yearValueConfig := annualValueInversionConf.Value
- // 最新数据的日期
- currDayTime, err := time.ParseInLocation(utils.FormatDate, allDataList[index-1].DataTime, time.Local)
- if err != nil {
- return
- }
- // 当前年的日期
- lastDayTime := dayList[len(dayList)-1]
- if currDayTime.Year() != lastDayTime.Year() {
- err = errors.New("年度值倒推不支持跨年预测")
- return
- }
- // 均值法
- if annualValueInversionConf.Type == 1 {
- // 当前年的期数
- currYearN := 0
- // 当前已经消耗的额度
- var currYearVal float64
- // 计算当前年的期数以及已经消耗的额度
- {
- if frequency != "周度" {
- for _, v := range allDataList {
- currTime, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- // 只是计算今年的
- if currTime.Year() != currDayTime.Year() {
- continue
- }
- currYearN++
- currYearVal = currYearVal + v.Value
- }
- } else {
- tmpDataList := make([]*data_manage.EdbDataList, 0)
- // 上一期的数据
- var lastData *data_manage.EdbDataList
- // 是否第一条数据
- isFirst := true
- for _, v := range allDataList {
- currTime, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- // 只是计算今年的
- if currTime.Year() != currDayTime.Year() {
- lastData = v
- continue
- }
- if isFirst {
- tmpDataList = append(tmpDataList, lastData)
- }
- isFirst = false
- tmpDataList = append(tmpDataList, v)
- currYearN++
- }
- // 需要插值法处理
- tmpHandleDataMap := make(map[string]float64)
- err = handleDataByLinearRegression(tmpDataList, tmpHandleDataMap)
- if err != nil {
- return
- }
- for tmpDate, val := range tmpHandleDataMap {
- tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if tmpDateTime.Year() != currDayTime.Year() {
- continue
- }
- currYearVal = currYearVal + val
- }
- currYearVal = currYearVal / 7
- }
- }
- var averageVal float64
- switch frequency {
- case "半年度":
- averageVal, _ = (decimal.NewFromFloat(yearValueConfig).Sub(decimal.NewFromFloat(currYearVal))).Div(decimal.NewFromInt(int64(2 - currYearN))).Float64()
- case "季度":
- averageVal, _ = (decimal.NewFromFloat(yearValueConfig).Sub(decimal.NewFromFloat(currYearVal))).Div(decimal.NewFromInt(int64(4 - currYearN))).Float64()
- case "月度":
- averageVal, _ = (decimal.NewFromFloat(yearValueConfig).Sub(decimal.NewFromFloat(currYearVal))).Div(decimal.NewFromInt(int64(12 - currYearN))).Float64()
- case "旬度":
- averageVal, _ = (decimal.NewFromFloat(yearValueConfig).Sub(decimal.NewFromFloat(currYearVal))).Div(decimal.NewFromInt(int64(36 - currYearN))).Float64()
- case "周度", "日度":
- //剩余期数=剩余自然日历天数/今年指标最新日期自然日历天数*今年至今指标数据期数
- // 当前年的第一天
- yearFirstDay := time.Date(currDayTime.Year(), 1, 1, 0, 0, 0, 0, time.Local)
- subDay := utils.GetTimeSubDay(yearFirstDay, currDayTime) + 1
- // 当前年的最后一天
- yearLastDay := time.Date(currDayTime.Year(), 12, 31, 0, 0, 0, 0, time.Local)
- subDay2 := utils.GetTimeSubDay(yearFirstDay, yearLastDay) + 1
- // 剩余期数
- surplusN := decimal.NewFromInt(int64(subDay2 - subDay)).Div(decimal.NewFromInt(int64(subDay))).Mul(decimal.NewFromInt(int64(currYearN)))
- // 剩余余额
- balance := decimal.NewFromFloat(annualValueInversionConf.Value).Sub(decimal.NewFromFloat(currYearVal))
- averageVal, _ = balance.Div(surplusN).Round(4).Float64()
- }
- // 保留四位小数
- averageVal, _ = decimal.NewFromFloat(averageVal).Round(4).Float64()
- for k, currentDate := range dayList {
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &data_manage.EdbDataList{
- EdbDataId: edbInfoId + 100000 + index + k,
- EdbInfoId: edbInfoId,
- DataTime: currentDateStr,
- Value: averageVal,
- DataTimestamp: currentDate.UnixNano() / 1e6,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = averageVal
- }
- // 最大最小值
- if averageVal < minValue {
- minValue = averageVal
- }
- if averageVal > maxValue {
- maxValue = averageVal
- }
- return
- }
- // 同比法分配
- // 同比法保证每期同比相等(同比增速=余额/同比年份相应日期的余额,预测值等于同比年份同期值*同比增速);
- // 同比法分配:同比增速=900/同比年份5.19的余额
- yearList := annualValueInversionConf.YearList
- if len(yearList) == 0 {
- //兼容历史数据
- yearList = append(yearList, annualValueInversionConf.Year)
- }
- // 每年截止到当前日期的累计值
- dateTotalMap := make(map[time.Time]float64)
- //把每一期的期数和日期绑定
- dateIndexMap := make(map[time.Time]int)
- indexDateMap := make(map[int]time.Time)
- // 每年的累计值(计算使用)
- yearTotalMap := make(map[int]float64)
- //数据按找后值填充的方式处理成连续自然日日度数据
- allDataListMap := make(map[string]float64)
- // todo 如果是日度和周度,用后置填充变频成连续自然日日度数据。计算1/1至指标最新日期(2024/3/3/1)的累计值
- switch frequency {
- case "日度", "周度":
- for _, v := range allDataList {
- allDataListMap[v.DataTime] = v.Value
- }
- //找到最早日期的的年份的1月1日,转成time格式
- earliestYear := allDataList[0].DataTime[:4]
- earliestYearFirstDay, _ := time.ParseInLocation(utils.FormatDate, earliestYear+"-01-01", time.Local)
- days := int(currDayTime.Sub(earliestYearFirstDay).Hours() / float64(24))
- //循环累加日期,直到循环到最新日期
- for i := 0; i <= days; i++ {
- currentDate := earliestYearFirstDay.AddDate(0, 0, i)
- currentDateStr := currentDate.Format(utils.FormatDate)
- val, ok := allDataListMap[currentDateStr]
- if !ok { //如果不存在,则填充后值
- //循环向后查找数据,直到找到
- for j := i + 1; j <= days; j++ {
- //循环往后取值
- currentDateTmp := earliestYearFirstDay.AddDate(0, 0, j)
- currentDateTmpStr := currentDateTmp.Format(utils.FormatDate)
- if tmpVal, ok1 := allDataListMap[currentDateTmpStr]; ok1 {
- allDataListMap[currentDateStr] = tmpVal
- val = tmpVal
- break
- }
- }
- }
- //计算每一天的年初至今累计值
- yearVal := yearTotalMap[currentDate.Year()]
- if frequency == "周度" {
- // 每日累计值需要当前值除7
- yearVal = yearVal + val/7
- } else {
- yearVal = yearVal + val
- }
- yearTotalMap[currentDate.Year()] = yearVal
- dateTotalMap[currentDate] = yearVal
- dateIndexMap[currentDate] = i
- indexDateMap[i] = currentDate
- }
- default:
- for k, v := range allDataList {
- currTime, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- allDataListMap[v.DataTime] = v.Value
- yearVal := yearTotalMap[currTime.Year()]
- yearVal = yearVal + v.Value
- yearTotalMap[currTime.Year()] = yearVal
- dateTotalMap[currTime] = yearVal
- dateIndexMap[currTime] = k
- indexDateMap[k] = currTime
- }
- }
- // 当年的余额
- currYearBalance := yearValueConfig - yearTotalMap[currDayTime.Year()]
- //fmt.Printf("当年的余额%.4f=给定额度%.4f-当年累计值%.4f\n", currYearBalance, yearValueConfig, yearTotalMap[currDayTime.Year()])
- // 循环统计同比年份同期余额
- var sum, avg float64
- for _, year := range yearList {
- yearTotal := yearTotalMap[year]
- //fmt.Printf("同比年份的累计值%.4f\n", yearTotal)
- tmpDate := time.Date(year, currDayTime.Month(), currDayTime.Day(), 0, 0, 0, 0, currDayTime.Location())
- //fmt.Printf("同比年份的同期%s\n", tmpDate)
- dateTotal, ok := dateTotalMap[tmpDate]
- //fmt.Printf("同比年份的同期累计值%.4f\n", dateTotal)
- if ok {
- sum = sum + (yearTotal - dateTotal)
- } else {
- // 查找下一期的余额
- tmpIndex, ok1 := dateIndexMap[tmpDate]
- if ok1 {
- for tmpDateTime := indexDateMap[tmpIndex+1]; tmpDateTime.Year() == year; tmpDateTime = indexDateMap[tmpIndex+1] {
- dateTotal, ok = dateTotalMap[tmpDateTime]
- if ok {
- //fmt.Printf("同比年份的同期累计值%.4f\n", dateTotal)
- sum = sum + (yearTotal - dateTotal)
- break
- }
- tmpIndex += 1
- }
- }
- }
- }
- //fmt.Printf("同比年份的余额%.4f\n", sum)
- avg = sum / float64(len(yearList))
- //fmt.Printf("同比年份的余额%.4f\n", avg)
- // 同比增速=当年余额/同比年份上一期日期的余额
- tbVal := decimal.NewFromFloat(currYearBalance).Div(decimal.NewFromFloat(avg))
- /*tbVal11, _ := tbVal.Round(4).Float64()
- fmt.Printf("同比增速%.4f\n", tbVal11)*/
- //(同比增速=余额/同比年份相应日期的余额的平均值,预测值等于同比年份同期值*同比增速);
- for k, currentDate := range dayList {
- // 循环遍历多个同比年份
- var valSum float64
- for _, year := range yearList {
- //多个同比年份的同期值的平均值
- tmpCurrentDate := time.Date(year, currentDate.Month(), currentDate.Day(), 0, 0, 0, 0, currentDate.Location())
- if tmpVal, ok := allDataListMap[tmpCurrentDate.Format(utils.FormatDate)]; ok {
- valSum += tmpVal
- } else {
- // 查找下一期的余额
- tmpIndex, ok1 := dateIndexMap[tmpCurrentDate]
- if ok1 {
- for tmpDateTime := indexDateMap[tmpIndex+1]; tmpDateTime.Year() == year; tmpDateTime = indexDateMap[tmpIndex+1] {
- tmpVal, ok = allDataListMap[tmpDateTime.Format(utils.FormatDate)]
- if ok {
- valSum += tmpVal
- break
- }
- tmpIndex += 1
- }
- }
- }
- }
- lastDateVal := valSum / float64(len(yearList))
- //预测值 = 同比年份同期值*同比增速
- tmpVal, _ := decimal.NewFromFloat(lastDateVal).Mul(tbVal).Round(4).Float64()
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &data_manage.EdbDataList{
- EdbDataId: edbInfoId + 100000 + index + k,
- EdbInfoId: edbInfoId,
- DataTime: currentDateStr,
- Value: tmpVal,
- DataTimestamp: currentDate.UnixNano() / 1e6,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = tmpVal
- yearVal := yearTotalMap[currentDate.Year()]
- yearVal = yearVal + tmpVal
- yearTotalMap[currentDate.Year()] = yearVal
- dateTotalMap[currentDate] = yearVal
- // 最大最小值
- if tmpVal < minValue {
- minValue = tmpVal
- }
- if tmpVal > maxValue {
- maxValue = tmpVal
- }
- }
- return
- }
- // getYearListBySeasonConf 根据配置获取年份列表
- func getYearListBySeasonConf(configValue string) (yearList []int, seasonConf SeasonConf, err error) {
- tmpErr := json.Unmarshal([]byte(configValue), &seasonConf)
- if tmpErr != nil {
- err = errors.New("年份配置信息异常:" + tmpErr.Error())
- return
- }
- //选择方式,1:连续N年;2:指定年份
- if seasonConf.YearType == 1 {
- if seasonConf.NValue < 1 {
- err = errors.New("连续N年不允许小于1")
- return
- }
- currYear := time.Now().Year()
- for i := 0; i < seasonConf.NValue; i++ {
- yearList = append(yearList, currYear-i-1)
- }
- } else {
- yearList = seasonConf.YearList
- }
- return
- }
|