From c6f9af8e00642b50485531d21598ed19fd110a8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido?= Date: Wed, 1 Aug 2018 13:59:33 -0300 Subject: [PATCH] Improvements on benchmarks.sliding_window_benchmarks for robustness; Bugfixes Hwang and CVFTS methods --- pyFTS/benchmarks/benchmarks.py | 18 +++++++---- pyFTS/models/hwang.py | 33 ++++++++++---------- pyFTS/models/nonstationary/cvfts.py | 1 + pyFTS/tests/nonstationary.py | 47 +++++++++++++++++++++++------ 4 files changed, 68 insertions(+), 31 deletions(-) diff --git a/pyFTS/benchmarks/benchmarks.py b/pyFTS/benchmarks/benchmarks.py index 07d8e14..fcdc02d 100644 --- a/pyFTS/benchmarks/benchmarks.py +++ b/pyFTS/benchmarks/benchmarks.py @@ -216,7 +216,6 @@ def sliding_window_benchmarks(data, windowsize, train=0.8, **kwargs): nodes = kwargs.get("nodes", ['127.0.0.1']) cluster, http_server = cUtil.start_dispy_cluster(experiment_method, nodes) - experiments = 0 jobs = [] inc = __pop("inc", 0.1, kwargs) @@ -242,8 +241,12 @@ def sliding_window_benchmarks(data, windowsize, train=0.8, **kwargs): if not distributed: if progress: progressbar.update(1) - job = experiment_method(deepcopy(model), None, train, test, **kwargs) - synthesis_method(dataset, tag, job, conn) + try: + 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) else: job = cluster.submit(deepcopy(model), None, train, test, **kwargs) jobs.append(job) @@ -257,7 +260,6 @@ def sliding_window_benchmarks(data, windowsize, train=0.8, **kwargs): for partition in partitions: for partitioner in partitioners_methods: - print(transformation, partition) data_train_fs = partitioner(data=train, npart=partition, transformation=transformation) @@ -276,8 +278,12 @@ def sliding_window_benchmarks(data, windowsize, train=0.8, **kwargs): if not distributed: if progress: progressbar.update(1) - job = experiment_method(deepcopy(model), deepcopy(partitioner), train, test, **kwargs) - synthesis_method(dataset, tag, job, conn) + try: + 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) 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/models/hwang.py b/pyFTS/models/hwang.py index dbfb050..48a0dce 100644 --- a/pyFTS/models/hwang.py +++ b/pyFTS/models/hwang.py @@ -17,18 +17,15 @@ class HighOrderFTS(fts.FTS): self.name = "Hwang High Order FTS" self.shortname = "Hwang" self.detail = "Hwang" + self.configure_lags(**kwargs) + + def configure_lags(self, **kwargs): + if "order" in kwargs: + self.order = kwargs.get("order", 2) + + self.max_lag = self.order def forecast(self, ndata, **kwargs): - - if 'order' in kwargs: - self.order = kwargs.get('order',self.order) - self.max_lag = self.order - - if self.sets == None: - self.sets = self.partitioner.sets - ordered_sets = self.partitioner.ordered_sets - else: - ordered_sets = FuzzySet.set_ordered(self.sets) l = len(self.sets) @@ -42,17 +39,17 @@ class HighOrderFTS(fts.FTS): for t in np.arange(self.order-1, len(ndata)): for ix in range(l): - s = ordered_sets[ix] - cn[ix] = self.sets[s].membership( FuzzySet.grant_bounds(ndata[t], self.sets, ordered_sets)) - for w in range(self.order - 1): - ow[w, ix] = self.sets[s].membership(FuzzySet.grant_bounds(ndata[t - w], self.sets, ordered_sets)) + s = self.partitioner.ordered_sets[ix] + cn[ix] = self.sets[s].membership( FuzzySet.grant_bounds(ndata[t], self.sets, self.partitioner.ordered_sets)) + for w in np.arange(self.order-1): + ow[w, ix] = self.sets[s].membership(FuzzySet.grant_bounds(ndata[t - w], self.sets, self.partitioner.ordered_sets)) rn[w, ix] = ow[w, ix] * cn[ix] ft[ix] = max(ft[ix], rn[w, ix]) mft = max(ft) out = 0.0 count = 0.0 for ix in range(l): - s = ordered_sets[ix] + s = self.partitioner.ordered_sets[ix] if ft[ix] == mft: out = out + self.sets[s].centroid count += 1.0 @@ -61,4 +58,8 @@ class HighOrderFTS(fts.FTS): return ret def train(self, data, **kwargs): - pass + + if self.sets == None: + self.sets = self.partitioner.sets + + self.configure_lags(**kwargs) diff --git a/pyFTS/models/nonstationary/cvfts.py b/pyFTS/models/nonstationary/cvfts.py index e7d155e..dfb21f2 100644 --- a/pyFTS/models/nonstationary/cvfts.py +++ b/pyFTS/models/nonstationary/cvfts.py @@ -46,6 +46,7 @@ class ConditionalVarianceFTS(hofts.HighOrderFTS): self.uod_clip = False self.order = 1 self.min_order = 1 + self.max_lag = 1 self.inputs = [] self.forecasts = [] self.residuals = [] diff --git a/pyFTS/tests/nonstationary.py b/pyFTS/tests/nonstationary.py index 50f6827..0782bb2 100644 --- a/pyFTS/tests/nonstationary.py +++ b/pyFTS/tests/nonstationary.py @@ -39,15 +39,44 @@ from pyFTS.partitioners import Grid, Util as pUtil from pyFTS.benchmarks import benchmarks as bchmk from pyFTS.models import chen -tag = 'chen_partitioning' +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}} -for ds in ['IMIV0']: #datasets.keys(): + +tag = 'benchmarks' + +def nsfts_partitioner_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 + + +for ds in datasets.keys(): dataset = datasets[ds] - bchmk.sliding_window_benchmarks(dataset, 4000, train=0.2, inc=0.2, - methods=[chen.ConventionalFTS], - benchmark_models=False, - transformations=[boxcox], #transformations[t] for t in transformations.keys()], - partitions=np.arange(3, 40, 1), - progress=False, type='point', - file="nsfts_benchmarks.db", dataset=ds, tag=tag) \ No newline at end of file + for tf in transformations.keys(): + transformation = transformations[tf] + + partitioning = partitions[ds][tf] + + bchmk.sliding_window_benchmarks(dataset, 2000, train=0.2, inc=0.2, + benchmark_models=False, + methods=[cvfts.ConditionalVarianceFTS], + partitioners_methods=[nsfts_partitioner_builder], + transformations=[transformation], + partitions=[partitioning], + progress=False, type='point', + file="nsfts_benchmarks.db", dataset=ds, tag=tag) \ No newline at end of file