Important: You only need to type the quoted commands in R console

Content

  1. GARCH Simulation
  2. Fit GARCH Model
  3. IGARCH
  4. GARCH-M
  5. TGARCH

1. GARCH Simulation

  • Simulation
require(fGarch)
## Loading required package: fGarch
## Loading required package: timeDate
## Loading required package: timeSeries
## Loading required package: fBasics
N = 200
x.vec = as.vector(garchSim(garchSpec(rseed = 1985), n = N)[,1])
garchFit(~ garch(1,1), data = x.vec, trace = FALSE)
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(1, 1), data = x.vec, trace = FALSE) 
## 
## Mean and Variance Equation:
##  data ~ garch(1, 1)
## <environment: 0x000000001d336340>
##  [data = x.vec]
## 
## Conditional Distribution:
##  norm 
## 
## Coefficient(s):
##         mu       omega      alpha1       beta1  
## 3.5418e-05  1.0819e-06  8.8855e-02  8.1200e-01  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##         Estimate  Std. Error  t value Pr(>|t|)    
## mu     3.542e-05   2.183e-04    0.162    0.871    
## omega  1.082e-06   1.051e-06    1.030    0.303    
## alpha1 8.885e-02   5.450e-02    1.630    0.103    
## beta1  8.120e-01   1.242e-01    6.538 6.25e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  861.9494    normalized:  4.309747 
## 
## Description:
##  Mon Apr 15 15:20:21 2019 by user: Sirius

2. Fit GARCH Model

  • Get data
require(quantmod)
## Loading required package: quantmod
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following object is masked from 'package:timeSeries':
## 
##     time<-
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## 
## Attaching package: 'TTR'
## The following object is masked from 'package:fBasics':
## 
##     volatility
## Version 0.4-0 included new data defaults. See ?getSymbols.
getSymbols("SP")
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## [1] "SP"
sp5 = diff(log(SP$SP.Adjusted))[-1]
  • Conduct test
acf(sp5)

Box.test(sp5,lag=12,type='Ljung')  # Some serial correlations
## 
##  Box-Ljung test
## 
## data:  sp5
## X-squared = 54.049, df = 12, p-value = 2.678e-07
pacf(sp5)

(n1 = arima(sp5,order=c(1,0,0)))
## 
## Call:
## arima(x = sp5, order = c(1, 0, 0))
## 
## Coefficients:
##           ar1  intercept
##       -0.1045      2e-04
## s.e.   0.0179      3e-04
## 
## sigma^2 estimated as 0.0004523:  log likelihood = 7513.9,  aic = -15021.81
tsdiag(n1)

Box.test(n1$residuals^2,lag=12,type='Ljung')  # confirm the existence of ARCH effect
## 
##  Box-Ljung test
## 
## data:  n1$residuals^2
## X-squared = 930.82, df = 12, p-value < 2.2e-16
  • Model fitting
n2 = garchFit(~arma(1,0)+garch(1,1),data=sp5,trace=F) #AR(1) for the mean equation and GARCH(1,1) for the volatility equation
summary(n2)
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~arma(1, 0) + garch(1, 1), data = sp5, trace = F) 
## 
## Mean and Variance Equation:
##  data ~ arma(1, 0) + garch(1, 1)
## <environment: 0x000000001c7adbc0>
##  [data = sp5]
## 
## Conditional Distribution:
##  norm 
## 
## Coefficient(s):
##          mu          ar1        omega       alpha1        beta1  
##  3.2979e-04  -6.3167e-02   5.2009e-06   4.1160e-02   9.4593e-01  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##          Estimate  Std. Error  t value Pr(>|t|)    
## mu      3.298e-04   3.252e-04    1.014 0.310448    
## ar1    -6.317e-02   1.880e-02   -3.360 0.000780 ***
## omega   5.201e-06   1.440e-06    3.612 0.000304 ***
## alpha1  4.116e-02   6.569e-03    6.265 3.72e-10 ***
## beta1   9.459e-01   9.069e-03  104.308  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  7801.012    normalized:  2.524599 
## 
## Description:
##  Mon Apr 15 15:20:27 2019 by user: Sirius 
## 
## 
## Standardised Residuals Tests:
##                                 Statistic p-Value   
##  Jarque-Bera Test   R    Chi^2  706.7697  0         
##  Shapiro-Wilk Test  R    W      0.9776186 0         
##  Ljung-Box Test     R    Q(10)  13.14701  0.2155682 
##  Ljung-Box Test     R    Q(15)  27.39143  0.02570426
##  Ljung-Box Test     R    Q(20)  35.19048  0.01911293
##  Ljung-Box Test     R^2  Q(10)  16.89577  0.07670239
##  Ljung-Box Test     R^2  Q(15)  18.53039  0.2358083 
##  Ljung-Box Test     R^2  Q(20)  24.97751  0.202293  
##  LM Arch Test       R    TR^2   17.11173  0.1454427 
## 
## Information Criterion Statistics:
##       AIC       BIC       SIC      HQIC 
## -5.045963 -5.036196 -5.045968 -5.042455
predict(n2,2)
##    meanForecast  meanError standardDeviation
## 1 -0.0004381447 0.01566221        0.01566221
## 2  0.0003574698 0.01575810        0.01572701
n3 = garchFit(~garch(1,1),data=sp5,trace=F)
summary(n3)
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(1, 1), data = sp5, trace = F) 
## 
## Mean and Variance Equation:
##  data ~ garch(1, 1)
## <environment: 0x000000001d061c18>
##  [data = sp5]
## 
## Conditional Distribution:
##  norm 
## 
## Coefficient(s):
##         mu       omega      alpha1       beta1  
## 3.0493e-04  5.4448e-06  4.3042e-02  9.4352e-01  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##         Estimate  Std. Error  t value Pr(>|t|)    
## mu     3.049e-04   3.244e-04    0.940 0.347253    
## omega  5.445e-06   1.500e-06    3.630 0.000284 ***
## alpha1 4.304e-02   6.902e-03    6.236  4.5e-10 ***
## beta1  9.435e-01   9.503e-03   99.285  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  7792.69    normalized:  2.521906 
## 
## Description:
##  Mon Apr 15 15:20:27 2019 by user: Sirius 
## 
## 
## Standardised Residuals Tests:
##                                 Statistic p-Value    
##  Jarque-Bera Test   R    Chi^2  704.8031  0          
##  Shapiro-Wilk Test  R    W      0.9775696 0          
##  Ljung-Box Test     R    Q(10)  24.19706  0.00709417 
##  Ljung-Box Test     R    Q(15)  36.63479  0.001430508
##  Ljung-Box Test     R    Q(20)  44.59722  0.001250952
##  Ljung-Box Test     R^2  Q(10)  17.1896   0.07027239 
##  Ljung-Box Test     R^2  Q(15)  18.62921  0.2310304  
##  Ljung-Box Test     R^2  Q(20)  25.1777   0.1947172  
##  LM Arch Test       R    TR^2   17.47188  0.1326921  
## 
## Information Criterion Statistics:
##       AIC       BIC       SIC      HQIC 
## -5.041224 -5.033410 -5.041227 -5.038417

3. IGARCH

require(rugarch)
## Loading required package: rugarch
## Loading required package: parallel
## 
## Attaching package: 'rugarch'
## The following object is masked from 'package:stats':
## 
##     sigma
spec3 = ugarchspec(variance.model=list(model="iGARCH", garchOrder=c(1,1)),
                     mean.model=list(armaOrder=c(0,0), include.mean=FALSE),
                     distribution.model="norm", fixed.pars=list(omega=0))
fit = ugarchfit(spec3, data = sp5)
summary(fit) # note: for S4 Mode, use "@" to find inherited class rather than "$"
##    Length     Class      Mode 
##         1 uGARCHfit        S4
fit
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : iGARCH(1,1)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## omega   0.000000          NA       NA       NA
## alpha1  0.030173    0.003339   9.0361        0
## beta1   0.969827          NA       NA       NA
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## omega   0.000000          NA       NA       NA
## alpha1  0.030173    0.006676   4.5194    6e-06
## beta1   0.969827          NA       NA       NA
## 
## LogLikelihood : 7769.698 
## 
## Information Criteria
## ------------------------------------
##                     
## Akaike       -5.0283
## Bayes        -5.0263
## Shibata      -5.0283
## Hannan-Quinn -5.0276
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic  p-value
## Lag[1]                      7.277 0.006984
## Lag[2*(p+q)+(p+q)-1][2]     7.965 0.006608
## Lag[4*(p+q)+(p+q)-1][5]     9.387 0.013205
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic   p-value
## Lag[1]                      18.94 1.349e-05
## Lag[2*(p+q)+(p+q)-1][5]     19.78 2.228e-05
## Lag[4*(p+q)+(p+q)-1][9]     21.32 9.162e-05
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]   0.03497 0.500 2.000  0.8517
## ARCH Lag[5]   1.94723 1.440 1.667  0.4830
## ARCH Lag[7]   2.94724 2.315 1.543  0.5236
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  0.5388
## Individual Statistics:             
## alpha1 0.5388
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          0.353 0.47 0.748
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias           0.6553 0.5123    
## Negative Sign Bias  1.6307 0.1031    
## Positive Sign Bias  1.0564 0.2909    
## Joint Effect        5.6994 0.1272    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     120.4    9.463e-17
## 2    30     128.4    1.801e-14
## 3    40     160.0    1.375e-16
## 4    50     160.6    8.744e-14
## 
## 
## Elapsed time : 0.8769951

4. GARCH-M

spec3 = ugarchspec(variance.model=list(model="sGARCH", garchOrder=c(1,1)),
                     mean.model=list(armaOrder=c(0,0),include.mean=TRUE,archm=TRUE,archpow=2),
                     distribution.model="norm")
fit = ugarchfit(spec3, data = sp5*100)
fit
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : sGARCH(1,1)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error    t value Pr(>|t|)
## mu     -0.004653    0.067842  -0.068584 0.945321
## archm   0.010490    0.017807   0.589097 0.555796
## omega   0.054695    0.014558   3.756910 0.000172
## alpha1  0.043122    0.006563   6.570270 0.000000
## beta1   0.943370    0.008992 104.907046 0.000000
## 
## Robust Standard Errors:
##         Estimate  Std. Error   t value Pr(>|t|)
## mu     -0.004653    0.065530 -0.071003 0.943395
## archm   0.010490    0.015570  0.673755 0.500467
## omega   0.054695    0.025015  2.186462 0.028782
## alpha1  0.043122    0.011688  3.689482 0.000225
## beta1   0.943370    0.015903 59.319254 0.000000
## 
## LogLikelihood : -6437.104 
## 
## Information Criteria
## ------------------------------------
##                    
## Akaike       4.1696
## Bayes        4.1794
## Shibata      4.1696
## Hannan-Quinn 4.1732
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic  p-value
## Lag[1]                      10.19 0.001412
## Lag[2*(p+q)+(p+q)-1][2]     10.96 0.001067
## Lag[4*(p+q)+(p+q)-1][5]     12.33 0.002281
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic  p-value
## Lag[1]                      9.956 0.001603
## Lag[2*(p+q)+(p+q)-1][5]    10.623 0.006365
## Lag[4*(p+q)+(p+q)-1][9]    12.288 0.015592
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]   0.06805 0.500 2.000  0.7942
## ARCH Lag[5]   1.46771 1.440 1.667  0.6008
## ARCH Lag[7]   2.73781 2.315 1.543  0.5644
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  1.5089
## Individual Statistics:              
## mu     0.07563
## archm  0.07407
## omega  0.18380
## alpha1 0.86425
## beta1  0.47027
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.28 1.47 1.88
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value    prob sig
## Sign Bias           0.4368 0.66230    
## Negative Sign Bias  1.8213 0.06866   *
## Positive Sign Bias  1.4248 0.15430    
## Joint Effect        6.1187 0.10598    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     124.0    1.979e-17
## 2    30     148.4    5.536e-18
## 3    40     166.0    1.336e-17
## 4    50     180.9    5.322e-17
## 
## 
## Elapsed time : 1.937701

5. TGARCH

m7 = garchFit(~aparch(1,1),data=sp5,trace=F,delta=2,include.delta=F)
summary(m7)
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~aparch(1, 1), data = sp5, delta = 2, include.delta = F, 
##     trace = F) 
## 
## Mean and Variance Equation:
##  data ~ aparch(1, 1)
## <environment: 0x000000002c7646a8>
##  [data = sp5]
## 
## Conditional Distribution:
##  norm 
## 
## Coefficient(s):
##         mu       omega      alpha1      gamma1       beta1  
## 1.1827e-04  5.2253e-06  4.0730e-02  2.2567e-01  9.4438e-01  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##         Estimate  Std. Error  t value Pr(>|t|)    
## mu     1.183e-04   3.279e-04    0.361  0.71831    
## omega  5.225e-06   1.328e-06    3.936 8.28e-05 ***
## alpha1 4.073e-02   6.295e-03    6.470 9.77e-11 ***
## gamma1 2.257e-01   7.566e-02    2.983  0.00286 ** 
## beta1  9.444e-01   8.352e-03  113.073  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  7798.068    normalized:  2.523647 
## 
## Description:
##  Mon Apr 15 15:20:33 2019 by user: Sirius 
## 
## 
## Standardised Residuals Tests:
##                                 Statistic p-Value    
##  Jarque-Bera Test   R    Chi^2  652.4988  0          
##  Shapiro-Wilk Test  R    W      0.9784191 0          
##  Ljung-Box Test     R    Q(10)  24.4355   0.00652466 
##  Ljung-Box Test     R    Q(15)  36.3757   0.00155993 
##  Ljung-Box Test     R    Q(20)  44.53136  0.001276801
##  Ljung-Box Test     R^2  Q(10)  15.2933   0.121729   
##  Ljung-Box Test     R^2  Q(15)  17.67024  0.2803989  
##  Ljung-Box Test     R^2  Q(20)  24.18837  0.2342605  
##  LM Arch Test       R    TR^2   16.04755  0.1890679  
## 
## Information Criterion Statistics:
##       AIC       BIC       SIC      HQIC 
## -5.044057 -5.034290 -5.044062 -5.040549
#plot(m7)

m8 = garchFit(~aparch(1,1),data=sp5,trace=F,delta=2,include.delta=F,cond.dist="sstd")
summary(m8)
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~aparch(1, 1), data = sp5, delta = 2, cond.dist = "sstd", 
##     include.delta = F, trace = F) 
## 
## Mean and Variance Equation:
##  data ~ aparch(1, 1)
## <environment: 0x000000002b0f1fc0>
##  [data = sp5]
## 
## Conditional Distribution:
##  sstd 
## 
## Coefficient(s):
##         mu       omega      alpha1      gamma1       beta1        skew  
## 1.8035e-04  3.7584e-06  5.1556e-02  1.3490e-01  9.4129e-01  9.8794e-01  
##      shape  
## 4.9499e+00  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##         Estimate  Std. Error  t value Pr(>|t|)    
## mu     1.804e-04   3.204e-04    0.563   0.5734    
## omega  3.758e-06   1.585e-06    2.372   0.0177 *  
## alpha1 5.156e-02   9.748e-03    5.289 1.23e-07 ***
## gamma1 1.349e-01   7.803e-02    1.729   0.0839 .  
## beta1  9.413e-01   1.106e-02   85.142  < 2e-16 ***
## skew   9.879e-01   2.390e-02   41.344  < 2e-16 ***
## shape  4.950e+00   4.570e-01   10.832  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  7907.606    normalized:  2.559096 
## 
## Description:
##  Mon Apr 15 15:20:34 2019 by user: Sirius 
## 
## 
## Standardised Residuals Tests:
##                                 Statistic p-Value    
##  Jarque-Bera Test   R    Chi^2  815.5246  0          
##  Shapiro-Wilk Test  R    W      0.9765433 0          
##  Ljung-Box Test     R    Q(10)  22.70288  0.01189805 
##  Ljung-Box Test     R    Q(15)  34.187    0.003205329
##  Ljung-Box Test     R    Q(20)  41.51742  0.003194883
##  Ljung-Box Test     R^2  Q(10)  13.55453  0.1942974  
##  Ljung-Box Test     R^2  Q(15)  15.91041  0.3880305  
##  Ljung-Box Test     R^2  Q(20)  24.02615  0.2412519  
##  LM Arch Test       R    TR^2   14.36733  0.2778687  
## 
## Information Criterion Statistics:
##       AIC       BIC       SIC      HQIC 
## -5.113661 -5.099987 -5.113671 -5.108750
#plot(m8)
predict(m8,6)
##   meanForecast  meanError standardDeviation
## 1 0.0001803532 0.01561807        0.01561807
## 2 0.0001803532 0.01569080        0.01569080
## 3 0.0001803532 0.01576276        0.01576276
## 4 0.0001803532 0.01583395        0.01583395
## 5 0.0001803532 0.01590440        0.01590440
## 6 0.0001803532 0.01597412        0.01597412