def season_length_test(self): """Test that the season length has to be greater than 0.""" for seasonLength in xrange(-4, 1): try: HoltWintersMethod(seasonLength=seasonLength) except ValueError: pass else: assert False # pragma: no cover for seasonLength in xrange(1, 12414, 412): HoltWintersMethod(seasonLength=seasonLength)
def initialization_test(self): """Test the initialization of the HoltWintersMethod method.""" HoltWintersMethod(0.2, 0.3, 0.4, 5) for alpha in [-0.1, 0.81, 1.1]: for beta in [-1.4, 0.12, 3.2]: for gamma in [-0.05, 1.3]: try: HoltWintersMethod(alpha, beta, gamma) except ValueError: pass else: assert False # pragma: no cover
def forecasting_test(self): data = [ 362.0, 385.0, 432.0, 341.0, 382.0, 409.0, 498.0, 387.0, 473.0, 513.0, 582.0, 474.0, 544.0, 582.0, 681.0, 557.0, 628.0, 707.0, 773.0, 592.0, 627.0, 725.0, 854.0, 661.0 ] tsSrc = TimeSeries.from_twodim_list(zip(range(len(data)), data)) expected = [[0.0, 362.0], [1.0, 379.93673257607463], [2.0, 376.86173719924875], [3.0, 376.0203652542205], [4.0, 408.21988583215574], [5.0, 407.16235446485433], [6.0, 430.0950666716297], [7.0, 429.89797609228435], [8.0, 489.4888959723074], [9.0, 507.8407281475308], [10.0, 506.3556647249702], [11.0, 523.9422448655133], [12.0, 556.0311543025242], [13.0, 573.6520991970604], [14.0, 590.2149136780341], [15.0, 611.8813425659495], [16.0, 637.0393967524727], [17.0, 684.6600411792656], [18.0, 675.9589298142507], [19.0, 659.0266828674846], [20.0, 644.0903317144154], [21.0, 690.4507762388047], [22.0, 735.3219292023371], [23.0, 737.9752345691215], [24.0, 669.767091965978], [25.0, 737.5272444120604], [26.0, 805.3947787747426], [27.0, 902.1522777060334]] hwm = HoltWintersMethod(.7556, 0.0000001, .9837, 4, valuesToForecast=4) res = tsSrc.apply(hwm) #print res assert len(res) == len(tsSrc) + 4 assert res == TimeSeries.from_twodim_list(expected)
def smoothing_test(self): """ Test if the smoothing works correctly""" data = [ 362.0, 385.0, 432.0, 341.0, 382.0, 409.0, 498.0, 387.0, 473.0, 513.0, 582.0, 474.0, 544.0, 582.0, 681.0, 557.0, 628.0, 707.0, 773.0, 592.0, 627.0, 725.0, 854.0, 661.0 ] tsSrc = TimeSeries.from_twodim_list(zip(range(len(data)), data)) expected = [[0.0, 362.0], [1.0, 379.93673257607463], [2.0, 376.86173719924875], [3.0, 376.0203652542205], [4.0, 408.21988583215574], [5.0, 407.16235446485433], [6.0, 430.0950666716297], [7.0, 429.89797609228435], [8.0, 489.4888959723074], [9.0, 507.8407281475308], [10.0, 506.3556647249702], [11.0, 523.9422448655133], [12.0, 556.0311543025242], [13.0, 573.6520991970604], [14.0, 590.2149136780341], [15.0, 611.8813425659495], [16.0, 637.0393967524727], [17.0, 684.6600411792656], [18.0, 675.9589298142507], [19.0, 659.0266828674846], [20.0, 644.0903317144154], [21.0, 690.4507762388047], [22.0, 735.3219292023371], [23.0, 737.9752345691215]] hwm = HoltWintersMethod(.7556, 0.0000001, .9837, 4, valuesToForecast=0) initialA_2 = hwm.computeA(2, tsSrc) assert initialA_2 == 510.5, "Third initial A_2 should be 510.5, but it %d" % initialA_2 initialTrend = hwm.initialTrendSmoothingFactors(tsSrc) assert initialTrend == 9.75, "Initial Trend should be 9.75 but is %d" % initialTrend #correctness is not proven, but will be enough for regression testing resTS = tsSrc.apply(hwm) expectedTS = TimeSeries.from_twodim_list(expected) assert len(resTS) == len(expectedTS) assert resTS == expectedTS, "Smoothing result not correct."
def initial_trend_values_test(self): hwm = HoltWintersMethod(seasonLength=4) data = [[0, 362.0], [1, 385.0], [2, 432.0], [3, 341.0], [4, 382.0], [5, 425.0]] tsSrc = TimeSeries.from_twodim_list(data) trend = hwm.initialTrendSmoothingFactors(tsSrc) assert trend == 7.5, "Initial Trend should be 7.5 but is %f" % trend
def sanity_test(self): """HoltWinters should throw an Exception if applied to a Time Series shorter than the season length""" hwm = HoltWintersMethod(seasonLength=2) data = [[0.0, 152]] tsSrc = TimeSeries.from_twodim_list(data) try: tsSrc.apply(hwm) except ValueError, e: pass
def season_factor_initialization_test(self): """ Test if seasonal correction factors are initialized correctly.""" hwm = HoltWintersMethod(seasonLength=4) data = [[0, 362.0], [1, 385.0], [2, 432.0], [3, 341.0], [4, 382.0], [5, 409.0], [6, 498.0], [7, 387.0], [8, 473.0], [9, 513.0], [10, 582.0], [11, 474.0]] tsSrc = TimeSeries.from_twodim_list(data) seasonValues = hwm.initSeasonFactors(tsSrc) #correctness is not proven, but will be enough for regression testing assert seasonValues == [ 0.9302895649920525, 0.9980629019785198, 1.1551483413078523, 0.9164991917215755 ], "Season Values are not initialized correctly" # pragma: no cover
def preset_season_factor_test(self): """Initial Season Factors should be presetable""" hwm = HoltWintersMethod(seasonLength=4) factors = [0, 1, 2, 3] hwm.set_parameter("seasonValues", factors) data = [[0, 362.0], [1, 385.0], [2, 432.0], [3, 341.0], [4, 382.0], [5, 409.0], [6, 498.0], [7, 387.0], [8, 473.0], [9, 513.0], [10, 582.0], [11, 474.0]] tsSrc = TimeSeries.from_twodim_list(data) seasonValues = hwm.initSeasonFactors(tsSrc) assert seasonValues == factors, "Preset Season Factors are not returned by initSeasonFactors" hwm.set_parameter("seasonValues", factors[:2]) try: hwm.initSeasonFactors(tsSrc) except AssertionError: pass else: assert False, "If preset season factors and season length do not comply, initSeasonFactors should throw an AssertionError" # pragma: no cover
forecast_check = TimeSeries(isNormalized=True) for j in range(number_obeservations, number_obeservations + number_forecast): forecast_check.add_entry(j, float(series_tuples[j + 7])) #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()