def optimize_value_creation_test(self): """Testing the first part of optimize_forecasting_method.""" self.bfm._requiredParameters = ["param1", "param2", "param3", "param4", "param5"] try: GridSearch(SMAPE, -1).optimize_forecasting_method(self.timeSeries, self.bfm) ## we looped down to the NotImplemetedError of BaseMethod.execute except NotImplementedError: pass else: assert False # pragma: no cover self.bfm._parameterIntervals = { "param3": [0.0, 1.0, True, True], "param4": [0.0, 1.0, True, True], "param5": [0.0, 1.0, True, True] } try: GridSearch(SMAPE, -5).optimize_forecasting_method(self.timeSeries, self.bfm) ## we looped down to the NotImplemetedError of BaseMethod.execute except NotImplementedError: pass else: assert False # pragma: no cover
def optimize(request): """ Performs Holt Winters Parameter Optimization on the given post data. Expects the following values set in the post of the request: seasonLength - integer valuesToForecast - integer data - two dimensional array of [timestamp, value] """ #Parse arguments seasonLength = int(request.POST.get('seasonLength', 6)) valuesToForecast = int(request.POST.get('valuesToForecast', 0)) data = json.loads(request.POST.get('data', [])) original = TimeSeries.from_twodim_list(data) original.normalize("day") #due to bug in TimeSeries.apply original.set_timeformat("%d.%m") #optimize smoothing hwm = HoltWintersMethod(seasonLength = seasonLength, valuesToForecast = valuesToForecast) gridSearch = GridSearch(SMAPE) optimal_forecasting, error, optimal_params = gridSearch.optimize(original, [hwm]) #perform smoothing smoothed = optimal_forecasting.execute(original) smoothed.set_timeformat("%d.%m") result = { 'params': optimal_params, 'original': original, 'smoothed': smoothed, 'error': round(error.get_error(), 3) } return Response(json.dumps(result, cls=PycastEncoder), content_type='application/json')
def optimize(request): """ Performs Holt Winters Parameter Optimization on the given post data. Expects the following values set in the post of the request: seasonLength - integer valuesToForecast - integer data - two dimensional array of [timestamp, value] """ #Parse arguments seasonLength = int(request.POST.get('seasonLength', 6)) valuesToForecast = int(request.POST.get('valuesToForecast', 0)) data = json.loads(request.POST.get('data', [])) original = TimeSeries.from_twodim_list(data) original.normalize("day") #due to bug in TimeSeries.apply original.set_timeformat("%d.%m") #optimize smoothing hwm = HoltWintersMethod(seasonLength = seasonLength, valuesToForecast = valuesToForecast) gridSearch = GridSearch(SMAPE) optimal_forecasting, error, optimal_params = gridSearch.optimize(original, [hwm]) #perform smoothing smoothed = optimal_forecasting.execute(original) smoothed.set_timeformat("%d.%m") result = { 'params': optimal_params, 'original': original, 'smoothed': smoothed, 'error': round(error.get_error(), 3) } return itty.Response(json.dumps(result, cls=PycastEncoder), content_type='application/json')
def inner_optimization_result_accuracy_test(self): """Test for the correct result of a GridSearch optimization.""" fm = ExponentialSmoothing() startingPercentage = 0.0 endPercentage = 100.0 ## manually select the best alpha self.timeSeries.normalize("second") results = [] for smoothingFactor in [alpha / 100.0 for alpha in xrange(1, 100)]: # pragma: no cover fm.set_parameter("smoothingFactor", smoothingFactor) resultTS = self.timeSeries.apply(fm) error = SMAPE() error.initialize(self.timeSeries, resultTS) results.append([error, smoothingFactor]) bestManualResult = min(results, key=lambda item: item[0].get_error(startingPercentage, endPercentage)) ## automatically determine the best alpha using GridSearch gridSearch = GridSearch(SMAPE, precision=-4) ## used, because we test a submethod here gridSearch._startingPercentage = startingPercentage gridSearch._endPercentage = endPercentage result = gridSearch.optimize_forecasting_method(self.timeSeries, fm) ## the grid search should have determined the same alpha bestManualAlpha = bestManualResult[1] errorManualResult = bestManualResult[0].get_error() bestGridSearchAlpha = result[1]["smoothingFactor"] errorGridSearchResult = result[0].get_error() assert errorManualResult > errorGridSearchResult
def train_target(self, data_list, model_list):# data_list: [date, value] orig = TimeSeries(isNormalized=True) for i in range(len(data_list)): orig.add_entry(data_list[i][0], data_list[i][1]) gridSearch = GridSearch(SMAPE) optimal_forecasting, error, optimal_params = gridSearch.optimize(orig, model_list) #print "======" + str(optimal_forecasting._parameters) return optimal_forecasting
def outer_optimization_result_test(self): """Test the multiple method optimization.""" fm1 = ExponentialSmoothing() fm2 = HoltMethod() self.timeSeries.normalize("second") ## automatically determine the best alpha using GridSearch gridSearch = GridSearch(SMAPE, precision=-2) result = gridSearch.optimize(self.timeSeries, [fm1, fm2])
def optimize_exception_test(self): """Test for exception while calling GridSearch.optimize.""" try: GridSearch(SMAPE, -2).optimize(self.timeSeries) except ValueError: pass else: assert False # pragma: no cover try: GridSearch(SMAPE, -1).optimize(self.timeSeries, [self.bfm]) ## we looped down to the NotImplemetedError of BaseMethod.execute except NotImplementedError: pass else: assert False # pragma: no cover
def optimization_loop_test(self): """Testing the optimozation loop.""" gridSearch = GridSearch(SMAPE, precision=-2) def crap_execute(ignoreMe): ts = self.timeSeries.to_twodim_list() ts = TimeSeries.from_twodim_list(ts) for entry in ts: entry[0] += 0.1 return ts self.bfm.execute = crap_execute result = gridSearch.optimization_loop(self.timeSeries, self.bfm, [], {}) assert result == []
def create_generator_test(self): """Test the parameter generation function.""" ## initialize a correct result precision = 10**-2 values_one = [i * precision for i in xrange(1,100)] values_two = [i * precision for i in xrange(201)] generator_one = GridSearch(SMAPE, precision=-2)._generate_next_parameter_value("parameter_one", self.bfm) generator_two = GridSearch(SMAPE, precision=-2)._generate_next_parameter_value("parameter_two", self.bfm) generated_one = [val for val in generator_one] generated_two = [val for val in generator_two] assert len(values_one) == len(generated_one) assert len(values_two) == len(generated_two) for idx in xrange(len(values_one)): value = str(values_one[idx])[:12] assert str(value) == str(generated_one[idx])[:len(value)] for idx in xrange(len(values_two)): value = str(values_two[idx])[:12] assert str(value) == str(generated_two[idx])[:len(value)]
from pycast.common.timeseries import TimeSeries from pycast.methods import HoltWintersMethod from pycast.optimization import GridSearch from pycast.errors import SymmetricMeanAbsolutePercentageError as SMAPE data_list = [1.1, 2.1, 3.3, 4.4, 5.5, 1.1, 2.2, 3.3] orig = TimeSeries(isNormalized=True) for i in range(len(data_list)): orig.add_entry(i, data_list[i]) #offset to first data entry in line seasonLength = int(5) season_values = [] for i in range(seasonLength): season_values.append(float(1.0)) #print season_values hwm = HoltWintersMethod(seasonLength = seasonLength, valuesToForecast = 3) hwm.set_parameter("seasonValues", season_values) gridSearch = GridSearch(SMAPE) optimal_forecasting, error, optimal_params = gridSearch.optimize(orig, [hwm]) predicted = optimal_forecasting.execute(orig) print str(orig) print str(predicted)
#print forecast_check #Season indices are given in season file season_indices_tuple = season_indices.readline().split(',') seasonLength = int(season_indices_tuple[1]) season_values = [] for i in range(seasonLength): season_values.append(float(season_indices_tuple[i + 2])) #print season_values hwm = HoltWintersMethod(seasonLength=seasonLength, valuesToForecast=number_forecast) hwm.set_parameter("seasonValues", season_values) #Optimize parameters gridSearch = GridSearch(SMAPE) optimal_forecasting, error, optimal_params = gridSearch.optimize( orig, [hwm]) predicted = optimal_forecasting.execute(orig) #Now add forecasted values to original and calculate error orig += forecast_check assert len(orig) == len( predicted ), "Prediction and original season should have the same length." total_error = SMAPE() total_error.initialize(orig, predicted) forecast_error = SMAPE() forecast_error.initialize(