<s> import unittest <EOL> from pycast . errors . symmetricmeanabsolutepercentageerror import SymmetricMeanAbsolutePercentageError as SMAPE <EOL> from pycast . common . timeseries import TimeSeries <EOL> from pycast . methods . basemethod import BaseForecastingMethod <EOL> from pycast . methods . exponentialsmoothing import ExponentialSmoothing , HoltMethod <EOL> from pycast . optimization . gridsearch import GridSearch <EOL> class GridSearchTest ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> bfm = BaseForecastingMethod ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> bfm . _parameterIntervals = { } <EOL> bfm . _parameterIntervals [ "<STR_LIT>" ] = [ <NUM_LIT:0.0> , <NUM_LIT:1.0> , False , False ] <EOL> bfm . _parameterIntervals [ "<STR_LIT>" ] = [ <NUM_LIT:0.0> , <NUM_LIT> , True , True ] <EOL> self . bfm = bfm <EOL> data = [ [ <NUM_LIT:0.0> , <NUM_LIT:0.0> ] , [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT:0.5> ] ] <EOL> self . timeSeries = TimeSeries . from_twodim_list ( data ) <EOL> self . timeSeries . normalize ( "<STR_LIT>" ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> del self . bfm <EOL> del self . timeSeries <EOL> def create_generator_test ( self ) : <EOL> """<STR_LIT>""" <EOL> precision = <NUM_LIT:10> ** - <NUM_LIT:2> <EOL> values_one = [ i * precision for i in xrange ( <NUM_LIT:1> , <NUM_LIT:100> ) ] <EOL> values_two = [ i * precision for i in xrange ( <NUM_LIT> ) ] <EOL> generator_one = GridSearch ( SMAPE , precision = - <NUM_LIT:2> ) . _generate_next_parameter_value ( "<STR_LIT>" , self . bfm ) <EOL> generator_two = GridSearch ( SMAPE , precision = - <NUM_LIT:2> ) . _generate_next_parameter_value ( "<STR_LIT>" , self . bfm ) <EOL> generated_one = [ val for val in generator_one ] <EOL> generated_two = [ val for val in generator_two ] <EOL> assert len ( values_one ) == len ( generated_one ) <EOL> assert len ( values_two ) == len ( generated_two ) <EOL> for idx in xrange ( len ( values_one ) ) : <EOL> value = str ( values_one [ idx ] ) [ : <NUM_LIT:12> ] <EOL> assert str ( value ) == str ( generated_one [ idx ] ) [ : len ( value ) ] <EOL> for idx in xrange ( len ( values_two ) ) : <EOL> value = str ( values_two [ idx ] ) [ : <NUM_LIT:12> ] <EOL> assert str ( value ) == str ( generated_two [ idx ] ) [ : len ( value ) ] <EOL> def optimize_exception_test ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> GridSearch ( SMAPE , - <NUM_LIT:2> ) . optimize ( self . timeSeries ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> assert False <EOL> try : <EOL> GridSearch ( SMAPE , - <NUM_LIT:1> ) . optimize ( self . timeSeries , [ self . bfm ] ) <EOL> except NotImplementedError : <EOL> pass <EOL> else : <EOL> assert False <EOL> def optimize_value_creation_test ( self ) : <EOL> """<STR_LIT>""" <EOL> self . bfm . _requiredParameters = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> try : <EOL> GridSearch ( SMAPE , - <NUM_LIT:1> ) . optimize_forecasting_method ( self . timeSeries , self . bfm ) <EOL> except NotImplementedError : <EOL> pass <EOL> else : <EOL> assert False <EOL> self . bfm . _parameterIntervals = { <EOL> "<STR_LIT>" : [ <NUM_LIT:0.0> , <NUM_LIT:1.0> , True , True ] , <EOL> "<STR_LIT>" : [ <NUM_LIT:0.0> , <NUM_LIT:1.0> , True , True ] , <EOL> "<STR_LIT>" : [ <NUM_LIT:0.0> , <NUM_LIT:1.0> , True , True ] <EOL> } <EOL> try : <EOL> GridSearch ( SMAPE , - <NUM_LIT:5> ) . optimize_forecasting_method ( self . timeSeries , self . bfm ) <EOL> except NotImplementedError : <EOL> pass <EOL> else : <EOL> assert False <EOL> def inner_optimization_result_test ( self ) : <EOL> """<STR_LIT>""" <EOL> fm = ExponentialSmoothing ( ) <EOL> startingPercentage = <NUM_LIT:0.0> <EOL> endPercentage = <NUM_LIT> <EOL> self . timeSeries . normalize ( "<STR_LIT>" ) <EOL> results = [ ] <EOL> for smoothingFactor in [ alpha / <NUM_LIT> for alpha in xrange ( <NUM_LIT:1> , <NUM_LIT:100> ) ] : <EOL> fm . set_parameter ( "<STR_LIT>" , smoothingFactor ) <EOL> resultTS = self . timeSeries . apply ( fm ) <EOL> error = SMAPE ( ) <EOL> error . initialize ( self . timeSeries , resultTS ) <EOL> results . append ( [ error , smoothingFactor ] ) <EOL> bestManualResult = min ( results , key = lambda item : item [ <NUM_LIT:0> ] . get_error ( startingPercentage , endPercentage ) ) <EOL> gridSearch = GridSearch ( SMAPE , precision = - <NUM_LIT:2> ) <EOL> gridSearch . _startingPercentage = startingPercentage <EOL> gridSearch . _endPercentage = endPercentage <EOL> result = gridSearch . optimize_forecasting_method ( self . timeSeries , fm ) <EOL> bestManualAlpha = bestManualResult [ <NUM_LIT:1> ] <EOL> errorManualResult = bestManualResult [ <NUM_LIT:0> ] . get_error ( ) <EOL> bestGridSearchAlpha = result [ <NUM_LIT:1> ] [ "<STR_LIT>" ] <EOL> errorGridSearchResult = result [ <NUM_LIT:0> ] . get_error ( ) <EOL> assert str ( errorManualResult ) [ : <NUM_LIT:8> ] >= str ( errorGridSearchResult ) [ : <NUM_LIT:8> ] <EOL> assert str ( bestManualAlpha ) [ : <NUM_LIT:5> ] == str ( bestGridSearchAlpha ) [ : <NUM_LIT:5> ] <EOL> def inner_optimization_result_accuracy_test ( self ) : <EOL> """<STR_LIT>""" <EOL> fm = ExponentialSmoothing ( ) <EOL> startingPercentage = <NUM_LIT:0.0> <EOL> endPercentage = <NUM_LIT> <EOL> self . timeSeries . normalize ( "<STR_LIT>" ) <EOL> results = [ ] <EOL> for smoothingFactor in [ alpha / <NUM_LIT> for alpha in xrange ( <NUM_LIT:1> , <NUM_LIT:100> ) ] : <EOL> fm . set_parameter ( "<STR_LIT>" , smoothingFactor ) <EOL> resultTS = self . timeSeries . apply ( fm ) <EOL> error = SMAPE ( ) <EOL> error . initialize ( self . timeSeries , resultTS ) <EOL> results . append ( [ error , smoothingFactor ] ) <EOL> bestManualResult = min ( results , key = lambda item : item [ <NUM_LIT:0> ] . get_error ( startingPercentage , endPercentage ) ) <EOL> gridSearch = GridSearch ( SMAPE , precision = - <NUM_LIT:4> ) <EOL> gridSearch . _startingPercentage = startingPercentage <EOL> gridSearch . _endPercentage = endPercentage <EOL> result = gridSearch . optimize_forecasting_method ( self . timeSeries , fm ) <EOL> bestManualAlpha = bestManualResult [ <NUM_LIT:1> ] <EOL> errorManualResult = bestManualResult [ <NUM_LIT:0> ] . get_error ( ) <EOL> bestGridSearchAlpha = result [ <NUM_LIT:1> ] [ "<STR_LIT>" ] <EOL> errorGridSearchResult = result [ <NUM_LIT:0> ] . get_error ( ) <EOL> assert errorManualResult > errorGridSearchResult <EOL> def outer_optimization_result_test ( self ) : <EOL> """<STR_LIT>""" <EOL> fm1 = ExponentialSmoothing ( ) <EOL> fm2 = HoltMethod ( ) <EOL> self . timeSeries . normalize ( "<STR_LIT>" ) <EOL> gridSearch = GridSearch ( SMAPE , precision = - <NUM_LIT:2> ) <EOL> result = gridSearch . optimize ( self . timeSeries , [ fm1 , fm2 ] ) <EOL> def optimization_loop_test ( self ) : <EOL> """<STR_LIT>""" <EOL> gridSearch = GridSearch ( SMAPE , precision = - <NUM_LIT:2> ) <EOL> def crap_execute ( ignoreMe ) : <EOL> ts = self . timeSeries . to_twodim_list ( ) <EOL> ts = TimeSeries . from_twodim_list ( ts ) <EOL> for entry in ts : <EOL> entry [ <NUM_LIT:0> ] += <NUM_LIT:0.1> <EOL> return ts <EOL> self . bfm . execute = crap_execute <EOL> result = gridSearch . optimization_loop ( self . timeSeries , self . bfm , [ ] , { } ) <EOL> assert result == [ ] </s>
