diff --git a/benchmarks/quantreg.py b/benchmarks/quantreg.py index 987fdd7..0426a04 100644 --- a/benchmarks/quantreg.py +++ b/benchmarks/quantreg.py @@ -24,6 +24,10 @@ class QuantileRegression(fts.FTS): def train(self, data, sets, order=1, parameters=None): self.order = order + + if parameters is not None: + self.alpha = parameters + tmp = np.array(self.doTransformations(data)) lagdata, ndata = lagmat(tmp, maxlag=order, trim="both", original='sep') diff --git a/ensemble.py b/ensemble.py index bf1a6d6..c85a709 100644 --- a/ensemble.py +++ b/ensemble.py @@ -8,6 +8,7 @@ from operator import itemgetter from pyFTS.common import FLR, FuzzySet, SortedCollection from pyFTS import fts + class EnsembleFTS(fts.FTS): def __init__(self, order, name, **kwargs): super(EnsembleFTS, self).__init__("Ensemble FTS") @@ -21,20 +22,66 @@ class EnsembleFTS(fts.FTS): self.models = [] self.parameters = [] - def train(self, data, sets, order=1,parameters=None): + def build(self, data, models, partitioners, partitions, max_order=3, transformation=None, indexer=None): + self.models = [] + + for count, model in enumerate(models, start=0): + mfts = model("") + if mfts.benchmark_only: + if transformation is not None: + mfts.appendTransformation(transformation) + mfts.train(data,None, order=1, parameters=None) + self.models.append(mfts) + else: + for partition in partitions: + for partitioner in partitioners: + data_train_fs = partitioner(data, partition, transformation=transformation) + mfts = model("") + + mfts.partitioner = data_train_fs + if not mfts.isHighOrder: + + if transformation is not None: + mfts.appendTransformation(transformation) + + mfts.train(data, data_train_fs.sets) + self.models.append(mfts) + else: + for order in np.arange(1, max_order + 1): + if order >= mfts.minOrder: + mfts = model("") + mfts.partitioner = data_train_fs + + if transformation is not None: + mfts.appendTransformation(transformation) + + mfts.train(data, data_train_fs.sets, order=order) + self.models.append(mfts) + + def train(self, data, sets, order=1,parameters=None): pass def forecast(self, data, **kwargs): + method = kwargs.get('method','mean') + ndata = np.array(self.doTransformations(data)) l = len(ndata) ret = [] - for k in np.arange(self.order - 1, l): - pass + for k in np.arange(0, l): + tmp = [] + for model in self.models: + if k >= model.minOrder - 1: + sample = ndata[k - model.order : k] + tmp.append( model.forecast(sample) ) + if method == 'mean': + ret.append( np.nanmean(tmp)) + elif method == 'median': + ret.append(np.percentile(tmp,50)) ret = self.doInverseTransformations(ret, params=[data[self.order - 1:]]) @@ -42,14 +89,25 @@ class EnsembleFTS(fts.FTS): def forecastInterval(self, data, **kwargs): + method = kwargs.get('method', 'extremum') + ndata = np.array(self.doTransformations(data)) l = len(ndata) ret = [] - for k in np.arange(self.order - 1, l): - pass + for k in np.arange(0, l): + tmp = [] + for model in self.models: + if k >= model.minOrder - 1: + sample = ndata[k - model.order : k] + tmp.append( model.forecast(sample) ) + if method == 'extremum': + ret.append( [ min(tmp), max(tmp) ] ) + elif method == 'quantile': + q = kwargs.get('q', [.05, .95]) + ret.append(np.percentile(tmp,q=q*100)) return ret