arbitragelab.cointegration_approach.coint_sim
This module allows simulation of cointegrated time series pairs.
Module Contents
Classes
This is a class that can be used to simulate cointegrated price series pairs. |
- class CointegrationSimulation(ts_num: int, ts_length: int)
This is a class that can be used to simulate cointegrated price series pairs.
The class will generate a price first-order difference time series defined by an AR(1) process, a cointegration error series defined by an AR(1) process, and calculate the other price series based on the cointegration equation.
- static initialize_params() Tuple[dict, dict]
Initialize the default parameters for the first-order difference of share S2 price series and cointegration error.
- Returns:
(dict, dict) Necessary parameters for share S2 price simulation; necessary parameters for cointegration error simulation.
- get_price_params() dict
Getter for price simulation parameters.
- Returns:
price_params: (dict) Necessary parameters for share S2 price simulation.
- get_coint_params() dict
Getter for cointegration error simulation parameters.
- Returns:
coint_params: (dict) Necessary parameters for cointegration error simulation.
- set_price_params(param: str, value: float)
Setter for price simulation parameters.
Change one specific parameter to a designated value. Possible parameters are [“ar_coeff”, “white_noise_var”, “constant_trend”].
- Parameters:
param – (str) Parameter dictionary key.
value – (float) Parameter value.
- set_coint_params(param: str, value: float)
Setter for cointegration error simulation parameters.
Change one specific parameter to a designated value. Possible parameters are [“ar_coeff”, “white_noise_var”, “constant_trend”, “beta”].
- Parameters:
param – (str) Parameter dictionary key.
value – (float) Parameter value.
- load_params(params: dict, target: str = 'price')
Setter for simulation parameters.
Change the entire parameter sets by loading the dictionary.
- Parameters:
params – (dict) Parameter dictionary.
target – (str) Indicate which parameter to load. Possible values are “price” and “coint”.
- simulate_ar(params: dict, burn_in: int = 50, use_statsmodels: bool = True) numpy.array
Simulate an AR(1) process without using the statsmodels package. The AR(1) process is defined as the following recurrence relation.
\[y_t = \mu + \phi y_{t-1} + e_t, \quad e_t \sim N(0, \sigma^2) \qquad \mathrm{i.i.d}\]- Parameters:
params – (dict) A parameter dictionary containing AR(1) coefficient, constant trend, and white noise variance.
burn_in – (int) The amount of data used to burn in the process.
use_statsmodel – (bool) If True, use statsmodels; otherwise, directly calculate recurrence.
- Returns:
(np.array) ts_num simulated series generated.
- simulate_coint(initial_price: float, use_statsmodels: bool = False) Tuple[numpy.array, numpy.array, numpy.array]
Generate cointegrated price series and cointegration error series.
- Parameters:
initial_price – (float) Starting price of share S2.
use_statsmodels – (bool) Use statsmodels API or use raw method. If True, then statsmodels API will be used.
- Returns:
(np.array, np.array, np.array) Price series of share S1, price series of share S2, and cointegration error.
- verify_ar(price_matrix: numpy.array) Tuple[float, float | None]
Test function to confirm that the simulated price series is an AR(1) process.
- Parameters:
price_matrix – (np.array) A matrix where each column is a hypothetical AR(1) process.
- Returns:
(float, float) The mean AR(1) coefficient of the process; the standard deviation of AR(1) coefficient of the process.
- verify_coint(price_series_x: numpy.array, price_series_y: numpy.array, x_name: str = 'Share S1', y_name: str = 'Share S2') Tuple[float, float | None]
Use the Engle-Granger test to verify if the simulated series are cointegrated.
- Parameters:
price_series_x – (np.array) A matrix where each column is a simulated price series of share S1.
price_series_y – (np.array) A matrix where each column is a simulated price series of share S2.
x_name – (str) Column name for share S1 column of Engle-Granger input dataframe.
y_name – (str) Column name for share S2 column of Engle-Granger input dataframe.
- Returns:
(float, float) Mean of hedge ratio; standard deviation of hedge ratio.
- plot_coint_series(series_x: numpy.array, series_y: numpy.array, coint_error: numpy.array, figw: float = 15.0, figh: float = 10.0) matplotlib.pyplot.Figure
Plot the simulated cointegrated series.
- Parameters:
series_x – (np.array) Price series of share S1
series_y – (np.array) price series of share S2
coint_error – (np.array) Cointegration error.
figw – (float) Figure width.
figh – (float) Figure height.
- Returns:
(plt.Figure) Figure with the simulated cointegrated series.