From f8927fd15843f883f8630299d4fa46cda923cd4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido?= Date: Wed, 31 Oct 2018 10:51:50 -0300 Subject: [PATCH] Bugfixes on seasonal and multivariate models --- pyFTS/models/multivariate/common.py | 3 ++- pyFTS/models/seasonal/common.py | 9 +++++-- pyFTS/models/seasonal/partitioner.py | 11 +++++++++ pyFTS/tests/general.py | 37 ++++++++++++++++++++-------- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/pyFTS/models/multivariate/common.py b/pyFTS/models/multivariate/common.py index f3869ff..c167845 100644 --- a/pyFTS/models/multivariate/common.py +++ b/pyFTS/models/multivariate/common.py @@ -3,7 +3,8 @@ import pandas as pd from pyFTS.common import FuzzySet def fuzzyfy_instance(data_point, var): - return FuzzySet.fuzzyfy(data_point, var.partitioner, mode='sets', method='fuzzy', alpha_cut=var.alpha_cut) + fsets = FuzzySet.fuzzyfy(data_point, var.partitioner, mode='sets', method='fuzzy', alpha_cut=var.alpha_cut) + return [(var.name, fs) for fs in fsets] diff --git a/pyFTS/models/seasonal/common.py b/pyFTS/models/seasonal/common.py index cb37c4c..0ec90fa 100644 --- a/pyFTS/models/seasonal/common.py +++ b/pyFTS/models/seasonal/common.py @@ -88,10 +88,15 @@ class FuzzySet(FuzzySet.FuzzySet): """ def __init__(self, datepart, name, mf, parameters, centroid, alpha=1.0, **kwargs): - super(FuzzySet, self).__init__(name, mf, parameters, centroid, alpha, type = 'datetime', **kwargs) + super(FuzzySet, self).__init__(name, mf, parameters, centroid, alpha, + type=kwargs.get('type', 'datetime'), + **kwargs) self.datepart = datepart self.type = 'seasonal' def membership(self, x): - dp = strip_datepart(x, self.datepart) + if self.type == 'datetime': + dp = strip_datepart(x, self.datepart) + else: + dp = x return self.mf(dp, self.parameters) * self.alpha \ No newline at end of file diff --git a/pyFTS/models/seasonal/partitioner.py b/pyFTS/models/seasonal/partitioner.py index 8310518..4a5d83a 100644 --- a/pyFTS/models/seasonal/partitioner.py +++ b/pyFTS/models/seasonal/partitioner.py @@ -64,6 +64,17 @@ class TimeGridPartitioner(partitioner.Partitioner): **kwargs)) tmp.centroid = c sets[set_name] = tmp + elif c == self.max - partlen: + tmp = Composite(set_name, superset=True) + tmp.append_set(FuzzySet(self.season, set_name, Membership.trimf, + [0.0000001, 0.0, + pl2], 0.0, alpha=.5, + **kwargs)) + tmp.append_set(FuzzySet(self.season, set_name, Membership.trimf, + [c - partlen, c, c + partlen], c, + **kwargs)) + tmp.centroid = c + sets[set_name] = tmp else: sets[set_name] = FuzzySet(self.season, set_name, Membership.trimf, [c - partlen, c, c + partlen], c, diff --git a/pyFTS/tests/general.py b/pyFTS/tests/general.py index 821c394..647b08d 100644 --- a/pyFTS/tests/general.py +++ b/pyFTS/tests/general.py @@ -16,21 +16,38 @@ from pyFTS.common import Transformations tdiff = Transformations.Differential(1) -data = pd.read_csv('/home/petronio/Downloads/priceHong').values +dataset = pd.read_csv('/home/petronio/Downloads/priceHong') +dataset['hour'] = dataset.index.values % 24 split = 24 * 800 -train = data[:split].flatten() -test = data[split:].flatten() +#train = data[:split].flatten() +#test = data[split:].flatten() -print(train) +#print(train) -fs_grid = Grid.GridPartitioner(data=train,npart=25) -#fs_entr.plot(ax[1]) +from pyFTS.models.multivariate import common, variable, mvfts +from pyFTS.partitioners import Grid +from pyFTS.models.seasonal.common import DateTime +from pyFTS.models.seasonal import partitioner as seasonal -for method in [hofts.HighOrderFTS, pwfts.ProbabilisticWeightedFTS]: - for order in [2,3]: - model = method(partitioner=fs_grid, order=order) - model.fit(train) +vhour = variable.Variable("Hour", data_label="hour", partitioner=seasonal.TimeGridPartitioner, npart=24, + data=dataset, partitioner_specific={'seasonality': DateTime.hour_of_day, 'type': 'common'}) +vprice = variable.Variable("Price", data_label="price", partitioner=Grid.GridPartitioner, npart=25, + data=dataset) + + +fig, ax = plt.subplots(nrows=2, ncols=1,figsize=[15,5]) + +vhour.partitioner.plot(ax[0]) +vprice.partitioner.plot(ax[1]) + +model = mvfts.MVFTS() +#model.shortname += ' ' + key +model.append_variable(vhour) +model.append_variable(vprice) +# model.shortname += ' ' + w +model.target_variable = vprice +model.fit(dataset.iloc[:split]) ''' from pyFTS.data import TAIEX, SP500, NASDAQ