2016-10-19 20:45:11 +04:00
|
|
|
import numpy as np
|
2016-12-22 20:36:50 +04:00
|
|
|
from pyFTS.common import FuzzySet,FLR
|
2017-01-11 00:05:51 +04:00
|
|
|
from pyFTS import fts
|
2016-12-22 20:36:50 +04:00
|
|
|
|
2016-10-19 20:45:11 +04:00
|
|
|
|
2017-02-24 20:29:55 +04:00
|
|
|
class HighOrderFLRG(object):
|
2017-05-05 22:33:27 +04:00
|
|
|
"""Conventional High Order Fuzzy Logical Relationship Group"""
|
2016-12-22 20:36:50 +04:00
|
|
|
def __init__(self, order):
|
|
|
|
self.LHS = []
|
|
|
|
self.RHS = {}
|
|
|
|
self.order = order
|
|
|
|
self.strlhs = ""
|
|
|
|
|
|
|
|
def appendRHS(self, c):
|
|
|
|
if c.name not in self.RHS:
|
|
|
|
self.RHS[c.name] = c
|
|
|
|
|
|
|
|
def strLHS(self):
|
|
|
|
if len(self.strlhs) == 0:
|
|
|
|
for c in self.LHS:
|
|
|
|
if len(self.strlhs) > 0:
|
2017-01-11 00:05:51 +04:00
|
|
|
self.strlhs += ", "
|
2016-12-22 20:36:50 +04:00
|
|
|
self.strlhs = self.strlhs + c.name
|
|
|
|
return self.strlhs
|
|
|
|
|
|
|
|
def appendLHS(self, c):
|
|
|
|
self.LHS.append(c)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
tmp = ""
|
|
|
|
for c in sorted(self.RHS):
|
|
|
|
if len(tmp) > 0:
|
|
|
|
tmp = tmp + ","
|
|
|
|
tmp = tmp + c
|
|
|
|
return self.strLHS() + " -> " + tmp
|
|
|
|
|
|
|
|
|
2017-02-24 20:29:55 +04:00
|
|
|
def __len__(self):
|
|
|
|
return len(self.RHS)
|
|
|
|
|
|
|
|
|
2016-10-19 20:45:11 +04:00
|
|
|
class HighOrderFTS(fts.FTS):
|
2017-05-05 22:33:27 +04:00
|
|
|
"""Conventional High Order 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(HighOrderFTS, self).__init__(1, "HOFTS" + name)
|
|
|
|
self.name = "High Order FTS"
|
2017-01-24 16:40:48 +04:00
|
|
|
self.shortname = "HOFTS" + name
|
2016-12-22 20:36:50 +04:00
|
|
|
self.detail = "Chen"
|
|
|
|
self.order = 1
|
|
|
|
self.setsDict = {}
|
2017-05-02 18:32:03 +04:00
|
|
|
self.is_high_order = True
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
|
|
def generateFLRG(self, flrs):
|
|
|
|
flrgs = {}
|
|
|
|
l = len(flrs)
|
|
|
|
for k in np.arange(self.order + 1, l):
|
|
|
|
flrg = HighOrderFLRG(self.order)
|
|
|
|
|
|
|
|
for kk in np.arange(k - self.order, k):
|
|
|
|
flrg.appendLHS(flrs[kk].LHS)
|
|
|
|
|
|
|
|
if flrg.strLHS() in flrgs:
|
|
|
|
flrgs[flrg.strLHS()].appendRHS(flrs[k].RHS)
|
|
|
|
else:
|
|
|
|
flrgs[flrg.strLHS()] = flrg;
|
|
|
|
flrgs[flrg.strLHS()].appendRHS(flrs[k].RHS)
|
|
|
|
return (flrgs)
|
|
|
|
|
2017-01-23 17:00:27 +04:00
|
|
|
def train(self, data, sets, order=1,parameters=None):
|
2017-01-27 14:26:47 +04:00
|
|
|
|
2017-01-30 03:59:50 +04:00
|
|
|
data = self.doTransformations(data, updateUoD=True)
|
2017-01-27 14:26:47 +04:00
|
|
|
|
2016-12-22 20:36:50 +04:00
|
|
|
self.order = order
|
|
|
|
self.sets = sets
|
|
|
|
for s in self.sets: self.setsDict[s.name] = s
|
|
|
|
tmpdata = FuzzySet.fuzzySeries(data, sets)
|
2017-01-11 00:05:51 +04:00
|
|
|
flrs = FLR.generateRecurrentFLRs(tmpdata)
|
2016-12-22 20:36:50 +04:00
|
|
|
self.flrgs = self.generateFLRG(flrs)
|
|
|
|
|
|
|
|
def getMidpoints(self, flrg):
|
|
|
|
ret = np.array([self.setsDict[s].centroid for s in flrg.RHS])
|
|
|
|
return ret
|
|
|
|
|
2017-04-15 02:57:39 +04:00
|
|
|
def forecast(self, data, **kwargs):
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
|
|
ret = []
|
|
|
|
|
|
|
|
l = len(data)
|
|
|
|
|
|
|
|
if l <= self.order:
|
|
|
|
return data
|
|
|
|
|
2017-01-27 14:26:47 +04:00
|
|
|
ndata = self.doTransformations(data)
|
|
|
|
|
2017-01-24 16:40:48 +04:00
|
|
|
for k in np.arange(self.order, l+1):
|
2017-01-27 14:26:47 +04:00
|
|
|
tmpdata = FuzzySet.fuzzySeries(ndata[k - self.order: k], self.sets)
|
2016-12-22 20:36:50 +04:00
|
|
|
tmpflrg = HighOrderFLRG(self.order)
|
|
|
|
|
|
|
|
for s in tmpdata: tmpflrg.appendLHS(s)
|
|
|
|
|
|
|
|
if tmpflrg.strLHS() not in self.flrgs:
|
|
|
|
ret.append(tmpdata[-1].centroid)
|
|
|
|
else:
|
|
|
|
flrg = self.flrgs[tmpflrg.strLHS()]
|
|
|
|
mp = self.getMidpoints(flrg)
|
|
|
|
|
|
|
|
ret.append(sum(mp) / len(mp))
|
|
|
|
|
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
|