## ARCH GARCH type models. #library(Rmetrics) library(fBasics) intel.monthly=read.csv("MonthlyINTC.csv",header=T) intel.prices=intel.monthly$Adj.Close[length(intel.monthly$Adj.Close):1] intel.returns=returns(intel.prices)[-1] acf(intel.returns) acf(intel.returns^2) pacf(intel.returns) ## pacf(intel.returns^2) ## slight evidence (no evidence last year) centered.i.returns=intel.returns-mean(intel.returns) intel.returns= centered.i.returns Box.test((intel.returns)^2,lag=10,type="Ljung") Box-Ljung test data: (intel.returns)^2 X-squared = 11.7755, df = 10, p-value = 0.3004 # From Last year: # Box-Ljung test # #data: (intel.returns)^2 #X-squared = 11.1566, df = 10, p-value = 0.3454 ## does not show ARCH effects. ## OK let us use daily data: intel.daily=read.csv("DailyINTCfrom2000.csv",header=T) intel.prices=intel.daily$Adj.Close[length(intel.daily$Adj.Close):1] ## I am rewriting the earlier variables intel.returns=returns(intel.prices)[-1] centered.i.returns=intel.returns-mean(intel.returns) intel.returns= centered.i.returns acf(intel.returns) acf(intel.returns^2) pacf(intel.returns) ## pacf(intel.returns^2) ## Box.test((intel.returns)^2,lag=10,type="Ljung") Box-Ljung test data: (intel.returns)^2 X-squared = 78.2933, df = 10, p-value = 1.084e-12 # From last year: # Box-Ljung test # #data: (intel.returns)^2 #X-squared = 26.9663, df = 10, p-value = 0.002636 ## There are arch type effects #*********** ## Now on to ARCH type modeling ## You need to install several things to make things work. ## I am assuming that you have installed R and Rmetrics packages on your system ## What follows is obviously text with instructions - do not send to R ###### 1) Download and install Ox Console: - Address: " http://www.doornik.com/download.html#oxcons " - Click: Download Ox Console (all platforms) "The Console version of Ox may be used without paying a fee for academic research, study and teaching purposes only!" - Fill in the information and agree to the terms. - Check your email after a couple minutes, find link at bottom of message: Ex: =========================================================== *** For Ox Console downloads (all platforms), go to : http://www.doornik.com/download/..../.... =========================================================== Ox Console 4.10 for Windows 2000/NT/9x (.exe, 4.1MB), choose a download site (you can use right-click to save to installation file): # - Install 'oxcons410.exe' use the default directory C:\Program Files\OxMetr.. Finally copy the subdirectory 'Ox' to 'C:\' . - Install 'oxcons510.exe' use the default directory C:\Program Files\OxMetr.. Finally copy the subdirectory 'Ox' to 'C:\' #################################### 2) Download and install G@RCH 4.2: Create a new directory(folder) on your PC: C:\Ox\Packages [The subdirectory "packages" may exist already. In this case, you do not need to create one.] #This is tricky as it seems that the version 6 is the newest and is not free anymore. #However the old version is still available. # Go to: http://www.core.ucl.ac.be/~laurent/G@RCH/site/garch42/default.htm (this is also available from www.garch.org -the main site) Click on "Download G@RCH Console" then Download G@rch Console 4.2 [You can also download Ox console from this web page.] Download "Garchv42.zip" and unzip into "C:\Ox\Packages" #################################### 3) Installation of the interface: Download from the course webpage or from the Tsay's webpage: "GarchOxModelling.ox" (http://faculty.chicagobooth.edu/ruey.tsay/teaching/bs41202/sp2009/GarchOxModelling.ox) Then copy the file "GarchOxModelling.ox" to "C:\Ox\lib". #[This is the modified file by several people according to the info on Tsay's webpage] #################################### 4) Download a corrected version of the garchOxFit function from Kung-Sik Chan: Download from the course webpage or from the Tsay's webpage: "garchoxfit_R.txt" : A corrected version of the garchOxFit function (http://faculty.chicagobooth.edu/ruey.tsay/teaching/bs41202/sp2009/garchoxfit_R.txt) Save as... garchoxfit_R in your R work folder with personal functions/libraries; you will have to access this file from within R #################################### 5) Using garchOxFit function in R: Begin each R session with the following commands after changing your working directory: library(timeSeries) ## This is the new version of fSeries which is now obsolete #library(fSeries) ## This is from last year source("garchoxfit_R.txt") ## this is why the file you downloaded has to be accessible from within current session The function usage is: > garchOxFit function (formula.mean = ~arma(0, 0), formula.var = ~garch(1, 1), series = x, cond.dist = c("gaussian", "t", "ged", "skewed-t"), include.mean = TRUE, truncation = 100, trace = TRUE, title = NULL, description = NULL,arch.in.mean=0) For information on package 'fSeries': library(help="timeSeries") ###################################### ### NOW back to the modeling ## Very important: A GARCH(r,m) model in the textbook and S-Plus is called a GARCH(m, r) model in R. ## In other words, ARCH order is the 2nd argument in R. ## For example in the statement bellow: formula.var=~garch(0,4) specifies that there are 4 terms in r_t (or a_t in the book) -- this is an ARCH(4) model ## The logic in R is the following: formula.var=~garch(m,r) specifies a formul for variance. The first argument refers to itself (variance) and the second to another argument (returns) m1=garchOxFit(formula.mean=~arma(0,0),formula.var=~garch(0,5),series=intel.returns) m2=garchOxFit(formula.mean=~arma(0,0),formula.var=~garch(0,18),series=intel.returns) ## Why 5 and 11? recall the pacf(retuns^2) plot m3=garchOxFit(formula.mean=~arma(0,0),formula.var=~garch(0,35),series=intel.returns) ## Why 5 and 11? recall the pacf(retuns^2) plot ## ALL THE ANALYSYS THAT FOLOOWS IS FROM LAST YEAR ############# WITH CURRENT DATA IS SIMILAR BUT NOT identical. # m1=garchOxFit(formula.mean=~arma(0,0),formula.var=~garch(0,4),series=intel.returns) ### Why 4 and 8? recall the pacf(retuns^2) plot ## A whole lot of output #m2=garchOxFit(formula.mean=~arma(0,0),formula.var=~garch(0,8),series=intel.returns) ####### Some output presented here (only about the simpler model m1): ## This is from last year since I didn't feel like rewriting everything again. Important are the notions not the numbers #The output in the beginning gives detail about the estimation of the parameters mean_r mean_sigma and param in the variance formula. ******************** ** SPECIFICATIONS ** ******************** Dependent variable : X Mean Equation : ARMA (0, 0) model. No regressor in the mean Variance Equation : GARCH (0, 4) model. No regressor in the variance The distribution is a Gauss distribution. Strong convergence using numerical derivatives Log-likelihood = 631.923 Please wait : Computing the Std Errors ... Maximum Likelihood Estimation (Std.Errors based on Second derivatives) Coefficient Std.Error t-value t-prob ## These are estimated parameter values Cst(M) 0.000255 0.0012366 0.2059 0.8371 Cst(V) 3.665068 0.48093 7.621 0.0000 ## Note the warning: this parameter is in fact 0.000365068 . That does not matter for testing purposes ARCH(Alpha1) 0.000000 0.025370 0.00 1.0000 ## Note this coeff is not signif ARCH(Alpha2) 0.249774 0.087742 2.847 0.0048 ARCH(Alpha3) 0.004439 0.049236 0.09015 0.9282 ## Neither are these two. After seeing this we should try a simpler model or have a look ar the ARCH(8) ARCH(Alpha4) 0.046301 0.035716 1.296 0.1960 No. Observations : 263 No. Parameters : 6 Mean (Y) : -0.00012 Variance (Y) : 0.00051 Skewness (Y) : -0.71426 Kurtosis (Y) : 7.44730 Log Likelihood : 631.923 Alpha[1]+Beta[1]: 0.30031 Warning : To avoid numerical problems, the estimated parameter Cst(V), and its std.Error have been multiplied by 10^4. The sample mean of squared residuals was used to start recursion. The unconditional variance does not exist and/or is not positive. The conditions are alpha[0] > 0, alpha[L] + beta[L] < 1 and alpha[i] + beta[i] >= 0. => See Doornik & Ooms (2001) for more details. Estimated Parameters Vector : 0.000255; 0.000367; 0.000000; 0.249774; 0.004439; 0.046301 *************** ** FORECASTS ** *************** Number of Forecasts: 15 ### Forecasted values Horizon Mean Variance 1 0.0002546 0.0005806 2 0.0002546 2.132e-005 3 0.0002546 0.0002522 4 0.0002546 9.044e-006 5 0.0002546 8.989e-005 6 0.0002546 4.398e-006 7 0.0002546 3.416e-005 8 0.0002546 1.951e-006 9 0.0002546 1.273e-005 10 0.0002546 8.783e-007 11 0.0002546 4.8e-006 12 0.0002546 4.025e-007 13 0.0002546 1.826e-006 14 0.0002546 1.99e-007 15 0.0002546 7.159e-007 --------------- Forecasts errors measures cannot be computed because there are not enough out-of-sample observations). Elapsed Time : 0.125 seconds (or 0.00208333 minutes). *********** ** TESTS ** *********** Statistic t-Test P-Value Skewness -0.20133 1.3405 0.18008 ## No skeweness Excess Kurtosis 1.3076 4.3692 1.2468e-005 ## Excess kurtosis present Jarque-Bera 20.514 .NaN 3.5119e-005 ## variables are correlated --------------- Information Criterium (to be minimized) Akaike -4.759867 Shibata -4.760878 Schwarz -4.678373 Hannan-Quinn -4.727117 --------------- Q-Statistics on Standardized Residuals Q( 10) = 8.91978 [0.5397335] Q( 15) = 13.6747 [0.5503311] Q( 20) = 19.9176 [0.4630965] H0 : No serial correlation ==> Accept H0 when prob. is High [Q < Chisq(lag)] ## residuals are uncorrelated -------------- Q-Statistics on Squared Standardized Residuals --> P-values adjusted by 4 degree(s) of freedom Q( 10) = 8.77401 [0.1866920] Q( 15) = 12.8813 [0.3011576] Q( 20) = 14.6227 [0.5524299] H0 : No serial correlation ==> Accept H0 when prob. is High [Q < Chisq(lag)] ## squared resid are uncorrelated -------------- ARCH 1-2 test: F(2,254) = 0.25524 [0.7749] ARCH 1-5 test: F(5,248) = 0.15953 [0.9770] ARCH 1-10 test: F(10,238)= 0.77692 [0.6510] -------------- Residual-Based Diagnostic for Conditional Heteroskedasticity of Tse (2001) RBD(10) = 7.10196 [0.7157854] RBD(15) = 10.5689 [0.7825149] RBD(20) = 15.5745 [0.7426485] ------------------------------------------------ P-values in brackets Diagnostic test based on the news impact curve (EGARCH vs. GARCH) Test P-value Sign Bias t-Test 0.54121 0.58836 Negative Size Bias t-Test 0.09057 0.92784 Positive Size Bias t-Test 0.19077 0.84871 Joint Test for the Three Effects 1.08145 0.78155 --------------- Joint Statistic of the Nyblom test of stability: 14.5966 Individual Nyblom Statistics: Cst(M) 0.53329 Cst(V) 4.09157 ARCH(Alpha1) 4.31121 ARCH(Alpha2) 10.48706 ARCH(Alpha3) 0.70638 ARCH(Alpha4) 3.30461 Rem: Asymptotic 1% critical value for individual statistics = 0.75. Asymptotic 5% critical value for individual statistics = 0.47. --------------- Adjusted Pearson Chi-square Goodness-of-fit test # Cells(g) Statistic P-Value(g-1) P-Value(g-k-1) 40 46.5057 0.190739 0.059653 50 60.0038 0.134792 0.044035 60 60.7262 0.413457 0.217445 Rem.: k = 6 = # estimated parameters m3=garchOxFit(formula.mean=~arma(0,0),formula.var=~garch(0,13),series=intel.returns) par(mfcol=c(2,1)) # To plot the returns and volatilities on the sample page plot(intel.returns,type="l") plot(sqrt(m3$condvars),type="l") par(mfcol=c(1,1)) sresi=m3$residuals/sqrt(m3$condvars) # Compute standardized residuals qqnorm(sresi) # Obtain normal probability plot (ideal case: a straight line) qqline(sresi) # Impose a straight line on the QQ-plot. par(mfrow=c(2,2)) ## simpler obtained diagnostic plots plot(m3) ## Somehow this does not work anymore par(mfrow=c(1,1)) ## The m2 model is not very good, all the param are insignificant, with the 8th marginally so. so we will better the model: m1=garchOxFit(formula.mean=~arma(0,0),formula.var=~garch(0,2),series=intel.returns) ### 2 because of the output above ## from output we obtain the model: ## r_t=0.000446+ a_t; a_t=sigma_t epsilon_t; sigmasquared_t= 0.0004+ 0.238934 a^2_{t-2} ## Model checks: Q( 10) = 9.84724 [0.4539963] Q( 15) = 13.9952 [0.5258906] Q( 20) = 20.1753 [0.4470143] H0 : No serial correlation ==> Accept H0 when prob. is High [Q < Chisq(lag)] -------------- Q-Statistics on Squared Standardized Residuals --> P-values adjusted by 2 degree(s) of freedom Q( 10) = 12.2135 [0.1419274] Q( 15) = 16.3353 [0.2314961] Q( 20) = 18.1695 [0.4445370] H0 : No serial correlation ==> Accept H0 when prob. is High [Q < Chisq(lag)] ## Expected daily return is about 0.0004 but it is not signifficantly different from zero ## alpha_1^2 was zero which is certainly < 1/3 so the 4th moment exists ## This all analysis was done assuming gaussian distribution for the errors. ## We could redo with the other 2 alternatives: m1.t.errors=garchOxFit(formula.mean=~arma(0,0),formula.var=~garch(0,4),series=intel.returns,cond.dist = "t") ## not signif terms. m1.t.errors=garchOxFit(formula.mean=~arma(0,0),formula.var=~garch(0,3),series=intel.returns,cond.dist = "t") m1.t.errors=garchOxFit(formula.mean=~arma(0,0),formula.var=~garch(0,2),series=intel.returns,cond.dist = "t") m1.t.errors=garchOxFit(formula.mean=~arma(0,0),formula.var=~garch(0,1),series=intel.returns,cond.dist = "t") ## Inconclusive. None of these models are very good ## Conclusion: Using a heavy tailed distribution reduces (distroys in this case) the ARCH effect ## how about the generalized error distribution?Try it yourself. m1.ged.errors=garchOxFit(formula.mean=~arma(0,0),formula.var=~garch(0,1),series=intel.returns,cond.dist = "ged")