def test_rolling_forecast_cv_bad_splits(): endog = y[:100] cv = RollingForecastCV(initial=90, step=1, h=4) gen = cv.split(endog) expected = [ (np.arange(0, 90), np.array([90, 91, 92, 93])), (np.arange(1, 91), np.array([91, 92, 93, 94])), (np.arange(2, 92), np.array([92, 93, 94, 95])), (np.arange(3, 93), np.array([93, 94, 95, 96])), (np.arange(4, 94), np.array([94, 95, 96, 97])), (np.arange(5, 95), np.array([95, 96, 97, 98])), (np.arange(6, 96), np.array([96, 97, 98, 99])), ] # should be 7 for i, (train, test) in enumerate(gen): assert_array_equal(train, expected[i][0]) assert_array_equal(test, expected[i][1]) # assert no extra splits with pytest.raises(StopIteration): next(gen)
def test_grouped_model_cross_validate(): metrics = ["smape", "mean_squared_error", "mean_absolute_error"] expected_columns = ( [f"{met}_mean" for met in metrics] + [f"{met}_stddev" for met in metrics] + ["grouping_key_columns", "key0"] ) train = data_generator.generate_test_data(1, 2, 765, "2019-01-01") grouped_model = GroupedPmdarima( model_template=AutoARIMA(max_order=5, out_of_sample_size=30), ).fit(train.df, train.key_columns, "y", "ds", silence_warnings=True) cross_validator = RollingForecastCV(h=90, step=120, initial=365) cv_metrics = grouped_model.cross_validate(train.df, metrics, cross_validator) assert len(cv_metrics) == 2 assert set(cv_metrics.columns).issubset(set(expected_columns))
silence_warnings=True, ) # Save to local directory save_dir = "/tmp/group_pmdarima/pipeline.gpmd" pipeline_arima.save(save_dir) # Load from saved model loaded_model = GroupedPmdarima.load(save_dir) print("\nPipeline AutoARIMA results:\n", "-" * 40) get_and_print_model_metrics_params(loaded_model) print("\nPredictions:\n", "-" * 40) prediction = loaded_model.predict( n_periods=30, alpha=0.2, predict_col="predictions", return_conf_int=True ) print(prediction.to_string()) print("\nCross validation metric results:\n", "-" * 40) cross_validator = RollingForecastCV(h=30, step=365, initial=730) cv_results = loaded_model.cross_validate( df=training_data, metrics=["mean_squared_error"], cross_validator=cross_validator, error_score=np.nan, verbosity=3, ) print(cv_results.to_string())
def test_cv_constructor_value_errors(): with pytest.raises(ValueError): RollingForecastCV(h=-1), # too low horizon with pytest.raises(ValueError): RollingForecastCV(step=-1), # too low step
# -*- coding: utf-8 -*- from pmdarima.compat.pytest import pytest_error_str from pmdarima.model_selection import RollingForecastCV, \ SlidingWindowForecastCV, check_cv, train_test_split from pmdarima.datasets import load_wineind import pytest import numpy as np from numpy.testing import assert_array_equal y = load_wineind() @pytest.mark.parametrize( 'cv', [ RollingForecastCV(), RollingForecastCV(h=4), RollingForecastCV(initial=150, h=10), RollingForecastCV(initial=12, h=16, step=7), ] ) def test_rolling_forecast_cv_passing(cv): # get all splits splits = list(cv.split(y)) last_train_step = None for train, test in splits: assert test.shape[0] == cv.h assert test[-1] == train[-1] + cv.h if last_train_step is not None: assert train[-1] == last_train_step + cv.step