def plot_distribution(ax, cmap, probabilitydist, fig, time_from, reference_data=None): """ Plot forecasted ProbabilityDistribution objects on a matplotlib axis :param ax: matplotlib axis :param cmap: matplotlib colormap name :param probabilitydist: list of ProbabilityDistribution objects :param fig: matplotlib figure :param time_from: starting time (on x axis) to begin the plots :param reference_data: :return: """ from matplotlib.patches import Rectangle from matplotlib.collections import PatchCollection patches = [] colors = [] for ct, dt in enumerate(probabilitydist): disp = 0.0 if reference_data is not None: disp = reference_data[time_from+ct] for y in dt.bins: s = Rectangle((time_from+ct, y+disp), 1, dt.resolution, fill=True, lw = 0) patches.append(s) colors.append(dt.density(y)) scale = Transformations.Scale() colors = scale.apply(colors) pc = PatchCollection(patches=patches, match_original=True) pc.set_clim([0, 1]) pc.set_cmap(cmap) pc.set_array(np.array(colors)) ax.add_collection(pc) cb = fig.colorbar(pc, ax=ax) cb.set_label('Density')
def plot_distribution(ax, cmap, probabilitydist, fig, time_from, reference_data=None): from matplotlib.patches import Rectangle from matplotlib.collections import PatchCollection patches = [] colors = [] for ct, dt in enumerate(probabilitydist): disp = 0.0 if reference_data is not None: disp = reference_data[time_from + ct] for y in dt.bins: s = Rectangle((time_from + ct, y + disp), 1, dt.resolution, fill=True, lw=0) patches.append(s) colors.append(dt.density(y)) scale = Transformations.Scale() colors = scale.apply(colors) pc = PatchCollection(patches=patches, match_original=True) pc.set_clim([0, 1]) pc.set_cmap(cmap) pc.set_array(np.array(colors)) ax.add_collection(pc) cb = fig.colorbar(pc, ax=ax) cb.set_label('Density')
def __init__(self, **kwargs): self.h = kwargs.get('h', .5) """Width parameter""" self.kernel = kwargs.get("kernel", "epanechnikov") """Kernel function""" self.data = kwargs.get("data", None) self.transf = Transformations.Scale(min=0, max=1) if self.data is not None: self.data = self.transf.apply(self.data)
def build(self, data): diff = Transformations.Differential(1) data2 = diff.apply(data) davg = np.abs(np.mean(data2) / 2) if davg <= 1.0: base = 0.1 elif 1 < davg <= 10: base = 1.0 elif 10 < davg <= 100: base = 10 else: base = 100 sets = {} kwargs = {'type': self.type, 'variable': self.variable} dlen = self.max - self.min npart = math.ceil(dlen / base) partition = math.ceil(self.min) for c in range(npart): _name = self.get_name(c) if self.membership_function == Membership.trimf: sets[_name] = FuzzySet.FuzzySet( _name, Membership.trimf, [partition - base, partition, partition + base], partition, **kwargs) elif self.membership_function == Membership.gaussmf: sets[_name] = FuzzySet.FuzzySet(_name, Membership.gaussmf, [partition, base / 2], partition) elif self.membership_function == Membership.trapmf: sets[_name] = FuzzySet.FuzzySet(_name, Membership.trapmf, [ partition - base, partition - (base / 2), partition + (base / 2), partition + base ], partition, **kwargs) partition += base return sets
def __init__(self,h, kernel="epanechnikov"): self.h = h """Width parameter""" self.kernel = kernel """Kernel function""" self.transf = Transformations.Scale(min=0,max=1)
#!/usr/bin/python # -*- coding: utf8 -*- import os import numpy as np import matplotlib.pylab as plt #from mpl_toolkits.mplot3d import Axes3D import pandas as pd from pyFTS.common import Transformations tdiff = Transformations.Differential(1) from pyFTS.data import TAIEX, SP500, NASDAQ dataset = TAIEX.get_data() #dataset = SP500.get_data()[11500:16000] #dataset = NASDAQ.get_data() #print(len(dataset)) from pyFTS.partitioners import Grid, Util as pUtil partitioner = Grid.GridPartitioner(data=dataset[:800], npart=10, transformation=tdiff) from pyFTS.common import Util as cUtil from pyFTS.benchmarks import benchmarks as bchmk, Util as bUtil, Measures, knn, quantreg, arima, naive from pyFTS.models import pwfts, song, chen, ifts, hofts from pyFTS.models.ensemble import ensemble model = chen.ConventionalFTS(partitioner=partitioner)
from pyFTS.data import TAIEX as tx from pyFTS.common import Transformations from pyFTS.data import SONDA df = SONDA.get_dataframe() train = df.iloc[0:578241] #three years test = df.iloc[1572480:2096640] #one year del df from pyFTS.partitioners import Grid, Util as pUtil from pyFTS.common import Transformations, Util from pyFTS.models.multivariate import common, variable, mvfts from pyFTS.models.seasonal import partitioner as seasonal from pyFTS.models.seasonal.common import DateTime bc = Transformations.BoxCox(0) tdiff = Transformations.Differential(1) np = 10 model = mvfts.MVFTS("") fig, axes = plt.subplots(nrows=5, ncols=1, figsize=[15, 10]) sp = { 'seasonality': DateTime.day_of_year, 'names': [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
def SelecaoSimples_MenorRMSE(original, parameters, modelo): ret = [] errors = [] forecasted_best = [] print("Série Original") fig = plt.figure(figsize=[20, 12]) fig.suptitle("Comparação de modelos ") ax0 = fig.add_axes([0, 0.5, 0.65, 0.45]) # left, bottom, width, height ax0.set_xlim([0, len(original)]) ax0.set_ylim([min(original), max(original)]) ax0.set_title('Série Temporal') ax0.set_ylabel('F(T)') ax0.set_xlabel('T') ax0.plot(original, label="Original") min_rmse = 100000.0 best = None for p in parameters: sets = Grid.GridPartitioner(data=original, npart=p).sets fts = modelo(str(p) + " particoes") fts.train(original, sets=sets) # print(original) forecasted = fts.forecast(original) forecasted.insert(0, original[0]) # print(forecasted) ax0.plot(forecasted, label=fts.name) error = Measures.rmse(np.array(forecasted), np.array(original)) print(p, error) errors.append(error) if error < min_rmse: min_rmse = error best = fts forecasted_best = forecasted handles0, labels0 = ax0.get_legend_handles_labels() ax0.legend(handles0, labels0) ax1 = fig.add_axes([0.7, 0.5, 0.3, 0.45]) # left, bottom, width, height ax1.set_title('Comparação dos Erros Quadráticos Médios') ax1.set_ylabel('RMSE') ax1.set_xlabel('Quantidade de Partições') ax1.set_xlim([min(parameters), max(parameters)]) ax1.plot(parameters, errors) ret.append(best) ret.append(forecasted_best) # Modelo diferencial print("\nSérie Diferencial") difffts = Transformations.differential(original) errors = [] forecastedd_best = [] ax2 = fig.add_axes([0, 0, 0.65, 0.45]) # left, bottom, width, height ax2.set_xlim([0, len(difffts)]) ax2.set_ylim([min(difffts), max(difffts)]) ax2.set_title('Série Temporal') ax2.set_ylabel('F(T)') ax2.set_xlabel('T') ax2.plot(difffts, label="Original") min_rmse = 100000.0 bestd = None for p in parameters: sets = Grid.GridPartitioner(data=difffts, npart=p) fts = modelo(str(p) + " particoes") fts.train(difffts, sets=sets) forecasted = fts.forecast(difffts) forecasted.insert(0, difffts[0]) ax2.plot(forecasted, label=fts.name) error = Measures.rmse(np.array(forecasted), np.array(difffts)) print(p, error) errors.append(error) if error < min_rmse: min_rmse = error bestd = fts forecastedd_best = forecasted handles0, labels0 = ax2.get_legend_handles_labels() ax2.legend(handles0, labels0) ax3 = fig.add_axes([0.7, 0, 0.3, 0.45]) # left, bottom, width, height ax3.set_title('Comparação dos Erros Quadráticos Médios') ax3.set_ylabel('RMSE') ax3.set_xlabel('Quantidade de Partições') ax3.set_xlim([min(parameters), max(parameters)]) ax3.plot(parameters, errors) ret.append(bestd) ret.append(forecastedd_best) return ret
import os import numpy as np import matplotlib.pylab as plt import pandas as pd from pyFTS.common import Util as cUtil, FuzzySet from pyFTS.partitioners import Grid, Entropy, Util as pUtil, Simple from pyFTS.benchmarks import benchmarks as bchmk, Measures from pyFTS.models import chen, yu, cheng, ismailefendi, hofts, pwfts, tsaur, song, sadaei, ifts from pyFTS.models.ensemble import ensemble from pyFTS.common import Membership, Util from pyFTS.benchmarks import arima, quantreg, BSTS, gaussianproc, knn from pyFTS.common import Transformations tdiff = Transformations.Differential(1) boxcox = Transformations.BoxCox(0) from pyFTS.data import Enrollments, AirPassengers ''' data = AirPassengers.get_data() roi = Transformations.ROI() #plt.plot(data) _roi = roi.apply(data) #plt.plot(_roi)
def __init__(self, h, kernel="epanechnikov"): self.h = h self.kernel = kernel self.transf = Transformations.Scale(min=0, max=1)
from pyFTS.partitioners import Grid from pyFTS.models import chen from pyFTS.benchmarks import Measures from pyFTS.common import Util as cUtil, fts import pandas as pd import numpy as np import os from pyFTS.common import Transformations from copy import deepcopy from pyFTS.nonstationary import flrg, util, honsfts, partitioners from pyFTS.models.nonstationary import nsfts bc = Transformations.BoxCox(0) import dispy import dispy.httpd os.chdir("/home/petronio/Dropbox/Doutorado/Codigos/") def evaluate_individual_model(model, partitioner, train, test, window_size, time_displacement): import numpy as np from pyFTS.partitioners import Grid from pyFTS.benchmarks import Measures try: model.train(train, sets=partitioner.sets, order=model.order, parameters=window_size) forecasts = model.forecast(test, time_displacement=time_displacement, window_size=window_size) _rmse = Measures.rmse(test[model.order:], forecasts[:-1]) _mape = Measures.mape(test[model.order:], forecasts[:-1]) _u = Measures.UStatistic(test[model.order:], forecasts[:-1])
train_split = 1000 test_length = 200 from pyFTS.common import Transformations from pyFTS.benchmarks import naive from pyFTS.models.incremental import TimeVariant, IncrementalEnsemble from pyFTS.models.nonstationary import common, perturbation, partitioners as nspart from pyFTS.models.nonstationary import nsfts, util as nsUtil from pyFTS.partitioners import Grid from pyFTS.models import hofts, pwfts from pyFTS.benchmarks import Measures from pyFTS.common import Transformations, Util as cUtil diff = Transformations.Differential(lag=1) train = dataset[:1000] test = dataset[1000:] #grid = Grid.GridPartitioner(data=train, transformation=diff) #model = pwfts.ProbabilisticWeightedFTS(partitioner=grid) #model.append_transformation(diff) model = naive.Naive() model.fit(train) for ct, ttrain, ttest in cUtil.sliding_window(test, 1000, .95, inc=.5): if model.shortname not in ('PWFTS', 'Naive'): model.predict(ttrain)