From f5652f7029836fc12ff5db57d5ec785880474304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido?= Date: Thu, 2 Aug 2018 11:13:24 -0300 Subject: [PATCH] Bugfixes in FTS, HOFTS and CVFTS --- pyFTS/benchmarks/Measures.py | 8 ++-- pyFTS/benchmarks/benchmarks.py | 12 +++-- pyFTS/common/fts.py | 2 +- pyFTS/models/nonstationary/cvfts.py | 3 +- pyFTS/models/nonstationary/partitioners.py | 16 +++++++ pyFTS/tests/nonstationary.py | 55 +++++++++++++--------- 6 files changed, 64 insertions(+), 32 deletions(-) diff --git a/pyFTS/benchmarks/Measures.py b/pyFTS/benchmarks/Measures.py index 7cdd07a..39c6fa9 100644 --- a/pyFTS/benchmarks/Measures.py +++ b/pyFTS/benchmarks/Measures.py @@ -332,11 +332,11 @@ def get_point_statistics(data, model, **kwargs): if not isinstance(forecasts, (list, np.ndarray)): forecasts = [forecasts] - nforecasts = np.array(forecasts[:-1]) + forecasts = np.array(forecasts[:-1]) - ret.append(np.round(rmse(ndata[model.max_lag:], nforecasts), 2)) - ret.append(np.round(mape(ndata[model.max_lag:], nforecasts), 2)) - ret.append(np.round(UStatistic(ndata[model.max_lag:], nforecasts), 2)) + ret.append(np.round(rmse(ndata[model.max_lag:], forecasts), 2)) + ret.append(np.round(mape(ndata[model.max_lag:], forecasts), 2)) + ret.append(np.round(UStatistic(ndata[model.max_lag:], forecasts), 2)) else: steps_ahead_sampler = kwargs.get('steps_ahead_sampler', 1) nforecasts = [] diff --git a/pyFTS/benchmarks/benchmarks.py b/pyFTS/benchmarks/benchmarks.py index fcdc02d..bd566aa 100644 --- a/pyFTS/benchmarks/benchmarks.py +++ b/pyFTS/benchmarks/benchmarks.py @@ -7,6 +7,7 @@ import datetime import time from copy import deepcopy +import traceback import matplotlib as plt import matplotlib.cm as cmx @@ -245,8 +246,8 @@ def sliding_window_benchmarks(data, windowsize, train=0.8, **kwargs): job = experiment_method(deepcopy(model), None, train, test, **kwargs) synthesis_method(dataset, tag, job, conn) except Exception as ex: - print("Error evaluating model " + model.shortname) - print(ex) + print('EXCEPTION! ', model.shortname, model.order) + traceback.print_exc() else: job = cluster.submit(deepcopy(model), None, train, test, **kwargs) jobs.append(job) @@ -279,11 +280,14 @@ def sliding_window_benchmarks(data, windowsize, train=0.8, **kwargs): if progress: progressbar.update(1) try: + print(model.shortname, model.order, partitioner.name, + partitioner.partitions, str(partitioner.transformation)) job = experiment_method(deepcopy(model), deepcopy(partitioner), train, test, **kwargs) synthesis_method(dataset, tag, job, conn) except Exception as ex: - print("Error evaluating model " + model.shortname + " " + str(partitioner)) - print(ex) + print('EXCEPTION! ',model.shortname, model.order, partitioner.name, + partitioner.partitions, str(partitioner.transformation)) + traceback.print_exc() else: job = cluster.submit(deepcopy(model), deepcopy(partitioner), train, test, **kwargs) job.id = id # associate an ID to identify jobs (if needed later) diff --git a/pyFTS/common/fts.py b/pyFTS/common/fts.py index 42d1a8a..48b9959 100644 --- a/pyFTS/common/fts.py +++ b/pyFTS/common/fts.py @@ -144,7 +144,7 @@ class FTS(object): if not self.is_multivariate: kwargs['type'] = type - ret = self.apply_inverse_transformations(ret, params=[data[self.order - 1:]], **kwargs) + ret = self.apply_inverse_transformations(ret, params=[data[self.max_lag - 1:]], **kwargs) return ret diff --git a/pyFTS/models/nonstationary/cvfts.py b/pyFTS/models/nonstationary/cvfts.py index dfb21f2..d64cc9a 100644 --- a/pyFTS/models/nonstationary/cvfts.py +++ b/pyFTS/models/nonstationary/cvfts.py @@ -38,6 +38,7 @@ class ConditionalVarianceFTS(hofts.HighOrderFTS): self.shortname = "CVFTS " self.detail = "" self.flrgs = {} + self.is_high_order = False if self.partitioner is not None: self.append_transformation(self.partitioner.transformation) @@ -256,4 +257,4 @@ class ConditionalVarianceFTS(hofts.HighOrderFTS): ret.append(itvl) - return ret + return ret \ No newline at end of file diff --git a/pyFTS/models/nonstationary/partitioners.py b/pyFTS/models/nonstationary/partitioners.py index e910683..55f5eb1 100644 --- a/pyFTS/models/nonstationary/partitioners.py +++ b/pyFTS/models/nonstationary/partitioners.py @@ -132,3 +132,19 @@ class SimpleNonStationaryPartitioner(partitioner.Partitioner): def build(self, data): return {} + + + +def simplenonstationary_gridpartitioner_builder(data, npart, transformation): + from pyFTS.partitioners import Grid + from pyFTS.models.nonstationary import perturbation, partitioners + + tmp_fs = Grid.GridPartitioner(data=data, npart=npart, transformation=transformation) + fs = partitioners.SimpleNonStationaryPartitioner(data, tmp_fs, + location=perturbation.polynomial, + location_params=[1, 0], + location_roots=0, + width=perturbation.polynomial, + width_params=[1, 0], + width_roots=0) + return fs diff --git a/pyFTS/tests/nonstationary.py b/pyFTS/tests/nonstationary.py index 0782bb2..b4e74cf 100644 --- a/pyFTS/tests/nonstationary.py +++ b/pyFTS/tests/nonstationary.py @@ -1,7 +1,7 @@ import os import numpy as np from pyFTS.common import Membership, Transformations -from pyFTS.models.nonstationary import common, perturbation, partitioners, util +from pyFTS.models.nonstationary import common, perturbation, partitioners as nspart, util from pyFTS.models.nonstationary import nsfts, cvfts from pyFTS.partitioners import Grid, Entropy import matplotlib.pyplot as plt @@ -33,36 +33,47 @@ tdiff = Transformations.Differential(1) boxcox = Transformations.BoxCox(0) -transformations = {'None': None, 'Differential(1)': tdiff, 'BoxCox(0)': boxcox } +transformations = { + 'None': None, + 'Differential(1)': tdiff, + 'BoxCox(0)': boxcox +} from pyFTS.partitioners import Grid, Util as pUtil from pyFTS.benchmarks import benchmarks as bchmk -from pyFTS.models import chen +from pyFTS.models import chen, hofts, pwfts, hwang -partitions = {'CMIV': {'BoxCox(0)': 17, 'Differential(1)': 7, 'None': 13}, - 'IMCV': {'BoxCox(0)': 22, 'Differential(1)': 9, 'None': 25}, - 'IMIV': {'BoxCox(0)': 27, 'Differential(1)': 11, 'None': 6}, - 'NASDAQ': {'BoxCox(0)': 39, 'Differential(1)': 10, 'None': 34}, - 'SP500': {'BoxCox(0)': 38, 'Differential(1)': 15, 'None': 39}, - 'TAIEX': {'BoxCox(0)': 36, 'Differential(1)': 18, 'None': 38}} +partitions = {'CMIV': {'BoxCox(0)': 36, 'Differential(1)': 11, 'None': 8}, + 'IMCV': {'BoxCox(0)': 36, 'Differential(1)': 20, 'None': 16}, + 'IMIV': {'BoxCox(0)': 39, 'Differential(1)': 12, 'None': 6}, + 'IMIV0': {'BoxCox(0)': 39, 'Differential(1)': 12, 'None': 3}, + 'NASDAQ': {'BoxCox(0)': 39, 'Differential(1)': 13, 'None': 36}, + 'SP500': {'BoxCox(0)': 33, 'Differential(1)': 7, 'None': 33}, + 'TAIEX': {'BoxCox(0)': 39, 'Differential(1)': 31, 'None': 33}} tag = 'benchmarks' +''' +for ds in datasets.keys(): + dataset = datasets[ds] -def nsfts_partitioner_builder(data, npart, transformation): - from pyFTS.partitioners import Grid - from pyFTS.models.nonstationary import perturbation, partitioners + for tf in transformations.keys(): + transformation = transformations[tf] - tmp_fs = Grid.GridPartitioner(data=data, npart=npart, transformation=transformation) - fs = partitioners.SimpleNonStationaryPartitioner(data, tmp_fs, - location=perturbation.polynomial, - location_params=[1, 0], - location_roots=0, - width=perturbation.polynomial, - width_params=[1, 0], - width_roots=0) - return fs + partitioning = partitions[ds][tf] + bchmk.sliding_window_benchmarks(dataset, 2000, train=0.2, inc=0.2, + methods=[ + hwang.HighOrderFTS, + hofts.HighOrderFTS, + pwfts.ProbabilisticWeightedFTS], + #orders = [3], + benchmark_models=False, + transformations=[transformation], + partitions=[partitioning], + progress=False, type='point', + file="nsfts_benchmarks.db", dataset=ds, tag=tag) +''' for ds in datasets.keys(): dataset = datasets[ds] @@ -75,7 +86,7 @@ for ds in datasets.keys(): bchmk.sliding_window_benchmarks(dataset, 2000, train=0.2, inc=0.2, benchmark_models=False, methods=[cvfts.ConditionalVarianceFTS], - partitioners_methods=[nsfts_partitioner_builder], + partitioners_methods=[nspart.simplenonstationary_gridpartitioner_builder], transformations=[transformation], partitions=[partitioning], progress=False, type='point',