Versão otimizada estável de PFTS.forecastAheadDistribution

This commit is contained in:
Petrônio Cândido de Lima e Silva 2017-01-14 10:27:29 -02:00
parent 8f2d2c8bcd
commit 29719053d4
3 changed files with 59 additions and 95 deletions

View File

@ -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)

View File

@ -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,))
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))

142
pfts.py
View File

@ -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):