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 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])
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)
#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( forecast_check, TimeSeries.from_twodim_list(predicted[-len(forecast_check):]))