Implementation of forecast_ahead in TimeVariant and IncrementalEnsemble methods

This commit is contained in:
Petrônio Cândido 2019-11-27 17:39:01 -03:00
parent 6c639545a3
commit a094ba04d4
3 changed files with 93 additions and 2 deletions

View File

@ -63,6 +63,15 @@ class IncrementalEnsembleFTS(ensemble.EnsembleFTS):
def forecast(self, data, **kwargs): def forecast(self, data, **kwargs):
l = len(data) l = len(data)
no_update = kwargs.get('no_update', False)
if no_update:
ret = []
for k in np.arange(self.order, l+1):
sample = data[k-self.order: k]
tmp = self.get_models_forecasts(sample)
point = self.get_point(tmp)
ret.append(point)
return ret
data_window = [] data_window = []
@ -88,6 +97,27 @@ class IncrementalEnsembleFTS(ensemble.EnsembleFTS):
return ret return ret
def forecast_ahead(self, data, steps, **kwargs):
if len(data) < self.order:
return data
if isinstance(data, np.ndarray):
data = data.tolist()
start = kwargs.get('start_at',0)
ret = data[:start+self.order]
for k in np.arange(start+self.order, steps+start+self.order):
tmp = self.forecast(ret[k-self.order:k], no_update=True, **kwargs)
if isinstance(tmp,(list, np.ndarray)):
tmp = tmp[-1]
ret.append(tmp)
data.append(tmp)
return ret[-steps:]

View File

@ -55,6 +55,11 @@ class Retrainer(fts.FTS):
def forecast(self, data, **kwargs): def forecast(self, data, **kwargs):
l = len(data) l = len(data)
no_update = kwargs.get('no_update',False)
if no_update:
return self.model.predict(data, **kwargs)
horizon = self.window_length + self.order horizon = self.window_length + self.order
ret = [] ret = []
@ -73,13 +78,34 @@ class Retrainer(fts.FTS):
return ret return ret
def forecast_ahead(self, data, steps, **kwargs):
if len(data) < self.order:
return data
if isinstance(data, np.ndarray):
data = data.tolist()
start = kwargs.get('start_at',0)
ret = data[:start+self.order]
for k in np.arange(start+self.order, steps+start+self.order):
tmp = self.forecast(ret[k-self.order:k], no_update=True, **kwargs)
if isinstance(tmp,(list, np.ndarray)):
tmp = tmp[-1]
ret.append(tmp)
data.append(tmp)
return ret[-steps:]
def offset(self): def offset(self):
return self.max_lag return self.max_lag
def __str__(self): def __str__(self):
"""String representation of the model""" """String representation of the model"""
return str(this.model) return str(self.model)
def __len__(self): def __len__(self):
""" """

View File

@ -23,10 +23,44 @@ from pyFTS.common import Transformations
from pyFTS.partitioners import Grid, Util as pUtil from pyFTS.partitioners import Grid, Util as pUtil
from pyFTS.benchmarks import benchmarks as bchmk from pyFTS.benchmarks import benchmarks as bchmk
from pyFTS.models import chen, hofts, pwfts, hwang from pyFTS.models import chen, hofts, pwfts, hwang
from pyFTS.models.incremental import TimeVariant, IncrementalEnsemble
train = dataset[:1000] train = dataset[:1000]
test = dataset[1000:] test = dataset[1000:]
window = 100
batch = 10
num_models = 3
model1 = TimeVariant.Retrainer(partitioner_method=Grid.GridPartitioner, partitioner_params={'npart': 35},
fts_method=pwfts.ProbabilisticWeightedFTS, fts_params={}, order=1,
batch_size=batch, window_length=window * num_models)
model2 = IncrementalEnsemble.IncrementalEnsembleFTS(partitioner_method=Grid.GridPartitioner,
partitioner_params={'npart': 35},
fts_method=pwfts.ProbabilisticWeightedFTS, fts_params={}, order=1,
batch_size=int(batch / 3), window_length=window,
num_models=num_models)
model1.fit(train)
model2.fit(train)
print(len(test))
'''
forecasts1 = model1.predict(test[:-10])
print(len(forecasts1))
forecasts1 = model1.predict(test[-10:], steps_ahead=10)
print(len(forecasts1))
'''
forecasts2 = model2.predict(test[:-10])
print(len(forecasts2))
forecasts2 = model2.predict(test[-10:], steps_ahead=10)
print(len(forecasts2))
'''
from pyFTS.models.nonstationary import partitioners as nspart, nsfts, honsfts from pyFTS.models.nonstationary import partitioners as nspart, nsfts, honsfts
fs = nspart.simplenonstationary_gridpartitioner_builder(data=train,npart=35,transformation=None) fs = nspart.simplenonstationary_gridpartitioner_builder(data=train,npart=35,transformation=None)
print(fs) print(fs)
@ -36,3 +70,4 @@ model.fit(train)
print(model) print(model)
forecasts = model.predict(test) forecasts = model.predict(test)
#print(forecasts) #print(forecasts)
'''