From 85a47e225a980d0229a18b9011211e118e5b2ccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido=20de=20Lima=20e=20Silva?= Date: Mon, 16 Jan 2017 18:32:15 -0200 Subject: [PATCH] =?UTF-8?q?-=20Otimiza=C3=A7=C3=B5es=20em=20pfts.forecastA?= =?UTF-8?q?headDistribution=20-=20Corre=C3=A7=C3=A3o=20da=20Issue=20#1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- benchmarks/benchmarks.py | 13 ++-------- chen.py | 2 +- common/SortedCollection.py | 9 ++++++- pfts.py | 52 +++++++++++++------------------------- 4 files changed, 28 insertions(+), 48 deletions(-) diff --git a/benchmarks/benchmarks.py b/benchmarks/benchmarks.py index 3b24cb1..fa8776a 100644 --- a/benchmarks/benchmarks.py +++ b/benchmarks/benchmarks.py @@ -108,27 +108,18 @@ def plotComparedIntervalsAhead(original, models, colors, distributions, time_fro count = 0 for fts in models: if fts.hasDistributionForecasting and distributions[count]: - density = fts.forecastAheadDistribution(original[time_from - fts.order:time_from], time_to, resolution) + density = fts.forecastAheadDistribution(original[time_from - fts.order:time_from], time_to, resolution, parameters=None) y = density.columns t = len(y) - # interpol between time_from and time_from+1 - #if interpol: - # diffs = [density[q][0] / 50 for q in density] - # for p in np.arange(0, 50): - # xx = [(time_from - 1) + 0.02 * p for q in np.arange(0, t)] - # alpha2 = np.array([diffs[q] * p for q in np.arange(0, t)]) * 100 - # ax.scatter(xx, y, c=alpha2, marker='s', linewidths=0, cmap='Oranges', - # norm=pltcolors.Normalize(vmin=0, vmax=1), vmin=0, vmax=1, edgecolors=None) for k in density.index: alpha = np.array([density[q][k] for q in density]) * 100 x = [time_from + k for x in np.arange(0, t)] for cc in np.arange(0,resolution,5): - ax.scatter(x, y+cc, c=alpha, marker='s', linewidths=0, cmap='Oranges', - norm=pltcolors.Normalize(vmin=0, vmax=1), vmin=0, vmax=1, edgecolors=None) + ax.scatter(x, y+cc, c=alpha, marker='s', linewidths=0, cmap='Oranges', edgecolors=None) if interpol and k < max(density.index): diffs = [(density[q][k + 1] - density[q][k])/50 for q in density] for p in np.arange(0,50): diff --git a/chen.py b/chen.py index 6b7877a..fb6c7b4 100644 --- a/chen.py +++ b/chen.py @@ -1,6 +1,6 @@ import numpy as np from pyFTS.common import FuzzySet, FLR -import fts +from pyFTS import fts class ConventionalFLRG: diff --git a/common/SortedCollection.py b/common/SortedCollection.py index 273169a..7356ac9 100644 --- a/common/SortedCollection.py +++ b/common/SortedCollection.py @@ -214,4 +214,11 @@ class SortedCollection(object): return self._items[g: l-1] else: return self._items[g - 1: l - 1] - raise ValueError('No item found inside keys: %r,%r' % (ge,le)) \ No newline at end of file + raise ValueError('No item found inside keys: %r,%r' % (ge,le)) + + def around(self, k): + g = bisect_right(self._keys, k) + l = bisect_left(self._keys, k) + if g != len(self) and l != len(self): + return self._items[g : l] + raise ValueError('No item found around key : %r' % (k,)) \ No newline at end of file diff --git a/pfts.py b/pfts.py index c4838aa..6522947 100644 --- a/pfts.py +++ b/pfts.py @@ -140,9 +140,9 @@ class ProbabilisticFTS(ifts.IntervalFTS): idx = np.ravel(tmp) # flatten the array if idx.size == 0: # the element is out of the bounds of the Universe of Discourse - if math.ceil(instance) <= self.sets[0].lower: + if instance <= self.sets[0].lower: idx = [0] - elif math.ceil(instance) >= self.sets[-1].upper: + elif instance >= self.sets[-1].upper: idx = [len(self.sets) - 1] else: raise Exception(instance) @@ -348,19 +348,19 @@ class ProbabilisticFTS(ifts.IntervalFTS): return grid - def gridCount(self, grid, resolution, interval): - for sbin in sorted(grid): - if sbin >= interval[0] and (sbin + resolution) <= interval[1]: - grid[sbin] = grid[sbin] + 1 - return grid - - def gridCountIndexed(self, grid, resolution, index, interval): + def gridCount(self, grid, resolution, index, interval): #print(interval) for k in index.inside(interval[0],interval[1]): #print(k) grid[k] += 1 return grid + def gridCountPoint(self, grid, resolution, index, point): + k = index.find_ge(point) + # print(k) + grid[k] += 1 + return grid + def buildTreeWithoutOrder(self, node, lags, level): if level not in lags: @@ -372,8 +372,7 @@ class ProbabilisticFTS(ifts.IntervalFTS): for child in node.getChildren(): self.buildTreeWithoutOrder(child, lags, level + 1) - - def forecastAheadDistribution(self, data, steps, resolution): + def forecastAheadDistribution(self, data, steps, resolution,parameters=None): ret = [] @@ -383,8 +382,6 @@ class ProbabilisticFTS(ifts.IntervalFTS): index = SortedCollection.SortedCollection(iterable=grid.keys()) - #print (index) - grids = [] for k in np.arange(0, steps): grids.append(self.getGridClean(resolution)) @@ -393,24 +390,13 @@ class ProbabilisticFTS(ifts.IntervalFTS): lags = {} - #print(k) - cc = 0 for i in intervals[k - self.order : k]: - #print(i) - - nq = 3 * k - if nq == 0: nq = 1 - if nq > 50: nq = 50 - st = 50 / nq - - #print(st) - quantiles = [] - for qt in np.arange(0, 50, st): + for qt in np.arange(0, 50, 2): quantiles.append(i[0] + qt * ((i[1] - i[0]) / 100)) quantiles.append(i[1] - qt * ((i[1] - i[0]) / 100)) quantiles.append(i[0] + ((i[1] - i[0]) / 2)) @@ -419,31 +405,27 @@ class ProbabilisticFTS(ifts.IntervalFTS): quantiles.sort() - #print(quantiles) - lags[cc] = quantiles cc += 1 - # Build the tree with all possible paths root = tree.FLRGTreeNode(None) self.buildTreeWithoutOrder(root, lags, 0) - #print(root) - # Trace the possible paths for p in root.paths(): path = list(reversed(list(filter(None.__ne__, p)))) - #print(path) - - qtle = self.forecastInterval(path) - - grids[k - self.order] = self.gridCountIndexed(grids[k - self.order], resolution, index, np.ravel(qtle)) + if parameters is None: + qtle = self.forecastInterval(path) + grids[k - self.order] = self.gridCount(grids[k - self.order], resolution, index, np.ravel(qtle)) + else: + qtle = self.forecast(path) + grids[k - self.order] = self.gridCountPoint(grids[k - self.order], resolution, index, np.ravel(qtle)) for k in np.arange(0, steps): tmp = np.array([grids[k][q] for q in sorted(grids[k])])