- Otimizações em pfts.forecastAheadDistribution

- Correção da Issue #1
This commit is contained in:
Petrônio Cândido de Lima e Silva 2017-01-16 18:32:15 -02:00
parent 29719053d4
commit 85a47e225a
4 changed files with 28 additions and 48 deletions

View File

@ -108,27 +108,18 @@ def plotComparedIntervalsAhead(original, models, colors, distributions, time_fro
count = 0 count = 0
for fts in models: for fts in models:
if fts.hasDistributionForecasting and distributions[count]: 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 y = density.columns
t = len(y) 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: for k in density.index:
alpha = np.array([density[q][k] for q in density]) * 100 alpha = np.array([density[q][k] for q in density]) * 100
x = [time_from + k for x in np.arange(0, t)] x = [time_from + k for x in np.arange(0, t)]
for cc in np.arange(0,resolution,5): for cc in np.arange(0,resolution,5):
ax.scatter(x, y+cc, c=alpha, marker='s', linewidths=0, cmap='Oranges', ax.scatter(x, y+cc, c=alpha, marker='s', linewidths=0, cmap='Oranges', edgecolors=None)
norm=pltcolors.Normalize(vmin=0, vmax=1), vmin=0, vmax=1, edgecolors=None)
if interpol and k < max(density.index): if interpol and k < max(density.index):
diffs = [(density[q][k + 1] - density[q][k])/50 for q in density] diffs = [(density[q][k + 1] - density[q][k])/50 for q in density]
for p in np.arange(0,50): for p in np.arange(0,50):

View File

@ -1,6 +1,6 @@
import numpy as np import numpy as np
from pyFTS.common import FuzzySet, FLR from pyFTS.common import FuzzySet, FLR
import fts from pyFTS import fts
class ConventionalFLRG: class ConventionalFLRG:

View File

@ -214,4 +214,11 @@ class SortedCollection(object):
return self._items[g: l-1] return self._items[g: l-1]
else: else:
return self._items[g - 1: l - 1] return self._items[g - 1: l - 1]
raise ValueError('No item found inside keys: %r,%r' % (ge,le)) 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,))

52
pfts.py
View File

@ -140,9 +140,9 @@ class ProbabilisticFTS(ifts.IntervalFTS):
idx = np.ravel(tmp) # flatten the array idx = np.ravel(tmp) # flatten the array
if idx.size == 0: # the element is out of the bounds of the Universe of Discourse 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] idx = [0]
elif math.ceil(instance) >= self.sets[-1].upper: elif instance >= self.sets[-1].upper:
idx = [len(self.sets) - 1] idx = [len(self.sets) - 1]
else: else:
raise Exception(instance) raise Exception(instance)
@ -348,19 +348,19 @@ class ProbabilisticFTS(ifts.IntervalFTS):
return grid return grid
def gridCount(self, grid, resolution, interval): def gridCount(self, grid, resolution, index, 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):
#print(interval) #print(interval)
for k in index.inside(interval[0],interval[1]): for k in index.inside(interval[0],interval[1]):
#print(k) #print(k)
grid[k] += 1 grid[k] += 1
return grid 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): def buildTreeWithoutOrder(self, node, lags, level):
if level not in lags: if level not in lags:
@ -372,8 +372,7 @@ class ProbabilisticFTS(ifts.IntervalFTS):
for child in node.getChildren(): for child in node.getChildren():
self.buildTreeWithoutOrder(child, lags, level + 1) self.buildTreeWithoutOrder(child, lags, level + 1)
def forecastAheadDistribution(self, data, steps, resolution,parameters=None):
def forecastAheadDistribution(self, data, steps, resolution):
ret = [] ret = []
@ -383,8 +382,6 @@ class ProbabilisticFTS(ifts.IntervalFTS):
index = SortedCollection.SortedCollection(iterable=grid.keys()) index = SortedCollection.SortedCollection(iterable=grid.keys())
#print (index)
grids = [] grids = []
for k in np.arange(0, steps): for k in np.arange(0, steps):
grids.append(self.getGridClean(resolution)) grids.append(self.getGridClean(resolution))
@ -393,24 +390,13 @@ class ProbabilisticFTS(ifts.IntervalFTS):
lags = {} lags = {}
#print(k)
cc = 0 cc = 0
for i in intervals[k - self.order : k]: 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 = [] 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[0] + qt * ((i[1] - i[0]) / 100))
quantiles.append(i[1] - 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.append(i[0] + ((i[1] - i[0]) / 2))
@ -419,31 +405,27 @@ class ProbabilisticFTS(ifts.IntervalFTS):
quantiles.sort() quantiles.sort()
#print(quantiles)
lags[cc] = quantiles lags[cc] = quantiles
cc += 1 cc += 1
# Build the tree with all possible paths # Build the tree with all possible paths
root = tree.FLRGTreeNode(None) root = tree.FLRGTreeNode(None)
self.buildTreeWithoutOrder(root, lags, 0) self.buildTreeWithoutOrder(root, lags, 0)
#print(root)
# Trace the possible paths # Trace the possible paths
for p in root.paths(): for p in root.paths():
path = list(reversed(list(filter(None.__ne__, p)))) path = list(reversed(list(filter(None.__ne__, p))))
#print(path) if parameters is None:
qtle = self.forecastInterval(path)
qtle = self.forecastInterval(path) grids[k - self.order] = self.gridCount(grids[k - self.order], resolution, index, np.ravel(qtle))
else:
grids[k - self.order] = self.gridCountIndexed(grids[k - self.order], resolution, index, np.ravel(qtle)) 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): for k in np.arange(0, steps):
tmp = np.array([grids[k][q] for q in sorted(grids[k])]) tmp = np.array([grids[k][q] for q in sorted(grids[k])])