From 29719053d4d68deddbd409e4cd18f9bc10d4e873 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido=20de=20Lima=20e=20Silva?= Date: Sat, 14 Jan 2017 10:27:29 -0200 Subject: [PATCH] =?UTF-8?q?Vers=C3=A3o=20otimizada=20est=C3=A1vel=20de=20P?= =?UTF-8?q?FTS.forecastAheadDistribution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- benchmarks/benchmarks.py | 2 +- common/SortedCollection.py | 10 ++- pfts.py | 142 +++++++++++++------------------------ 3 files changed, 59 insertions(+), 95 deletions(-) diff --git a/benchmarks/benchmarks.py b/benchmarks/benchmarks.py index 91df83f..3b24cb1 100644 --- a/benchmarks/benchmarks.py +++ b/benchmarks/benchmarks.py @@ -108,7 +108,7 @@ def plotComparedIntervalsAhead(original, models, colors, distributions, time_fro count = 0 for fts in models: if fts.hasDistributionForecasting and distributions[count]: - density = fts.forecastAheadDistribution2(original[time_from - fts.order:time_from], time_to, resolution) + density = fts.forecastAheadDistribution(original[time_from - fts.order:time_from], time_to, resolution) y = density.columns t = len(y) diff --git a/common/SortedCollection.py b/common/SortedCollection.py index f2dd8de..273169a 100644 --- a/common/SortedCollection.py +++ b/common/SortedCollection.py @@ -201,11 +201,17 @@ class SortedCollection(object): l = bisect_right(self._keys, le) if g != len(self) and l != len(self): return self._items[g : l] - raise ValueError('No item found with key at or above: %r' % (k,)) + raise ValueError('No item found between keys : %r,%r' % (ge,le)) def inside(self, ge, le): g = bisect_right(self._keys, ge) l = bisect_left(self._keys, le) if g != len(self) and l != len(self): return self._items[g : l] - raise ValueError('No item found with key at or above: %r' % (k,)) \ No newline at end of file + elif g != len(self): + return self._items[g-1: l] + elif l != len(self): + 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 diff --git a/pfts.py b/pfts.py index 40256b4..c4838aa 100644 --- a/pfts.py +++ b/pfts.py @@ -372,84 +372,79 @@ class ProbabilisticFTS(ifts.IntervalFTS): for child in node.getChildren(): self.buildTreeWithoutOrder(child, lags, level + 1) - def forecastAheadDistribution2(self, data, steps, resolution): + + def forecastAheadDistribution(self, data, steps, resolution): ret = [] intervals = self.forecastAheadInterval(data, steps) - lags = {} - - cc = 0 - - for i in intervals: - nq = 2 * cc - if nq == 0: nq = 1 - if nq > 50: nq = 50 - st = 50 / nq - - quantiles = [] - - for qt in np.arange(0, 50, st): - quantiles.append(i[0] + qt * ((i[1] - i[0]) / 100)) - quantiles.append(i[0] - qt * ((i[1] - i[0]) / 100)) - quantiles.append(i[0] + ((i[1] - i[0]) / 2)) - - quantiles = list(set(quantiles)) - - 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) - - #return - - # Trace the possible paths and build the PFLRG's - grid = self.getGridClean(resolution) - ##index = SortedCollection.SortedCollection(key=lambda (k,v): itemgetter(1)(v)) - index = SortedCollection.SortedCollection(iterable=grid.keys()) + #print (index) + grids = [] for k in np.arange(0, steps): grids.append(self.getGridClean(resolution)) - for p in root.paths(): - path = list(reversed(list(filter(None.__ne__, p)))) + for k in np.arange(self.order, steps + self.order): - #print(path) + lags = {} - for k in np.arange(self.order, steps + self.order): + #print(k) - sample = path[k - self.order : k] + cc = 0 - #print(sample) + for i in intervals[k - self.order : k]: - qtle = self.forecastInterval(sample) + #print(i) - #grids[k - self.order] = self.gridCountPoints(grids[k - self.order], resolution, np.ravel(qtle)) + nq = 3 * k + if nq == 0: nq = 1 + if nq > 50: nq = 50 + st = 50 / nq - # grids[k - self.order] = self.gridCount(grids[k - self.order], resolution, np.ravel(qtle)) + #print(st) + + quantiles = [] + + for qt in np.arange(0, 50, st): + 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)) + + quantiles = list(set(quantiles)) + + 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)) - #return - - #print(grid) - for k in np.arange(0, steps): tmp = np.array([grids[k][q] for q in sorted(grids[k])]) ret.append(tmp / sum(tmp)) @@ -459,43 +454,6 @@ class ProbabilisticFTS(ifts.IntervalFTS): return df - def forecastAheadDistribution(self, data, steps, resolution): - - ret = [] - - intervals = self.forecastAheadInterval(data, steps) - - for k in np.arange(self.order, steps+self.order): - - grid = self.getGridClean(resolution) - grid = self.gridCount(grid, resolution, intervals[k]) - - nq = 2 * k - if nq > 50: nq = 50 - st = 50 / nq - - for qt in np.arange(0, 50, st): - # print(qt) - qtle_lower = self.forecastInterval( - [intervals[x][0] + qt * ((intervals[x][1] - intervals[x][0]) / 100 ) for x in - np.arange(k - self.order, k)]) - grid = self.gridCount(grid, resolution, np.ravel(qtle_lower)) - qtle_upper = self.forecastInterval( - [intervals[x][1] - qt * ((intervals[x][1] - intervals[x][0]) / 100 ) for x in - np.arange(k - self.order, k)]) - grid = self.gridCount(grid, resolution, np.ravel(qtle_upper)) - qtle_mid = self.forecastInterval( - [intervals[x][0] + (intervals[x][1] - intervals[x][0]) / 2 for x in np.arange(k - self.order, k)]) - grid = self.gridCount(grid, resolution, np.ravel(qtle_mid)) - - tmp = np.array([grid[k] for k in sorted(grid)]) - - ret.append(tmp / sum(tmp)) - - grid = self.getGridClean(resolution) - df = pd.DataFrame(ret, columns=sorted(grid)) - return df - def __str__(self): tmp = self.name + ":\n" for r in sorted(self.flrgs):