def test_singlets_holtwinters(seasonal, h, datatype): global airpassengers airpassengers = np.asarray(airpassengers, dtype=datatype) train = airpassengers[:-h] test = airpassengers[-h:] if seasonal == "multiplicative": pytest.xfail("Statsmodels nan errors with gcc 9.3 (Issue #3384)") sm_hw = sm_ES(train, initialization_method='heuristic', seasonal=seasonal, seasonal_periods=12) sm_hw = sm_hw.fit() # train = cudf.Series(train) cu_hw = cuml_ES(train, seasonal=seasonal, seasonal_periods=12) cu_hw.fit() cu_pred = cu_hw.forecast(h) sm_pred = sm_hw.forecast(h) cu_r2 = r2_score(cu_pred.to_numpy(), test) sm_r2 = r2_score(sm_pred, test) assert (cu_r2 >= sm_r2) or (abs(cu_r2 - sm_r2) < 2e-1)
def test_start_freq_holtwinters(frequency, start_periods): global airpassengers, co2, nybirths data = np.asarray([airpassengers, co2, nybirths], dtype=np.float64) cu_hw = cuml_ES(data, ts_num=3, seasonal_periods=frequency, start_periods=start_periods) cu_hw.fit() cu_hw.forecast(5)
def test_get_season(freq): data = np.sin(np.arange(0, 100)*freq) cu_hw = cuml_ES(data) cu_hw.fit() evens = np.arange(0, 98, 2) odds = evens+1 seasons = cu_hw.get_season().to_numpy() base = seasons[0] assert pytest.approx(seasons[evens], 1e-4) == base assert pytest.approx(seasons[odds], 1e-4) == -1*base
def test_series_holtwinters(idx, h): global airpassengers, co2, nybirths data = np.asarray([airpassengers, co2, nybirths], dtype=np.float64) cu_hw = cuml_ES(data, ts_num=3) cu_hw.fit() cu_hw.forecast(h, idx) cu_hw.score(idx) cu_hw.get_level(idx) cu_hw.get_trend(idx) cu_hw.get_season(idx)
def test_inputs_holtwinters(datatype): global airpassengers, co2, nybirths data = np.asarray([airpassengers, co2, nybirths], dtype=datatype) cu_hw = cuml_ES(data, ts_num=3) cu_hw.fit() cu_hw.forecast(5) cu_hw.score() cu_hw.get_level(0) cu_hw.get_trend(1) cu_hw.get_season(2)
def test_multits_holtwinters(seasonal, h, datatype): global airpassengers, co2 airpassengers = np.asarray(airpassengers, dtype=datatype) co2 = np.asarray(co2, dtype=datatype) if seasonal == "multiplicative": pytest.xfail("Statsmodels nan errors with gcc 9.3 (Issue #3384)") air_train = airpassengers[:-h] air_test = airpassengers[-h:] co2_train = co2[:-h] co2_test = co2[-h:] data = np.asarray([air_train, co2_train], dtype=datatype) cu_hw = cuml_ES(data, seasonal=seasonal, seasonal_periods=12, ts_num=2) sm_air_hw = sm_ES(air_train, initialization_method='heuristic', seasonal=seasonal, seasonal_periods=12) sm_co2_hw = sm_ES(co2_train, initialization_method='heuristic', seasonal=seasonal, seasonal_periods=12) cu_hw.fit() sm_air_hw = sm_air_hw.fit() sm_co2_hw = sm_co2_hw.fit() cu_air_pred = cu_hw.forecast(h, 0) cu_co2_pred = cu_hw.forecast(h, 1) sm_air_pred = sm_air_hw.forecast(h) sm_co2_pred = sm_co2_hw.forecast(h) cu_air_r2 = r2_score(cu_air_pred.to_numpy(), air_test) cu_co2_r2 = r2_score(cu_co2_pred.to_numpy(), co2_test) sm_air_r2 = r2_score(sm_air_pred, air_test) sm_co2_r2 = r2_score(sm_co2_pred, co2_test) assert (cu_air_r2 >= sm_air_r2) or (abs(cu_air_r2 - sm_air_r2) < 4) assert (cu_co2_r2 >= sm_co2_r2) or (abs(cu_co2_r2 - sm_co2_r2) < 4) full_cu_pred = cu_hw.forecast(h) air_cu_r2 = r2_score(full_cu_pred[0].to_numpy(), air_test) co2_cu_r2 = r2_score(full_cu_pred[1].to_numpy(), co2_test) assert (air_cu_r2 >= sm_air_r2) or (abs(air_cu_r2 - sm_air_r2) < 4) assert (co2_cu_r2 >= sm_co2_r2) or (abs(co2_cu_r2 - sm_co2_r2) < 4)
def test_multits_holtwinters(seasonal, h, datatype, input_type): global airpassengers, co2 airpassengers = np.asarray(airpassengers, dtype=datatype) co2 = np.asarray(co2, dtype=datatype) air_train = airpassengers[:-h] air_test = airpassengers[-h:] co2_train = co2[:-h] co2_test = co2[-h:] data = np.asarray([air_train, co2_train], dtype=datatype) if input_type == 'cudf': data = cudf.DataFrame({i: data[i] for i in range(data.shape[0])}) cu_hw = cuml_ES(data, seasonal=seasonal, seasonal_periods=12, ts_num=2) sm_air_hw = sm_ES(air_train, seasonal=seasonal, seasonal_periods=12) sm_co2_hw = sm_ES(co2_train, seasonal=seasonal, seasonal_periods=12) cu_hw.fit() sm_air_hw = sm_air_hw.fit() sm_co2_hw = sm_co2_hw.fit() cu_air_pred = cu_hw.forecast(h, 0) cu_co2_pred = cu_hw.forecast(h, 1) sm_air_pred = sm_air_hw.forecast(h) sm_co2_pred = sm_co2_hw.forecast(h) cu_air_r2 = r2_score(cu_air_pred, air_test) cu_co2_r2 = r2_score(cu_co2_pred, co2_test) sm_air_r2 = r2_score(sm_air_pred, air_test) sm_co2_r2 = r2_score(sm_co2_pred, co2_test) assert (cu_air_r2 >= sm_air_r2) or (abs(cu_air_r2 - sm_air_r2) < 2e-1) assert (cu_co2_r2 >= sm_co2_r2) or (abs(cu_co2_r2 - sm_co2_r2) < 2e-1) full_cu_pred = cu_hw.forecast(h) air_cu_r2 = r2_score(full_cu_pred[0], air_test) co2_cu_r2 = r2_score(full_cu_pred[1], co2_test) assert (air_cu_r2 >= sm_air_r2) or (abs(air_cu_r2 - sm_air_r2) < 2e-1) assert (co2_cu_r2 >= sm_co2_r2) or (abs(co2_cu_r2 - sm_co2_r2) < 2e-1)
def test_inputs_holtwinters(datatype, input_type): global airpassengers, co2, nybirths data = np.asarray([airpassengers, co2, nybirths], dtype=datatype) if input_type == 'cudf': data = cudf.DataFrame({i: data[i] for i in range(data.shape[0])}) elif input_type == 'cupy': try: import cupy as cp data = cp.asarray(data) except ImportError: pytest.skip("CuPy import error -- skipping test.") cu_hw = cuml_ES(data, ts_num=3) cu_hw.fit() cu_hw.forecast(5) cu_hw.score() cu_hw.get_level(0) cu_hw.get_trend(1) cu_hw.get_season(2)
def test_singlets_holtwinters(seasonal, h, datatype): global airpassengers airpassengers = np.asarray(airpassengers, dtype=datatype) train = airpassengers[:-h] test = airpassengers[-h:] sm_hw = sm_ES(train, seasonal=seasonal, seasonal_periods=12) sm_hw = sm_hw.fit() # train = cudf.Series(train) cu_hw = cuml_ES(train, seasonal=seasonal, seasonal_periods=12) cu_hw.fit() cu_pred = cu_hw.forecast(h) sm_pred = sm_hw.forecast(h) cu_r2 = r2_score(cu_pred, test) sm_r2 = r2_score(sm_pred, test) assert (cu_r2 >= sm_r2) or (abs(cu_r2 - sm_r2) < 2e-1)
def test_multits_holtwinters(seasonal, h, datatype): global airpassengers, co2 airpassengers = np.asarray(airpassengers, dtype=datatype) co2 = np.asarray(co2, dtype=datatype) air_train = airpassengers[:-h] air_test = airpassengers[-h:] co2_train = co2[:-h] co2_test = co2[-h:] data = np.asarray([air_train, co2_train], dtype=datatype) cu_hw = cuml_ES(data, seasonal=seasonal, seasonal_periods=12, ts_num=2) sm_air_hw = sm_ES(air_train, seasonal=seasonal, seasonal_periods=12) sm_co2_hw = sm_ES(co2_train, seasonal=seasonal, seasonal_periods=12) cu_hw.fit() sm_air_hw = sm_air_hw.fit() sm_co2_hw = sm_co2_hw.fit() cu_air_pred = cu_hw.forecast(h, 0) cu_co2_pred = cu_hw.forecast(h, 1) sm_air_pred = sm_air_hw.forecast(h) sm_co2_pred = sm_co2_hw.forecast(h) cu_air_r2 = r2_score(cu_air_pred.to_array(), air_test) cu_co2_r2 = r2_score(cu_co2_pred.to_array(), co2_test) sm_air_r2 = r2_score(sm_air_pred, air_test) sm_co2_r2 = r2_score(sm_co2_pred, co2_test) assert (cu_air_r2 >= sm_air_r2) or (abs(cu_air_r2 - sm_air_r2) < 4) assert (cu_co2_r2 >= sm_co2_r2) or (abs(cu_co2_r2 - sm_co2_r2) < 4) full_cu_pred = cu_hw.forecast(h) air_cu_r2 = r2_score(full_cu_pred[0], air_test) co2_cu_r2 = r2_score(full_cu_pred[1], co2_test) assert (air_cu_r2 >= sm_air_r2) or (abs(air_cu_r2 - sm_air_r2) < 4) assert (co2_cu_r2 >= sm_co2_r2) or (abs(co2_cu_r2 - sm_co2_r2) < 4)
def test_get_trend(slope): data = np.arange(0, 100*slope, slope, dtype=np.float64) cu_hw = cuml_ES(data) cu_hw.fit() assert pytest.approx(cu_hw.get_trend().to_numpy(), 1e-4) == slope
def test_get_level(level): data = np.array([level]*100, dtype=np.float64) cu_hw = cuml_ES(data) cu_hw.fit() assert pytest.approx(cu_hw.get_level().to_numpy(), 1e-4) == level
def test_eps_holtwinters(eps): global airpassengers, co2, nybirths data = np.asarray([airpassengers, co2, nybirths], dtype=np.float64) cu_hw = cuml_ES(data, eps=eps, ts_num=3) cu_hw.fit() cu_hw.forecast(5)
def test_seasonal_holtwinters(seasonal): global airpassengers, co2, nybirths data = np.asarray([airpassengers, co2, nybirths], dtype=np.float64) cu_hw = cuml_ES(data, seasonal=seasonal, ts_num=3) cu_hw.fit() cu_hw.forecast(5)