2016-10-19 17:27:57 +04:00
|
|
|
import numpy as np
|
2017-02-15 05:01:44 +04:00
|
|
|
import pandas as pd
|
|
|
|
from pyFTS import tree
|
|
|
|
from pyFTS.common import FuzzySet, SortedCollection
|
2016-12-22 20:36:50 +04:00
|
|
|
|
2017-01-24 16:40:48 +04:00
|
|
|
class FTS(object):
|
2016-12-22 20:36:50 +04:00
|
|
|
def __init__(self, order, name):
|
|
|
|
self.sets = {}
|
|
|
|
self.flrgs = {}
|
|
|
|
self.order = order
|
|
|
|
self.shortname = name
|
|
|
|
self.name = name
|
|
|
|
self.detail = name
|
2017-01-23 17:00:27 +04:00
|
|
|
self.isHighOrder = False
|
2017-01-25 18:17:07 +04:00
|
|
|
self.minOrder = 1
|
2017-01-11 00:05:51 +04:00
|
|
|
self.hasSeasonality = False
|
|
|
|
self.hasPointForecasting = True
|
|
|
|
self.hasIntervalForecasting = False
|
|
|
|
self.hasDistributionForecasting = False
|
2017-02-05 02:40:27 +04:00
|
|
|
self.isMultivariate = False
|
2017-01-11 00:05:51 +04:00
|
|
|
self.dump = False
|
2017-01-26 16:19:34 +04:00
|
|
|
self.transformations = []
|
|
|
|
self.transformations_param = []
|
2017-01-30 03:59:50 +04:00
|
|
|
self.original_max = 0
|
|
|
|
self.original_min = 0
|
2017-02-24 20:29:55 +04:00
|
|
|
self.partitioner = None
|
2017-03-03 15:53:55 +04:00
|
|
|
self.auto_update = False
|
|
|
|
self.benchmark_only = False
|
2016-12-22 20:36:50 +04:00
|
|
|
|
|
|
|
def fuzzy(self, data):
|
|
|
|
best = {"fuzzyset": "", "membership": 0.0}
|
|
|
|
|
|
|
|
for f in self.sets:
|
|
|
|
fset = self.sets[f]
|
|
|
|
if best["membership"] <= fset.membership(data):
|
|
|
|
best["fuzzyset"] = fset.name
|
|
|
|
best["membership"] = fset.membership(data)
|
|
|
|
|
|
|
|
return best
|
|
|
|
|
|
|
|
def forecast(self, data):
|
|
|
|
pass
|
|
|
|
|
2017-01-11 00:05:51 +04:00
|
|
|
def forecastInterval(self, data):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def forecastDistribution(self, data):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def forecastAhead(self, data, steps):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def forecastAheadInterval(self, data, steps):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def forecastAheadDistribution(self, data, steps):
|
|
|
|
pass
|
|
|
|
|
2017-02-24 20:29:55 +04:00
|
|
|
def train(self, data, sets, order=1, parameters=None):
|
2016-12-22 20:36:50 +04:00
|
|
|
pass
|
|
|
|
|
|
|
|
def getMidpoints(self, flrg):
|
|
|
|
ret = np.array([s.centroid for s in flrg.RHS])
|
|
|
|
return ret
|
|
|
|
|
2017-01-26 16:19:34 +04:00
|
|
|
def appendTransformation(self, transformation):
|
2017-03-22 06:17:06 +04:00
|
|
|
if transformation is not None:
|
|
|
|
self.transformations.append(transformation)
|
2017-01-26 16:19:34 +04:00
|
|
|
|
2017-01-30 03:59:50 +04:00
|
|
|
def doTransformations(self,data,params=None,updateUoD=False):
|
2017-01-26 16:19:34 +04:00
|
|
|
ndata = data
|
2017-01-30 03:59:50 +04:00
|
|
|
if updateUoD:
|
|
|
|
if min(data) < 0:
|
|
|
|
self.original_min = min(data) * 1.1
|
|
|
|
else:
|
|
|
|
self.original_min = min(data) * 0.9
|
|
|
|
|
|
|
|
if max(data) > 0:
|
|
|
|
self.original_max = max(data) * 1.1
|
|
|
|
else:
|
|
|
|
self.original_max = max(data) * 0.9
|
|
|
|
|
2017-01-27 14:26:47 +04:00
|
|
|
if len(self.transformations) > 0:
|
|
|
|
if params is None:
|
|
|
|
params = [ None for k in self.transformations]
|
|
|
|
|
|
|
|
for c, t in enumerate(self.transformations, start=0):
|
|
|
|
ndata = t.apply(ndata,params[c])
|
2017-01-26 16:19:34 +04:00
|
|
|
|
|
|
|
return ndata
|
|
|
|
|
2017-01-27 14:26:47 +04:00
|
|
|
def doInverseTransformations(self, data, params=None):
|
2017-01-26 16:19:34 +04:00
|
|
|
ndata = data
|
2017-01-27 14:26:47 +04:00
|
|
|
if len(self.transformations) > 0:
|
|
|
|
if params is None:
|
|
|
|
params = [None for k in self.transformations]
|
|
|
|
|
|
|
|
for c, t in enumerate(reversed(self.transformations), start=0):
|
|
|
|
ndata = t.inverse(ndata, params[c])
|
2017-01-26 16:19:34 +04:00
|
|
|
|
|
|
|
return ndata
|
|
|
|
|
2016-12-22 20:36:50 +04:00
|
|
|
def __str__(self):
|
|
|
|
tmp = self.name + ":\n"
|
|
|
|
for r in sorted(self.flrgs):
|
|
|
|
tmp = tmp + str(self.flrgs[r]) + "\n"
|
|
|
|
return tmp
|
2017-02-15 05:01:44 +04:00
|
|
|
|
2017-02-24 20:29:55 +04:00
|
|
|
def __len__(self):
|
|
|
|
return len(self.flrgs)
|
|
|
|
|
|
|
|
def len_total(self):
|
|
|
|
return sum([len(k) for k in self.flrgs])
|
|
|
|
|
2017-02-15 05:01:44 +04:00
|
|
|
def buildTreeWithoutOrder(self, node, lags, level):
|
|
|
|
|
|
|
|
if level not in lags:
|
|
|
|
return
|
|
|
|
|
|
|
|
for s in lags[level]:
|
|
|
|
node.appendChild(tree.FLRGTreeNode(s))
|
|
|
|
|
|
|
|
for child in node.getChildren():
|
|
|
|
self.buildTreeWithoutOrder(child, lags, level + 1)
|
|
|
|
|
2017-02-16 00:16:13 +04:00
|
|
|
def inputoutputmapping(self,bins=100):
|
2017-02-15 05:01:44 +04:00
|
|
|
|
|
|
|
dim_uod = tuple([bins for k in range(0,self.order)])
|
|
|
|
|
|
|
|
dim_fs = tuple([ len(self.sets) for k in range(0, self.order)])
|
|
|
|
|
|
|
|
simulation_uod = np.zeros(shape=dim_uod, dtype=float)
|
|
|
|
|
|
|
|
simulation_fs = np.zeros(shape=dim_fs, dtype=float)
|
|
|
|
|
|
|
|
percentiles = np.linspace(self.sets[0].lower, self.sets[-1].upper, bins).tolist()
|
|
|
|
|
|
|
|
pdf_uod = {}
|
|
|
|
|
|
|
|
for k in percentiles:
|
|
|
|
pdf_uod[k] = 0
|
|
|
|
|
|
|
|
pdf_fs = {}
|
|
|
|
for k in self.sets:
|
|
|
|
pdf_fs[k.name] = 0
|
|
|
|
|
|
|
|
lags = {}
|
|
|
|
|
|
|
|
for o in np.arange(0, self.order):
|
|
|
|
lags[o] = percentiles
|
|
|
|
|
|
|
|
# Build the tree with all possible paths
|
|
|
|
|
|
|
|
root = tree.FLRGTreeNode(None)
|
|
|
|
|
|
|
|
self.buildTreeWithoutOrder(root, lags, 0)
|
|
|
|
|
|
|
|
# Trace the possible paths
|
|
|
|
|
|
|
|
|
|
|
|
for p in root.paths():
|
|
|
|
path = list(reversed(list(filter(None.__ne__, p))))
|
|
|
|
|
|
|
|
index_uod = tuple([percentiles.index(k) for k in path])
|
|
|
|
|
|
|
|
index_fs = tuple([ FuzzySet.getMaxMembershipFuzzySetIndex(k, self.sets) for k in path])
|
|
|
|
|
|
|
|
forecast = self.forecast(path)[0]
|
|
|
|
|
|
|
|
simulation_uod[index_uod] = forecast
|
|
|
|
|
|
|
|
simulation_fs[index_fs] = forecast
|
|
|
|
|
2017-02-16 00:16:13 +04:00
|
|
|
return [simulation_fs, simulation_uod ]
|
2017-02-15 05:01:44 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-24 20:29:55 +04:00
|
|
|
|