psytest.adftest module#

psytest.adftest#

This module contains the functions related to the Augmented Dickey-Fuller test for unit roots. The functions allow us to calculate both the test statistic and the asymptotic distribution of the test statistic. Additionally, it provides functions for rolling ADF tests and cumulative distribution functions based on a Wiener process.

psytest.adftest.adfuller_fit(y: ndarray[tuple[int, ...], dtype[float64]], kmax: int) ndarray[tuple[int, ...], dtype[float64]][source]#

Calculates the fitted values of the Augmented Dickey-Fuller regression.

Parameters:
  • y (NDArray[float64]) – The time series data.

  • kmax (int) – Maximum lag to use in the test.

Returns:

fit – The fitted values of the regression.

Return type:

NDArray[float64]

psytest.adftest.adfuller_stat(y: ndarray[tuple[int, ...], dtype[float64]], kmax: int) float[source]#

Calculates the test statistic for the Augmented Dickey-Fuller.

Parameters:
  • y (NDArray[float64]) – The time series data.

  • kmax (int) – Maximum lag to use in the test.

Returns:

tstat – The test statistic.

Return type:

float

Notes

The Augmented Dickey-Fuller test statistic is calculated as: .. math:

\text{ADF} = \frac{\hat{\beta}}{\sqrt{\widehat{\mathrm{Var}}(\hat{\beta})}}

where \(\hat{\beta}\) is the coefficient of \(y_{t-1}\) and \(\widehat{\mathrm{Var}}(\hat{\beta})\) is its estimated variance in the regression:F .. math:

\Delta y_{t} = \alpha + \beta y_{t-1} + \sum_{k=2}^{kmax} \gamma_k \Delta y_{t-k} + \epsilon_t
psytest.adftest.adfuller_dist(nobs: int, nreps: int, kmax: int) ndarray[tuple[int, ...], dtype[float64]][source]#

Simulates the asymptotic distribution of the Augmented Dickey-Fuller test statistic.

Parameters:
  • nobs (int) – Number of observations in the time series.

  • nreps (int) – Number of simulations to perform.

  • kmax (int) – Maximum lag to use in the test.

Returns:

distribution – The simulated distribution of the test statistics.

Return type:

NDArray[float64]

psytest.adftest.rolling_adfuller_stat(y: ndarray[tuple[int, ...], dtype[float64]], kmax: int, r1: float = 0, r2: float = 1.0) float[source]#

Calculates the Augmented Dickey-Fuller test statistic for a window of the time series.

Parameters:
  • y (NDArray[float64]) – Values of the time series.

  • r1 (float, optional) – Start index. Defaults to 0.

  • r2 (float, optional) – End index. Defaults to 1.

  • kmax (int, optional) – Maximum lag to use in the test.

Returns:

teststat – Value of the test statistic.

Return type:

float

Notes

The rolling ADF test statistic is calculated as: .. math:

\text{ADF}(r_1, r_2) = \frac{\hat{\beta}}{\sqrt{\widehat{\mathrm{Var}}(\hat{\beta})}}

where \(\hat{\beta}\) is the coefficient of \(y_{t-1}\) and \(\widehat{\mathrm{Var}}(\hat{\beta})\) is its estimated variance in the regression: .. math:

\Delta y_{t} = \alpha + \beta y_{t-1} + \sum_{k=2}^{kmax} \gamma_k \Delta y_{t-k} + \epsilon_t
Raises:

ValueError – If r1 or r2 are not in the range [0, 1] or if r1 is greater than r2.

psytest.adftest.rolling_adfuller_cdf(wiener: ndarray[tuple[int, ...], dtype[float64]], r1: float, r2: float) float[source]#

Calculates the cumulative asymptotic distribution of the Augmented Dickey-Fuller test statistic based on a Wiener process.

Parameters:
  • wiener (NDArray[float64]) – Values of the Wiener process.

  • r1 (float) – Start index.

  • r2 (float) – End index.

Returns:

cdf – Value of the cumulative distribution function.

Return type:

float

Raises:

ValueError – If r1 or r2 are not in the range [0, 1] or if r1 is greater than r2.