Testing stationary process and time-series in Python (using cryptos)
- ADF and KPSS tests for checking stationarity;
- Obtaining market data of cryptocurrencies;
- Discussion on statistical tests;
Hello, all!
It is important to check stationarity! |
In quantitative finance, for instance, mean-reverting models assume that determined parameter (e.g. prices, volatility) follows a stationary process. In the following, I present a code used to generate a typical stationary process by means of a Ornstein-Uhlenbach model:
$dx= (\theta - \alpha x)dt + \sigma dW$
where $\theta$ is the long-term average and $W$ denotes a Brownian motion. Hence:
Notice that the series tends to return to the mean value. Of course, this is a very interesting characteristic for trades, for example, since one has an indication of the current average adopted by the time series. However, one should always remember that the analysis presented hereafter (as well as any other statistical model) does not predict a value in the future. This is because the current regime can change at any point in time, modifying completely the parameters of the stochastic model.
Getting crypto market data
In order to show the application of the stationary tests, I will use market data of three important cryptocurrencies: Bitcoin, Ethereum, and TrueUSD. I will use the API cryptoCMD which connects to the site CoinMarketCap to obtain the data we will evaluate:The variable 'numeraire' I have created in order to take into account the possibility of getting the coin values as function of a third one (like LTC, for instance) instead of dollar.
Now that we already have the data we need, let´s move forward to the stationary tests.
Test statistics
I will show two different statistical tests. The first is the Augmented Dick-Fuller test (ADF) whose null hypothesis is the presence of a unit root in the sample of the time-series against the alternative of stationarity or trend-stationarity. The second test is the Kwiatkowski-Phillips-Schmidt-Shin test (KPSS) whose null hypothesis is the stationarity (or trend-stationarity) against the alternative of the presence of unit root. In fact, as recommended in the must-read paper of Kwiatkowski et al. published in 1992, "Testing the null hypothesis of stationarity against alternative of unit root", one should perform both kinds of test in order to fully characterize a time-series. In the case of the methods chosen in this post, a high likelihood of stationarity in the sample of a time-series will outcome from the cases in which null hypothesis is rejected in the ADF test but accepted in the KPSS test.
Quote from Kwiatkowski´s paper. |
ADF test
The ADF in Python is within the powerful package statsmodel and the implementation is quite easy:Let´s us take a look to the results of each of the coins chosen before [BTC, ETH, TUSD]:
Note that for both BTC and ETH the value of the ADF statistic is considerably higher than the critical values, even considering the higher tolerance 10%. In addition, the p-value is high. These factors indicate that for both BTC and ETH the null hypothesis of the presence of unit root should not be reject. Conversely, the results for TUSD show lower ADF statistics compared to the critical values as well as a low p-value, both indicating that we should reject the hypothesis of a unit root.
Intuitively, the result is coherent, considering that BTC and ETH behaves more similarly to random walks whereas TUSD oscillates around the value of one dollar as initially proposed in the corresponding white paper.
KPSS test
Using KPSS we directly check the null hypothesis of stationarity in the time-series, which allows to complement the indication of the ADF test. The implementation is simple:
providing us the following results:
Once again, for both BTC and ETH the statistics of the test is higher than the critical values and for TUSD the kpss statistics is lower than the critical values. These results lead us to accept the null hypothesis of stationarity for the TUSD and reject it for BTC and ETH. In order to be more certain about our analysis, I can also test BTC and ETH once again, this time with respect to the possibility of trend-stationarity, in which stationarity might be related to a mean value which decreases (or increases) linearly with time. This can be done by simply introducing the argument regression='ct' in the kpss test and the results are presented below:
For TUSD the results remain consistent, confirming by far the stationarity. For BTC, under the a typical alpha value of 5%, the low p-value for this coin suggests the rejection of the null hypothesis for this case. For ETH, the p-value of 6.8% is higher than the typical alpha of 5%, then we compare the kpss statistic against the critical values. In fact, we can only reject the null hypothesis under the highest critical value (at 10%), while using more relaxing criteria we cannot reject the hypothesis of trend stationarity for the coin ETH.
In the example present above, the results of the ADF and KPSS complement themselves allowing to us to characterize the time-series with respect to unit root and stationarity. As mentioned before, stationarity is a worthful property for quantitative trading strategies. It is worth mentioning that even if a determined asset does not behaves suitably to the strategy, one can always try different portfolios that might behave as a stationary process, even for a short duration.
I hope you enjoyed the post. Leave your comments and share.
May the Force be with you.
#=================================
Diogo de Moura Pedroso
LinkedIn: www.linkedin.com/in/diogomourapedroso
#=================================
Diogo de Moura Pedroso
LinkedIn: www.linkedin.com/in/diogomourapedroso
Comments
Post a Comment