pyFTS/yu.py
2017-05-07 11:41:31 -03:00

93 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
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. 609624, 2005.
"""
import numpy as np
from pyFTS.common import FuzzySet,FLR
from pyFTS import fts
class WeightedFLRG(object):
"""First Order Weighted Fuzzy Logical Relationship Group"""
def __init__(self, LHS, **kwargs):
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
class WeightedFTS(fts.FTS):
"""First Order Weighted Fuzzy Time Series"""
def __init__(self, name, **kwargs):
super(WeightedFTS, self).__init__(1, "WFTS " + name)
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)
def train(self, data, sets,order=1,parameters=None):
self.sets = sets
ndata = self.doTransformations(data)
tmpdata = FuzzySet.fuzzySeries(ndata, sets)
flrs = FLR.generateRecurrentFLRs(tmpdata)
self.flrgs = self.generateFLRG(flrs)
def forecast(self, data, **kwargs):
l = 1
data = np.array(data)
ndata = self.doTransformations(data)
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()))
ret = self.doInverseTransformations(ret, params=[data[self.order - 1:]])
return ret