diff --git a/.coveragerc b/.coveragerc index a77c529eec..6e6fceea28 100644 --- a/.coveragerc +++ b/.coveragerc @@ -21,6 +21,8 @@ exclude_lines = pass # Ignore failure messages pytest.xfail + # Ignore ImportError protection + except ImportError include = */arch/* omit = */_version.py diff --git a/arch/tests/bootstrap/test_bootstrap.py b/arch/tests/bootstrap/test_bootstrap.py index 8a17aff8aa..8f15921519 100644 --- a/arch/tests/bootstrap/test_bootstrap.py +++ b/arch/tests/bootstrap/test_bootstrap.py @@ -25,6 +25,11 @@ class TestBootstrap(TestCase): + + @staticmethod + def func(y, axis=0): + return y.mean(axis=axis) + @classmethod def setup_class(cls): warnings.simplefilter("always", RuntimeWarning) @@ -38,11 +43,6 @@ def setup_class(cls): cls.z_df = pd.DataFrame(cls.z) cls.x_df = pd.DataFrame(cls.x) - def func(y): - return y.mean(axis=0) - - cls.func = func - def test_numpy(self): x, y, z = self.x, self.y, self.z bs = IIDBootstrap(y) @@ -179,23 +179,17 @@ def test_errors(self): IIDBootstrap(index=x) bs = IIDBootstrap(y) - def func(y): - return y.mean(axis=0) - with pytest.raises(ValueError): - bs.conf_int(func, method='unknown') + bs.conf_int(self.func, method='unknown') with pytest.raises(ValueError): - bs.conf_int(func, tail='dragon') + bs.conf_int(self.func, tail='dragon') with pytest.raises(ValueError): - bs.conf_int(func, size=95) + bs.conf_int(self.func, size=95) def test_cov(self): - def func(y): - return y.mean(axis=0) - bs = IIDBootstrap(self.x) num_bootstrap = 10 - cov = bs.cov(func=func, reps=num_bootstrap, recenter=False) + cov = bs.cov(func=self.func, reps=num_bootstrap, recenter=False) bs.reset() results = np.zeros((num_bootstrap, 2)) @@ -208,15 +202,15 @@ def func(y): assert_allclose(cov, direct_cov) bs.reset() - cov = bs.cov(func=func, recenter=True, reps=num_bootstrap) + cov = bs.cov(func=self.func, recenter=True, reps=num_bootstrap) errors = results - results.mean(axis=0) direct_cov = errors.T.dot(errors) / num_bootstrap assert_allclose(cov, direct_cov) bs = IIDBootstrap(self.x_df) - cov = bs.cov(func=func, reps=num_bootstrap, recenter=False) + cov = bs.cov(func=self.func, reps=num_bootstrap, recenter=False) bs.reset() - var = bs.var(func=func, reps=num_bootstrap, recenter=False) + var = bs.var(func=self.func, reps=num_bootstrap, recenter=False) bs.reset() results = np.zeros((num_bootstrap, 2)) count = 0 @@ -229,7 +223,7 @@ def func(y): assert_allclose(var, np.diag(direct_cov)) bs.reset() - cov = bs.cov(func=func, recenter=True, reps=num_bootstrap) + cov = bs.cov(func=self.func, recenter=True, reps=num_bootstrap) errors = results - results.mean(axis=0) direct_cov = errors.T.dot(errors) / num_bootstrap assert_allclose(cov, direct_cov) @@ -238,23 +232,20 @@ def test_conf_int_basic(self): num_bootstrap = 200 bs = IIDBootstrap(self.x) - def func(y): - return y.mean(axis=0) - - ci = bs.conf_int(func, reps=num_bootstrap, size=0.90, method='basic') + ci = bs.conf_int(self.func, reps=num_bootstrap, size=0.90, method='basic') bs.reset() - ci_u = bs.conf_int(func, tail='upper', reps=num_bootstrap, size=0.95, + ci_u = bs.conf_int(self.func, tail='upper', reps=num_bootstrap, size=0.95, method='basic') bs.reset() - ci_l = bs.conf_int(func, tail='lower', reps=num_bootstrap, size=0.95, + ci_l = bs.conf_int(self.func, tail='lower', reps=num_bootstrap, size=0.95, method='basic') bs.reset() results = np.zeros((num_bootstrap, 2)) count = 0 for pos, _ in bs.bootstrap(num_bootstrap): - results[count] = func(*pos) + results[count] = self.func(*pos) count += 1 - mu = func(self.x) + mu = self.func(self.x) upper = mu + (mu - np.percentile(results, 5, axis=0)) lower = mu + (mu - np.percentile(results, 95, axis=0)) @@ -272,22 +263,19 @@ def test_conf_int_percentile(self): num_bootstrap = 200 bs = IIDBootstrap(self.x) - def func(y): - return y.mean(axis=0) - - ci = bs.conf_int(func, reps=num_bootstrap, size=0.90, + ci = bs.conf_int(self.func, reps=num_bootstrap, size=0.90, method='percentile') bs.reset() - ci_u = bs.conf_int(func, tail='upper', reps=num_bootstrap, size=0.95, + ci_u = bs.conf_int(self.func, tail='upper', reps=num_bootstrap, size=0.95, method='percentile') bs.reset() - ci_l = bs.conf_int(func, tail='lower', reps=num_bootstrap, size=0.95, + ci_l = bs.conf_int(self.func, tail='lower', reps=num_bootstrap, size=0.95, method='percentile') bs.reset() results = np.zeros((num_bootstrap, 2)) count = 0 for pos, _ in bs.bootstrap(num_bootstrap): - results[count] = func(*pos) + results[count] = self.func(*pos) count += 1 upper = np.percentile(results, 95, axis=0) @@ -307,20 +295,17 @@ def test_conf_int_norm(self): num_bootstrap = 200 bs = IIDBootstrap(self.x) - def func(y): - return y.mean(axis=0) - - ci = bs.conf_int(func, reps=num_bootstrap, size=0.90, + ci = bs.conf_int(self.func, reps=num_bootstrap, size=0.90, method='norm') bs.reset() - ci_u = bs.conf_int(func, tail='upper', reps=num_bootstrap, size=0.95, + ci_u = bs.conf_int(self.func, tail='upper', reps=num_bootstrap, size=0.95, method='var') bs.reset() - ci_l = bs.conf_int(func, tail='lower', reps=num_bootstrap, size=0.95, + ci_l = bs.conf_int(self.func, tail='lower', reps=num_bootstrap, size=0.95, method='cov') bs.reset() - cov = bs.cov(func, reps=num_bootstrap) - mu = func(self.x) + cov = bs.cov(self.func, reps=num_bootstrap) + mu = self.func(self.x) std_err = np.sqrt(np.diag(cov)) upper = mu + stats.norm.ppf(0.95) * std_err lower = mu + stats.norm.ppf(0.05) * std_err @@ -338,20 +323,16 @@ def test_reuse(self): num_bootstrap = 100 bs = IIDBootstrap(self.x) - def func(y): - return y.mean(axis=0) - - ci = bs.conf_int(func, reps=num_bootstrap) + ci = bs.conf_int(self.func, reps=num_bootstrap) old_results = bs._results.copy() - ci_reuse = bs.conf_int(func, reps=num_bootstrap, reuse=True) + ci_reuse = bs.conf_int(self.func, reps=num_bootstrap, reuse=True) results = bs._results assert_equal(results, old_results) assert_equal(ci, ci_reuse) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always", RuntimeWarning) warnings.simplefilter("always") - bs.conf_int(func, tail='lower', reps=num_bootstrap // 2, - reuse=True) + bs.conf_int(self.func, tail='lower', reps=num_bootstrap // 2, reuse=True) assert_equal(len(w), 1) def test_studentized(self): @@ -359,23 +340,20 @@ def test_studentized(self): bs = IIDBootstrap(self.x) bs.seed(23456) - def func(y): - return y.mean(axis=0) - def std_err_func(mu, y): errors = y - mu var = (errors ** 2.0).mean(axis=0) return np.sqrt(var / y.shape[0]) - ci = bs.conf_int(func, reps=num_bootstrap, method='studentized', + ci = bs.conf_int(self.func, reps=num_bootstrap, method='studentized', std_err_func=std_err_func) bs.reset() - base = func(self.x) + base = self.func(self.x) results = np.zeros((num_bootstrap, 2)) stud_results = np.zeros((num_bootstrap, 2)) count = 0 for pos, _ in bs.bootstrap(reps=num_bootstrap): - results[count] = func(*pos) + results[count] = self.func(*pos) std_err = std_err_func(results[count], *pos) stud_results[count] = (results[count] - base) / std_err count += 1 @@ -393,20 +371,20 @@ def std_err_func(mu, y): assert_allclose(ci, ci_direct) bs.reset() - ci = bs.conf_int(func, reps=num_bootstrap, method='studentized', + ci = bs.conf_int(self.func, reps=num_bootstrap, method='studentized', studentize_reps=50) bs.reset() - base = func(self.x) + base = self.func(self.x) results = np.zeros((num_bootstrap, 2)) stud_results = np.zeros((num_bootstrap, 2)) count = 0 for pos, _ in bs.bootstrap(reps=num_bootstrap): - results[count] = func(*pos) + results[count] = self.func(*pos) inner_bs = IIDBootstrap(*pos) seed = bs.random_state.randint(2 ** 31 - 1) inner_bs.seed(seed) - cov = inner_bs.cov(func, reps=50) + cov = inner_bs.cov(self.func, reps=50) std_err = np.sqrt(np.diag(cov)) stud_results[count] = (results[count] - base) / std_err count += 1 @@ -426,7 +404,7 @@ def std_err_func(mu, y): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") - bs.conf_int(func, reps=num_bootstrap, method='studentized', + bs.conf_int(self.func, reps=num_bootstrap, method='studentized', std_err_func=std_err_func, reuse=True) assert_equal(len(w), 1) @@ -435,12 +413,9 @@ def test_conf_int_bias_corrected(self): bs = IIDBootstrap(self.x) bs.seed(23456) - def func(y): - return y.mean(axis=0) - - ci = bs.conf_int(func, reps=num_bootstrap, method='bc') + ci = bs.conf_int(self.func, reps=num_bootstrap, method='bc') bs.reset() - ci_db = bs.conf_int(func, reps=num_bootstrap, method='debiased') + ci_db = bs.conf_int(self.func, reps=num_bootstrap, method='debiased') assert_equal(ci, ci_db) base, results = bs._base, bs._results p = np.zeros(2) @@ -522,22 +497,16 @@ def test_extra_kwargs(self): bs.seed(23456) num_bootstrap = 100 - def func(y, axis=0): - return y.mean(axis=axis) - - bs.cov(func, reps=num_bootstrap, extra_kwargs=extra_kwargs) + bs.cov(self.func, reps=num_bootstrap, extra_kwargs=extra_kwargs) bs = IIDBootstrap(axis=self.x) bs.seed(23456) with pytest.raises(ValueError): - bs.cov(func, reps=num_bootstrap, extra_kwargs=extra_kwargs) + bs.cov(self.func, reps=num_bootstrap, extra_kwargs=extra_kwargs) def test_jackknife(self): - def func(x): - return x.mean(axis=0) - x = self.x - results = _loo_jackknife(func, len(x), (x,), {}) + results = _loo_jackknife(self.func, len(x), (x,), {}) direct_results = np.zeros_like(x) for i in range(len(x)): @@ -549,15 +518,15 @@ def func(x): temp = list(x[:i]) temp.extend(list(x[i + 1:])) y = np.array(temp) - direct_results[i] = func(y) + direct_results[i] = self.func(y) assert_allclose(direct_results, results) x = self.x_df - results_df = _loo_jackknife(func, len(x), (x,), {}) + results_df = _loo_jackknife(self.func, len(x), (x,), {}) assert_equal(results, results_df) y = self.y - results = _loo_jackknife(func, len(y), (y,), {}) + results = _loo_jackknife(self.func, len(y), (y,), {}) direct_results = np.zeros_like(y) for i in range(len(y)): @@ -569,11 +538,11 @@ def func(x): temp = list(y[:i]) temp.extend(list(y[i + 1:])) z = np.array(temp) - direct_results[i] = func(z) + direct_results[i] = self.func(z) assert_allclose(direct_results, results) y = self.y_series - results_series = _loo_jackknife(func, len(y), (y,), {}) + results_series = _loo_jackknife(self.func, len(y), (y,), {}) assert_allclose(results, results_series) def test_bca(self): @@ -581,10 +550,7 @@ def test_bca(self): bs = IIDBootstrap(self.x) bs.seed(23456) - def func(y): - return y.mean(axis=0) - - ci_direct = bs.conf_int(func, reps=num_bootstrap, method='bca') + ci_direct = bs.conf_int(self.func, reps=num_bootstrap, method='bca') bs.reset() base, results = bs._base, bs._results p = np.zeros(2) @@ -594,9 +560,9 @@ def func(y): b = b[:, None] q = stats.norm.ppf(np.array([0.025, 0.975])) - base = func(self.x) + base = self.func(self.x) nobs = self.x.shape[0] - jk = _loo_jackknife(func, nobs, [self.x], {}) + jk = _loo_jackknife(self.func, nobs, [self.x], {}) u = (nobs - 1) * (jk - base) u2 = np.sum(u * u, 0) u3 = np.sum(u * u * u, 0) @@ -623,14 +589,11 @@ def test_apply(self): bs = IIDBootstrap(self.x) bs.seed(23456) - def func(y): - return y.mean(0) - - results = bs.apply(func, 1000) + results = bs.apply(self.func, 1000) bs.reset(23456) direct_results = [] for pos, _ in bs.bootstrap(1000): - direct_results.append(func(*pos)) + direct_results.append(self.func(*pos)) direct_results = np.array(direct_results) assert_equal(results, direct_results) @@ -638,14 +601,11 @@ def test_apply_series(self): bs = IIDBootstrap(self.y_series) bs.seed(23456) - def func(y): - return y.mean(0) - - results = bs.apply(func, 1000) + results = bs.apply(self.func, 1000) bs.reset(23456) direct_results = [] for pos, _ in bs.bootstrap(1000): - direct_results.append(func(*pos)) + direct_results.append(self.func(*pos)) direct_results = np.array(direct_results) direct_results = direct_results[:, None] assert_equal(results, direct_results) diff --git a/arch/tests/unitroot/test_unitroot.py b/arch/tests/unitroot/test_unitroot.py index 82926e1eae..c533e40647 100644 --- a/arch/tests/unitroot/test_unitroot.py +++ b/arch/tests/unitroot/test_unitroot.py @@ -18,7 +18,7 @@ from arch.compat.python import iteritems from arch.unitroot import ADF, DFGLS, PhillipsPerron, KPSS, VarianceRatio from arch.unitroot.critical_values.dickey_fuller import tau_2010 -from arch.unitroot.unitroot import _autolag_ols +from arch.unitroot.unitroot import _autolag_ols, mackinnonp, mackinnoncrit DECIMAL_5 = 5 DECIMAL_4 = 4 @@ -154,7 +154,10 @@ def test_dfgls(self): def test_dfgls_auto(self): dfgls = DFGLS(self.inflation, trend='ct', method='BIC', max_lags=3) assert_equal(dfgls.lags, 2) + assert_equal(dfgls.max_lags, 3) assert_almost_equal(dfgls.stat, -2.9035369, DECIMAL_4) + dfgls.max_lags = 1 + assert_equal(dfgls.lags, 1) def test_dfgls_bad_trend(self): dfgls = DFGLS(self.inflation, trend='ct', method='BIC', max_lags=3) @@ -163,6 +166,12 @@ def test_dfgls_bad_trend(self): assert dfgls != 0.0 + def test_dfgls_auto_low_memory(self): + y = np.cumsum(self.rng.standard_normal(200000)) + dfgls = DFGLS(y, trend='c', method='BIC', low_memory=None) + assert isinstance(dfgls.stat, float) + assert dfgls._low_memory + def test_negative_lag(self): adf = ADF(self.inflation) with pytest.raises(ValueError): @@ -361,6 +370,9 @@ def test_trends_low_memory(trend): adf2 = ADF(y, trend=trend, low_memory=True, max_lags=16) assert adf.lags == adf2.lags assert adf.max_lags == 16 + adf.max_lags = 1 + assert_equal(adf.lags, 1) + assert_equal(adf.max_lags, 1) @pytest.mark.parametrize('trend', ['nc', 'c', 'ct', 'ctt']) @@ -384,7 +396,7 @@ def test_unknown_method(): ADF(y, method='unknown').stat -def test_auto_lowmemoty(): +def test_auto_low_memory(): rnd = np.random.RandomState(12345) y = np.cumsum(rnd.randn(250)) adf = ADF(y, trend='ct') @@ -392,3 +404,31 @@ def test_auto_lowmemoty(): y = np.cumsum(rnd.randn(1000000)) adf = ADF(y, trend='ct') assert adf._low_memory is True + + +def test_mackinnonp_errors(): + with pytest.raises(ValueError): + mackinnonp(-1.0, regression="c", num_unit_roots=2, dist_type='ADF-z') + with pytest.raises(ValueError): + mackinnonp(-1.0, dist_type='unknown') + + +def test_mackinnonp_small(): + val_large = mackinnonp(-7.0, regression="c", num_unit_roots=1, dist_type='adf-z') + val = mackinnonp(-10.0, regression="c", num_unit_roots=1, dist_type='adf-z') + assert val < val_large + + +def test_mackinnonp_large(): + val = mackinnonp(100.0, regression="c", num_unit_roots=1) + assert val == 1.0 + + +def test_mackinnoncrit_errors(): + with pytest.raises(ValueError): + mackinnoncrit(regression='ttc') + with pytest.raises(ValueError): + mackinnoncrit(dist_type='unknown') + cv_50 = mackinnoncrit(nobs=50) + cv_inf = mackinnoncrit() + assert np.all(cv_50 <= cv_inf) diff --git a/arch/tests/utility/test_array.py b/arch/tests/utility/test_array.py index 18328ebf3e..bea5872cda 100644 --- a/arch/tests/utility/test_array.py +++ b/arch/tests/utility/test_array.py @@ -169,15 +169,10 @@ def test_date_to_index_timestamp(self): y = Series(np.arange(3000.0), index=dr) date_index = y.index date = y.index[1000] - - try: - date_pydt = date.to_pydatetime() - except AttributeError: - # Old pandas - date_pydt = date.to_datetime() - + date_pydt = date.to_pydatetime() date_npdt = date.to_datetime64() date_str = date_pydt.strftime('%Y-%m-%d') + index = date_to_index(date, date_index) index_pydt = date_to_index(date_pydt, date_index) index_npdt = date_to_index(date_npdt, date_index) diff --git a/arch/unitroot/unitroot.py b/arch/unitroot/unitroot.py index 3af642848d..d4a87d2e35 100644 --- a/arch/unitroot/unitroot.py +++ b/arch/unitroot/unitroot.py @@ -357,6 +357,8 @@ def _reset(self): """Resets the unit root test so that it will be recomputed """ self._stat = None + print(self._stat is None) + assert self._stat is None def _compute_if_needed(self): """Checks whether the statistic needs to be computed, and computed if @@ -642,6 +644,9 @@ def max_lags(self): @max_lags.setter def max_lags(self, value): + if self._max_lags != value: + self._reset() + self._lags = None self._max_lags = value @@ -724,7 +729,7 @@ class DFGLS(UnitRootTest): """ def __init__(self, y, lags=None, trend='c', - max_lags=None, method='AIC', low_memory=False): + max_lags=None, method='AIC', low_memory=None): valid_trends = ('c', 'ct') super(DFGLS, self).__init__(y, lags, trend, valid_trends) self._max_lags = max_lags @@ -809,6 +814,9 @@ def max_lags(self): @max_lags.setter def max_lags(self, value): + if self._max_lags != value: + self._reset() + self._lags = None self._max_lags = value