diff --git a/arch/tests/unitroot/test_adf_t_pvalue.py b/arch/tests/unitroot/test_adf_t_pvalue.py index c845f108bb..98f9d39ceb 100644 --- a/arch/tests/unitroot/test_adf_t_pvalue.py +++ b/arch/tests/unitroot/test_adf_t_pvalue.py @@ -1,10 +1,14 @@ """ -Tests that the ADF t-test p-value is a smooth function of the test statistic. +Tests of the ADF t-test p-value and critical values. With one unit root, the p-value is a cubic in stat below adf_t_star and another cubic in stat above it. The two pieces are estimated jointly so that they have the same value and the same slope at adf_t_star, and the p-value is monotone in the statistic. -With more than one unit root, the p-values are those of MacKinnon (1994). +The critical values are fit to the same simulations, and the p-value is fit to the +asymptotic quantiles that are the constants of the critical value regressions, so +the p-values of the asymptotic critical values are 1%, 5% and 10%. With more than +one unit root, the p-values are those of MacKinnon (1994) and the critical values +are those of MacKinnon (2010). """ from typing import Literal @@ -15,32 +19,44 @@ from scipy import stats from arch.unitroot.critical_values.dickey_fuller import ( + adf_t_cv_approx, adf_t_star, + tau_2010, tau_large_p, tau_max, tau_min, tau_small_p, tau_star, ) -from arch.unitroot.unitroot import mackinnonp +from arch.unitroot.unitroot import mackinnoncrit, mackinnonp TRENDS = ["n", "c", "ct", "ctt"] -# Mean over the simulation files adf_t_-NNNN.npz (written by -# adf_z_critical_values_simulation_joblib.py in critical_values/simulation) of the 1, -# 5, 10, 50 and 90 percentiles of the ADF t-statistic with T=2000 observations, -# rounded to 4 decimals. These are the data that the p-value models are fit to, and -# are the reference for the accuracy of the fitted p-values. +# The asymptotic 1, 5, 10, 50 and 90 percentiles of the ADF t-statistic, rounded to +# 4 decimals. These are the constants of the regressions of the percentiles in the +# simulation files adf_t_-NNNN.npz (written by +# adf_z_critical_values_simulation_joblib.py in critical_values/simulation) on powers +# of 1/nobs, which adf_t_simulation_process.py fits the p-value models to. They are +# the reference for the accuracy of the fitted p-values. PERCENTILES = np.array([0.01, 0.05, 0.10, 0.50, 0.90]) SIMULATED_QUANTILES = { - "n": [-2.5671, -1.9410, -1.6164, -0.4997, 0.8881], - "c": [-3.4332, -2.8631, -2.5676, -1.5660, -0.4394], - "ct": [-3.9634, -3.4127, -3.1284, -2.1801, -1.2449], - "ctt": [-4.3775, -3.8353, -3.5551, -2.6196, -1.7348], + "n": [-2.5658, -1.9409, -1.6167, -0.5001, 0.8875], + "c": [-3.4303, -2.8615, -2.5668, -1.5664, -0.4403], + "ct": [-3.9585, -3.4104, -3.1270, -2.1806, -1.2464], + "ctt": [-4.3712, -3.8323, -3.5532, -2.6201, -1.7365], } -# The largest error seen is 1.8e-5 for the 1, 5 and 10 percentiles and 5.7e-3 for the +# The largest error seen is 3.3e-5 for the 1, 5 and 10 percentiles and 6.3e-3 for the # 50 and 90 percentiles (cubic polynomials cannot track the whole distribution) ATOL = np.array([1e-4, 1e-4, 1e-4, 7.5e-3, 7.5e-3]) +# Mean over the simulation files of the 1, 5 and 10 percentiles with nobs = 24 and +# nobs = 99 observations in the regression (series of 25 and 100 observations), +# rounded to 4 decimals. Their standard errors are at most 0.00022. +SIMULATED_CRITICAL_VALUES = { + "n": {24: [-2.6649, -1.9557, -1.6086], 99: [-2.5886, -1.9440, -1.6144]}, + "c": {24: [-3.7379, -2.9922, -2.6356], 99: [-3.4983, -2.8913, -2.5826]}, + "ct": {24: [-4.3940, -3.6122, -3.2431], 99: [-4.0535, -3.4558, -3.1536]}, + "ctt": {24: [-4.9400, -4.1087, -3.7194], 99: [-4.4923, -3.8935, -3.5909]}, +} def adf_t_pvalue( @@ -128,3 +144,41 @@ def test_more_unit_roots_use_mackinnon_1994(trend, num_unit_roots): coef = tau_small_p[trend][idx] if stat <= star else tau_large_p[trend][idx] expected = stats.norm.cdf(np.polyval(np.asarray(coef)[::-1], stat)) assert_allclose(adf_t_pvalue(stat, trend, num_unit_roots), expected) + + +@pytest.mark.parametrize("trend", TRENDS) +def test_pvalue_at_asymptotic_critical_values(trend): + # The p-value is fit to the constants of the critical value regressions + critical_values = mackinnoncrit(1, trend, np.inf, "adf-t") + pvals = [adf_t_pvalue(float(cv), trend) for cv in critical_values] + assert_allclose(pvals, [0.01, 0.05, 0.10], rtol=1e-3) + + +@pytest.mark.parametrize("trend", TRENDS) +@pytest.mark.parametrize("nobs", [24, 99, np.inf]) +def test_critical_values_match_simulated_distribution(trend, nobs): + coef = np.array(adf_t_cv_approx[trend]) + critical_values = mackinnoncrit(1, trend, nobs, "adf-t") + if nobs == np.inf: + assert_allclose(critical_values, coef[:, 0]) + expected = SIMULATED_QUANTILES[trend][:3] + else: + assert_allclose(critical_values, np.polyval(coef[:, ::-1].T, 1.0 / nobs)) + expected = SIMULATED_CRITICAL_VALUES[trend][nobs] + # The largest error of the critical value regressions is 0.0003, which is 2.5 + # standard errors of the simulated percentiles + assert_allclose(critical_values, expected, rtol=0, atol=6e-4) + + +@pytest.mark.parametrize("trend", ["c", "ct", "ctt"]) +@pytest.mark.parametrize("num_unit_roots", [2, 3, 12]) +@pytest.mark.parametrize("nobs", [50, np.inf]) +def test_more_unit_roots_critical_values_use_mackinnon_2010( + trend, num_unit_roots, nobs +): + coef = tau_2010[trend][num_unit_roots - 1] + if nobs == np.inf: + expected = coef[:, 0] + else: + expected = np.polyval(coef[:, ::-1].T, 1.0 / nobs) + assert_allclose(mackinnoncrit(num_unit_roots, trend, nobs, "adf-t"), expected) diff --git a/arch/tests/unitroot/test_unitroot.py b/arch/tests/unitroot/test_unitroot.py index c8278a1de8..4dd458a731 100644 --- a/arch/tests/unitroot/test_unitroot.py +++ b/arch/tests/unitroot/test_unitroot.py @@ -23,7 +23,7 @@ from statsmodels.tsa.stattools._stattools import _autolag from arch.unitroot import ADF, DFGLS, KPSS, PhillipsPerron, VarianceRatio, ZivotAndrews -from arch.unitroot.critical_values.dickey_fuller import tau_2010 +from arch.unitroot.critical_values.dickey_fuller import adf_t_cv_approx from arch.unitroot.unitroot import ( _autolag_ols, _autolag_ols_low_memory, @@ -73,7 +73,7 @@ def test_adf_no_options(self): adf = ADF(self.inflation) assert_almost_equal(adf.stat, -3.09310, DECIMAL_4) assert_equal(adf.lags, 2) - assert_almost_equal(adf.pvalue, 0.027261, DECIMAL_4) + assert_almost_equal(adf.pvalue, 0.027104, DECIMAL_4) adf.regression.summary() adf2 = ADF(self.inflation, low_memory=True) assert_equal(adf2.lags, 2) @@ -110,7 +110,7 @@ def test_adf_auto_bic(self): def test_adf_critical_value(self): adf = ADF(self.inflation, trend="c", lags=3) adf_cv = adf.critical_values - temp = polyval(tau_2010["c"][0, :, ::-1].T, 1.0 / adf.nobs) + temp = polyval(np.array(adf_t_cv_approx["c"])[:, ::-1].T, 1.0 / adf.nobs) cv = {"1%": temp[0], "5%": temp[1], "10%": temp[2]} for k, v in cv.items(): assert_almost_equal(v, adf_cv[k]) diff --git a/arch/unitroot/critical_values/dickey_fuller.py b/arch/unitroot/critical_values/dickey_fuller.py index e947625cfe..d17ca46206 100644 --- a/arch/unitroot/critical_values/dickey_fuller.py +++ b/arch/unitroot/critical_values/dickey_fuller.py @@ -369,26 +369,51 @@ } -# t values from new simulations, exercises n 556, c 579, ct 555, ctt 535, 250,000 per ex. -# One unit root. The critical values are those of MacKinnon (2010). +# t values from new simulations, 250,000 per ex., exercises +# n 1,966, c 1,954, ct 1,947, ctt 1,949. +# One unit root. The critical values and the p-values are fit to the same +# simulations, and the p-value at an asymptotic critical value is its size. adf_t_min = {"n": -inf, "c": -inf, "ct": -inf, "ctt": -inf} -adf_t_star = {"n": -1.03833, "c": -1.68285, "ct": -1.92679, "ctt": -2.13407} +adf_t_star = {"n": -1.03884, "c": -1.72243, "ct": -1.92756, "ctt": -2.13529} adf_t_max = {"n": inf, "c": inf, "ct": inf, "ctt": inf} adf_t_small_p = { - "n": [0.6913311061, 1.328421579, 0.07898346541, 0.007566600286], - "c": [2.274014487, 1.55543507, 0.07742152573, 0.004266936815], - "ct": [3.507377352, 1.83472974, 0.1181542989, 0.006714561968], - "ctt": [4.345415704, 1.936286377, 0.122291238, 0.006429102324], + "n": [0.6833411665, 1.315029094, 0.07244639622, 0.006663824941], + "c": [2.313783659, 1.593247201, 0.0894645463, 0.005636419168], + "ct": [3.507025793, 1.833226361, 0.1179704595, 0.006853449833], + "ctt": [4.350765654, 1.937712641, 0.1226570161, 0.006592764095], } adf_t_large_p = { - "n": [0.4783539379, 0.9431502833, -0.07048469259, 0.03071681015], - "c": [1.722406835, 0.9847953661, -0.01642755056, 0.03425437747], - "ct": [2.658511483, 1.014988682, -0.04678493047, 0.02324731751], - "ctt": [3.273070004, 0.9528914873, -0.09294265035, 0.01116800584], + "n": [0.4778283114, 0.9445076761, -0.06959215169, 0.02995625396], + "c": [1.724023251, 0.9854879487, -0.01986811093, 0.03160449619], + "ct": [2.65925795, 1.009846397, -0.05183902784, 0.02199224487], + "ctt": [3.282981007, 0.9677512622, -0.08327592936, 0.01320964366], +} + +adf_t_cv_approx = { + "n": [ + [-2.56583, -2.2208, -3.93978, 0.0], + [-1.94092, -0.29436, -1.50446, 0.0], + [-1.61673, 0.24128, -1.09348, 0.0], + ], + "c": [ + [-3.43028, -6.5442, -16.78148, -78.88107], + [-2.86153, -2.88378, -4.85339, -27.21776], + [-2.56678, -1.54286, -2.01148, -13.71388], + ], + "ct": [ + [-3.95852, -9.14587, -23.49649, -192.96108], + [-3.41043, -4.41263, -8.00584, -56.60505], + [-3.12704, -2.59751, -3.33152, -29.29239], + ], + "ctt": [ + [-4.37119, -11.58943, -34.74746, -359.09432], + [-3.83233, -5.91188, -12.44579, -117.08312], + [-3.55323, -3.66531, -5.18029, -62.89369], + ], } diff --git a/arch/unitroot/critical_values/simulation/adf_t_simulation_process.py b/arch/unitroot/critical_values/simulation/adf_t_simulation_process.py index 068f8a9ab9..37423841dd 100644 --- a/arch/unitroot/critical_values/simulation/adf_t_simulation_process.py +++ b/arch/unitroot/critical_values/simulation/adf_t_simulation_process.py @@ -6,25 +6,34 @@ python adf_t_simulation_process.py -The p-value of the t-test statistic with one unit root is fit to the quantiles -with T=2000. The values are written to a section of dickey_fuller.py that is -before the z-test values, so that adf_z_simlation_process.py keeps it, and which -is replaced if it exists. The critical values remain those of MacKinnon (2010) in -tau_2010, and the simulated quantiles are compared with them. +The statistic of a series of length T is computed from the nobs = T - 1 +observations of the regression of ADF, which is the nobs that ADF and +PhillipsPerron give mackinnoncrit. Each simulated quantile is regressed on a +constant and powers of 1/nobs. The regressions of the 1%, 5% and 10% quantiles +are the critical values, as for the ADF z-test. The constants of the regressions +are the asymptotic quantiles, and the p-value is fit to them, so that the p-value +at an asymptotic critical value is its size, up to the error of the fit. The +values are for one unit root. They are written to a section of dickey_fuller.py +that is before the z-test values, so that adf_z_simlation_process.py keeps it, +and which is replaced if it exists. """ import argparse import os import pathlib +import runpy from adf_simulation import PERCENTILES, T_OUTPUT_PATH, TIME_SERIES_LENGTHS, TRENDS from black import FileMode, TargetVersion, format_file_contents import numpy as np import pandas as pd from scipy import stats -from shared import fit_smooth_pval_model, format_dict - -from arch.unitroot.unitroot import mackinnoncrit +from shared import ( + PvalueResult, + estimate_response_surface, + fit_smooth_pval_model, + format_dict, +) # The small p polynomial is a cubic in the statistic, and not a quadratic as in # MacKinnon (1994) @@ -56,10 +65,32 @@ help="Simulations per file, for the comment in the output", ) args = parser.parse_args() +output = pathlib.Path(args.output) +# MacKinnon (2010), for comparison, from the file that is updated. Importing it from +# arch.unitroot would fail if the file does not have the values that this writes. +tau_2010 = runpy.run_path(str(output))["tau_2010"] + + +def pvalue(stat: np.ndarray, pvals: PvalueResult) -> np.ndarray: + small, large = np.array(pvals.small_p), np.array(pvals.large_p) + with np.errstate(over="ignore"): + return stats.norm.cdf( + np.where( + stat <= pvals.tau_star, + np.polyval(small[::-1], stat), + np.polyval(large[::-1], stat), + ) + ) + data_path = pathlib.Path(args.data_path) critical_values = (1.0, 5.0, 10.0) +cv_rows = [int(np.argmin(np.abs(PERCENTILES - cv))) for cv in critical_values] +nobs = TIME_SERIES_LENGTHS - 1 +rhs = (1.0 / nobs[:, None]) ** np.arange(4) +probs = PERCENTILES / 100.0 counts = {} +adf_t_cv_approx = {} adf_t_max = {} adf_t_min = {} adf_t_star = {} @@ -76,19 +107,23 @@ with np.load(df) as data: all_results.append(data["results"]) results = np.hstack(all_results) - cols = TIME_SERIES_LENGTHS.tolist() * len(data_files) - results_df = pd.DataFrame( - results, index=pd.Index(PERCENTILES / 100.0), columns=cols - ) + cols = nobs.tolist() * len(data_files) + results_df = pd.DataFrame(results, index=pd.Index(probs), columns=cols) counts[trend] = len(data_files) + surface = estimate_response_surface(results_df) + adf_t_cv_approx[trend] = [ + [float(val) for val in surface.iloc[row]] for row in cv_rows + ] + asymptotic = surface.iloc[:, :1] + # The two p-value polynomials are fit jointly so that the p-value is continuous, # has a continuous first derivative and is monotone in the statistic error: RuntimeError | None = None for max_tail_step in MAX_TAIL_STEPS: try: pvals = fit_smooth_pval_model( - results_df[2000], + asymptotic, small_order=SMALL_ORDER, use_log=False, max_tail_step=max_tail_step, @@ -107,53 +142,57 @@ adf_t_small_p[trend] = [float(val) for val in pvals.small_p] adf_t_large_p[trend] = [float(val) for val in pvals.large_p] - # The fit at the average simulated quantiles that it is estimated from - stat = np.asarray(results_df[2000]).mean(axis=1) - small, large = np.array(pvals.small_p), np.array(pvals.large_p) - with np.errstate(over="ignore"): - fitted = stats.norm.cdf( - np.where( - stat <= pvals.tau_star, - np.polyval(small[::-1], stat), - np.polyval(large[::-1], stat), - ) - ) - probs = PERCENTILES / 100.0 - err = np.abs(fitted - probs) + # The fit at the asymptotic quantiles that it is estimated from + err = np.abs(pvalue(asymptotic.iloc[:, 0].to_numpy(), pvals) - probs) tail = probs <= 0.15 print( f"{trend}: {len(data_files)} files, tau_star {pvals.tau_star}, largest " f"error in p {err[tail].max():.1e} if p <= 0.15 and {err[~tail].max():.4f} " f"otherwise, relative error at p={probs[0]:g} {err[0] / probs[0]:.2%}" ) - # The regression of ADF with a series of length T has T - 1 observations + asymptotic_cv = np.array([cv[0] for cv in adf_t_cv_approx[trend]]) + print( + " p-value at the asymptotic critical values: " + + ", ".join(f"{val:.6f}" for val in pvalue(asymptotic_cv, pvals)) + ) + # The critical values compared with the average simulated quantiles and with + # the critical values of MacKinnon (2010) for j, cv in enumerate(critical_values): - row = results_df.iloc[int(np.argmin(np.abs(PERCENTILES - cv)))] - grouped = row.groupby(level=0) - mean = grouped.mean() - se = grouped.std() / np.sqrt(grouped.count()) - tau = mean.index.to_numpy() - mackinnon = np.array([mackinnoncrit(1, trend, t - 1, "adf-t")[j] for t in tau]) - diff = mean.to_numpy() - mackinnon + grouped = results_df.iloc[cv_rows[j]].groupby(level=0) + mean = grouped.mean().to_numpy() + se = (grouped.std() / np.sqrt(grouped.count())).to_numpy() + fitted = rhs @ np.array(adf_t_cv_approx[trend][j]) + mackinnon = rhs @ tau_2010[trend][0, j] + fit_err = fitted - mean + diff = fitted - mackinnon + worst_fit = int(np.argmax(np.abs(fit_err / se))) worst = int(np.argmax(np.abs(diff))) print( - f" {cv:g}%: largest difference from MacKinnon (2010) {diff[worst]:+.4f} " - f"at T={tau[worst]} ({diff[worst] / se.iloc[worst]:+.1f} s.e.)" + f" {cv:g}%: {adf_t_cv_approx[trend][j]}, largest error " + f"{fit_err[worst_fit]:+.4f} at nobs={nobs[worst_fit]} " + f"({fit_err[worst_fit] / se[worst_fit]:+.1f} s.e.), largest difference " + f"from MacKinnon (2010) {diff[worst]:+.4f} at nobs={nobs[worst]}" ) +t_section = f"{T_MARKER} new simulations, {args.ex_size:,} per ex., " if len(set(counts.values())) == 1: - exercises = f"{counts[TRENDS[0]]:,} exercises" + t_section += f"{counts[TRENDS[0]]:,} exercises.\n" else: - exercises = "exercises " + ", ".join(f"{t} {c:,}" for t, c in counts.items()) -t_section = f"{T_MARKER} new simulations, {exercises}, {args.ex_size:,} per ex.\n" -t_section += "# One unit root. The critical values are those of MacKinnon (2010).\n" + t_section += "exercises\n# " + ", ".join(f"{t} {c:,}" for t, c in counts.items()) + t_section += ".\n" +t_section += ( + "# One unit root. The critical values and the p-values are fit to the same\n" +) +t_section += ( + "# simulations, and the p-value at an asymptotic critical value is its size.\n" +) t_section += "adf_t_min = " + format_dict(adf_t_min) t_section += "\n\nadf_t_star = " + format_dict(adf_t_star) t_section += "\n\nadf_t_max = " + format_dict(adf_t_max) t_section += "\n\nadf_t_small_p = " + format_dict(adf_t_small_p) t_section += "\n\nadf_t_large_p = " + format_dict(adf_t_large_p) +t_section += "\n\nadf_t_cv_approx = " + format_dict(adf_t_cv_approx) -output = pathlib.Path(args.output) lines = output.read_text().splitlines(keepends=True) z_start = next(i for i, line in enumerate(lines) if line.startswith(Z_MARKER)) t_start = next( diff --git a/arch/unitroot/critical_values/simulation/shared.py b/arch/unitroot/critical_values/simulation/shared.py index 67b29f8bad..3c4ef57935 100644 --- a/arch/unitroot/critical_values/simulation/shared.py +++ b/arch/unitroot/critical_values/simulation/shared.py @@ -52,6 +52,54 @@ def estimate_cv_regression( return out +def estimate_response_surface( + results: pd.DataFrame, max_pvalue: float = 0.05, decimals: int = 5 +) -> pd.DataFrame: + """ + Regress each simulated quantile on a constant and powers of 1/nobs. + + Parameters + ---------- + results : DataFrame + The simulated quantiles, indexed by the probability. Each column is one + simulation of the quantiles, and is labeled with the number of observations + that the statistic is computed from. + max_pvalue : float, default 0.05 + While the p-value of the highest power of 1/nobs is larger than this, it is + dropped and the regression is estimated again. The constant and 1/nobs are + always included. + decimals : int, default 5 + The number of decimals of the returned coefficients. + + Returns + ------- + DataFrame + The coefficients of 1, 1/nobs, 1/nobs**2 and 1/nobs**3, indexed by the + probability. The coefficients of the powers that are dropped are 0. The + constant is the asymptotic quantile. + + Notes + ----- + Unlike estimate_cv_regression, which sets the coefficients that are not + significant to 0 and keeps the others, the regression is estimated again + without the powers that are dropped, so that the coefficients that are kept fit + the quantiles. + """ + nobs = np.asarray(results.columns, dtype=float) + rhs = (1.0 / nobs[:, None]) ** np.arange(4) + out = np.zeros((results.shape[0], 4)) + for i in range(results.shape[0]): + lhs = np.asarray(results.iloc[i], dtype=float) + for order in range(4, 1, -1): + res = OLS(lhs, rhs[:, :order]).fit() + if order == 2 or res.pvalues[-1] <= max_pvalue: + break + out[i, :order] = np.round(res.params, decimals) + return pd.DataFrame( + out, index=results.index, columns=["1", "1/nobs", "1/nobs**2", "1/nobs**3"] + ) + + def fit_pval_model( quantiles: pd.Series | pd.DataFrame, small_order: int = 3, diff --git a/arch/unitroot/unitroot.py b/arch/unitroot/unitroot.py index 806cde4f81..cacc49cc16 100644 --- a/arch/unitroot/unitroot.py +++ b/arch/unitroot/unitroot.py @@ -70,6 +70,7 @@ dfgls_tau_star, ) from arch.unitroot.critical_values.dickey_fuller import ( + adf_t_cv_approx, adf_t_large_p, adf_t_max, adf_t_min, @@ -959,10 +960,12 @@ class ADF(UnitRootTest, metaclass=AbstractDocStringInheritor): appears to be a unit root. The p-values are from an approximation of the asymptotic distribution of the - statistic that follows the methodology of MacKinnon (1994), fit to new - simulations, and are a smooth and increasing function of the statistic. The - critical values are from the response surface regressions of MacKinnon (2010), - and depend on the number of observations. + statistic that follows the methodology of MacKinnon (1994), and are a smooth + and increasing function of the statistic. The critical values are from + response surface regressions that follow the methodology of MacKinnon (2010), + and depend on the number of observations. Both are fit to the same new + simulations, and the p-values of the asymptotic critical values are 1%, 5% and + 10%. If the p-value is close to significant, then the critical values should be used to judge whether to reject the null. @@ -1000,14 +1003,14 @@ class ADF(UnitRootTest, metaclass=AbstractDocStringInheritor): >>> print(f"{adf.stat:0.4f}") -3.0931 >>> print(f"{adf.pvalue:0.4f}") - 0.0273 + 0.0271 >>> adf.lags 2 >>> adf = ADF(inflation, trend="ct") >>> print(f"{adf.stat:0.4f}") -3.2111 >>> print(f"{adf.pvalue:0.4f}") - 0.0825 + 0.0822 """ def __init__( @@ -2353,9 +2356,8 @@ def mackinnoncrit( """ Return the critical values for cointegrating and the ADF test. - In 2010 MacKinnon updated the values of his 1994 paper with critical values - for the augmented Dickey-Fuller tests. These new values are to be - preferred and are used here. + The critical values are from response surface regressions that follow the + methodology of MacKinnon (2010), and depend on the number of observations. Parameters ---------- @@ -2368,12 +2370,11 @@ def mackinnoncrit( Following MacKinnon (1996), these stand for the type of regression run. "c" for constant and no trend, "ct" for constant with a linear trend, "ctt" for constant with a linear and quadratic trend, and "n" for - no constant. The values for the no constant case are taken from the - 1996 paper, as they were not updated for 2010 due to the unrealistic - assumptions that would underlie such a case. Not case sensitive. + no constant. Not case sensitive. nobs : int or float, optional - This is the sample size, which must be positive. If the sample size is - numpy.inf, then the asymptotic critical values are returned. + This is the sample size, the number of observations in the regression of + the test, which must be positive. If the sample size is numpy.inf, then + the asymptotic critical values are returned. dist_type : {"adf-t", "adf-z", "dfgls"}, optional Type of test statistic. Not case sensitive. @@ -2395,8 +2396,11 @@ def mackinnoncrit( Notes ----- - Results for ADF t-stats from MacKinnon (1994,2010). Results for DFGLS and - ADF z-tests use the same methodology as MacKinnon. + The values for the ADF t-test with one unit root are fit to the same + simulations as the p-values of mackinnonp, and the p-values of the asymptotic + critical values are 1%, 5% and 10%. The values for the ADF z-test and the + DFGLS test are also fit to new simulations. The values for the ADF t-test with + more than one unit root are from MacKinnon (2010). References ---------- @@ -2414,7 +2418,10 @@ def mackinnoncrit( "float", check_float("nobs", nobs, lower=0, inclusive="right", allow_inf=True) ) - if lower_dist_type == "adf-t": + if lower_dist_type == "adf-t" and num_unit_roots == 1: + poly_coef = array(adf_t_cv_approx[regression]).T + asymptotic_cv = array(adf_t_cv_approx[regression])[:, 0] + elif lower_dist_type == "adf-t": asymptotic_cv = tau_2010[regression][num_unit_roots - 1, :, 0] poly_coef = tau_2010[regression][num_unit_roots - 1, :, :].T elif lower_dist_type == "adf-z": diff --git a/doc/source/changes/9.0.rst b/doc/source/changes/9.0.rst index 69f2fd2670..b0603b3f72 100644 --- a/doc/source/changes/9.0.rst +++ b/doc/source/changes/9.0.rst @@ -443,20 +443,27 @@ to restore the previous results. - The p-value of :class:`~arch.unitroot.ADF`, which :class:`~arch.unitroot.PhillipsPerron` uses with ``test_type="tau"`` and ``mackinnonp`` returns with ``dist_type="adf-t"`` and one unit root, is now a smooth - and increasing function of the statistic. The p-values are from a new simulation - with 134,000,000 to 145,000,000 draws for each trend. The p-value is approximated by - one polynomial below a cut-off and another above it. These were estimated separately - (MacKinnon, 1994), so the p-value jumped up at the cut-off, by 0.004 with - ``trend="n"`` and by up to 0.0006 otherwise, and jumped up to 1 at the largest - statistic covered, by up to 0.003. The two are now cubics, estimated jointly so that - they have the same value and slope at the cut-off. The approximation is also more - accurate in the left tail: the largest error of the p-values of the 1%, 5% and 10% - quantiles falls from 0.0004 to 0.00002, and the largest relative error of p-values - below 0.15 from 2.5% to 0.12%. In the middle of the distribution, the largest - difference from the simulated distribution is 0.006 with ``"n"`` (0.004 before) and - at most 0.003 otherwise, as before. P-values below 0.15 change by at most 0.0005, - and the largest change is 0.004, at a p-value near 0.6 with ``"c"``. The p-values - with more than one unit root and the critical values are unchanged (:issue:`1008`). + and increasing function of the statistic, and the critical values with one unit root + are fit to the same simulations. The simulations have 486,750,000 to 491,500,000 + draws for each trend. The p-value is approximated by one polynomial below a cut-off + and another above it. These were estimated separately (MacKinnon, 1994), so the + p-value jumped up at the cut-off, by 0.004 with ``trend="n"`` and by up to 0.0006 + otherwise, and jumped up to 1 at the largest statistic covered, by up to 0.003. The + two are now cubics, estimated jointly so that they have the same value and slope at + the cut-off. They are fit to the asymptotic quantiles, which are the constants of the + regressions of the simulated quantiles on powers of 1/nobs, and the critical values + are the regressions of the 1%, 5% and 10% quantiles. The p-values of the asymptotic + critical values are then 1%, 5% and 10%, to 0.00002, where the values of MacKinnon + (1994, 2010) differed by up to 0.0001. The approximation is also more accurate in + the left tail: the largest error of the p-values of the 1%, 5% and 10% quantiles + falls from 0.00008 to 0.00001, and the largest relative error of p-values below 0.15 + from 5.3% to 0.05%. In the middle of the distribution, the largest difference from + the simulated distribution is 0.006 with ``"n"`` and 0.004 with ``"c"`` (0.004 and + 0.003 before), and at most 0.002 otherwise, as before. P-values below 0.15 change by + at most 0.0002, and the largest change is 0.004, at a p-value near 0.6 with + ``"c"``. The critical values change by at most 0.0008 in the smallest samples and by + at most 0.0003 with 100 or more observations. The p-values and critical values with + more than one unit root are unchanged (:issue:`1008`, :issue:`1017`). - Integer, unsigned integer and Boolean data are now converted to ``float64`` when they are passed to an estimator, so that no calculation is done in integer arithmetic. Previously a calculation that used the products or differences of integer data could