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
6 changes: 3 additions & 3 deletions arch/tests/unitroot/test_adf_t_pvalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@
"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 3.3e-5 for the 1, 5 and 10 percentiles and 6.3e-3 for the
# The largest error seen is 3.3e-5 for the 1, 5 and 10 percentiles and 6.2e-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]},
"n": {24: [-2.6649, -1.9558, -1.6086], 99: [-2.5887, -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]},
"ctt": {24: [-4.9399, -4.1087, -3.7194], 99: [-4.4923, -3.8935, -3.5909]},
}


Expand Down
71 changes: 55 additions & 16 deletions arch/tests/unitroot/test_adf_z_pvalue.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""
Tests that the ADF-Z p-value is a smooth function of the test statistic.
Tests of the ADF-Z p-value and critical values.

The p-value is a cubic in log|stat| below adf_z_star and a cubic in stat above it.
The p-value is a quartic in log|stat| below adf_z_star and a cubic in stat above it.
The two pieces are estimated jointly so that they have the same value and the same
slope at adf_z_star, and the p-value is monotone in the statistic.
slope at adf_z_star, and the p-value is monotone in the statistic. 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%.
"""

import importlib.util
Expand All @@ -15,26 +18,40 @@
import pytest
from scipy import stats

from arch.unitroot.critical_values.dickey_fuller import adf_z_star
from arch.unitroot.unitroot import mackinnonp
from arch.unitroot.critical_values.dickey_fuller import adf_z_cv_approx, adf_z_star
from arch.unitroot.unitroot import mackinnoncrit, mackinnonp

TRENDS = ["n", "c", "ct", "ctt"]

# Mean over the 2,000 simulation files adf_z_<trend>-NNNN.npz (written by the
# adf_z_critical_values_simulation scripts in critical_values/simulation) of the 1, 5,
# 10, 50 and 90 percentiles of the ADF-Z statistic with T=2000 observations, rounded
# to 4 decimals. These are the data that the p-value models are fit to, and are the
# The asymptotic 1, 5, 10, 50 and 90 percentiles of the ADF-Z statistic, rounded to 4
# decimals. These are the constants of the regressions of the percentiles in the 2,000
# simulation files adf_z_<trend>-NNNN.npz (written by
# adf_z_critical_values_simulation_joblib.py in critical_values/simulation) on powers
# of 1/nobs, which adf_z_simlation_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": [-13.6630, -8.0260, -5.7053, -0.8518, 0.9285],
"c": [-20.5667, -14.0644, -11.2311, -4.3534, -0.8439],
"ct": [-29.2396, -21.6459, -18.1980, -9.0901, -3.7642],
"ctt": [-36.4242, -28.0074, -24.1075, -13.3801, -6.6372],
"n": [-13.6946, -8.0394, -5.7137, -0.8528, 0.9280],
"c": [-20.6250, -14.0929, -11.2503, -4.3570, -0.8455],
"ct": [-29.3556, -21.7105, -18.2449, -9.1027, -3.7673],
"ctt": [-36.5964, -28.1110, -24.1853, -13.4049, -6.6436],
}
# The largest error seen is 7.8e-5 for the 1, 5 and 10 percentiles and 3.1e-3 for the
# 50 and 90 percentiles (cubic polynomials cannot track the whole distribution)
ATOL = np.array([1.5e-4, 1.5e-4, 1.5e-4, 5e-3, 5e-3])
# The largest error seen is 4.1e-5 for the 1, 5 and 10 percentiles and 6.7e-3 for the
# 50 and 90 percentiles (the 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.0019.
SIMULATED_CRITICAL_VALUES = {
"n": {24: [-11.4410, -7.0767, -5.1360], 99: [-13.0796, -7.7830, -5.5619]},
"c": {24: [-16.4941, -11.9877, -9.8359], 99: [-19.4769, -13.5272, -10.8754]},
"ct": {24: [-21.7334, -17.2364, -14.9653], 99: [-27.1485, -20.4596, -17.3423]},
"ctt": {24: [-25.4915, -21.1170, -18.8359], 99: [-33.2677, -26.0924, -22.6684]},
}
# The largest error of the critical value regressions is 0.0075 with "ctt" at 1% and
# nobs = 24 (7 standard errors of the simulated percentile), and 0.0014 (2.5 standard
# errors) otherwise. The asymptotic critical values are the reference quantiles.
CV_ATOL = {24: 1e-2, 99: 2.5e-3, np.inf: 1e-4}

# Wide enough that the p-value is 0 or 1 to machine precision at the ends
GRID_LIMITS = {"n": -200.0, "c": -200.0, "ct": -400.0, "ctt": -600.0}
Expand Down Expand Up @@ -112,6 +129,28 @@ def test_pvalue_matches_simulated_distribution(trend):
assert np.all(np.abs(pvals - PERCENTILES) <= ATOL)


@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-z")
pvals = [adf_z_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_z_cv_approx[trend])
critical_values = mackinnoncrit(1, trend, nobs, "adf-z")
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]
assert_allclose(critical_values, expected, rtol=0, atol=CV_ATOL[nobs])


@pytest.fixture(scope="module")
def simulation_shared():
path = (
Expand Down
12 changes: 7 additions & 5 deletions arch/tests/unitroot/test_smooth_pvalue_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
),
# Similar to ADF-Z
"exponential-log": (-4.0 * np.exp(-0.7 * NORMAL_QUANTILES), 4, True),
"exponential-log-quartic": (-4.0 * np.exp(-0.7 * NORMAL_QUANTILES), 5, True),
}
MAX_TAIL_STEP = 1e-12

Expand Down Expand Up @@ -203,16 +204,17 @@ def test_fit_significant_digits(simulation_shared, digits):
assert float(f"{value:.{digits - 1}e}") == value


def test_fit_small_order_invalid(simulation_shared):
@pytest.mark.parametrize("small_order", [2, 6])
def test_fit_small_order_invalid(simulation_shared, small_order):
quantiles = simulated(CASES["exponential-log"][0])
with pytest.raises(ValueError, match="small_order must be 3 or 4"):
simulation_shared.fit_smooth_pval_model(quantiles, small_order=5)
with pytest.raises(ValueError, match="small_order must be 3, 4 or 5"):
simulation_shared.fit_smooth_pval_model(quantiles, small_order=small_order)


@pytest.mark.parametrize(
("small_order", "use_log"),
[(3, False), (4, False), (3, True), (4, True)],
ids=["quadratic", "cubic", "log-quadratic", "log-cubic"],
[(3, False), (4, False), (5, False), (3, True), (4, True), (5, True)],
ids=["quadratic", "cubic", "quartic", "log-quadratic", "log-cubic", "log-quartic"],
)
def test_junction_constraints(simulation_shared, small_order, use_log):
rng = np.random.default_rng(1234)
Expand Down
2 changes: 1 addition & 1 deletion arch/tests/unitroot/test_unitroot.py
Original file line number Diff line number Diff line change
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.027104, DECIMAL_4)
assert_almost_equal(adf.pvalue, 0.027106, DECIMAL_4)
adf.regression.summary()
adf2 = ADF(self.inflation, low_memory=True)
assert_equal(adf2.lags, 2)
Expand Down
4 changes: 3 additions & 1 deletion arch/unitroot/_engle_granger.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ def engle_granger_cv(trend: str, num_x: int, nobs: int) -> pd.Series:
The number of cross-sectional regressors in the model.
Must be between 1 and 11.
nobs : int
The number of observations in the time series.
The number of observations in the ADF regression of the residuals, which
is the number of observations in the time series less the number of lags
less 1. The critical values are fit to simulations in this nobs.

Returns
-------
Expand Down
91 changes: 46 additions & 45 deletions arch/unitroot/critical_values/dickey_fuller.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,95 +369,96 @@
}


# t values from new simulations, 250,000 per ex., exercises
# n 1,966, c 1,954, ct 1,947, ctt 1,949.
# t values from new simulations, 250,000 per ex., 2,000 exercises.
# 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.03884, "c": -1.72243, "ct": -1.92756, "ctt": -2.13529}
adf_t_star = {"n": -1.03884, "c": -1.70276, "ct": -1.92756, "ctt": -2.13529}

adf_t_max = {"n": inf, "c": inf, "ct": inf, "ctt": inf}

adf_t_small_p = {
"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],
"n": [0.6836852219, 1.315480901, 0.07263159277, 0.006687123265],
"c": [2.308678903, 1.588493406, 0.08800898388, 0.005489394431],
"ct": [3.505829042, 1.832153392, 0.1176540505, 0.006822842442],
"ctt": [4.350249461, 1.937373616, 0.1225858139, 0.006588008193],
}

adf_t_large_p = {
"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],
"n": [0.4778795534, 0.9444210777, -0.06962952399, 0.03000302019],
"c": [1.72315283, 0.9857849826, -0.01406669498, 0.03481596722],
"ct": [2.659007943, 1.008840017, -0.05285077557, 0.02171517328],
"ctt": [3.281745927, 0.9651128956, -0.08502778083, 0.01284826647],
}

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],
[-2.56583, -2.21905, -3.98064, 0.0],
[-1.94091, -0.29351, -1.53148, 0.0],
[-1.61674, 0.24192, -1.11074, 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],
[-3.4303, -6.54325, -16.79014, -78.74877],
[-2.86153, -2.88397, -4.85708, -26.98494],
[-2.56677, -1.54355, -1.97303, -14.18317],
],
"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],
[-3.95851, -9.14992, -23.30656, -194.98745],
[-3.41043, -4.41328, -7.99116, -56.543],
[-3.12704, -2.59659, -3.40084, -28.09664],
],
"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],
[-4.3712, -11.59065, -34.60391, -361.28589],
[-3.83233, -5.90996, -12.50339, -116.72904],
[-3.55323, -3.66321, -5.26853, -61.94961],
],
}


# Z values from new simulations, 2,000 exercises, 250,000 per ex.
# Z values from new simulations, 250,000 per ex., 2,000 exercises.
# 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_z_min = {"n": -inf, "c": -inf, "ct": -inf, "ctt": -inf}

adf_z_star = {"n": -4.19211, "c": -8.14944, "ct": -13.099, "ctt": -15.93082}
adf_z_star = {"n": -1.83702, "c": -3.14129, "ct": -5.93276, "ctt": -8.24519}

adf_z_max = {"n": inf, "c": inf, "ct": inf, "ctt": inf}

# The small p parameters are for np.log(np.abs(stat))
adf_z_small_p = {
"n": [0.1128585132, -0.7519533397, 0.05363711911, -0.04697405824],
"c": [2.088134483, -1.636716716, 0.2693618616, -0.06974896038],
"ct": [4.111050235, -2.389235384, 0.427668324, -0.08437837715],
"ctt": [5.673700637, -2.973275585, 0.5518855813, -0.09562857546],
"n": [-0.1780983743, -0.2224742066, -0.2985627781, 0.05499834644, -0.01080878312],
"c": [1.186687586, -0.3554158055, -0.4073498794, 0.0877299448, -0.01356550117],
"ct": [2.370196501, -0.208289258, -0.5926765005, 0.1269131906, -0.0162693314],
"ctt": [3.238426923, -0.1006542359, -0.7131286635, 0.1507726862, -0.01783062046],
}

adf_z_large_p = {
"n": [0.5094184144, 0.6839706602, 0.120355783, 0.01018310207],
"c": [1.716168045, 0.5500431791, 0.04263836138, 0.001606489684],
"ct": [2.704353828, 0.4512169076, 0.0212562381, 0.0004817109902],
"ctt": [3.429989758, 0.4149740654, 0.01567632693, 0.000285455431],
"n": [0.5060346197, 0.7114864934, 0.116003454, 0.0005506503572],
"c": [1.719416611, 0.5435502671, 0.0349093361, 6.449528063e-05],
"ct": [2.683617312, 0.4310766477, 0.01600015499, 7.659418533e-05],
"ctt": [3.396767671, 0.3966952367, 0.01267343771, 0.0001299056083],
}

adf_z_cv_approx = {
"n": [
[-13.69464, 63.39558, -188.65735, 286.67928],
[-8.03941, 26.16037, -55.17125, 0.0],
[-5.71384, 15.38915, -23.26257, 0.0],
[-13.69457, 63.35494, -247.92292, 591.36974],
[-8.03939, 26.15035, -80.25024, 175.36092],
[-5.71369, 15.32536, -34.85607, 0.0],
],
"c": [
[-20.6251, 119.00752, -422.57873, 743.47694],
[-14.0929, 57.98623, -137.0056, 0.0],
[-11.25026, 38.2072, -70.72371, 0.0],
[-20.62498, 118.92923, -532.7576, 1394.08562],
[-14.09288, 57.97266, -193.30391, 362.3315],
[-11.25026, 38.20468, -108.45849, 164.38619],
],
"ct": [
[-29.35612, 232.23486, -1170.13934, 3209.13496],
[-21.71065, 130.0962, -500.98106, 1113.20613],
[-18.24497, 93.26993, -305.55437, 585.03699],
[-29.3556, 231.91837, -1368.25043, 4604.98636],
[-21.71045, 129.97569, -618.20581, 1804.67391],
[-18.24487, 93.2048, -391.76978, 1045.62368],
],
"ctt": [
[-36.59763, 354.7435, -2242.38386, 7798.9413],
[-28.11149, 212.35851, -1062.25936, 3096.6951],
[-24.1856, 158.61472, -696.85452, 1863.55439],
[-36.59641, 354.00743, -2518.58072, 9948.58706],
[-28.11099, 212.05398, -1241.9985, 4313.92147],
[-24.18529, 158.42745, -835.35719, 2724.38175],
],
}
Loading
Loading