コード例 #1
0
ファイル: test_arma.py プロジェクト: yutianbo2011/pydse
def test_forecast():
    AR = (np.array([1, .5, .3, 0, .2, .1, 0, .2, .05, 1, .5,
                    .3]), np.array([3, 2, 2]))
    MA = (np.array([1, .2, 0, .1, 0, 0, 1, .3]), np.array([2, 2, 2]))
    arma = ARMA(A=AR, B=MA, C=None)

    series0 = np.array([
        1.58239012, 0.85063747, -0.11981462, -1.69017627, -0.19912156,
        0.02830831, 0.16284912, 2.42364792, -0.15007052, -1.27531927
    ])
    series1 = np.array([
        -0.5172168, -0.4261651, 0.2958942, -0.8559883, 0.7033546, -1.0857290,
        -0.2788928, 0.7393030, -0.2999778, 0.6363970
    ])
    R_result = np.vstack([series0, series1]).T

    # One ahead forecast generated with R
    # ( arma <- l(arma, R_result); R_pred <- arma$estimates$pred )
    pred0 = np.array([
        0.00000000, -0.37127368, -0.54455969, -0.14820550, 0.72904133,
        0.32310958, 0.16860013, 0.01899776, -0.91366463, -0.47630989
    ])
    pred1 = np.array([
        0.00000000, -0.05479565, 0.05066136, 0.03484596, 0.26779526,
        0.15181266, -0.05463401, 0.36191171, -0.43331575, -0.16779191
    ])
    R_pred = np.vstack([pred0, pred1]).T

    pred = arma.forecast(y=R_result)
    nptest.assert_almost_equal(R_pred, pred)

    # One ahead forecast with trend generated with R
    # ( arma_trend <- l(arma_trend, R_result_trend);
    # R_pred_trend <- arma_trend$estimates$pred )
    TREND = np.array([1., 2.])
    arma_trend = ARMA(A=AR, B=MA, C=None, TREND=TREND)
    series0 = np.array([
        2.5823901, 0.9506375, 0.2701854, -1.1311763, 0.1139784, 0.4486183,
        0.6048481, 2.8091458, 0.2663152, -0.8590244
    ])
    series1 = np.array([
        1.48278321, 0.37383493, 1.17589420, 0.37601165, 1.67255459,
        -0.05844899, 0.80133515, 1.76057423, 0.74401876, 1.68619050
    ])
    R_result_trend = np.vstack([series0, series1]).T

    pred0 = np.array([
        1.00000000, -0.27127368, -0.15455969, 0.41079450, 1.04214133,
        0.74341958, 0.61059913, 0.40449566, -0.49727892, -0.06001498
    ])
    pred1 = np.array([
        2.00000000, 0.7452043, 0.9306614, 1.2668460, 1.2369953, 1.1790927,
        1.0255940, 1.3831829, 0.6106808, 0.8820015
    ])
    R_pred_trend = np.vstack([pred0, pred1]).T

    pred_trend = arma_trend.forecast(y=R_result_trend)
    nptest.assert_almost_equal(R_pred_trend, pred_trend)
コード例 #2
0
ファイル: test_arma.py プロジェクト: FlorianWilhelm/pydse
def test_forecast():
    AR = (np.array([1, .5, .3, 0, .2, .1, 0, .2, .05, 1, .5, .3]),
          np.array([3, 2, 2]))
    MA = (np.array([1, .2, 0, .1, 0, 0, 1, .3]),
          np.array([2, 2, 2]))
    arma = ARMA(A=AR, B=MA, C=None)

    series0 = np.array(
        [1.58239012, 0.85063747, -0.11981462, -1.69017627, -0.19912156,
         0.02830831, 0.16284912, 2.42364792, -0.15007052, -1.27531927])
    series1 = np.array(
        [-0.5172168, -0.4261651, 0.2958942, -0.8559883, 0.7033546, -1.0857290,
         -0.2788928, 0.7393030, -0.2999778, 0.6363970])
    R_result = np.vstack([series0, series1]).T

    # One ahead forecast generated with R
    # ( arma <- l(arma, R_result); R_pred <- arma$estimates$pred )
    pred0 = np.array(
        [0.00000000, -0.37127368, -0.54455969, -0.14820550, 0.72904133,
         0.32310958, 0.16860013, 0.01899776, -0.91366463, -0.47630989])
    pred1 = np.array(
        [0.00000000, -0.05479565, 0.05066136, 0.03484596, 0.26779526,
         0.15181266, -0.05463401, 0.36191171, -0.43331575, -0.16779191])
    R_pred = np.vstack([pred0, pred1]).T

    pred = arma.forecast(y=R_result)
    nptest.assert_almost_equal(R_pred, pred)

    # One ahead forecast with trend generated with R
    # ( arma_trend <- l(arma_trend, R_result_trend);
    # R_pred_trend <- arma_trend$estimates$pred )
    TREND = np.array([1., 2.])
    arma_trend = ARMA(A=AR, B=MA, C=None, TREND=TREND)
    series0 = np.array(
        [2.5823901, 0.9506375, 0.2701854, -1.1311763, 0.1139784, 0.4486183,
         0.6048481, 2.8091458, 0.2663152, -0.8590244])
    series1 = np.array(
        [1.48278321, 0.37383493, 1.17589420, 0.37601165, 1.67255459,
         -0.05844899, 0.80133515, 1.76057423, 0.74401876, 1.68619050])
    R_result_trend = np.vstack([series0, series1]).T

    pred0 = np.array(
        [1.00000000, -0.27127368, -0.15455969, 0.41079450, 1.04214133,
         0.74341958, 0.61059913, 0.40449566, -0.49727892, -0.06001498])
    pred1 = np.array(
        [2.00000000, 0.7452043, 0.9306614, 1.2668460, 1.2369953, 1.1790927,
         1.0255940, 1.3831829, 0.6106808, 0.8820015])
    R_pred_trend = np.vstack([pred0, pred1]).T

    pred_trend = arma_trend.forecast(y=R_result_trend)
    nptest.assert_almost_equal(R_pred_trend, pred_trend)
コード例 #3
0
ファイル: test_arma.py プロジェクト: FlorianWilhelm/pydse
def test_forecast_with_horizon():
    rand_state = np.random.RandomState()
    rand_state.seed(0)
    AR = (np.array([1, 0.3, 0.5]), np.array([3, 1, 1]))
    MA = (np.array([1, .1]), np.array([2, 1, 1]))
    TREND = np.arange(1, 21)[:, np.newaxis]
    arma = ARMA(A=AR, B=MA, C=None, TREND=TREND, rand_state=rand_state)
    truth = np.array([2.4560947, 2.6685808, 1.5132628, 0.7132449, 2.9468331,
                      4.3717505, 4.1798191, 6.9642557, 5.8248726, 4.0477605,
                      5.6456776, 7.8781892, 7.4855431, 7.3738101, 9.2561578])
    result = np.array([1., 1.408781, 1.097358, 2.253321, 3.875388, 4.666472,
                       4.185586, 4.559602, 5.061279, 4.846770, 6.793335,
                       8.167651, 7.784758, 7.785321, 9.003934, 9.561470,
                       9.503480, 10.368221, 11.137794, 11.474551])
    pred = arma.forecast(truth, horizon=5)
    nptest.assert_almost_equal(pred, result[:, np.newaxis], decimal=5)
コード例 #4
0
ファイル: test_arma.py プロジェクト: yutianbo2011/pydse
def test_forecast_with_horizon():
    rand_state = np.random.RandomState()
    rand_state.seed(0)
    AR = (np.array([1, 0.3, 0.5]), np.array([3, 1, 1]))
    MA = (np.array([1, .1]), np.array([2, 1, 1]))
    TREND = np.arange(1, 21)[:, np.newaxis]
    arma = ARMA(A=AR, B=MA, C=None, TREND=TREND, rand_state=rand_state)
    truth = np.array([
        2.4560947, 2.6685808, 1.5132628, 0.7132449, 2.9468331, 4.3717505,
        4.1798191, 6.9642557, 5.8248726, 4.0477605, 5.6456776, 7.8781892,
        7.4855431, 7.3738101, 9.2561578
    ])
    result = np.array([
        1., 1.408781, 1.097358, 2.253321, 3.875388, 4.666472, 4.185586,
        4.559602, 5.061279, 4.846770, 6.793335, 8.167651, 7.784758, 7.785321,
        9.003934, 9.561470, 9.503480, 10.368221, 11.137794, 11.474551
    ])
    pred = arma.forecast(truth, horizon=5)
    nptest.assert_almost_equal(pred, result[:, np.newaxis], decimal=5)