psytest.utils.functions module#

psytest.utils.functions#

This module contains utility functions for the psytest package. It includes functions for generating random walks, simulating Markov processes, and calculating default parameters for bubble detection. It should NOT be used directly by users and is solely intended for internal use within the psytest package and testing purposes.

psytest.utils.functions.r0_default(nobs: int) int[source]#

Calculates the default r0 parameter following Phillips, Shi & Yu (2015) as a function of the number of observations.

Parameters:

nobs (int) – Number of observations

Returns:

default_r0 – The default r0 parameter

Return type:

int

Notes

Following the original paper, we set the default r0 parameter as:

\[r_0 = 0.01 \times 0.08 \times \sqrt{n}\]

where \(n\) is the number of observations.

psytest.utils.functions.minlength_default(nobs: int, delta: float) int[source]#

Calculates the minimum bubble length based on the number of observations.

Parameters:
  • nobs (int) – Number of observations

  • delta (float) – The delta parameter for the bubble length calculation

Returns:

minlength – Minimum bubble length

Return type:

int

Notes

Following the original paper, we set the minimum bubble length as:

\[\text{min_length} = \delta\frac{\log(n)}{n}\]

where \(n\) is the number of observations.

psytest.utils.functions.random_walk(nreps: int, nobs: int) ndarray[tuple[int, ...], dtype[float64]][source]#

Generates a monte carlo simulation of random walks.

Parameters:
  • nreps (int) – Number of repetitions.

  • nobs (int) – Number of observations.

Returns:

random_walk_matrix – Matrix of shape (nreps, nobs) with the random walks.

Return type:

NDArray[float64]

Notes

\[RW_{i, t} = \sum_{s=1}^{t} \frac{\varepsilon_{i, s}}{\sqrt{n}}\]
psytest.utils.functions.simulate_markov(nobs: int, p=0.975, beta_list: list[float] = [1.01, 1]) tuple[ndarray[tuple[int, ...], dtype[float64]], ndarray[tuple[int, ...], dtype[float64]]][source]#

Simulates a two regime AR(1) process with a Markov switching beta.

Parameters:
  • nobs (int) – Number of observations for the process

  • p (float, optional) – Probability of staying in the same regime. Defaults to 0.975.

  • beta_list (list[float], optional) – List of \(\beta\) values for the regimes. Defaults to [1.01, 1].

Returns:

[list[float], list[float]] – the first one with the beta values and the second one with the simulated process values.

Return type:

A tuple containing two lists

Notes

The process is defined as:

\[y_t = \beta_t y_{t-1} + \varepsilon_t\]

where \(\varepsilon_t \sim \mathcal{N}(0, 1)\) and \(\beta_t\) is a Markov switching variable that takes values from beta_list. The transition probability is defined by p, which is the probability of staying in the same regime.

Raises:
psytest.utils.functions.size_rgrid(r0: float, rstep: float) int[source]#

Calculates the size of the rgrid starting at r0 and with step rstep.

Parameters:
  • r0 (float) – Minimum index to evaluate the test statistics.

  • rstep (float) – Step size for the index.

Returns:

size – Size of the rgrid.

Return type:

int

Notes

The size is calculated as:

\[\text{size} = \left\lfloor \frac{1 - r_0}{\text{rstep}} \right\rfloor + 1\]