def error_calculation_test(self): """Test the calculation of the SymmetricMeanAbsolutePercentageError.""" dataPtsOrg = [2.30, .373, .583, 1.88, 1.44, -0.0852, -.341, .619, .131, 1.27, 0] dataPtsCalc = [-1.21, -.445, .466, .226, -.694, -.575, 2.73, -1.49, -1.45, -.193, 0] tsOrg = TimeSeries() tsCalc = TimeSeries() for idx in xrange(len(dataPtsOrg)): tsOrg.add_entry(float(idx), dataPtsOrg[idx]) tsCalc.add_entry(float(idx), dataPtsCalc[idx]) smape = SymmetricMeanAbsolutePercentageError() smape.initialize(tsOrg, tsCalc) ## compare the strings due to accuracy assert "1.5706" == str(smape.get_error())[:6]
def holtWinters(request): """ Performs Holt Winters Smoothing on the given post data. Expects the following values set in the post of the request: smoothingFactor - float trendSmoothingFactor - float seasonSmoothingFactor - float seasonLength - integer valuesToForecast - integer data - two dimensional array of [timestamp, value] """ #Parse arguments smoothingFactor = float(request.POST.get('smoothingFactor', 0.2)) trendSmoothingFactor = float(request.POST.get('trendSmoothingFactor', 0.3)) seasonSmoothingFactor = float(request.POST.get('seasonSmoothingFactor', 0.4)) seasonLength = int(request.POST.get('seasonLength', 6)) valuesToForecast = int(request.POST.get('valuesToForecast', 0)) data = json.loads(request.POST.get('data', [])) #perform smoothing hwm = HoltWintersMethod(smoothingFactor = smoothingFactor, trendSmoothingFactor = trendSmoothingFactor, seasonSmoothingFactor = seasonSmoothingFactor, seasonLength = seasonLength, valuesToForecast = valuesToForecast) original = TimeSeries.from_twodim_list(data) original.set_timeformat("%d.%m") smoothed = hwm.execute(original) smoothed.set_timeformat("%d.%m") error = SMAPE() error.initialize(original, smoothed) #process the result result = { 'original': original, 'smoothed': smoothed, 'error': round(error.get_error(), 3) } return Response(json.dumps(result, cls=PycastEncoder), content_type='application/json')
def holtWinters(request): """ Performs Holt Winters Smoothing on the given post data. Expects the following values set in the post of the request: smoothingFactor - float trendSmoothingFactor - float seasonSmoothingFactor - float seasonLength - integer valuesToForecast - integer data - two dimensional array of [timestamp, value] """ #Parse arguments smoothingFactor = float(request.POST.get('smoothingFactor', 0.2)) trendSmoothingFactor = float(request.POST.get('trendSmoothingFactor', 0.3)) seasonSmoothingFactor = float(request.POST.get('seasonSmoothingFactor', 0.4)) seasonLength = int(request.POST.get('seasonLength', 6)) valuesToForecast = int(request.POST.get('valuesToForecast', 0)) data = json.loads(request.POST.get('data', [])) #perform smoothing hwm = HoltWintersMethod(smoothingFactor = smoothingFactor, trendSmoothingFactor = trendSmoothingFactor, seasonSmoothingFactor = seasonSmoothingFactor, seasonLength = seasonLength, valuesToForecast = valuesToForecast) original = TimeSeries.from_twodim_list(data) original.set_timeformat("%d.%m") smoothed = hwm.execute(original) smoothed.set_timeformat("%d.%m") error = SMAPE() error.initialize(original, smoothed) #process the result result = { 'original': original, 'smoothed': smoothed, 'error': round(error.get_error(), 3) } return itty.Response(json.dumps(result, cls=PycastEncoder), content_type='application/json')
def error_calculation_test(self): """Test the calculation of the SymmetricMeanAbsolutePercentageError.""" dataPtsOrg = [ 2.30, .373, .583, 1.88, 1.44, -0.0852, -.341, .619, .131, 1.27, 0 ] dataPtsCalc = [ -1.21, -.445, .466, .226, -.694, -.575, 2.73, -1.49, -1.45, -.193, 0 ] tsOrg = TimeSeries() tsCalc = TimeSeries() for idx in xrange(len(dataPtsOrg)): tsOrg.add_entry(float(idx), dataPtsOrg[idx]) tsCalc.add_entry(float(idx), dataPtsCalc[idx]) smape = SymmetricMeanAbsolutePercentageError() smape.initialize(tsOrg, tsCalc) ## compare the strings due to accuracy assert "1.5706" == str(smape.get_error())[:6]
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):])) print counter, error.get_error(), total_error.get_error( ), forecast_error.get_error() counter += 1
#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(forecast_check, TimeSeries.from_twodim_list(predicted[-len(forecast_check):])) print counter, error.get_error(), total_error.get_error(), forecast_error.get_error() counter += 1