2017-02-09 17:04:48 +04:00
|
|
|
import numpy as np
|
|
|
|
from pyFTS.common import FuzzySet,FLR
|
|
|
|
from pyFTS import fts, sfts, chen
|
|
|
|
|
|
|
|
|
|
|
|
class ContextualSeasonalFLRG(object):
|
2017-05-02 18:32:03 +04:00
|
|
|
"""
|
|
|
|
Contextual Seasonal Fuzzy Logical Relationship Group
|
|
|
|
"""
|
2017-02-09 17:04:48 +04:00
|
|
|
def __init__(self, seasonality):
|
|
|
|
self.season = seasonality
|
|
|
|
self.flrgs = {}
|
|
|
|
|
|
|
|
def append(self, flr):
|
|
|
|
if flr.LHS.name in self.flrgs:
|
|
|
|
self.flrgs[flr.LHS.name].append(flr.RHS)
|
|
|
|
else:
|
|
|
|
self.flrgs[flr.LHS.name] = chen.ConventionalFLRG(flr.LHS)
|
|
|
|
self.flrgs[flr.LHS.name].append(flr.RHS)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
tmp = str(self.season) + ": \n "
|
|
|
|
tmp2 = "\t"
|
|
|
|
for r in sorted(self.flrgs):
|
|
|
|
tmp2 += str(self.flrgs[r]) + "\n\t"
|
|
|
|
return tmp + tmp2 + "\n"
|
|
|
|
|
|
|
|
|
|
|
|
class ContextualMultiSeasonalFTS(sfts.SeasonalFTS):
|
2017-05-02 18:32:03 +04:00
|
|
|
"""
|
|
|
|
Contextual Multi-Seasonal Fuzzy Time Series
|
|
|
|
"""
|
|
|
|
def __init__(self, order, name, indexer, **kwargs):
|
2017-02-09 17:04:48 +04:00
|
|
|
super(ContextualMultiSeasonalFTS, self).__init__("CMSFTS")
|
|
|
|
self.name = "Contextual Multi Seasonal FTS"
|
|
|
|
self.shortname = "CMSFTS " + name
|
|
|
|
self.detail = ""
|
|
|
|
self.seasonality = 1
|
2017-05-02 18:32:03 +04:00
|
|
|
self.has_seasonality = True
|
|
|
|
self.has_point_forecasting = True
|
|
|
|
self.is_high_order = True
|
|
|
|
self.is_multivariate = True
|
2017-02-09 17:04:48 +04:00
|
|
|
self.indexer = indexer
|
|
|
|
self.flrgs = {}
|
|
|
|
|
|
|
|
def generateFLRG(self, flrs):
|
|
|
|
flrgs = {}
|
|
|
|
|
|
|
|
for flr in flrs:
|
|
|
|
|
2017-02-10 17:09:59 +04:00
|
|
|
if str(flr.index) not in flrgs:
|
2017-02-09 17:04:48 +04:00
|
|
|
flrgs[str(flr.index)] = ContextualSeasonalFLRG(flr.index)
|
|
|
|
|
|
|
|
flrgs[str(flr.index)].append(flr)
|
|
|
|
|
|
|
|
return (flrgs)
|
|
|
|
|
|
|
|
def train(self, data, sets, order=1, parameters=None):
|
|
|
|
self.sets = sets
|
|
|
|
self.seasonality = parameters
|
|
|
|
flrs = FLR.generateIndexedFLRs(self.sets, self.indexer, data)
|
|
|
|
self.flrgs = self.generateFLRG(flrs)
|
|
|
|
|
|
|
|
def getMidpoints(self, flrg, data):
|
2017-02-10 17:09:59 +04:00
|
|
|
if data.name in flrg.flrgs:
|
|
|
|
ret = np.array([s.centroid for s in flrg.flrgs[data.name].RHS])
|
|
|
|
return ret
|
|
|
|
else:
|
|
|
|
return np.array([data.centroid])
|
2017-02-09 17:04:48 +04:00
|
|
|
|
2017-04-15 02:57:39 +04:00
|
|
|
def forecast(self, data, **kwargs):
|
2017-02-09 17:04:48 +04:00
|
|
|
|
|
|
|
ret = []
|
|
|
|
|
|
|
|
index = self.indexer.get_season_of_data(data)
|
|
|
|
ndata = self.indexer.get_data(data)
|
|
|
|
|
|
|
|
for k in np.arange(1, len(data)):
|
|
|
|
|
|
|
|
flrg = self.flrgs[str(index[k])]
|
|
|
|
|
|
|
|
d = FuzzySet.getMaxMembershipFuzzySet(ndata[k], self.sets)
|
|
|
|
|
|
|
|
mp = self.getMidpoints(flrg, d)
|
|
|
|
|
|
|
|
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-09 17:04:48 +04:00
|
|
|
ret = []
|
|
|
|
for i in steps:
|
|
|
|
flrg = self.flrgs[str(i)]
|
|
|
|
|
|
|
|
mp = self.getMidpoints(flrg)
|
|
|
|
|
|
|
|
ret.append(sum(mp) / len(mp))
|
|
|
|
|
|
|
|
ret = self.doInverseTransformations(ret, params=data)
|
|
|
|
|
|
|
|
return ret
|