Beispiel #1
0
def test_snaive_abnormal_input(data, exception):
    '''
    test snaive raises correct exceptions on abnormal input
    '''
    model = b.SNaive(1)
    with pytest.raises(exception):
        model.fit(data)
Beispiel #2
0
def test_snaive_fit_predict(data, horizon, expected):
    '''
    test the correct number of error metric functions are returned.
    '''
    model = b.SNaive(1)
    # fit_predict for point forecasts only
    preds = model.fit_predict(pd.Series(data), horizon)
    assert len(preds) == expected
Beispiel #3
0
def test_snaive_call_predict_before_fit():
    '''
    test SNaive raises correct exceptions when
    predict is called before fit
    '''
    model = b.SNaive(7)
    with pytest.raises(UnboundLocalError):
        model.predict(10)
Beispiel #4
0
def test_snaive_forecast_output(data, period, expected):
    '''
    test naive1 carries forward the last value in the series
    '''
    model = b.SNaive(period)
    model.fit(data)
    # point forecasts only
    preds = model.predict(period)
    assert np.array_equal(preds, expected)
Beispiel #5
0
def test_snaive_forecast_input_dataframe(data, horizon, expected):
    '''
    test the correct number of error metric functions are returned.
    '''
    model = b.SNaive(1)
    model.fit(pd.DataFrame(data))
    # point forecasts only
    preds = model.predict(horizon)
    assert len(preds) == expected
Beispiel #6
0
def test_snaive_pi_horizon(data, horizon, alpha, expected):
    '''
    test the correct forecast horizon is returned for prediction
    interval for SNaive
    '''
    model = b.SNaive(1)
    model.fit(pd.Series(data))
    # point forecasts only
    _, intervals = model.predict(horizon, return_predict_int=True, alpha=alpha)
    assert len(intervals[0]) == expected
Beispiel #7
0
def test_snaive_fitted_values_length(training_length):
    '''
    test SNaive .fittedvalues length is as expected
    '''
    np.random.seed(1066)
    train = np.random.poisson(lam=50, size=training_length)

    model = b.SNaive(7)
    model.fit(train)

    expected = training_length

    assert len(model.fittedvalues) == expected
Beispiel #8
0
def test_snaive_fitted_values_nan_length(period):
    '''
    test SNaive .fittedvalues has the correct number of NaNs
    i.e. = to the seasonal period.
    '''
    np.random.seed(1066)
    train = np.random.poisson(lam=50, size=200)

    model = b.SNaive(period)
    model.fit(train)

    expected = period
    n_nan = np.isnan(model.fittedvalues).sum()

    assert n_nan == expected
Beispiel #9
0
def test_snaive_prediction_interval_95_low():
    '''
    test snaive 95% lower prediction interval
    intervals are matched from R forecast package
    '''
    np.random.seed(1066)
    train = np.random.poisson(lam=50, size=100)
    low = [25.12464, 50.12464, 42.12464, 23.12464, 16.89199]
    # high = [64.87536, 89.87536, 81.87536, 62.87536, 73.10801]

    # quarterly data
    model = b.SNaive(period=4)
    model.fit(train)
    _, intervals = model.predict(5, return_predict_int=True, alpha=[0.05])

    print(intervals[0].T[0])
    assert pytest.approx(intervals[0].T[0]) == low
Beispiel #10
0
def test_snaive_prediction_interval_80_high():
    '''
    test snaive 80% upper prediction interval
    intervals are matched from R forecast package
    '''
    np.random.seed(1066)
    train = np.random.poisson(lam=50, size=100)
    # low = [32.00420, 57.00420, 49.00420, 30.00420, 26.62116]
    high = [57.99580, 82.99580, 74.99580, 55.99580, 63.37884]

    # quarterly data
    model = b.SNaive(period=4)
    model.fit(train)
    _, intervals = model.predict(5, return_predict_int=True, alpha=[0.2])

    print(intervals[0].T[1])
    assert pytest.approx(intervals[0].T[1]) == high
Beispiel #11
0
    '''
    test the correct forecast horizon is returned for prediction
    interval for Average
    '''
    model = b.Average()
    model.fit(pd.Series(data))
    # point forecasts only
    _, intervals = model.predict(horizon, return_predict_int=True, alpha=alpha)
    assert len(intervals[0]) == expected


@pytest.mark.parametrize(
    "model, data, horizon, alpha, expected",
    [(b.Naive1(), [1, 2, 3, 4, 5], 12, [0.2, 0.05], 2),
     (b.Naive1(), [1, 2, 3, 4, 5], 24, [0.2, 0.10, 0.05], 3),
     (b.SNaive(1), [1, 2, 3], 8, [0.8], 1),
     (b.SNaive(1), [1, 2, 3, 4, 5], 24, [0.2, 0.10, 0.05], 3),
     (b.Naive1(), [1, 2, 3], 8, None, 2), (b.SNaive(1), [1, 2, 3], 8, None, 2),
     (b.Average(), [1, 2, 3], 8, None, 2), (b.Drift(), [1, 2, 3], 8, None, 2),
     (b.Drift(), [1, 2, 3], 8, [0.8], 1), (b.Drift(), [1, 2, 3], 8, None, 2),
     (b.Average(), [1, 2, 3, 4, 5], 24, [0.2, 0.10, 0.05], 3)])
def test_naive_pi_set_number(model, data, horizon, alpha, expected):
    '''
    test the correct number of Prediction intervals are
    returned for prediction interval for all Naive forecasting classes
    '''
    model.fit(pd.Series(data))
    # point forecasts only
    _, intervals = model.predict(horizon, return_predict_int=True, alpha=alpha)
    assert len(intervals) == expected