library(fGarch)
library(tseries)
setwd("C:/Users/User/Google Drive/P_Project/volatility_garch_sd")
df <- read.fwf("textbook_example.txt", widths = 8, header = T)[,2] #load data
df <- ts(df)
plot( df , ylab = "Price", main = "Time Series of Closing Price")
#first differencing on the closing price to get returns
dax <- diff(log(df))
#preview latest 20 days returns
print( tail(dax, 20) )
## [1] 0.004987542 -0.010000083 0.005012542 0.024692613 -0.012270093
## [6] 0.031594365 0.000000000 0.000000000 -0.007202912 0.000000000
## [11] 0.011976191 0.004750603 -0.009523882 0.000000000 0.016607736
## [16] 0.007034027 0.000000000 -0.007034027 0.023256862 0.011428696
#plot the returns time series
plot(dax, xlab = "Time", main = "Time Series of Returns")
log <- capture.output({ #ignore this function (To suppress unnecessary message output ##
fit <- garchFit(~garch(1, 1), data = dax)
})
summary(fit)
Title:
GARCH Modelling
Call:
garchFit(formula = ~garch(1, 1), data = dax)
Mean and Variance Equation:
data ~ garch(1, 1)
<environment: 0x000000001673dc58>
[data = dax]
Conditional Distribution:
norm
Coefficient(s):
mu omega alpha1 beta1
4.8475e-03 1.4785e-10 1.0000e-08 9.8923e-01
Std. Errors:
based on Hessian
Error Analysis:
Estimate Std. Error t value Pr(>|t|)
mu 4.847e-03 2.648e-03 1.831 0.0671 .
omega 1.478e-10 3.177e-05 0.000 1.0000
alpha1 1.000e-08 NA NA NA
beta1 9.892e-01 8.236e-02 12.011 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Log Likelihood:
60.40333 normalized: 3.020166
Description:
Wed Apr 26 00:01:31 2017 by user: User
Standardised Residuals Tests:
Statistic p-Value
Jarque-Bera Test R Chi^2 1.443836 0.4858196
Shapiro-Wilk Test R W 0.9341599 0.185607
Ljung-Box Test R Q(10) 6.627275 0.7601013
Ljung-Box Test R Q(15) 11.36599 0.7262447
Ljung-Box Test R Q(20) NA NA
Ljung-Box Test R^2 Q(10) 4.982482 0.8923452
Ljung-Box Test R^2 Q(15) 11.12652 0.7435748
Ljung-Box Test R^2 Q(20) NA NA
LM Arch Test R TR^2 8 0.7851304
Information Criterion Statistics:
AIC BIC SIC HQIC
-5.640333 -5.441186 -5.703860 -5.601457
# predict(fit, n.ahead = 10,plot = T)
#1-day ahead volatility forecast
sd1 <- predict(fit, n.ahead = 15,plot = T)[1,3]
print(sd1)
## [1] 0.01057778
Therefore, the 1-day ahead forecast volatility is 0.0105778.
sd2 <- sd(dax)
print(sd2)
## [1] 0.01215933
The sample standard deviation of returns is 0.0121593, which is higher than the 1-day ahead forecast volatility, 0.0105778.