Beispiel #1
0
def test_strategy_mean_seasonal(fh, sp, window_length):
    if (window_length is not None
            and window_length > sp) or (window_length is None):
        f = NaiveForecaster(strategy="mean",
                            sp=sp,
                            window_length=window_length)
        f.fit(y_train)
        y_pred = f.predict(fh)

        # check predicted index
        assert_correct_pred_time_index(y_pred.index, y_train.index[-1], fh)

        if window_length is None:
            window_length = len(y_train)

        # check values
        fh = check_fh(fh)  # get well formatted fh
        reps = np.int(np.ceil(max(fh) / sp))
        last_window = y_train.iloc[-window_length:].values
        last_window = np.pad(
            last_window,
            (0, sp - len(last_window) % sp),
            "constant",
            constant_values=np.nan,
        )

        last_window = last_window.reshape(
            np.int(np.ceil(len(last_window) / sp)), sp)
        expected = np.tile(np.nanmean(last_window, axis=0), reps=reps)[fh - 1]
        np.testing.assert_array_equal(y_pred, expected)
Beispiel #2
0
def test_predict_in_sample(Forecaster, fh, y_train):
    f = _construct_instance(Forecaster)
    try:
        f.fit(y_train, fh=fh)
        y_pred = f.predict()
        assert_correct_pred_time_index(y_pred, y_train, fh)
    except NotImplementedError:
        pass
Beispiel #3
0
def test_predict_in_sample_full(Forecaster, y_train):
    f = _construct_instance(Forecaster)
    fh = -np.arange(len(y_train))  # full in-sample fh
    try:
        f.fit(y_train, fh=fh)
        y_pred = f.predict()
        assert_correct_pred_time_index(y_pred, y_train, fh)
    except NotImplementedError:
        pass
Beispiel #4
0
def test_predict_time_index(Forecaster, index_type, fh_type, is_relative,
                            steps):
    y_train = make_forecasting_problem(index_type=index_type)
    cutoff = y_train.index[-1]
    fh = _make_fh(cutoff, steps, fh_type, is_relative)
    f = _construct_instance(Forecaster)
    try:
        f.fit(y_train, fh=fh)
        y_pred = f.predict()
        assert_correct_pred_time_index(y_pred.index, y_train.index[-1], fh)
    except NotImplementedError:
        pass
Beispiel #5
0
def test_strategy_last_seasonal(fh, sp):
    f = NaiveForecaster(strategy="last", sp=sp)
    f.fit(y_train)
    y_pred = f.predict(fh)

    # check predicted index
    assert_correct_pred_time_index(y_pred.index, y_train.index[-1], fh)

    # check values
    fh = check_fh(fh)  # get well formatted fh
    reps = np.int(np.ceil(max(fh) / sp))
    expected = np.tile(y_train.iloc[-sp:], reps=reps)[fh - 1]
    np.testing.assert_array_equal(y_pred, expected)
Beispiel #6
0
def check_pred_ints(pred_ints, y_train, y_pred, fh):
    # make iterable
    if isinstance(pred_ints, pd.DataFrame):
        pred_ints = [pred_ints]

    for pred_int in pred_ints:
        assert list(pred_int.columns) == ["lower", "upper"]
        assert_correct_pred_time_index(pred_int.index, y_train.index[-1], fh)

        # check if errors are weakly monotonically increasing
        pred_errors = y_pred - pred_int["lower"]
        # assert pred_errors.is_mononotic_increasing
        assert np.all(
            pred_errors.values[1:].round(4) >= pred_errors.values[:-1].round(4)
        )
Beispiel #7
0
def test_predict_time_index_in_sample_full(Forecaster, index_type, fh_type,
                                           is_relative):
    # Check that predicted time index matched forecasting horizon for full in-sample
    # predictions.
    y_train = make_forecasting_problem(index_type=index_type)
    cutoff = y_train.index[-1]
    steps = -np.arange(len(y_train))  # full in-sample fh
    fh = _make_fh(cutoff, steps, fh_type, is_relative)
    f = _construct_instance(Forecaster)
    try:
        f.fit(y_train, fh=fh)
        y_pred = f.predict()
        assert_correct_pred_time_index(y_pred.index, y_train.index[-1], fh)
    except NotImplementedError:
        pass
Beispiel #8
0
def test_update_predict_single(Forecaster, fh):
    # Check correct time index of update-predict
    f = _construct_instance(Forecaster)
    f.fit(y_train, fh=fh)
    y_pred = f.update_predict_single(y_test)
    assert_correct_pred_time_index(y_pred.index, y_test.index[-1], fh)
Beispiel #9
0
def test_update_predict_single(Forecaster, fh):
    f = _construct_instance(Forecaster)
    f.fit(y_train, fh)
    y_pred = f.update_predict_single(y_test)
    assert_correct_pred_time_index(y_pred, y_test, fh)
Beispiel #10
0
def test_predict_time_index(Forecaster, fh, y_train):
    f = _construct_instance(Forecaster)
    f.fit(y_train, fh)
    y_pred = f.predict()
    assert_correct_pred_time_index(y_pred, y_train, fh)