2017-02-05 02:40:27 +04:00
|
|
|
import numpy as np
|
|
|
|
from pyFTS.common import FuzzySet,FLR
|
|
|
|
from pyFTS import fts, sfts
|
|
|
|
|
2017-02-08 19:23:41 +04:00
|
|
|
|
2017-02-05 02:40:27 +04:00
|
|
|
class MultiSeasonalFTS(sfts.SeasonalFTS):
|
2017-05-02 18:32:03 +04:00
|
|
|
"""
|
|
|
|
Multi-Seasonal Fuzzy Time Series
|
|
|
|
"""
|
2017-07-04 01:39:10 +04:00
|
|
|
def __init__(self, name, indexer, **kwargs):
|
2017-02-05 02:40:27 +04:00
|
|
|
super(MultiSeasonalFTS, self).__init__("MSFTS")
|
|
|
|
self.name = "Multi Seasonal FTS"
|
2017-02-09 17:04:48 +04:00
|
|
|
self.shortname = "MSFTS " + name
|
2017-02-05 02:40:27 +04:00
|
|
|
self.detail = ""
|
|
|
|
self.seasonality = 1
|
|
|
|
self.hasSeasonality = True
|
|
|
|
self.hasPointForecasting = True
|
|
|
|
self.isHighOrder = True
|
|
|
|
self.isMultivariate = True
|
|
|
|
self.indexer = indexer
|
|
|
|
self.flrgs = {}
|
|
|
|
|
|
|
|
def generateFLRG(self, flrs):
|
|
|
|
flrgs = {}
|
|
|
|
|
2017-02-08 19:23:41 +04:00
|
|
|
for flr in flrs:
|
2017-02-05 02:40:27 +04:00
|
|
|
|
2017-02-08 19:23:41 +04:00
|
|
|
if str(flr.index) not in self.flrgs:
|
|
|
|
flrgs[str(flr.index)] = sfts.SeasonalFLRG(flr.index)
|
2017-02-05 02:40:27 +04:00
|
|
|
|
2017-02-08 19:23:41 +04:00
|
|
|
flrgs[str(flr.index)].append(flr.RHS)
|
2017-02-05 02:40:27 +04:00
|
|
|
|
|
|
|
return (flrgs)
|
|
|
|
|
|
|
|
def train(self, data, sets, order=1, parameters=None):
|
|
|
|
self.sets = sets
|
|
|
|
self.seasonality = parameters
|
2017-02-08 19:23:41 +04:00
|
|
|
#ndata = self.indexer.set_data(data,self.doTransformations(self.indexer.get_data(data)))
|
|
|
|
flrs = FLR.generateIndexedFLRs(self.sets, self.indexer, data)
|
2017-02-05 02:40:27 +04:00
|
|
|
self.flrgs = self.generateFLRG(flrs)
|
|
|
|
|
2017-04-15 02:57:39 +04:00
|
|
|
def forecast(self, data, **kwargs):
|
2017-02-05 02:40:27 +04:00
|
|
|
|
2017-02-08 19:23:41 +04:00
|
|
|
ret = []
|
2017-02-05 02:40:27 +04:00
|
|
|
|
2017-02-08 19:23:41 +04:00
|
|
|
index = self.indexer.get_season_of_data(data)
|
|
|
|
ndata = self.indexer.get_data(data)
|
2017-02-05 02:40:27 +04:00
|
|
|
|
2017-02-08 19:23:41 +04:00
|
|
|
for k in np.arange(1, len(data)):
|
2017-02-05 02:40:27 +04:00
|
|
|
|
2017-02-08 19:23:41 +04:00
|
|
|
flrg = self.flrgs[str(index[k])]
|
2017-02-05 02:40:27 +04:00
|
|
|
|
2017-02-08 19:23:41 +04:00
|
|
|
mp = self.getMidpoints(flrg)
|
2017-02-05 02:40:27 +04:00
|
|
|
|
2017-02-08 19:23:41 +04:00
|
|
|
ret.append(sum(mp) / len(mp))
|
|
|
|
|
|
|
|
ret = self.doInverseTransformations(ret, params=[ndata[self.order - 1:]])
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
2017-04-15 02:57:39 +04:00
|
|
|
def forecastAhead(self, data, steps, **kwargs):
|
2017-02-08 19:23:41 +04:00
|
|
|
ret = []
|
|
|
|
for i in steps:
|
|
|
|
flrg = self.flrgs[str(i)]
|
2017-02-05 02:40:27 +04:00
|
|
|
|
|
|
|
mp = self.getMidpoints(flrg)
|
|
|
|
|
|
|
|
ret.append(sum(mp) / len(mp))
|
|
|
|
|
2017-02-08 19:23:41 +04:00
|
|
|
ret = self.doInverseTransformations(ret, params=data)
|
2017-02-05 02:40:27 +04:00
|
|
|
|
|
|
|
return ret
|