Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 67 additions & 13 deletions arch/tests/unitroot/test_adf_t_pvalue.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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_<trend>-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_<trend>-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(
Expand Down Expand Up @@ -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)
6 changes: 3 additions & 3 deletions arch/tests/unitroot/test_unitroot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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])
Expand Down
47 changes: 36 additions & 11 deletions arch/unitroot/critical_values/dickey_fuller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
],
}


Expand Down
121 changes: 80 additions & 41 deletions arch/unitroot/critical_values/simulation/adf_t_simulation_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = {}
Expand All @@ -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,
Expand All @@ -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(
Expand Down
Loading
Loading