2016-10-25 20:04:37 +04:00
|
|
|
import numpy as np
|
2016-11-06 03:24:36 +04:00
|
|
|
import pandas as pd
|
2016-10-25 20:04:37 +04:00
|
|
|
from pyFTS import *
|
|
|
|
|
2016-10-26 19:01:30 +04:00
|
|
|
class ProbabilisticFLRG(hofts.HighOrderFLRG):
|
2016-10-25 20:04:37 +04:00
|
|
|
def __init__(self,order):
|
2016-10-26 19:01:30 +04:00
|
|
|
super(ProbabilisticFLRG, self).__init__(order)
|
2016-10-25 21:52:44 +04:00
|
|
|
self.RHS = {}
|
2016-10-25 20:04:37 +04:00
|
|
|
self.frequencyCount = 0
|
2016-10-25 21:52:44 +04:00
|
|
|
|
2016-10-25 20:04:37 +04:00
|
|
|
def appendRHS(self,c):
|
|
|
|
self.frequencyCount = self.frequencyCount + 1
|
2016-10-25 21:52:44 +04:00
|
|
|
if c.name in self.RHS:
|
|
|
|
self.RHS[c.name] = self.RHS[c.name] + 1
|
2016-10-25 20:04:37 +04:00
|
|
|
else:
|
2016-10-25 21:52:44 +04:00
|
|
|
self.RHS[c.name] = 1
|
|
|
|
|
|
|
|
def getProbability(self,c):
|
|
|
|
return self.RHS[c] / self.frequencyCount
|
2016-10-25 20:04:37 +04:00
|
|
|
|
|
|
|
def __str__(self):
|
2016-10-25 21:52:44 +04:00
|
|
|
tmp2 = ""
|
|
|
|
for c in sorted(self.RHS):
|
|
|
|
if len(tmp2) > 0:
|
2016-10-26 19:01:30 +04:00
|
|
|
tmp2 = tmp2 + ", "
|
2016-10-25 21:52:44 +04:00
|
|
|
tmp2 = tmp2 + c + "(" + str(round(self.RHS[c]/self.frequencyCount,3)) + ")"
|
|
|
|
return self.strLHS() + " -> " + tmp2
|
2016-10-25 20:04:37 +04:00
|
|
|
|
|
|
|
class ProbabilisticIntervalFTS(ifts.IntervalFTS):
|
|
|
|
def __init__(self,name):
|
2016-10-25 22:21:32 +04:00
|
|
|
super(ProbabilisticIntervalFTS, self).__init__("PIFTS")
|
2016-10-27 23:14:17 +04:00
|
|
|
self.shortname = "PIFTS " + name
|
2016-10-25 22:21:32 +04:00
|
|
|
self.name = "Probabilistic Interval FTS"
|
|
|
|
self.detail = "Silva, P.; Guimarães, F.; Sadaei, H."
|
2016-10-25 20:04:37 +04:00
|
|
|
self.flrgs = {}
|
|
|
|
self.globalFrequency = 0
|
2016-10-25 22:21:32 +04:00
|
|
|
self.isInterval = True
|
2016-10-25 20:04:37 +04:00
|
|
|
|
|
|
|
def generateFLRG(self, flrs):
|
|
|
|
flrgs = {}
|
|
|
|
l = len(flrs)
|
|
|
|
for k in np.arange(self.order +1, l):
|
2016-10-26 19:01:30 +04:00
|
|
|
flrg = ProbabilisticFLRG(self.order)
|
2016-10-25 20:04:37 +04:00
|
|
|
|
|
|
|
for kk in np.arange(k - self.order, k):
|
|
|
|
flrg.appendLHS( flrs[kk].LHS )
|
|
|
|
|
|
|
|
if flrg.strLHS() in flrgs:
|
|
|
|
flrgs[flrg.strLHS()].appendRHS(flrs[k].RHS)
|
|
|
|
else:
|
|
|
|
flrgs[flrg.strLHS()] = flrg;
|
|
|
|
flrgs[flrg.strLHS()].appendRHS(flrs[k].RHS)
|
|
|
|
|
|
|
|
self.globalFrequency = self.globalFrequency + 1
|
|
|
|
return (flrgs)
|
2016-10-25 21:52:44 +04:00
|
|
|
|
|
|
|
def getProbability(self, flrg):
|
2016-10-26 19:01:30 +04:00
|
|
|
if flrg.strLHS() in self.flrgs:
|
|
|
|
return self.flrgs[ flrg.strLHS() ].frequencyCount / self.globalFrequency
|
|
|
|
else:
|
2016-10-26 19:52:15 +04:00
|
|
|
return 1/ self.globalFrequency
|
2016-10-25 21:52:44 +04:00
|
|
|
|
|
|
|
def getUpper(self,flrg):
|
|
|
|
if flrg.strLHS() in self.flrgs:
|
|
|
|
tmp = self.flrgs[ flrg.strLHS() ]
|
|
|
|
ret = sum(np.array([ tmp.getProbability(s) * self.setsDict[s].upper for s in tmp.RHS]))
|
|
|
|
else:
|
|
|
|
ret = flrg.LHS[-1].upper
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def getLower(self,flrg):
|
|
|
|
if flrg.strLHS() in self.flrgs:
|
|
|
|
tmp = self.flrgs[ flrg.strLHS() ]
|
|
|
|
ret = sum(np.array([ tmp.getProbability(s) * self.setsDict[s].lower for s in tmp.RHS]))
|
|
|
|
else:
|
|
|
|
ret = flrg.LHS[-1].lower
|
|
|
|
return ret
|
2016-10-25 20:04:37 +04:00
|
|
|
|
|
|
|
def forecast(self,data):
|
|
|
|
|
|
|
|
ndata = np.array(data)
|
|
|
|
|
2016-11-06 03:24:36 +04:00
|
|
|
#print(ndata)
|
|
|
|
|
2016-10-25 20:04:37 +04:00
|
|
|
l = len(ndata)
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
|
2016-11-01 22:03:10 +04:00
|
|
|
for k in np.arange(self.order-1,l):
|
2016-10-25 20:04:37 +04:00
|
|
|
|
2016-10-27 23:14:17 +04:00
|
|
|
affected_flrgs = []
|
|
|
|
affected_flrgs_memberships = []
|
2016-10-26 19:01:30 +04:00
|
|
|
norms = []
|
2016-10-25 20:04:37 +04:00
|
|
|
|
|
|
|
up = []
|
|
|
|
lo = []
|
|
|
|
|
2016-11-01 23:39:13 +04:00
|
|
|
# Find the sets which membership > 0 for each lag
|
2016-10-25 20:04:37 +04:00
|
|
|
count = 0
|
|
|
|
lags = {}
|
|
|
|
if self.order > 1:
|
2016-11-01 22:03:10 +04:00
|
|
|
subset = ndata[k-(self.order-1) : k+1 ]
|
2016-11-01 23:39:13 +04:00
|
|
|
|
2016-10-25 20:04:37 +04:00
|
|
|
for instance in subset:
|
|
|
|
mb = common.fuzzyInstance(instance, self.sets)
|
|
|
|
tmp = np.argwhere( mb )
|
2016-11-01 23:39:13 +04:00
|
|
|
idx = np.ravel(tmp) #flatten the array
|
|
|
|
|
|
|
|
if idx.size == 0: # the element is out of the bounds of the Universe of Discourse
|
|
|
|
if instance <= self.sets[0].lower:
|
|
|
|
idx = [0]
|
|
|
|
if instance >= self.sets[-1].upper:
|
|
|
|
idx = [len(self.sets)-1]
|
|
|
|
|
2016-10-25 20:04:37 +04:00
|
|
|
lags[count] = idx
|
2016-11-01 23:39:13 +04:00
|
|
|
count = count + 1
|
|
|
|
|
2016-10-25 20:04:37 +04:00
|
|
|
|
2016-11-01 23:39:13 +04:00
|
|
|
# Build the tree with all possible paths
|
2016-10-25 20:04:37 +04:00
|
|
|
|
|
|
|
root = tree.FLRGTreeNode(None)
|
|
|
|
|
|
|
|
self.buildTree(root,lags,0)
|
|
|
|
|
2016-11-01 23:39:13 +04:00
|
|
|
# Trace the possible paths and build the PFLRG's
|
2016-10-25 20:04:37 +04:00
|
|
|
|
|
|
|
for p in root.paths():
|
|
|
|
path = list(reversed(list(filter(None.__ne__, p))))
|
|
|
|
flrg = hofts.HighOrderFLRG(self.order)
|
|
|
|
for kk in path: flrg.appendLHS(self.sets[ kk ])
|
|
|
|
|
2016-10-25 21:52:44 +04:00
|
|
|
##
|
2016-10-27 23:14:17 +04:00
|
|
|
affected_flrgs.append( flrg )
|
2016-10-25 20:04:37 +04:00
|
|
|
|
2016-11-01 23:39:13 +04:00
|
|
|
# Find the general membership of FLRG
|
2016-10-27 23:14:17 +04:00
|
|
|
affected_flrgs_memberships.append(min(self.getSequenceMembership(subset, flrg.LHS)))
|
2016-10-25 20:04:37 +04:00
|
|
|
else:
|
|
|
|
|
2016-10-25 21:52:44 +04:00
|
|
|
mv = common.fuzzyInstance(ndata[k],self.sets) # get all membership values
|
|
|
|
tmp = np.argwhere( mv ) # get the indices of values > 0
|
|
|
|
idx = np.ravel(tmp) # flatten the array
|
2016-10-25 20:04:37 +04:00
|
|
|
for kk in idx:
|
|
|
|
flrg = hofts.HighOrderFLRG(self.order)
|
|
|
|
flrg.appendLHS(self.sets[ kk ])
|
2016-10-27 23:14:17 +04:00
|
|
|
affected_flrgs.append( flrg )
|
|
|
|
affected_flrgs_memberships.append(mv[kk])
|
2016-10-25 20:04:37 +04:00
|
|
|
|
|
|
|
count = 0
|
2016-10-27 23:14:17 +04:00
|
|
|
for flrg in affected_flrgs:
|
|
|
|
# achar o os bounds de cada FLRG, ponderados pela probabilidade e pertinência
|
|
|
|
norm = self.getProbability(flrg) * affected_flrgs_memberships[count]
|
2016-10-26 19:01:30 +04:00
|
|
|
up.append( norm * self.getUpper(flrg) )
|
|
|
|
lo.append( norm * self.getLower(flrg) )
|
|
|
|
norms.append(norm)
|
2016-10-25 20:04:37 +04:00
|
|
|
count = count + 1
|
|
|
|
|
|
|
|
# gerar o intervalo
|
2016-10-26 19:01:30 +04:00
|
|
|
norm = sum(norms)
|
2016-11-01 23:39:13 +04:00
|
|
|
if norm == 0:
|
|
|
|
ret.append( [ 0, 0 ] )
|
|
|
|
else:
|
|
|
|
ret.append( [ sum(lo)/norm, sum(up)/norm ] )
|
2016-10-25 20:04:37 +04:00
|
|
|
|
|
|
|
return ret
|
2016-11-01 23:39:13 +04:00
|
|
|
|
2016-11-01 22:03:10 +04:00
|
|
|
def forecastAhead(self,data,steps):
|
|
|
|
ret = [[data[k],data[k]] for k in np.arange(len(data)-self.order,len(data))]
|
|
|
|
for k in np.arange(self.order,steps):
|
2016-11-07 15:18:24 +04:00
|
|
|
if ret[-1][0] <= self.sets[0].lower and ret[-1][1] >= self.sets[-1].upper:
|
2016-11-01 23:39:13 +04:00
|
|
|
ret.append(ret[-1])
|
|
|
|
else:
|
|
|
|
lower = self.forecast( [ret[x][0] for x in np.arange(k-self.order,k)] )
|
|
|
|
upper = self.forecast( [ret[x][1] for x in np.arange(k-self.order,k)] )
|
|
|
|
ret.append([np.min(lower),np.max(upper)])
|
2016-11-01 22:03:10 +04:00
|
|
|
|
|
|
|
return ret
|
2016-11-06 03:24:36 +04:00
|
|
|
|
|
|
|
def getGridClean(self,resolution):
|
|
|
|
grid = {}
|
|
|
|
for sbin in np.arange(self.sets[0].lower,self.sets[-1].upper,resolution):
|
|
|
|
grid[sbin] = 0
|
2016-11-01 22:03:10 +04:00
|
|
|
|
2016-11-06 03:24:36 +04:00
|
|
|
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 forecastDistributionAhead(self,data,steps,resolution):
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
|
|
|
|
intervals = self.forecastAhead(data,steps)
|
|
|
|
|
|
|
|
for k in np.arange(self.order,steps):
|
|
|
|
|
|
|
|
grid = self.getGridClean(resolution)
|
|
|
|
grid = self.gridCount(grid,resolution, intervals[k])
|
2016-11-07 15:18:24 +04:00
|
|
|
|
|
|
|
for qt in np.arange(1,50,2):
|
|
|
|
#print(qt)
|
|
|
|
qtle_lower = self.forecast([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.forecast([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.forecast([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))
|
2016-11-06 03:24:36 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-10-26 19:01:30 +04:00
|
|
|
def __str__(self):
|
|
|
|
tmp = self.name + ":\n"
|
|
|
|
for r in sorted(self.flrgs):
|
|
|
|
p = round(self.flrgs[r].frequencyCount / self.globalFrequency,3)
|
|
|
|
tmp = tmp + "(" + str(p) + ") " + str(self.flrgs[r]) + "\n"
|
|
|
|
return tmp
|