2017-05-07 18:41:31 +04:00
|
|
|
|
"""
|
|
|
|
|
First Order Exponentialy Weighted Fuzzy Time Series by Sadaei et al. (2013)
|
|
|
|
|
|
|
|
|
|
H. J. Sadaei, R. Enayatifar, A. H. Abdullah, and A. Gani, “Short-term load forecasting using a hybrid model with a
|
|
|
|
|
refined exponentially weighted fuzzy time series and an improved harmony search,” Int. J. Electr. Power Energy Syst., vol. 62, no. from 2005, pp. 118–129, 2014.
|
|
|
|
|
"""
|
|
|
|
|
|
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-09-08 16:03:32 +04:00
|
|
|
|
|
2017-05-02 18:32:03 +04:00
|
|
|
|
|
2017-02-24 20:29:55 +04:00
|
|
|
|
class ExponentialyWeightedFLRG(object):
|
2017-05-05 22:33:27 +04:00
|
|
|
|
"""First Order Exponentialy Weighted Fuzzy Logical Relationship Group"""
|
2016-12-22 20:36:50 +04:00
|
|
|
|
def __init__(self, LHS, c):
|
|
|
|
|
self.LHS = LHS
|
|
|
|
|
self.RHS = []
|
|
|
|
|
self.count = 0.0
|
|
|
|
|
self.c = c
|
|
|
|
|
|
|
|
|
|
def append(self, c):
|
|
|
|
|
self.RHS.append(c)
|
|
|
|
|
self.count = self.count + 1.0
|
|
|
|
|
|
|
|
|
|
def weights(self):
|
|
|
|
|
wei = [self.c ** k for k in np.arange(0.0, self.count, 1.0)]
|
|
|
|
|
tot = sum(wei)
|
2017-01-24 16:40:48 +04:00
|
|
|
|
return np.array([k / tot for k in wei])
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
tmp = self.LHS.name + " -> "
|
|
|
|
|
tmp2 = ""
|
|
|
|
|
cc = 0
|
|
|
|
|
wei = [self.c ** k for k in np.arange(0.0, self.count, 1.0)]
|
|
|
|
|
tot = sum(wei)
|
|
|
|
|
for c in sorted(self.RHS, key=lambda s: s.name):
|
|
|
|
|
if len(tmp2) > 0:
|
|
|
|
|
tmp2 = tmp2 + ","
|
|
|
|
|
tmp2 = tmp2 + c.name + "(" + str(wei[cc] / tot) + ")"
|
|
|
|
|
cc = cc + 1
|
|
|
|
|
return tmp + tmp2
|
|
|
|
|
|
2017-02-24 20:29:55 +04:00
|
|
|
|
def __len__(self):
|
|
|
|
|
return len(self.RHS)
|
|
|
|
|
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
2016-09-08 16:03:32 +04:00
|
|
|
|
class ExponentialyWeightedFTS(fts.FTS):
|
2017-05-05 22:33:27 +04:00
|
|
|
|
"""First Order Exponentialy Weighted 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(ExponentialyWeightedFTS, self).__init__(1, "EWFTS")
|
|
|
|
|
self.name = "Exponentialy Weighted FTS"
|
|
|
|
|
self.detail = "Sadaei"
|
|
|
|
|
self.c = 1
|
|
|
|
|
|
|
|
|
|
def generateFLRG(self, flrs, c):
|
|
|
|
|
flrgs = {}
|
|
|
|
|
for flr in flrs:
|
|
|
|
|
if flr.LHS.name in flrgs:
|
|
|
|
|
flrgs[flr.LHS.name].append(flr.RHS)
|
|
|
|
|
else:
|
|
|
|
|
flrgs[flr.LHS.name] = ExponentialyWeightedFLRG(flr.LHS, c);
|
|
|
|
|
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=2):
|
|
|
|
|
self.c = parameters
|
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)
|
2017-01-24 16:40:48 +04:00
|
|
|
|
self.flrgs = self.generateFLRG(flrs, self.c)
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
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
|