def print_point_statistics(data, models, externalmodels = None, externalforecasts = None, indexers=None): """ Run point benchmarks on given models and data and print the results :param data: test data :param models: a list of FTS models to benchmark :param externalmodels: a list with benchmark models (façades for other methods) :param externalforecasts: :param indexers: :return: """ ret = "Model & Order & RMSE & SMAPE & Theil's U \\\\ \n" for count,model in enumerate(models,start=0): _rmse, _smape, _u = Measures.get_point_statistics(data, model, indexers) ret += model.shortname + " & " ret += str(model.order) + " & " ret += str(_rmse) + " & " ret += str(_smape)+ " & " ret += str(_u) #ret += str(round(Measures.TheilsInequality(np.array(data[fts.order:]), np.array(forecasts[:-1])), 4)) ret += " \\\\ \n" if externalmodels is not None: l = len(externalmodels) for k in np.arange(0,l): ret += externalmodels[k] + " & " ret += " 1 & " ret += str(round(Measures.rmse(data, externalforecasts[k][:-1]), 2)) + " & " ret += str(round(Measures.smape(data, externalforecasts[k][:-1]), 2))+ " & " ret += str(round(Measures.UStatistic(data, externalforecasts[k][:-1]), 2)) ret += " \\\\ \n" print(ret)
def print_point_statistics(data, models, externalmodels=None, externalforecasts=None, indexers=None): ret = "Model & Order & RMSE & SMAPE & Theil's U \\\\ \n" for count, model in enumerate(models, start=0): _rmse, _smape, _u = Measures.get_point_statistics( data, model, indexers) ret += model.shortname + " & " ret += str(model.order) + " & " ret += str(_rmse) + " & " ret += str(_smape) + " & " ret += str(_u) #ret += str(round(Measures.TheilsInequality(np.array(data[fts.order:]), np.array(forecasts[:-1])), 4)) ret += " \\\\ \n" if externalmodels is not None: l = len(externalmodels) for k in np.arange(0, l): ret += externalmodels[k] + " & " ret += " 1 & " ret += str(round(Measures.rmse(data, externalforecasts[k][:-1]), 2)) + " & " ret += str( round(Measures.smape(data, externalforecasts[k][:-1]), 2)) + " & " ret += str( round(Measures.UStatistic(data, externalforecasts[k][:-1]), 2)) ret += " \\\\ \n" print(ret)
def cluster_method(individual, train, test): from pyFTS.common import Util, Membership from pyFTS.models import hofts from pyFTS.partitioners import Grid, Entropy from pyFTS.benchmarks import Measures if individual['mf'] == 1: mf = Membership.trimf elif individual['mf'] == 2: mf = Membership.trapmf elif individual['mf'] == 3 and individual['partitioner'] != 2: mf = Membership.gaussmf else: mf = Membership.trimf if individual['partitioner'] == 1: partitioner = Grid.GridPartitioner(data=train, npart=individual['npart'], func=mf) elif individual['partitioner'] == 2: npart = individual['npart'] if individual['npart'] > 10 else 10 partitioner = Entropy.EntropyPartitioner(data=train, npart=npart, func=mf) model = hofts.WeightedHighOrderFTS(partitioner=partitioner, lags=individual['lags'], alpha_cut=individual['alpha'], order=individual['order']) model.fit(train) rmse, mape, u = Measures.get_point_statistics(test, model) size = len(model) return individual, rmse, size, mape, u
def evaluation1(dataset, individual): from pyFTS.common import Util from pyFTS.benchmarks import Measures try: results = [] lengths = [] for count, train, test in Util.sliding_window(dataset, 800, train=.8, inc=.25): model = phenotype(individual, train) if model is None: return (None) rmse, _, _ = Measures.get_point_statistics(test, model) lengths.append(len(model)) results.append(rmse) _lags = sum(model.lags) * 100 rmse = np.nansum( [.6 * np.nanmean(results), .4 * np.nanstd(results)]) len_lags = np.nansum([.4 * np.nanmean(lengths), .6 * _lags]) return len_lags, rmse except Exception as ex: print("EXCEPTION!", str(ex), str(individual)) return np.inf
from pyFTS.benchmarks import Measures from pyFTS.partitioners import Grid, Entropy from pyFTS.models import hofts from pyFTS.common import Membership x = [k for k in np.arange(-2 * np.pi, 2 * np.pi, 0.1)] y = [np.sin(k) for k in x] rows = [] fig, ax = plt.subplots(nrows=1, ncols=1, figsize=[15, 5]) ax.plot(y, label='Original', color='black') for npart in np.arange(5, 35, 5): part = Grid.GridPartitioner(data=y, npart=npart) model = hofts.HighOrderFTS(order=1, partitioner=part) model.fit(y) forecasts = model.predict(y) ax.plot(forecasts[:-1], label=str(npart) + " partitions") rmse, mape, u = Measures.get_point_statistics(y, model) rows.append([npart, rmse, mape, u]) handles, labels = ax.get_legend_handles_labels() lgd = ax.legend(handles, labels, loc=2, bbox_to_anchor=(1, 1)) df = pd.DataFrame(rows, columns=['Partitions', 'RMSE', 'MAPE', 'U'])
def run_point(mfts, partitioner, train_data, test_data, window_key=None, **kwargs): """ Point forecast benchmark function to be executed on cluster nodes :param mfts: FTS model :param partitioner: Universe of Discourse partitioner :param train_data: data used to train the model :param test_data: ata used to test the model :param window_key: id of the sliding window :param transformation: data transformation :param indexer: seasonal indexer :return: a dictionary with the benchmark results """ import time from pyFTS.models import yu, chen, hofts, pwfts, ismailefendi, sadaei, song, cheng, hwang from pyFTS.partitioners import Grid, Entropy, FCM from pyFTS.benchmarks import Measures, naive, arima, quantreg from pyFTS.common import Transformations tmp = [ song.ConventionalFTS, chen.ConventionalFTS, yu.WeightedFTS, ismailefendi.ImprovedWeightedFTS, cheng.TrendWeightedFTS, sadaei.ExponentialyWeightedFTS, hofts.HighOrderFTS, hwang.HighOrderFTS, pwfts.ProbabilisticWeightedFTS ] tmp2 = [ Grid.GridPartitioner, Entropy.EntropyPartitioner, FCM.FCMPartitioner ] tmp4 = [naive.Naive, arima.ARIMA, quantreg.QuantileRegression] tmp3 = [Measures.get_point_statistics] tmp5 = [Transformations.Differential] indexer = kwargs.get('indexer', None) steps_ahead = kwargs.get('steps_ahead', 1) method = kwargs.get('method', None) if mfts.benchmark_only: _key = mfts.shortname + str( mfts.order if mfts.order is not None else "") else: pttr = str(partitioner.__module__).split('.')[-1] _key = mfts.shortname + " n = " + str( mfts.order) + " " + pttr + " q = " + str(partitioner.partitions) mfts.partitioner = partitioner mfts.append_transformation(partitioner.transformation) _key += str(steps_ahead) _key += str(method) if method is not None else "" _start = time.time() mfts.fit(train_data, **kwargs) _end = time.time() times = _end - _start _start = time.time() _rmse, _smape, _u = Measures.get_point_statistics(test_data, mfts, **kwargs) _end = time.time() times += _end - _start ret = { 'key': _key, 'obj': mfts, 'rmse': _rmse, 'smape': _smape, 'u': _u, 'time': times, 'window': window_key, 'steps': steps_ahead, 'method': method } return ret
from pyFTS.data import TAIEX, SP500, NASDAQ, Malaysia dataset = Malaysia.get_data('temperature')[:1000] p = Grid.GridPartitioner(data=dataset, npart=20) print(p) model = hofts.WeightedHighOrderFTS(partitioner=p, order=2) model.fit(dataset) #[22, 22, 23, 23, 24]) print(model) Measures.get_point_statistics(dataset, model) ''' #dataset = SP500.get_data()[11500:16000] #dataset = NASDAQ.get_data() #print(len(dataset)) bchmk.sliding_window_benchmarks(dataset, 1000, train=0.8, inc=0.2, methods=[chen.ConventionalFTS], #[pwfts.ProbabilisticWeightedFTS], benchmark_models=False, transformations=[None], #orders=[1, 2, 3], partitions=np.arange(10, 100, 2), progress=False, type="point", #steps_ahead=[1,2,4,6,8,10], distributed=False, nodes=['192.168.0.110', '192.168.0.107', '192.168.0.106'],
model2 = pwfts.ProbabilisticWeightedFTS(partitioner=fs, lags=[1, 2]) #model2.append_transformation(tdiff) model2.shortname = "2" #model = pwfts.ProbabilisticWeightedFTS(partitioner=fs, order=2)# lags=[1,2]) model1.fit(train) model2.fit(train) #print(model1) #print(model2) for model in [model1, model2]: #forecasts = model.predict(test) print(model.shortname) print(Measures.get_point_statistics(test, model)) #handles, labels = ax.get_legend_handles_labels() #ax.legend(handles, labels, loc=2, bbox_to_anchor=(1, 1)) #print(Measures.get_point_statistics(test,model)) ''' bchmk.sliding_window_benchmarks(train,1000,0.8, methods=[pwfts.ProbabilisticWeightedFTS], #,ifts.IntervalFTS], orders=[1,2,3], partitions=[10]) ''' ''' from pyFTS.common import FLR,FuzzySet,Membership,SortedCollection taiex_fs1 = Grid.GridPartitioner(data=train, npart=30)