arbitragelab.optimal_mean_reversion.cir_model
Module Contents
Classes
This class implements the algorithms for optimal stopping and optimal switching problems for |
- class CoxIngersollRoss
Bases:
arbitragelab.optimal_mean_reversion.ou_model.OrnsteinUhlenbeck
This class implements the algorithms for optimal stopping and optimal switching problems for assets with mean-reverting tendencies based on the Cox-Ingersoll-Ross model mentioned in the following publication:’Tim Leung and Xin Li Optimal Mean reversion Trading: Mathematical Analysis and Practical Applications(November 26, 2015)’ <https://www.amazon.com/Optimal-Mean-Reversion-Trading-Mathematical/dp/9814725919>`_
Constructing a portfolio with mean-reverting properties is usually attempted by simultaneously taking a position in two highly correlated or co-moving assets and is labeled as “pairs trading”. One of the most important problems faced by investors is to determine when to open and close a position.
Optimal stopping problem is established on the premise of maximizing the output of one optimal enter-exit pair of trades, optimal switching, on the other hand, aims to maximize the cumulative reward of an infinite number of trades made at respective optimal levels.
To find the liquidation and entry price levels for both optimal switching and optimal stopping for each approach we formulate an optimal double-stopping problem that gives the optimal entry and exit level rules.
- fit(data: pandas.DataFrame, data_frequency: str, discount_rate: tuple, transaction_cost: tuple, start: str = None, end: str = None, stop_loss: float = None)
Fits the Cox-Ingersoll-Ross model to given data and assigns the discount rates, transaction costs and stop-loss level for further exit or entry-level calculation.
- Parameters:
data – (np.array/pd.DataFrame) An array with time series of portfolio prices / An array with time series of of two assets prices. The dimensions should be either nx1 or nx2.
data_frequency – (str) Data frequency [“D” - daily, “M” - monthly, “Y” - yearly].
discount_rate – (float/tuple) A discount rate either for both entry and exit time or a list/tuple of discount rates with exit rate and entry rate in respective order.
transaction_cost – (float/tuple) A transaction cost either for both entry and exit time or a list/tuple of transaction costs with exit cost and entry cost in respective order.
start – (Datetime) A date from which you want your training data to start.
end – (Datetime) A date at which you want your training data to end.
stop_loss – (float/int) A stop-loss level - the position is assumed to be closed immediately upon reaching this pre-defined price level.
- fit_to_portfolio(data: numpy.array = None, start: str = None, end: str = None)
Fits the Cox-Ingersoll-Ross model to time series for portfolio prices.
- Parameters:
data – (np.array) All given prices of two assets to construct a portfolio from.
start – (Datetime) A date from which you want your training data to start.
end – (Datetime) A date at which you want your training data to end.
- fit_to_assets(data: numpy.array = None, start: str = None, end: str = None)
Creates the optimal portfolio in terms of the Cox-Ingersoll-Ross model from two given time series for asset prices and fits the values of the model’s parameters. (p.13)
- Parameters:
data – (np.array) All given prices of two assets to construct a portfolio from.
start – (Datetime) A date from which you want your training data to start.
end – (Datetime) A date at which you want your training data to end.
- cir_model_simulation(n: int, theta_given: float = None, mu_given: float = None, sigma_given: float = None, delta_t_given: float = None) numpy.array
Simulates values of a CIR process with given parameters or parameters fitted to our data.
- Parameters:
n – (int) Number of simulated values.
theta_given – (float) Long-term mean.
mu_given – (float) Mean reversion speed.
sigma_given – (float) The amplitude of randomness in the system.
delta_t_given – (float) Delta between observations, calculated in years.
- Returns:
(np.array) simulated portfolio prices.
- optimal_coefficients(portfolio: numpy.array) tuple
Finds the optimal CIR model coefficients depending on the portfolio prices time series given. (p.13)
- Parameters:
portfolio – (np.array) Portfolio prices.
- Returns:
(tuple) Optimal parameters (theta, mu, sigma_square, max_LL).
- optimal_liquidation_level() float
Calculates the optimal liquidation portfolio level. (p.85)
- Returns:
(float) Optimal liquidation portfolio level.
- optimal_entry_level() float
Calculates the optimal entry portfolio level. (p.86)
- Returns:
(float) Optimal entry portfolio level.
- optimal_switching_levels() numpy.array
Calculates the optimal switching levels.
- Returns:
(np.array) Optimal switching levels.
- cir_plot_levels(data: pandas.DataFrame, switching: bool = False) matplotlib.figure.Figure
Plots the found optimal exit and entry levels on the graph alongside with the given data.
- Parameters:
data – (np.array/pd.DataFrame) Time series of portfolio prices.
switching – (bool) A flag whether to take stop-loss level into account when showcasing the results.
- Returns:
(plt.Figure) Figure with optimal exit and entry levels.
- cir_description(switching: bool = False) pandas.Series
Returns all the general parameters of the model, training interval timestamps if provided, the goodness of fit, allocated trading costs and discount rates, which stands for the optimal ratio between two assets in the created portfolio, default optimal levels calculated. If re-entering the market is optimal shows optimal switching levels.
- Parameters:
switching – (bool) Flag that signals whether to output switching data.
- Returns:
(pd.Series) Summary data for all model parameters and optimal levels.
- static portfolio_from_prices(prices, b_variable)
Constructs a portfolio based on two given asset prices and the relative amount of investment for one of them.
- Parameters:
prices – (np.array) An array of prices of the two assets used to create a portfolio.
b_variable – (float) A coefficient representing the investment. into the second asset, investing into the first one equals one.
- Returns:
(np.array) Portfolio prices. (p. 11)
- half_life()
Returns the half-life of the fitted OU process. Half-life stands for the average time that it takes for the process to revert to its long term mean on a half of its initial deviation.
- Returns:
(float) Half-life of the fitted OU process
- ou_model_simulation(n, theta_given=None, mu_given=None, sigma_given=None, delta_t_given=None)
Simulates values of an OU process with given parameters or parameters fitted to our data
- Parameters:
n – (int) Number of simulated values.
theta_given – (float) Long-term mean.
mu_given – (float) Mean revesion speed.
sigma_given – (float) The amplitude of randomness in the system.
delta_t_given – (float) Delta between observations, calculated in years.
- Returns:
(np.array) simulated portfolio prices.
- check_fit()
Shows the parameters of the OU model fitted to the data and parameters achieved from the simulated process with the fitted coefficients for further assessment. The main incentive is to have simulated max log-likelihood (mll) function close to a fitted one. If the simulated mll is much greater it means that data provided to the model is not good enough to be modeled by an OU process.
Note: The check can occasionally return numbers significantly different from the fitted mll due to the random nature of the simulated process and the possibility of outliers. It is advised to perform the check multiple times and if it’s consistently showing very different values of mll you might suspect the unsuitability of your data.
- Returns:
(pd.Dataframe) Values for fitted and simulated OU model parameters
- description()
Returns all the general parameters of the model, training interval timestamps if provided, the goodness of fit, allocated trading costs and discount rates, stop-loss level, beta, which stands for the optimal ratio between two assets in the created portfolio, and optimal levels calculated. If the stop-loss level was given optimal levels that account for stop-loss would be added to the list.
- Returns:
(pd.Series) Summary data for all model parameters and optimal levels.
- plot_levels(data, stop_loss=False)
Plots the found optimal exit and entry levels on the graph alongside with the given data.
- Parameters:
data – (np.array/pd.DataFrame) Time series of portfolio prices / time series of of two assets prices of size (n x 2).
stop_loss – (bool) A flag whether to take stop-loss level into account. when showcasing the results.
- Returns:
(plt.Figure) Figure with optimal exit and entry levels.
- V(price)
Calculates the expected discounted value of liquidation of the position. (p.23)
- Parameters:
price – (float) Portfolio value.
- Returns:
(float) Expected discounted liquidation value.
- V_sl(price)
Calculates the expected discounted value of liquidation of the position considering the stop-loss level. (p. 31)
- Parameters:
price – (float) Portfolio value.
- Returns:
(float) Expected discounted value of liquidating the position considering the stop-loss level.
- optimal_liquidation_level_stop_loss()
Calculates the optimal liquidation portfolio level considering the stop-loss level. (p.31)
- Returns:
(float) Optimal liquidation portfolio level considering the stop-loss.
- optimal_entry_interval_stop_loss()
Calculates the optimal entry portfolio interval considering the stop-loss level. (p.35)
- Returns:
(tuple) Optimal entry portfolio interval considering the stop-loss.