2017-05-07 18:41:31 +04:00
|
|
|
|
"""
|
|
|
|
|
Trend Weighted Fuzzy Time Series by Cheng, Chen and Wu (2009)
|
|
|
|
|
|
|
|
|
|
C.-H. Cheng, Y.-S. Chen, and Y.-L. Wu, “Forecasting innovation diffusion of products using trend-weighted fuzzy time-series model,”
|
|
|
|
|
Expert Syst. Appl., vol. 36, no. 2, pp. 1826–1832, 2009.
|
|
|
|
|
"""
|
|
|
|
|
|
2017-04-20 02:58:25 +04:00
|
|
|
|
import numpy as np
|
|
|
|
|
from pyFTS.common import FuzzySet,FLR
|
|
|
|
|
from pyFTS import fts, yu
|
|
|
|
|
|
|
|
|
|
|
2017-05-07 18:41:31 +04:00
|
|
|
|
class TrendWeightedFLRG(yu.WeightedFLRG):
|
|
|
|
|
"""
|
|
|
|
|
First Order Trend Weighted Fuzzy Logical Relationship Group
|
|
|
|
|
"""
|
2017-04-20 02:58:25 +04:00
|
|
|
|
def __init__(self, LHS, **kwargs):
|
2017-05-07 18:41:31 +04:00
|
|
|
|
super(TrendWeightedFLRG, self).__init__(LHS)
|
2017-04-20 02:58:25 +04:00
|
|
|
|
|
|
|
|
|
def weights(self):
|
|
|
|
|
count_nochange = 0.0
|
|
|
|
|
count_up = 0.0
|
|
|
|
|
count_down = 0.0
|
|
|
|
|
weights = []
|
|
|
|
|
|
|
|
|
|
for c in self.RHS:
|
|
|
|
|
tmp = 0
|
2017-05-07 18:41:31 +04:00
|
|
|
|
if self.LHS.centroid == c.centroid:
|
2017-04-20 02:58:25 +04:00
|
|
|
|
count_nochange += 1.0
|
|
|
|
|
tmp = count_nochange
|
2017-05-07 18:41:31 +04:00
|
|
|
|
elif self.LHS.centroid > c.centroid:
|
2017-04-20 02:58:25 +04:00
|
|
|
|
count_down += 1.0
|
|
|
|
|
tmp = count_down
|
|
|
|
|
else:
|
|
|
|
|
count_up += 1.0
|
|
|
|
|
tmp = count_up
|
|
|
|
|
weights.append(tmp)
|
|
|
|
|
|
|
|
|
|
tot = sum(weights)
|
|
|
|
|
return np.array([k / tot for k in weights])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TrendWeightedFTS(yu.WeightedFTS):
|
2017-05-05 22:33:27 +04:00
|
|
|
|
"""First Order Trend Weighted Fuzzy Time Series"""
|
2017-05-03 00:16:49 +04:00
|
|
|
|
def __init__(self, name, **kwargs):
|
2017-05-07 18:41:31 +04:00
|
|
|
|
super(TrendWeightedFTS, self).__init__("TWFTS " + name)
|
2017-04-20 02:58:25 +04:00
|
|
|
|
self.name = "Trend Weighted FTS"
|
|
|
|
|
self.detail = "Cheng"
|
|
|
|
|
|
|
|
|
|
def generateFLRG(self, flrs):
|
|
|
|
|
flrgs = {}
|
|
|
|
|
for flr in flrs:
|
|
|
|
|
if flr.LHS.name in flrgs:
|
|
|
|
|
flrgs[flr.LHS.name].append(flr.RHS)
|
|
|
|
|
else:
|
2017-05-07 18:41:31 +04:00
|
|
|
|
flrgs[flr.LHS.name] = TrendWeightedFLRG(flr.LHS)
|
2017-04-20 02:58:25 +04:00
|
|
|
|
flrgs[flr.LHS.name].append(flr.RHS)
|
|
|
|
|
return (flrgs)
|