示例#1
0
def test_hyndman_blog():
    # This is the exact code Hyndman ran in his blog post on the matter:
    # https://robjhyndman.com/hyndsight/longseasonality/
    n = 2000
    m = 200
    y = np.random.RandomState(1).normal(size=n) + \
        (np.arange(1, n + 1) % 100 / 30)

    trans = FourierFeaturizer(m=m, k=5).fit(y)
    _, xreg = trans.transform(y)

    arima = pm.auto_arima(
        y,
        X=xreg,
        seasonal=False,
        maxiter=2,  # very short
        start_p=4,
        max_p=5,
        d=0,
        max_q=1,
        start_q=0,
        simple_differencing=True)  # type: pm.ARIMA

    # Show we can forecast 10 in the future
    _, xreg_test = trans.transform(y, n_periods=10)
    arima.predict(n_periods=10, X=xreg_test)
示例#2
0
    def test_r_equivalency(self, exog):
        y = self.y
        expected = self.expected

        trans = FourierFeaturizer(m=5, k=2).fit(y)
        _, xreg = trans.transform(y, exog)

        # maybe subset
        assert_array_almost_equal(expected, xreg[:, -4:])

        # maybe assert on exog
        if exog is not None:
            assert_array_almost_equal(exog, xreg[:, :3])

            # Test a bad forecast (exog dim does not match n_periods dim)
            with pytest.raises(ValueError):
                trans.transform(y, exogenous=np.random.rand(5, 3), n_periods=2)
示例#3
0
    def test_r_equivalency(self, X):
        y = self.y
        expected = self.expected

        trans = FourierFeaturizer(m=5, k=2).fit(y)
        _, xreg = trans.transform(y, X)

        # maybe subset
        if hasattr(xreg, 'iloc'):
            xreg = xreg.values
        assert_array_almost_equal(expected, xreg[:, -4:])

        # maybe assert on X
        if X is not None:
            assert_array_almost_equal(X, xreg[:, :3])

            # Test a bad forecast (X dim does not match n_periods dim)
            with pytest.raises(ValueError):
                trans.transform(y, X=np.random.rand(5, 3), n_periods=2)
示例#4
0
def test_update_transform():
    n = 150
    m = 10
    y = np.random.RandomState(1).normal(size=n) + \
        (np.arange(1, n + 1) % 100 / 30)

    train, test = y[:100], y[100:]

    trans = FourierFeaturizer(m=m, k=5).fit(train)
    _, xreg = trans.transform(train)

    # Now update with the test set and show the xreg is diff
    yt, Xt = trans.update_and_transform(test, X=None)
    assert yt is test
    assert Xt.shape[0] == test.shape[0]
    assert trans.n_ == y.shape[0]

    # Now assert that if we do a vanilla transform with no n_periods, the last
    # 50 are the same as the Xt we just got and the first 100 are the same as
    # we got earlier
    _, xreg2 = trans.transform(y)
    assert_array_almost_equal(xreg2[:100], xreg)
    assert_array_almost_equal(xreg2[100:], Xt)
示例#5
0
def test_value_error_on_fit():
    feat = FourierFeaturizer(m=12, k=8)
    with pytest.raises(ValueError) as ve:
        feat.fit_transform(wineind)
    assert 'k must be' in pytest_error_str(ve)
示例#6
0
def test_value_error_check():
    feat = FourierFeaturizer(m=12)
    with pytest.raises(ValueError) as ve:
        feat._check_y_X(wineind, None, null_allowed=False)
    assert 'non-None' in pytest_error_str(ve)