From a8e3ba4810780c3aa107f47bd180ed2605ff720e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido?= Date: Fri, 6 Oct 2017 13:04:33 -0300 Subject: [PATCH] - Non Stationary Fuzzy Time Series - NSFTS: stable version --- pyFTS/common/FLR.py | 2 +- pyFTS/nonstationary/common.py | 4 +-- pyFTS/nonstationary/nsfts.py | 16 ++++++++- pyFTS/nonstationary/util.py | 10 ++++-- pyFTS/partitioners/partitioner.py | 2 +- pyFTS/tests/nonstationary.py | 54 +++++++++++++++++++++++++++---- 6 files changed, 74 insertions(+), 14 deletions(-) diff --git a/pyFTS/common/FLR.py b/pyFTS/common/FLR.py index 86a12ff..0ff89b1 100644 --- a/pyFTS/common/FLR.py +++ b/pyFTS/common/FLR.py @@ -52,7 +52,7 @@ def generateRecurrentFLRs(fuzzyData): tmp = FLR(l, r) flrs.append(tmp) else: - tmp = FLR.FLR(lhs,rhs) + tmp = FLR(lhs,rhs) flrs.append(tmp) return flrs diff --git a/pyFTS/nonstationary/common.py b/pyFTS/nonstationary/common.py index 510ddd3..cf35b23 100644 --- a/pyFTS/nonstationary/common.py +++ b/pyFTS/nonstationary/common.py @@ -221,9 +221,9 @@ class PolynomialNonStationaryPartitioner(partitioner.Partitioner): def get_polynomial_perturbations(self, data, **kwargs): w = kwargs.get("window_size", int(len(data) / 5)) deg = kwargs.get("degree", 2) - xmax = [0] + xmax = [data[0]] tmax = [0] - xmin = [0] + xmin = [data[0]] tmin = [0] lengs = [0] tlengs = [0] diff --git a/pyFTS/nonstationary/nsfts.py b/pyFTS/nonstationary/nsfts.py index bb2d39c..65a0c42 100644 --- a/pyFTS/nonstationary/nsfts.py +++ b/pyFTS/nonstationary/nsfts.py @@ -76,11 +76,21 @@ class NonStationaryFTS(fts.FTS): for k in np.arange(0, l): + #print("input: " + str(ndata[k])) + tdisp = k + time_displacement affected_sets = [ [set, set.membership(ndata[k], tdisp)] for set in self.sets if set.membership(ndata[k], tdisp) > 0.0] + if len(affected_sets) == 0: + if self.sets[0].get_lower(tdisp) > ndata[k]: + affected_sets.append([self.sets[0], 1.0]) + elif self.sets[-1].get_upper(tdisp) < ndata[k]: + affected_sets.append([self.sets[-1], 1.0]) + + #print(affected_sets) + tmp = [] for aset in affected_sets: if aset[0] in self.flrgs: @@ -88,7 +98,11 @@ class NonStationaryFTS(fts.FTS): else: tmp.append(aset[0].get_midpoint(tdisp) * aset[1]) - ret.append(sum(tmp)) + pto = sum(tmp) + + #print(pto) + + ret.append(pto) ret = self.doInverseTransformations(ret, params=[data[self.order - 1:]]) diff --git a/pyFTS/nonstationary/util.py b/pyFTS/nonstationary/util.py index 3da746c..7214d6e 100644 --- a/pyFTS/nonstationary/util.py +++ b/pyFTS/nonstationary/util.py @@ -15,13 +15,19 @@ def plot_sets(uod, sets, start=0, end=10, tam=[5, 5], colors=None, save=False, f param = set.perturbated_parameters[t] if set.mf == Membership.trimf: - axes.plot([t, t+1, t], param) + if t == start: + axes.plot([t, t+1, t], param, label=set.name) + else: + axes.plot([t, t + 1, t], param) ticks.extend(["t+"+str(t),""]) axes.set_ylabel("Universe of Discourse") axes.set_xlabel("Time") - plt.xticks([k for k in np.arange(0,2*end,1)], ticks, rotation='vertical') + plt.xticks([k for k in np.arange(0,end,1)], ticks, rotation='vertical') + + handles0, labels0 = axes.get_legend_handles_labels() + lgd = axes.legend(handles0, labels0, loc=2, bbox_to_anchor=(1, 1)) plt.tight_layout() diff --git a/pyFTS/partitioners/partitioner.py b/pyFTS/partitioners/partitioner.py index 7ce2ecf..d7f9f4b 100644 --- a/pyFTS/partitioners/partitioner.py +++ b/pyFTS/partitioners/partitioner.py @@ -79,4 +79,4 @@ class Partitioner(object): ax.plot(tmpx, tmpy) def __str__(self): - return self.name + return self.name + ":\n ".join([str(a) + "\n" for a in self.sets]) diff --git a/pyFTS/tests/nonstationary.py b/pyFTS/tests/nonstationary.py index 8022803..3df23c1 100644 --- a/pyFTS/tests/nonstationary.py +++ b/pyFTS/tests/nonstationary.py @@ -1,10 +1,13 @@ +import os import numpy as np from pyFTS.common import Membership from pyFTS.nonstationary import common,perturbation,util,nsfts from pyFTS.partitioners import Grid import matplotlib.pyplot as plt +import pandas as pd +os.chdir("/home/petronio/Dropbox/Doutorado/Codigos/") - +""" def generate_heteroskedastic_linear(mu_ini, sigma_ini, mu_inc, sigma_inc, it=10, num=35): mu = mu_ini sigma = sigma_ini @@ -17,6 +20,8 @@ def generate_heteroskedastic_linear(mu_ini, sigma_ini, mu_inc, sigma_inc, it=10, lmv1 = generate_heteroskedastic_linear(1,0.1,1,0.3) +#lmv1 = generate_heteroskedastic_linear(5,0.1,0,0.2) +#lmv1 = generate_heteroskedastic_linear(1,0.3,1,0) ns = 5 #number of fuzzy sets ts = 200 @@ -25,19 +30,54 @@ test = lmv1[ts:] w = 25 deg = 4 -fig, axes = plt.subplots(nrows=1, ncols=1, figsize=[10,5]) - tmp_fs = Grid.GridPartitioner(train[:35], 10) fs = common.PolynomialNonStationaryPartitioner(train, tmp_fs, window_size=35, degree=1) nsfts1 = nsfts.NonStationaryFTS("", partitioner=fs) -nsfts1.train(train[:35]) +nsfts1.train(train[:100]) -tmp = nsfts1.forecast(test, time_displacement=200) +print(fs) -axes.plot(test) -axes.plot(tmp) +print(nsfts1) + +tmp = nsfts1.forecast(test[:10], time_displacement=200) print(tmp) +""" + +passengers = pd.read_csv("DataSets/AirPassengers.csv", sep=",") +passengers = np.array(passengers["Passengers"]) + +ts = 80 + +trainp = passengers[:ts] +testp = passengers[ts:] + +tmp_fsp = Grid.GridPartitioner(trainp[:50], 10) + +fsp = common.PolynomialNonStationaryPartitioner(trainp, tmp_fsp, window_size=20, degree=1) + +nsftsp = nsfts.NonStationaryFTS("", partitioner=fsp) + +nsftsp.train(trainp[:50]) + +print(fsp) + +print(nsftsp) + +tmpp = nsftsp.forecast(testp, time_displacement=ts) + +print(testp) +print(tmpp) + +#fig, axes = plt.subplots(nrows=1, ncols=1, figsize=[15,5]) + +""" +axes.plot(testp, label="Original") +#axes.plot(tmpp, label="NSFTS") + +handles0, labels0 = axes.get_legend_handles_labels() +lgd = axes.legend(handles0, labels0, loc=2) +""" \ No newline at end of file