2017-05-07 18:41:31 +04:00
|
|
|
|
"""
|
|
|
|
|
Simple First Order Seasonal Fuzzy Time Series implementation of Song (1999) based of Conventional FTS by Chen (1996)
|
|
|
|
|
|
|
|
|
|
Q. Song, “Seasonal forecasting in fuzzy time series,” Fuzzy sets Syst., vol. 107, pp. 235–236, 1999.
|
|
|
|
|
|
|
|
|
|
S.-M. Chen, “Forecasting enrollments based on fuzzy time series,” Fuzzy Sets Syst., vol. 81, no. 3, pp. 311–319, 1996.
|
|
|
|
|
"""
|
|
|
|
|
|
2016-10-18 15:54:49 +04:00
|
|
|
|
import numpy as np
|
2016-12-22 20:36:50 +04:00
|
|
|
|
from pyFTS.common import FuzzySet,FLR
|
2017-02-05 02:40:27 +04:00
|
|
|
|
from pyFTS import fts
|
2016-10-18 15:54:49 +04:00
|
|
|
|
|
2017-02-09 17:04:48 +04:00
|
|
|
|
|
|
|
|
|
class SeasonalFLRG(FLR.FLR):
|
2017-05-05 22:33:27 +04:00
|
|
|
|
"""First Order Seasonal Fuzzy Logical Relationship Group"""
|
2016-12-22 20:36:50 +04:00
|
|
|
|
def __init__(self, seasonality):
|
2017-02-09 17:04:48 +04:00
|
|
|
|
super(SeasonalFLRG, self).__init__(None,None)
|
2016-12-22 20:36:50 +04:00
|
|
|
|
self.LHS = seasonality
|
|
|
|
|
self.RHS = []
|
|
|
|
|
|
|
|
|
|
def append(self, c):
|
|
|
|
|
self.RHS.append(c)
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
tmp = str(self.LHS) + " -> "
|
|
|
|
|
tmp2 = ""
|
|
|
|
|
for c in sorted(self.RHS, key=lambda s: s.name):
|
|
|
|
|
if len(tmp2) > 0:
|
|
|
|
|
tmp2 = tmp2 + ","
|
|
|
|
|
tmp2 = tmp2 + c.name
|
|
|
|
|
return tmp + tmp2
|
|
|
|
|
|
2017-02-24 20:29:55 +04:00
|
|
|
|
def __len__(self):
|
|
|
|
|
return len(self.RHS)
|
|
|
|
|
|
2016-10-18 15:54:49 +04:00
|
|
|
|
|
|
|
|
|
class SeasonalFTS(fts.FTS):
|
2017-05-05 22:33:27 +04:00
|
|
|
|
"""First Order Seasonal Fuzzy Time Series"""
|
2017-05-03 00:16:49 +04:00
|
|
|
|
def __init__(self, name, **kwargs):
|
2016-12-22 20:36:50 +04:00
|
|
|
|
super(SeasonalFTS, self).__init__(1, "SFTS")
|
|
|
|
|
self.name = "Seasonal FTS"
|
|
|
|
|
self.detail = "Chen"
|
|
|
|
|
self.seasonality = 1
|
2017-05-02 18:32:03 +04:00
|
|
|
|
self.has_seasonality = True
|
|
|
|
|
self.has_point_forecasting = True
|
|
|
|
|
self.is_high_order = False
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
|
|
|
|
def generateFLRG(self, flrs):
|
2017-05-22 01:04:10 +04:00
|
|
|
|
flrgs = {}
|
|
|
|
|
for ct, flr in enumerate(flrs, start=1):
|
2017-02-05 02:40:27 +04:00
|
|
|
|
|
2017-05-22 01:04:10 +04:00
|
|
|
|
season = self.indexer.get_season_by_index(ct)[0]
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
2017-05-22 07:44:07 +04:00
|
|
|
|
ss = str(season)
|
|
|
|
|
|
|
|
|
|
if ss not in flrgs:
|
|
|
|
|
flrgs[ss] = SeasonalFLRG(season)
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
2017-05-22 01:04:10 +04:00
|
|
|
|
#print(season)
|
2017-05-22 07:44:07 +04:00
|
|
|
|
flrgs[ss].append(flr.RHS)
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
|
|
|
|
return (flrgs)
|
|
|
|
|
|
2017-05-22 01:04:10 +04:00
|
|
|
|
def train(self, data, sets, order=1, parameters=None):
|
2016-12-22 20:36:50 +04:00
|
|
|
|
self.sets = sets
|
2017-01-27 14:26:47 +04:00
|
|
|
|
ndata = self.doTransformations(data)
|
|
|
|
|
tmpdata = FuzzySet.fuzzySeries(ndata, sets)
|
2017-05-22 07:44:07 +04:00
|
|
|
|
flrs = FLR.generateRecurrentFLRs(tmpdata)
|
2016-12-22 20:36:50 +04:00
|
|
|
|
self.flrgs = self.generateFLRG(flrs)
|
|
|
|
|
|
2017-04-15 02:57:39 +04:00
|
|
|
|
def forecast(self, data, **kwargs):
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
2017-02-05 02:40:27 +04:00
|
|
|
|
ndata = np.array(self.doTransformations(data))
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
|
|
|
|
l = len(ndata)
|
|
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
|
|
|
|
|
|
for k in np.arange(1, l):
|
2017-02-05 02:40:27 +04:00
|
|
|
|
|
2017-05-22 01:04:10 +04:00
|
|
|
|
season = self.indexer.get_season_by_index(k)[0]
|
2017-02-05 02:40:27 +04:00
|
|
|
|
|
2017-05-22 07:44:07 +04:00
|
|
|
|
flrg = self.flrgs[str(season)]
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
|
|
|
|
mp = self.getMidpoints(flrg)
|
|
|
|
|
|
2017-05-22 07:44:07 +04:00
|
|
|
|
ret.append(np.percentile(mp, 50))
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
2017-01-27 14:26:47 +04:00
|
|
|
|
ret = self.doInverseTransformations(ret, params=[data[self.order - 1:]])
|
|
|
|
|
|
2016-12-22 20:36:50 +04:00
|
|
|
|
return ret
|