Exemple #1
0
    def test_harx(self):
        harx = HARX(self.y, self.x, lags=[1, 5, 22])
        params = np.array([1.0, 0.4, 0.3, 0.2, 1.0, 1.0])
        data = harx.simulate(params, self.T, x=randn(self.T + 500, 1))
        iv = randn(22, 1)
        data = harx.simulate(params,
                             self.T,
                             x=randn(self.T + 500, 1),
                             initial_value=iv)
        assert_equal(data.shape, (self.T, 3))
        cols = ['data', 'volatility', 'errors']
        for c in cols:
            assert_true(c in data)

        bounds = harx.bounds()
        for b in bounds:
            assert_equal(b[0], -np.inf)
            assert_equal(b[1], np.inf)
        assert_equal(len(bounds), 5)

        assert_equal(harx.num_params, 1 + 3 + self.x.shape[1])
        assert_equal(harx.constant, True)
        a, b = harx.constraints()
        assert_equal(a, np.empty((0, 5)))
        assert_equal(b, np.empty(0))
        res = harx.fit()
        assert_raises(RuntimeError, res.forecast, horizon=10)
        assert_raises(ValueError, res.forecast, params=np.array([1.0, 1.0]))
        nobs = self.T - 22
        rhs = np.ones((nobs, 5))
        y = self.y
        lhs = y[22:]
        for i in range(self.T - 22):
            rhs[i, 1] = y[i + 21]
            rhs[i, 2] = np.mean(y[i + 17:i + 22])
            rhs[i, 3] = np.mean(y[i:i + 22])
        rhs[:, 4] = self.x[22:, 0]
        params = np.linalg.pinv(rhs).dot(lhs)
        assert_almost_equal(params, res.params[:-1])

        assert_equal(harx.first_obs, 22)
        assert_equal(harx.last_obs, 1000)
        assert_equal(harx.hold_back, None)
        assert_equal(harx.lags, [1, 5, 22])
        assert_equal(harx.nobs, self.T - 22)
        assert_equal(harx.name, 'HAR-X')
        assert_equal(harx.use_rotated, False)
        harx
        harx._repr_html_()
        res = harx.fit(cov_type='mle')
        res
Exemple #2
0
    def test_harx(self):
        harx = HARX(self.y, self.x, lags=[1, 5, 22])
        assert harx.x is self.x
        params = np.array([1.0, 0.4, 0.3, 0.2, 1.0, 1.0])
        harx.simulate(params, self.T, x=self.rng.randn(self.T + 500, 1))
        iv = self.rng.randn(22, 1)
        data = harx.simulate(params,
                             self.T,
                             x=self.rng.randn(self.T + 500, 1),
                             initial_value=iv)
        assert_equal(data.shape, (self.T, 3))
        cols = ["data", "volatility", "errors"]
        for c in cols:
            assert c in data

        bounds = harx.bounds()
        for b in bounds:
            assert_equal(b[0], -np.inf)
            assert_equal(b[1], np.inf)
        assert_equal(len(bounds), 5)

        assert_equal(harx.num_params, 1 + 3 + self.x.shape[1])
        assert_equal(harx.constant, True)
        a, b = harx.constraints()
        assert_equal(a, np.empty((0, 5)))
        assert_equal(b, np.empty(0))
        res = harx.fit(disp=DISPLAY)
        with pytest.raises(ValueError):
            res.forecast(params=np.array([1.0, 1.0]))
        nobs = self.T - 22
        rhs = np.ones((nobs, 5))
        y = self.y
        lhs = y[22:]
        for i in range(self.T - 22):
            rhs[i, 1] = y[i + 21]
            rhs[i, 2] = np.mean(y[i + 17:i + 22])
            rhs[i, 3] = np.mean(y[i:i + 22])
        rhs[:, 4] = self.x[22:, 0]
        params = np.linalg.pinv(rhs).dot(lhs)
        assert_almost_equal(params, res.params[:-1])

        assert harx.hold_back is None
        assert_equal(harx.lags, [1, 5, 22])
        assert_equal(harx.name, "HAR-X")
        assert_equal(harx.use_rotated, False)
        assert isinstance(harx.__repr__(), str)
        harx._repr_html_()
        res = harx.fit(cov_type="classic", disp=DISPLAY)
        assert isinstance(res.__repr__(), str)
Exemple #3
0
    def test_harx(self):
        harx = HARX(self.y, self.x, lags=[1, 5, 22])
        params = np.array([1.0, 0.4, 0.3, 0.2, 1.0, 1.0])
        data = harx.simulate(params, self.T, x=randn(self.T + 500, 1))
        iv = randn(22, 1)
        data = harx.simulate(params, self.T, x=randn(self.T + 500, 1),
                             initial_value=iv)
        assert_equal(data.shape, (self.T, 3))
        cols = ['data', 'volatility', 'errors']
        for c in cols:
            assert_true(c in data)

        bounds = harx.bounds()
        for b in bounds:
            assert_equal(b[0], -np.inf)
            assert_equal(b[1], np.inf)
        assert_equal(len(bounds), 5)

        assert_equal(harx.num_params, 1 + 3 + self.x.shape[1])
        assert_equal(harx.constant, True)
        a, b = harx.constraints()
        assert_equal(a, np.empty((0, 5)))
        assert_equal(b, np.empty(0))
        res = harx.fit()
        assert_raises(RuntimeError, res.forecast, horizon=10)
        assert_raises(ValueError, res.forecast, params=np.array([1.0, 1.0]))
        nobs = self.T - 22
        rhs = np.ones((nobs, 5))
        y = self.y
        lhs = y[22:]
        for i in range(self.T - 22):
            rhs[i, 1] = y[i + 21]
            rhs[i, 2] = np.mean(y[i + 17:i + 22])
            rhs[i, 3] = np.mean(y[i:i + 22])
        rhs[:, 4] = self.x[22:, 0]
        params = np.linalg.pinv(rhs).dot(lhs)
        assert_almost_equal(params, res.params[:-1])

        assert_equal(harx.first_obs, 22)
        assert_equal(harx.last_obs, 1000)
        assert_equal(harx.hold_back, None)
        assert_equal(harx.lags, [1, 5, 22])
        assert_equal(harx.nobs, self.T - 22)
        assert_equal(harx.name, 'HAR-X')
        assert_equal(harx.use_rotated, False)
        harx
        harx._repr_html_()
        res = harx.fit(cov_type='mle')
        res
Exemple #4
0
    def test_har(self):
        har = HARX(self.y, lags=[1, 5, 22])
        params = np.array([1.0, 0.4, 0.3, 0.2, 1.0])
        data = har.simulate(params, self.T)
        assert_equal(data.shape, (self.T, 3))
        cols = ['data', 'volatility', 'errors']
        for c in cols:
            assert c in data

        bounds = har.bounds()
        for b in bounds:
            assert_equal(b[0], -np.inf)
            assert_equal(b[1], np.inf)
        assert_equal(len(bounds), 4)

        assert_equal(har.num_params, 4)
        assert_equal(har.constant, True)
        a, b = har.constraints()
        assert_equal(a, np.empty((0, 4)))
        assert_equal(b, np.empty(0))
        res = har.fit(disp=DISPLAY)
        nobs = self.T - 22
        rhs = np.ones((nobs, 4))
        y = self.y
        lhs = y[22:]
        for i in range(self.T - 22):
            rhs[i, 1] = y[i + 21]
            rhs[i, 2] = np.mean(y[i + 17:i + 22])
            rhs[i, 3] = np.mean(y[i:i + 22])
        params = np.linalg.pinv(rhs).dot(lhs)
        assert_almost_equal(params, res.params[:-1])

        with pytest.raises(ValueError):
            res.forecast(horizon=6, start=0)
        forecasts = res.forecast(horizon=6)
        t = self.y.shape[0]
        direct = pd.DataFrame(index=np.arange(t),
                              columns=['h.' + str(i + 1) for i in range(6)],
                              dtype=np.float64)

        params = np.asarray(res.params)
        fcast = np.zeros(t + 6)
        for i in range(21, t):
            fcast[:i + 1] = self.y[:i + 1]
            fcast[i + 1:] = 0.0
            for h in range(6):
                fcast[i + h + 1] = params[0]
                fcast[i + h + 1] += params[1] * fcast[i + h:i + h + 1]
                fcast[i + h + 1] += params[2] * fcast[
                                                i + h - 4:i + h + 1].mean()
                fcast[i + h + 1] += params[3] * fcast[
                                                i + h - 21:i + h + 1].mean()
            direct.iloc[i, :] = fcast[i + 1:i + 7]
        assert isinstance(forecasts, ARCHModelForecast)
        # TODO
        # assert_frame_equal(direct, forecasts)
        forecasts = res.forecast(res.params, horizon=6)
        assert isinstance(forecasts, ARCHModelForecast)
        # TODO
        # assert_frame_equal(direct, forecasts)

        assert_equal(har.hold_back, None)
        assert_equal(har.lags, [1, 5, 22])
        assert_equal(har.name, 'HAR')
        assert_equal(har.use_rotated, False)

        har = HARX(self.y_series, lags=[1, 5, 22])
        res = har.fit(disp=DISPLAY)
        direct = pd.DataFrame(index=self.y_series.index,
                              columns=['h.' + str(i + 1) for i in range(6)],
                              dtype=np.float64)
        forecasts = res.forecast(horizon=6)
        params = np.asarray(res.params)
        fcast = np.zeros(t + 6)
        for i in range(21, t):
            fcast[:i + 1] = self.y[:i + 1]
            fcast[i + 1:] = 0.0
            for h in range(6):
                fcast[i + h + 1] = params[0]
                fcast[i + h + 1] += params[1] * fcast[i + h:i + h + 1]
                fcast[i + h + 1] += params[2] * fcast[
                                                i + h - 4:i + h + 1].mean()
                fcast[i + h + 1] += params[3] * fcast[
                                                i + h - 21:i + h + 1].mean()
            direct.iloc[i, :] = fcast[i + 1:i + 7]
        assert isinstance(forecasts, ARCHModelForecast)
Exemple #5
0
    def test_har(self):
        har = HARX(self.y, lags=[1, 5, 22])
        params = np.array([1.0, 0.4, 0.3, 0.2, 1.0])
        data = har.simulate(params, self.T)
        assert_equal(data.shape, (self.T, 3))
        cols = ['data', 'volatility', 'errors']
        for c in cols:
            assert c in data

        bounds = har.bounds()
        for b in bounds:
            assert_equal(b[0], -np.inf)
            assert_equal(b[1], np.inf)
        assert_equal(len(bounds), 4)

        assert_equal(har.num_params, 4)
        assert_equal(har.constant, True)
        a, b = har.constraints()
        assert_equal(a, np.empty((0, 4)))
        assert_equal(b, np.empty(0))
        res = har.fit(disp=DISPLAY)
        nobs = self.T - 22
        rhs = np.ones((nobs, 4))
        y = self.y
        lhs = y[22:]
        for i in range(self.T - 22):
            rhs[i, 1] = y[i + 21]
            rhs[i, 2] = np.mean(y[i + 17:i + 22])
            rhs[i, 3] = np.mean(y[i:i + 22])
        params = np.linalg.pinv(rhs).dot(lhs)
        assert_almost_equal(params, res.params[:-1])

        with pytest.raises(ValueError):
            res.forecast(horizon=6, start=0)
        forecasts = res.forecast(horizon=6)
        t = self.y.shape[0]
        direct = pd.DataFrame(index=np.arange(t),
                              columns=['h.' + str(i + 1) for i in range(6)],
                              dtype=np.float64)

        params = np.asarray(res.params)
        fcast = np.zeros(t + 6)
        for i in range(21, t):
            fcast[:i + 1] = self.y[:i + 1]
            fcast[i + 1:] = 0.0
            for h in range(6):
                fcast[i + h + 1] = params[0]
                fcast[i + h + 1] += params[1] * fcast[i + h:i + h + 1]
                fcast[i + h + 1] += params[2] * fcast[
                                                i + h - 4:i + h + 1].mean()
                fcast[i + h + 1] += params[3] * fcast[
                                                i + h - 21:i + h + 1].mean()
            direct.iloc[i, :] = fcast[i + 1:i + 7]
        assert isinstance(forecasts, ARCHModelForecast)
        # TODO
        # assert_frame_equal(direct, forecasts)
        forecasts = res.forecast(res.params, horizon=6)
        assert isinstance(forecasts, ARCHModelForecast)
        # TODO
        # assert_frame_equal(direct, forecasts)

        assert_equal(har.hold_back, None)
        assert_equal(har.lags, [1, 5, 22])
        assert_equal(har.name, 'HAR')
        assert_equal(har.use_rotated, False)

        har = HARX(self.y_series, lags=[1, 5, 22])
        res = har.fit(disp=DISPLAY)
        direct = pd.DataFrame(index=self.y_series.index,
                              columns=['h.' + str(i + 1) for i in range(6)],
                              dtype=np.float64)
        forecasts = res.forecast(horizon=6)
        params = np.asarray(res.params)
        fcast = np.zeros(t + 6)
        for i in range(21, t):
            fcast[:i + 1] = self.y[:i + 1]
            fcast[i + 1:] = 0.0
            for h in range(6):
                fcast[i + h + 1] = params[0]
                fcast[i + h + 1] += params[1] * fcast[i + h:i + h + 1]
                fcast[i + h + 1] += params[2] * fcast[
                                                i + h - 4:i + h + 1].mean()
                fcast[i + h + 1] += params[3] * fcast[
                                                i + h - 21:i + h + 1].mean()
            direct.iloc[i, :] = fcast[i + 1:i + 7]
        assert isinstance(forecasts, ARCHModelForecast)