Bugfixes in pwfts

This commit is contained in:
Petrônio Cândido 2018-04-11 01:31:17 -03:00
parent 50c2b501b1
commit f9204b87c9
2 changed files with 62 additions and 72 deletions

View File

@ -106,8 +106,7 @@ class ProbabilisticWeightedFTS(ifts.IntervalFTS):
self.has_probability_forecasting = True self.has_probability_forecasting = True
self.is_high_order = True self.is_high_order = True
self.auto_update = kwargs.get('update',False) self.auto_update = kwargs.get('update',False)
self.interval_method = kwargs.get('interval_method','extremum')
self.alpha = kwargs.get('alpha', 0.05)
def train(self, data, **kwargs): def train(self, data, **kwargs):
@ -259,22 +258,25 @@ class ProbabilisticWeightedFTS(ifts.IntervalFTS):
def forecast(self, data, **kwargs): def forecast(self, data, **kwargs):
method = kwargs.get('method','heuristic') method = kwargs.get('method','heuristic')
if method == 'heuristic': l = len(data)
return self.point_heuristic(data, **kwargs)
elif method == 'expected_value':
return self.point_expected_value(data, **kwargs)
else:
raise Exception("Unknown point forecasting method!")
def point_heuristic(self, ndata, **kwargs):
l = len(ndata)
ret = [] ret = []
for k in np.arange(self.order - 1, l): for k in np.arange(self.order - 1, l):
sample = data[k - (self.order - 1): k + 1]
sample = ndata[k - (self.order - 1): k + 1] if method == 'heuristic':
ret.append(self.point_heuristic(sample, **kwargs))
elif method == 'expected_value':
ret.append(self.point_expected_value(sample, **kwargs))
else:
raise ValueError("Unknown point forecasting method!")
if self.auto_update and k > self.order+1: self.update_model(data[k - self.order - 1 : k])
return ret
def point_heuristic(self, sample, **kwargs):
flrgs = self.generate_lhs_flrg(sample) flrgs = self.generate_lhs_flrg(sample)
@ -283,42 +285,25 @@ class ProbabilisticWeightedFTS(ifts.IntervalFTS):
for flrg in flrgs: for flrg in flrgs:
norm = self.flrg_lhs_conditional_probability(sample, flrg) norm = self.flrg_lhs_conditional_probability(sample, flrg)
if norm == 0: if norm == 0:
norm = self.flrg_lhs_unconditional_probability(flrg) # * 0.001 norm = self.flrg_lhs_unconditional_probability(flrg)
mp.append(norm * self.get_midpoint(flrg)) mp.append(norm * self.get_midpoint(flrg))
norms.append(norm) norms.append(norm)
# gerar o intervalo
norm = sum(norms) norm = sum(norms)
if norm == 0: if norm == 0:
ret.append(0) return 0
else: else:
ret.append(sum(mp) / norm) return sum(mp) / norm
if self.auto_update and k > self.order+1: self.update_model(ndata[k - self.order - 1 : k])
return ret def point_expected_value(self, sample, **kwargs):
return self.forecast_distribution(sample)[0].expected_value()
def point_expected_value(self, data, **kwargs):
l = len(data)
ret = []
for k in np.arange(self.order - 1, l):
sample = data[k - (self.order - 1): k + 1]
tmp = self.forecast_distribution(sample)[0].expected_value()
ret.append(tmp)
return ret
def forecast_interval(self, ndata, **kwargs): def forecast_interval(self, ndata, **kwargs):
if 'method' in kwargs: method = kwargs.get('method','heuristic')
self.interval_method = kwargs.get('method','heuristic') alpha = kwargs.get('alpha', 0.05)
if 'alpha' in kwargs:
self.alpha = kwargs.get('alpha', 0.05)
l = len(ndata) l = len(ndata)
@ -326,21 +311,23 @@ class ProbabilisticWeightedFTS(ifts.IntervalFTS):
for k in np.arange(self.order - 1, l): for k in np.arange(self.order - 1, l):
if self.interval_method == 'heuristic': sample = ndata[k - (self.order - 1): k + 1]
self.interval_heuristic(k, ndata, ret)
if method == 'heuristic':
ret.append(self.interval_heuristic(sample))
elif method == 'quantile':
ret.append(self.interval_quantile(sample, alpha))
else: else:
self.interval_quantile(k, ndata, ret) raise ValueError("Unknown interval forecasting method!")
return ret return ret
def interval_quantile(self, k, ndata, ret): def interval_quantile(self, ndata, alpha):
dist = self.forecast_distribution(ndata) dist = self.forecast_distribution(ndata)
itvl = dist[0].quantile([self.alpha, 1.0 - self.alpha]) itvl = dist[0].quantile([alpha, 1.0 - alpha])
ret.append(itvl) return itvl
def interval_heuristic(self, k, ndata, ret): def interval_heuristic(self, sample):
sample = ndata[k - (self.order - 1): k + 1]
flrgs = self.generate_lhs_flrg(sample) flrgs = self.generate_lhs_flrg(sample)
@ -358,11 +345,11 @@ class ProbabilisticWeightedFTS(ifts.IntervalFTS):
# gerar o intervalo # gerar o intervalo
norm = sum(norms) norm = sum(norms)
if norm == 0: if norm == 0:
ret.append([0, 0]) return [0, 0]
else: else:
lo_ = sum(lo) / norm lo_ = sum(lo) / norm
up_ = sum(up) / norm up_ = sum(up) / norm
ret.append([lo_, up_]) return [lo_, up_]
def forecast_distribution(self, ndata, **kwargs): def forecast_distribution(self, ndata, **kwargs):

View File

@ -44,8 +44,11 @@ pfts1_taiex.shortname = "1st Order"
print(pfts1_taiex) print(pfts1_taiex)
tmp = pfts1_taiex.predict(dataset[train_split:train_split+200], type='interval', tmp = pfts1_taiex.predict(dataset[train_split:train_split+20], type='interval',
method='quantile', alpha=.05, steps_ahead=10) method='heuristic')
print(tmp)