- Non Stationary Fuzzy Time Series - NSFTS: stable version

This commit is contained in:
Petrônio Cândido 2017-10-06 13:04:33 -03:00
parent b3db1a60a3
commit a8e3ba4810
6 changed files with 74 additions and 14 deletions

View File

@ -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

View File

@ -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]

View File

@ -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:]])

View File

@ -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()

View File

@ -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])

View File

@ -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)
"""