Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    def local_error_test(self):
        """Test SymmetricMeanAbsolutePercentageError local error."""
        dataPtsOrg  = [2.30,     .373,           .583,          1.88,  1.44,         -0.0852, -.341,  .619,  .131,  1.27, 4.0]
        dataPtsCalc = [-1.21,   -.445,           .466,          .226, -.694,           -.575,  2.73, -1.49, -1.45, -.193, 4.0]
        localErrors = [  2.0,     2.0, 0.223069590086, 1.57075023742,   2.0,   1.48379279006,   2.0,   2.0,   2.0,   2.0, 0.0]

        smape = SymmetricMeanAbsolutePercentageError()

        for idx in xrange(len(dataPtsOrg)):
            le = smape.local_error([dataPtsOrg[idx]], [dataPtsCalc[idx]])
            ple = localErrors[idx]

            ## compare the strings due to accuracy
            assert str(le) == str(ple)
Ejemplo n.º 3
0
    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]
Ejemplo n.º 4
0
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')
Ejemplo n.º 5
0
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')
Ejemplo n.º 6
0
    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]
Ejemplo n.º 7
0
    def local_error_test(self):
        """Test SymmetricMeanAbsolutePercentageError local error."""
        dataPtsOrg = [
            2.30, .373, .583, 1.88, 1.44, -0.0852, -.341, .619, .131, 1.27, 4.0
        ]
        dataPtsCalc = [
            -1.21, -.445, .466, .226, -.694, -.575, 2.73, -1.49, -1.45, -.193,
            4.0
        ]
        localErrors = [
            2.0, 2.0, 0.223069590086, 1.57075023742, 2.0, 1.48379279006, 2.0,
            2.0, 2.0, 2.0, 0.0
        ]

        smape = SymmetricMeanAbsolutePercentageError()

        for idx in xrange(len(dataPtsOrg)):
            le = smape.local_error([dataPtsOrg[idx]], [dataPtsCalc[idx]])
            ple = localErrors[idx]

            ## compare the strings due to accuracy
            assert str(le) == str(ple)
Ejemplo n.º 8
0
            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
Ejemplo n.º 9
0
			#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