2017-05-07 18:41:31 +04:00
|
|
|
|
"""
|
|
|
|
|
First Order Weighted Fuzzy Time Series by Yu(2005)
|
|
|
|
|
|
|
|
|
|
H.-K. Yu, “Weighted fuzzy time series models for TAIEX forecasting,”
|
|
|
|
|
Phys. A Stat. Mech. its Appl., vol. 349, no. 3, pp. 609–624, 2005.
|
|
|
|
|
"""
|
|
|
|
|
|
2016-10-18 15:50:27 +04:00
|
|
|
|
import numpy as np
|
2016-12-22 20:36:50 +04:00
|
|
|
|
from pyFTS.common import FuzzySet,FLR
|
2017-01-23 17:00:27 +04:00
|
|
|
|
from pyFTS import fts
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
2016-09-08 16:03:32 +04:00
|
|
|
|
|
2017-05-05 22:01:45 +04:00
|
|
|
|
class WeightedFLRG(object):
|
2017-05-05 22:33:27 +04:00
|
|
|
|
"""First Order Weighted Fuzzy Logical Relationship Group"""
|
2017-05-05 22:01:45 +04:00
|
|
|
|
def __init__(self, LHS, **kwargs):
|
2016-12-22 20:36:50 +04:00
|
|
|
|
self.LHS = LHS
|
|
|
|
|
self.RHS = []
|
|
|
|
|
self.count = 1.0
|
|
|
|
|
|
|
|
|
|
def append(self, c):
|
|
|
|
|
self.RHS.append(c)
|
|
|
|
|
self.count = self.count + 1.0
|
|
|
|
|
|
|
|
|
|
def weights(self):
|
|
|
|
|
tot = sum(np.arange(1.0, self.count, 1.0))
|
|
|
|
|
return np.array([k / tot for k in np.arange(1.0, self.count, 1.0)])
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
tmp = self.LHS.name + " -> "
|
|
|
|
|
tmp2 = ""
|
|
|
|
|
cc = 1.0
|
|
|
|
|
tot = sum(np.arange(1.0, self.count, 1.0))
|
|
|
|
|
for c in sorted(self.RHS, key=lambda s: s.name):
|
|
|
|
|
if len(tmp2) > 0:
|
|
|
|
|
tmp2 = tmp2 + ","
|
|
|
|
|
tmp2 = tmp2 + c.name + "(" + str(round(cc / tot, 3)) + ")"
|
|
|
|
|
cc = cc + 1.0
|
|
|
|
|
return tmp + tmp2
|
|
|
|
|
|
2016-09-02 22:55:55 +04:00
|
|
|
|
|
2016-09-08 16:03:32 +04:00
|
|
|
|
class WeightedFTS(fts.FTS):
|
2017-05-05 22:33:27 +04:00
|
|
|
|
"""First Order Weighted Fuzzy Time Series"""
|
2017-05-03 00:16:49 +04:00
|
|
|
|
def __init__(self, name, **kwargs):
|
2017-02-24 20:29:55 +04:00
|
|
|
|
super(WeightedFTS, self).__init__(1, "WFTS " + name)
|
2016-12-22 20:36:50 +04:00
|
|
|
|
self.name = "Weighted FTS"
|
|
|
|
|
self.detail = "Yu"
|
|
|
|
|
|
|
|
|
|
def generateFLRG(self, flrs):
|
|
|
|
|
flrgs = {}
|
|
|
|
|
for flr in flrs:
|
|
|
|
|
if flr.LHS.name in flrgs:
|
|
|
|
|
flrgs[flr.LHS.name].append(flr.RHS)
|
|
|
|
|
else:
|
|
|
|
|
flrgs[flr.LHS.name] = WeightedFLRG(flr.LHS);
|
|
|
|
|
flrgs[flr.LHS.name].append(flr.RHS)
|
|
|
|
|
return (flrgs)
|
|
|
|
|
|
2017-01-23 17:00:27 +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)
|
2016-12-22 20:36:50 +04:00
|
|
|
|
flrs = FLR.generateRecurrentFLRs(tmpdata)
|
|
|
|
|
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
|
|
|
|
l = 1
|
|
|
|
|
|
2017-01-27 14:26:47 +04:00
|
|
|
|
data = np.array(data)
|
|
|
|
|
|
|
|
|
|
ndata = self.doTransformations(data)
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
|
|
|
|
l = len(ndata)
|
|
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
|
|
|
|
|
|
for k in np.arange(0, l):
|
|
|
|
|
|
|
|
|
|
mv = FuzzySet.fuzzyInstance(ndata[k], self.sets)
|
|
|
|
|
|
|
|
|
|
actual = self.sets[np.argwhere(mv == max(mv))[0, 0]]
|
|
|
|
|
|
|
|
|
|
if actual.name not in self.flrgs:
|
|
|
|
|
ret.append(actual.centroid)
|
|
|
|
|
else:
|
|
|
|
|
flrg = self.flrgs[actual.name]
|
|
|
|
|
mp = self.getMidpoints(flrg)
|
|
|
|
|
|
|
|
|
|
ret.append(mp.dot(flrg.weights()))
|
|
|
|
|
|
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
|