diff --git a/pyFTS/common/FuzzySet.py b/pyFTS/common/FuzzySet.py index db489ee..087d6dd 100644 --- a/pyFTS/common/FuzzySet.py +++ b/pyFTS/common/FuzzySet.py @@ -59,7 +59,8 @@ class FuzzySet(object): def set_ordered(fuzzySets): - return [k for k in sorted(fuzzySets.keys())] + tmp1 = [fuzzySets[k] for k in fuzzySets.keys()] + return [k.name for k in sorted(tmp1, key=lambda x: x.centroid)] def fuzzyfy_instance(inst, fuzzySets, ordered_sets=None): diff --git a/pyFTS/models/pwfts.py b/pyFTS/models/pwfts.py index fe51ec6..3980122 100644 --- a/pyFTS/models/pwfts.py +++ b/pyFTS/models/pwfts.py @@ -395,31 +395,45 @@ class ProbabilisticWeightedFTS(ifts.IntervalFTS): return ret + def __check_point_bounds(self, point): + lower_set = self.partitioner.lower_set() + upper_set = self.partitioner.upper_set() + return point <= lower_set.lower or point >= upper_set.upper + def forecast_ahead(self, data, steps, **kwargs): ret = [data[k] for k in np.arange(len(data) - self.order, len(data))] for k in np.arange(self.order - 1, steps): - if ret[-1] <= self.sets[0].lower or ret[-1] >= self.sets[-1].upper: + if self.__check_point_bounds(ret[-1]) : ret.append(ret[-1]) else: - mp = self.forecast([ret[x] for x in np.arange(k - self.order, k)]) - + mp = self.forecast([ret[x] for x in np.arange(k - self.order, k)], **kwargs) ret.append(mp) - return ret + return ret[self.order:] + + def __check_interval_bounds(self, interval): + if len(self.transformations) > 0: + lower_set = self.partitioner.lower_set() + upper_set = self.partitioner.upper_set() + return interval[0] <= lower_set.lower and interval[1] >= upper_set.upper + elif len(self.transformations) == 0: + return interval[0] <= self.original_min and interval[1] >= self.original_max def forecast_ahead_interval(self, data, steps, **kwargs): l = len(data) - ret = [[data[k], data[k]] for k in np.arange(l - self.order, l)] + start = kwargs.get('start', self.order) + + sample = data[start - (self.order - 1): start + 1] + + ret = [[k, k] for k in sample] for k in np.arange(self.order, steps+self.order): - if (len(self.transformations) > 0 and ret[-1][0] <= self.sets[0].lower and ret[-1][1] >= self.sets[ - -1].upper) or (len(self.transformations) == 0 and ret[-1][0] <= self.original_min and ret[-1][ - 1] >= self.original_max): + if self.__check_interval_bounds(ret[-1]): ret.append(ret[-1]) else: lower = self.forecast_interval([ret[x][0] for x in np.arange(k - self.order, k)], **kwargs) @@ -427,7 +441,7 @@ class ProbabilisticWeightedFTS(ifts.IntervalFTS): ret.append([np.min(lower), np.max(upper)]) - return ret + return ret[self.order:] def forecast_ahead_distribution(self, ndata, steps, **kwargs): @@ -490,9 +504,7 @@ class ProbabilisticWeightedFTS(ifts.IntervalFTS): ret.append(dist) - ret = ret[self.order:] - - return ret + return ret[self.order:] def __str__(self): tmp = self.name + ":\n" diff --git a/pyFTS/partitioners/partitioner.py b/pyFTS/partitioners/partitioner.py index f9578b9..6cc5fcb 100644 --- a/pyFTS/partitioners/partitioner.py +++ b/pyFTS/partitioners/partitioner.py @@ -77,6 +77,12 @@ class Partitioner(object): def get_name(self, counter): return self.prefix + str(counter) if self.setnames is None else self.setnames[counter] + def lower_set(self): + return self.sets[self.ordered_sets[0]] + + def upper_set(self): + return self.sets[self.ordered_sets[-1]] + def plot(self, ax): """ Plot the diff --git a/pyFTS/tests/general.py b/pyFTS/tests/general.py index fdf9040..ca9a030 100644 --- a/pyFTS/tests/general.py +++ b/pyFTS/tests/general.py @@ -31,21 +31,21 @@ train_split = 2000 test_length = 200 from pyFTS.partitioners import Grid, Util as pUtil -partitioner = Grid.GridPartitioner(data=dataset[:train_split], npart=30) -#partitioner = Grid.GridPartitioner(data=dataset[:train_split], npart=10, transformation=tdiff) +#partitioner = Grid.GridPartitioner(data=dataset[:train_split], npart=30) +partitioner = Grid.GridPartitioner(data=dataset[:train_split], npart=10, transformation=tdiff) from pyFTS.common import fts,tree from pyFTS.models import hofts, pwfts pfts1_taiex = pwfts.ProbabilisticWeightedFTS("1", partitioner=partitioner) -#pfts1_taiex.append_transformation(tdiff) +pfts1_taiex.append_transformation(tdiff) pfts1_taiex.fit(dataset[:train_split], save_model=True, file_path='pwfts') pfts1_taiex.shortname = "1st Order" print(pfts1_taiex) -tmp = pfts1_taiex.predict(dataset[train_split:train_split+20], type='interval', - method='heuristic') +tmp = pfts1_taiex.predict(dataset[train_split:train_split+20], type='point', + method='heuristic', steps_ahead=10) print(tmp)