library("quantmod") # for downloading fx data library("pracma") # for hurst exponent library("vrtest") # variance ratio test library("tseries") # for adf test library("fUnitRoots") # for adf test ## first lets fetch USD/CAD data for last 5 years getFX("UDSCAD") usdCad=unclass(USDCAD) # unwrap price column # estimate log return n=length(usdCad) usdcadLog=log(usdCad[1:n]) ## First use Augmented Dickey–Fuller Test (adf.test) to test USD/CAD is statationary >adfTest(usdCad, lag=1) Title: Augmented Dickey-Fuller Test Test Results: PARAMETER: Lag Order: 1 STATISTIC: Dickey-Fuller: 0.2556 P VALUE: 0.6978 Description: Sun Nov 03 16:47:27 2013 by user: deniz ## As you see above, null hypothesis (unit root) can not be rejected with p-value ~70 % ## So we demonstrated it is not stationary. So if there is a trend or mean reverting. Hurst exponent (H) can be used for this purpose (Note Hursy exponent relies on that random walk diffuse in proportion to square root of time.). #Value of H can be interpreted such as: #H=0.5:Brownian motion (Random walk) #H<0.5:Mean reverting #H>0.5:Trending > hurst(usdcadLog) Hurst exponent [1] 0.9976377 #So, USDCAD is in trending phase. ## Another way to test stationary is to use V: > vrtest::Auto.VR(usdcadLog) [1] 83.37723 > vrtest::Lo.Mac(usdcadLog,c(2,4,10)) $Stats M1 M2 k=2 22.10668 15.98633 k=4 35.03888 25.46031 k=10 56.21660 41.58861 ## Another way to analyse stationary condition is via linear regression in which we will try to establish if there is a link between data and diff(data(t-1)) >deltaUsdcadLog=c(0,usdcadLog[2:n]-usdcadLog[1:n-1]) > r=lm(deltaUsdcadLog ~ usdcadLog) > summary(r) Call: lm(formula = deltaUsdcadLog ~ usdcadLog) Residuals: Min 1Q Median 3Q Max -0.0121267 -0.0013094 -0.0000982 0.0012327 0.0103982 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -7.133e-05 1.306e-04 -0.546 0.5853 usdcadLog 8.772e-03 5.234e-03 1.676 0.0944 . --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.002485 on 498 degrees of freedom Multiple R-squared: 0.005608, Adjusted R-squared: 0.003611 F-statistic: 2.808 on 1 and 498 DF, p-value: 0.0944 > r$coefficients[2] usdcadLog 0.008771754 ## Coefficient (Beta) gives clue about if there is mean reverting. If it is negative, there is a mean reverting. As you see above, it is positive, therefore as we already concluded before, it is trending. If it was negative, we would use following to find out half life of mean revertion: >-log(2)/r$coefficients[2]
Sunday, November 03, 2013
Stationary Tests : Augmented Dickey–Fuller (ADF), Hurst Exponent, Variance Ratio (VRTest) of Time Series within R
Several trading strategies (momentum, mean reverting, ...) are based on if data is stationary or not. In this text, i demonstrate how to test it statistically.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment