Beispiel #1
0
 def test_cov_nw_axis(self):
     y = np.random.randn(100, 2)
     e = y - y.mean(0)
     gamma_0 = e.T.dot(e)
     gamma_1 = e[1:].T.dot(e[:-1])
     gamma_2 = e[2:].T.dot(e[:-2])
     w1, w2 = 1.0 - (1.0 / 3.0), 1.0 - (2.0 / 3.0)
     expected = (gamma_0 + w1 * (gamma_1 + gamma_1.T) +
                 w2 * (gamma_2 + gamma_2.T)) / 100.0
     assert_almost_equal(cov_nw(y.T, lags=2, axis=1), expected)
Beispiel #2
0
 def test_cov_nw_axis(self):
     y = self.rng.randn(100, 2)
     e = y - y.mean(0)
     gamma_0 = e.T.dot(e)
     gamma_1 = e[1:].T.dot(e[:-1])
     gamma_2 = e[2:].T.dot(e[:-2])
     w1, w2 = 1.0 - (1.0 / 3.0), 1.0 - (2.0 / 3.0)
     expected = (gamma_0 + w1 * (gamma_1 + gamma_1.T) + w2 *
                 (gamma_2 + gamma_2.T)) / 100.0
     assert_almost_equal(cov_nw(y.T, lags=2, axis=1), expected)
Beispiel #3
0
    def _compute_statistic(self):
        """Core routine to estimate PP test statistics"""
        # 1. Estimate Regression
        y, trend = self._y, self._trend
        nobs = y.shape[0]

        if self._lags is None:
            self._lags = int(ceil(12. * power(nobs / 100., 1 / 4.)))
        lags = self._lags

        rhs = y[:-1, None]
        rhs = _add_column_names(rhs, 0)
        lhs = y[1:, None]
        if trend != 'nc':
            rhs = add_trend(rhs, trend)

        resols = OLS(lhs, rhs).fit()
        k = rhs.shape[1]
        n, u = resols.nobs, resols.resid
        lam2 = cov_nw(u, lags, demean=False)
        lam = sqrt(lam2)
        # 2. Compute components
        s2 = u.dot(u) / (n - k)
        s = sqrt(s2)
        gamma0 = s2 * (n - k) / n
        sigma = resols.bse[0]
        sigma2 = sigma**2.0
        rho = resols.params[0]
        # 3. Compute statistics
        self._stat_tau = sqrt(gamma0 / lam2) * ((rho - 1) / sigma) \
            - 0.5 * ((lam2 - gamma0) / lam) * (n * sigma / s)
        self._stat_rho = n * (rho - 1) \
            - 0.5 * (n ** 2.0 * sigma2 / s2) * (lam2 - gamma0)

        self._nobs = int(resols.nobs)
        if self._test_type == 'rho':
            self._stat = self._stat_rho
            dist_type = 'ADF-z'
        else:
            self._stat = self._stat_tau
            dist_type = 'ADF-t'

        self._pvalue = mackinnonp(self._stat,
                                  regression=trend,
                                  dist_type=dist_type)
        critical_values = mackinnoncrit(regression=trend,
                                        nobs=n,
                                        dist_type=dist_type)
        self._critical_values = {
            "1%": critical_values[0],
            "5%": critical_values[1],
            "10%": critical_values[2]
        }

        self._title = self._test_name + ' (Z-' + self._test_type + ')'
Beispiel #4
0
    def _compute_statistic(self):
        """Core routine to estimate PP test statistics"""
        # 1. Estimate Regression
        y, trend = self._y, self._trend
        nobs = y.shape[0]

        if self._lags is None:
            self._lags = int(ceil(12. * power(nobs / 100., 1 / 4.)))
        lags = self._lags

        rhs = y[:-1, None]
        lhs = y[1:, None]
        if trend != 'nc':
            rhs = add_trend(rhs, trend)

        resols = OLS(lhs, rhs).fit()
        k = rhs.shape[1]
        n, u = resols.nobs, resols.resid
        lam2 = cov_nw(u, lags, demean=False)
        lam = sqrt(lam2)
        # 2. Compute components
        s2 = u.dot(u) / (n - k)
        s = sqrt(s2)
        gamma0 = s2 * (n - k) / n
        sigma = resols.bse[0]
        sigma2 = sigma ** 2.0
        rho = resols.params[0]
        # 3. Compute statistics
        self._stat_tau = sqrt(gamma0 / lam2) * ((rho - 1) / sigma) \
            - 0.5 * ((lam2 - gamma0) / lam) * (n * sigma / s)
        self._stat_rho = n * (rho - 1) \
            - 0.5 * (n ** 2.0 * sigma2 / s2) * (lam2 - gamma0)

        self._nobs = int(resols.nobs)
        if self._test_type == 'rho':
            self._stat = self._stat_rho
            dist_type = 'ADF-z'
        else:
            self._stat = self._stat_tau
            dist_type = 'ADF-t'

        self._pvalue = mackinnonp(self._stat,
                                  regression=trend,
                                  dist_type=dist_type)
        critical_values = mackinnoncrit(regression=trend,
                                        nobs=n,
                                        dist_type=dist_type)
        self._critical_values = {"1%": critical_values[0],
                                 "5%": critical_values[1],
                                 "10%": critical_values[2]}

        self._title = self._test_name + ' (Z-' + self._test_type + ')'
Beispiel #5
0
 def test_errors(self):
     y = self.rng.randn(100, 2)
     with pytest.raises(ValueError):
         cov_nw(y, 200)
     with pytest.raises(ValueError):
         cov_nw(y, axis=3)
     with pytest.raises(ValueError):
         cov_nw(y, ddof=200)
Beispiel #6
0
 def test_errors(self):
     y = np.random.randn(100, 2)
     with pytest.raises(ValueError):
         cov_nw(y, 200)
     with pytest.raises(ValueError):
         cov_nw(y, axis=3)
     with pytest.raises(ValueError):
         cov_nw(y, ddof=200)
Beispiel #7
0
 def _compute_statistic(self):
     # 1. Estimate model with trend
     nobs, y, trend = self._nobs, self._y, self._trend
     z = add_trend(nobs=nobs, trend=trend)
     res = OLS(y, z).fit()
     # 2. Compute KPSS test
     u = res.resid
     if self._lags is None:
         self._lags = int(ceil(12. * power(nobs / 100., 1 / 4.)))
     lam = cov_nw(u, self._lags, demean=False)
     s = cumsum(u)
     self._stat = 1 / (nobs ** 2.0) * sum(s ** 2.0) / lam
     self._nobs = u.shape[0]
     self._pvalue, critical_values = kpss_crit(self._stat, trend)
     self._critical_values = {"1%": critical_values[0],
                              "5%": critical_values[1],
                              "10%": critical_values[2]}
Beispiel #8
0
 def test_cov_nw_2d(self):
     y = self.rng.randn(100, 2)
     simple_cov = cov_nw(y, lags=0)
     e = y - y.mean(0)
     assert_almost_equal(e.T.dot(e) / e.shape[0], simple_cov)
Beispiel #9
0
 def test_cov_nw_no_demean(self):
     y = self.inflation
     simple_cov = cov_nw(y, lags=0, demean=False)
     assert_almost_equal(y.dot(y) / y.shape[0], simple_cov)
Beispiel #10
0
 def test_cov_nw_ddof(self):
     y = self.inflation
     simple_cov = cov_nw(y, lags=0, ddof=1)
     e = y - y.mean()
     n = e.shape[0]
     assert_almost_equal(e.dot(e) / (n - 1), simple_cov)
Beispiel #11
0
 def test_cov_nw(self):
     y = self.inflation
     simple_cov = cov_nw(y, lags=0)
     e = y - y.mean()
     assert_almost_equal(e.dot(e) / e.shape[0], simple_cov)
Beispiel #12
0
 def test_cov_nw_2d(self):
     y = np.random.randn(100, 2)
     simple_cov = cov_nw(y, lags=0)
     e = y - y.mean(0)
     assert_almost_equal(e.T.dot(e) / e.shape[0], simple_cov)
Beispiel #13
0
 def test_cov_nw_no_demean(self):
     y = self.inflation
     simple_cov = cov_nw(y, lags=0, demean=False)
     assert_almost_equal(y.dot(y) / y.shape[0], simple_cov)
Beispiel #14
0
 def test_cov_nw_ddof(self):
     y = self.inflation
     simple_cov = cov_nw(y, lags=0, ddof=1)
     e = y - y.mean()
     n = e.shape[0]
     assert_almost_equal(e.dot(e) / (n - 1), simple_cov)
Beispiel #15
0
 def test_cov_nw(self):
     y = self.inflation
     simple_cov = cov_nw(y, lags=0)
     e = y - y.mean()
     assert_almost_equal(e.dot(e) / e.shape[0], simple_cov)