Versão otimizada estável de PFTS.forecastAheadDistribution
This commit is contained in:
parent
8f2d2c8bcd
commit
29719053d4
@ -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)
|
||||
|
@ -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))
|
96
pfts.py
96
pfts.py
@ -372,27 +372,47 @@ 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)
|
||||
|
||||
grid = self.getGridClean(resolution)
|
||||
|
||||
index = SortedCollection.SortedCollection(iterable=grid.keys())
|
||||
|
||||
#print (index)
|
||||
|
||||
grids = []
|
||||
for k in np.arange(0, steps):
|
||||
grids.append(self.getGridClean(resolution))
|
||||
|
||||
for k in np.arange(self.order, steps + self.order):
|
||||
|
||||
lags = {}
|
||||
|
||||
#print(k)
|
||||
|
||||
cc = 0
|
||||
|
||||
for i in intervals:
|
||||
nq = 2 * cc
|
||||
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):
|
||||
quantiles.append(i[0] + qt * ((i[1] - i[0]) / 100))
|
||||
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))
|
||||
@ -405,6 +425,7 @@ class ProbabilisticFTS(ifts.IntervalFTS):
|
||||
|
||||
cc += 1
|
||||
|
||||
|
||||
# Build the tree with all possible paths
|
||||
|
||||
root = tree.FLRGTreeNode(None)
|
||||
@ -413,43 +434,17 @@ class ProbabilisticFTS(ifts.IntervalFTS):
|
||||
|
||||
#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())
|
||||
|
||||
grids = []
|
||||
for k in np.arange(0, steps):
|
||||
grids.append(self.getGridClean(resolution))
|
||||
# Trace the possible paths
|
||||
|
||||
for p in root.paths():
|
||||
path = list(reversed(list(filter(None.__ne__, p))))
|
||||
|
||||
#print(path)
|
||||
|
||||
for k in np.arange(self.order, steps + self.order):
|
||||
|
||||
sample = path[k - self.order : k]
|
||||
|
||||
#print(sample)
|
||||
|
||||
qtle = self.forecastInterval(sample)
|
||||
|
||||
#grids[k - self.order] = self.gridCountPoints(grids[k - self.order], resolution, np.ravel(qtle))
|
||||
|
||||
# grids[k - self.order] = self.gridCount(grids[k - self.order], resolution, np.ravel(qtle))
|
||||
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):
|
||||
|
Loading…
Reference in New Issue
Block a user