psytest package#
Module contents#
psytest#
This package provides a replication of the [1] tests for the detection of multiple bubbles in time series data. The main class is psytest.bubbles.PSYBubbles
(See psytest.bubbles
for details), which implements the tests and provides methods for their application and interpretation.
Usage#
Load the package and create an instance of the psytest.bubbles.PSYBubbles
class:
>>> from psytest import PSYBubbles
Load your data (formatted as a numpy.ndarray
) to an instance of the psytest.bubbles.PSYBubbles
class:
>>> from psytest import PSYBubbles
>>> psy = PSYBubbles(data = data)
If your data is a pandas.Series
, you can use the psytest.bubbles.from_pandas()
function to initialize the class:
>>> psy = PSYBubbles.from_pandas(data)
- class psytest.PSYBubbles(data: ndarray[tuple[int, ...], dtype[float64]], minwindow: int | None = None, minlength: int | None = None, lagmax: int | None = None, rstep: float | None = None, delta: float = 1.0)[source]#
Bases:
Generic
[IndexType
]Class to perform the Phillips, Shi & Yu (2015) test for bubbles in time series data.
- Parameters:
data¶ (
numpy.ndarray
of dtypenumpy.float64
) – Time series values.minwindow¶ (int | None, optional) – Minimum window size for the estimation. Defaults to using
psytest.utils.functions.r0_default()
.minlength¶ (int | None, optional) – Minimum bubble length. Defaults to
psytest.utils.functions.minlength_default()
.lagmax¶ (int, optional) – Maximum lag \(k_{\max}\). If none, uses
psytest.info_criteria.find_optimal_kmax()
to find the optimal value.rstep¶ (float | None, optional) – Step size \(r_{\text{step}}\). Defaults to \(1/n\) where \(n\) is the size of
data
.delta¶ (float, optional) – Default is 1.0. The parameter to determine the minimum length of bubbles. Used only if
minlength
is None.
- Raises:
TypeError – If arguments are not of the expected type.
ValueError – If
r0
andrstep
are not in the range (0, 1) or ifkmax
is not a positive integer.
- teststat(force: bool = False) dict[float | IndexType, float] [source]#
Retrieves the BSADF test statistic.
- Parameters:
- Returns:
teststat – Test statistics for each observation. If the object contains an index. It will be used as keys for the dictionary. Otherwise, the function returns the test statistic as a function of \(r_2\) in
psytest.PSYBubbles.r2grid()
.- Return type:
dictionary with float keys and float values
Notes
The test statistic is defined as:
\[\text{BSADF}_{r_2} = \max_{r_1 \in [0, r_2 - r_0]} \text{ADF}(y_{r_1:r_2})\]See the original paper for more details.
- critval(alpha: float | list[float], fast: Literal[True]) dict[float | IndexType, NDArray[float64]] [source]#
- critval(alpha: float | list[float], fast: Literal[False], nreps: int, nobs: int | None) dict[float | IndexType, NDArray[float64]]
Retrieves BSADF critical values using Monte Carlo Simulations.
- Parameters:
alpha¶ (list[float] | float, optional) – Significance levels \(\alpha\). Defaults to ALPHA_LIST (see
psytest.utils.defaults
)fast¶ (bool, optional) – If
True
(Default), uses tabulated critical values. Otherwise, simulates them usingnreps
simulations of sizenobs
.nreps¶ (int, optional) – Number of simulations (required if
fast
isTrue
).nobs¶ (int | None, optional) – Number of observations (used if
fast
isFalse
). Defaults to None, setting it topsytest.PSYBubbles.nobs
.
- Returns:
critval – Dictionary with critical values for each \(r_2\) in
psytest.PSYBubbles.r2grid()
. The keys are the \(r_2\) values and the values are the critical values for the given significance level. Ifalpha
is a list, the keys are the \(r_2\) values and the values are an array of critical values for each significance level. Ifalpha
is a float, the keys are the \(r_2\) values and the values are the critical values for the given significance level.- Return type:
- Raises:
Notes
This function uses
functools.lru_cache
to cache the results for faster access. So after the first call, subsequent calls with the same parameters will utilize the cached results to reduce computation time.
- find_bubbles(alpha: float, fast: Literal[True]) ndarray[tuple[int, ...], dtype[_ScalarType_co]] [source]#
- find_bubbles(alpha: float, fast: Literal[False], nreps: int, nobs: int | None) ndarray[tuple[int, ...], dtype[_ScalarType_co]]
Identifies the bubble periods in the time series.
- Parameters:
alpha¶ (float) – Significance level \(\alpha\). Defaults to 0.05.
fast¶ (bool, optional) – If
True
(Default), uses tabulated critical values. Otherwise, simulates them usingnreps
simulations of sizenobs
.nreps¶ (int, optional) – Number of simulations (required if
fast
isTrue
).nobs¶ (int | None, optional) – Number of observations (used if
fast
isFalse
). Defaults to None, setting it topsytest.PSYBubbles.nobs
.
- Returns:
bubbles – Array with 2 columns. The first element contains the start time of the bubble, given by the index of the data. The second element contains the end time of the data. If the bubble has not ended by the end of the data, it is set to
None
.- Return type:
Notes
The start of a bubble is defined as the point \(r_s\) where the test statistic exceeds the critical value:
\[\text{BSADF}_{r_s} > \text{CV}_{r_s, \alpha}\]And the end of a bubble is defined as the point \(r_e > r_s\) where the test statistic falls below the critical value:
\[\text{BSADF}_{r_e} < \text{CV}_{r_e, \alpha}\]Following the original paper, we require that the end period be at least \(\text{minlength}\) periods after the start period. Therefore, if a bubble start is detected at \(r_s\), but ends before \(r_s + \text{minlength}\), it is disregarded.
- classmethod from_pandas(data: Series, minwindow: int | None = None, minlength: int | None = None, lagmax: int = 0, rstep: float | None = None) PSYBubbles [source]#
Creates a PSYBubbles object from a
pandas.Series
.- Parameters:
data¶ (
pandas.Series
of dtypenumpy.float64
) – Time series data.minwindow¶ (int | None, optional) – Minimum window size to calculate the test. Defaults to r0_default.
lagmax¶ (int, optional) – Maximum lag \(k_{\max}\). Defaults to 0.
rstep¶ (float | None, optional) – Step size \(r_{\text{step}}\). Defaults to \(1/n\).
- Returns:
psybubbles – An instance of the PSYBubbles class with
psytest.bubbles.PSYBubbles.index
set to the providedpandas.Series.index
.- Return type:
- Raises:
TypeError – If
data
is not apandas.Series
or ifdata
dtype is not of typenumpy.float64
.