2017-05-07 18:41:31 +04:00
|
|
|
|
"""
|
|
|
|
|
First Order Conventional Fuzzy Time Series by Chen (1996)
|
|
|
|
|
|
|
|
|
|
S.-M. Chen, “Forecasting enrollments based on fuzzy time series,” Fuzzy Sets Syst., vol. 81, no. 3, pp. 311–319, 1996.
|
|
|
|
|
"""
|
|
|
|
|
|
2016-10-18 21:45:07 +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 01:51:00 +04:00
|
|
|
|
|
2017-01-24 16:40:48 +04:00
|
|
|
|
class ConventionalFLRG(object):
|
2017-05-05 22:33:27 +04:00
|
|
|
|
"""First Order Conventional Fuzzy Logical Relationship Group"""
|
2016-12-22 20:36:50 +04:00
|
|
|
|
def __init__(self, LHS):
|
|
|
|
|
self.LHS = LHS
|
|
|
|
|
self.RHS = set()
|
|
|
|
|
|
|
|
|
|
def append(self, c):
|
|
|
|
|
self.RHS.add(c)
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
tmp = self.LHS.name + " -> "
|
|
|
|
|
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
|
2016-09-02 22:55:55 +04:00
|
|
|
|
|
2017-02-24 20:29:55 +04:00
|
|
|
|
def __len__(self):
|
|
|
|
|
return len(self.RHS)
|
|
|
|
|
|
2016-09-02 22:55:55 +04:00
|
|
|
|
|
2016-09-08 01:51:00 +04:00
|
|
|
|
class ConventionalFTS(fts.FTS):
|
2017-05-05 22:33:27 +04:00
|
|
|
|
"""Conventional 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(ConventionalFTS, self).__init__(1, "CFTS " + name)
|
2016-12-22 20:36:50 +04:00
|
|
|
|
self.name = "Conventional FTS"
|
|
|
|
|
self.detail = "Chen"
|
|
|
|
|
self.flrgs = {}
|
|
|
|
|
|
|
|
|
|
def generateFLRG(self, flrs):
|
|
|
|
|
flrgs = {}
|
|
|
|
|
for flr in flrs:
|
|
|
|
|
if flr.LHS.name in flrgs:
|
|
|
|
|
flrgs[flr.LHS.name].append(flr.RHS)
|
|
|
|
|
else:
|
2017-02-09 17:04:48 +04:00
|
|
|
|
flrgs[flr.LHS.name] = ConventionalFLRG(flr.LHS)
|
2016-12-22 20:36:50 +04:00
|
|
|
|
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.generateNonRecurrentFLRs(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
|
|
|
|
|
2017-01-27 14:26:47 +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(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(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
|