Libraries

library(quantmod)
library(xts)
library(PerformanceAnalytics)
library(rugarch)

GARCH Models

  • Generalized
  • AutoRegressive
  • Conditional
  • Heteroscedasticity

 
 

Data

# Apple daily prices
getSymbols("AAPL",from = "2008-01-01",to = "2022-04-12")
[1] "AAPL"
chartSeries(AAPL)

# Daily returns
return <- CalculateReturns(AAPL$AAPL.Close)
return <- return[-1]
hist(return)

chart.Histogram(return,
                methods = c('add.density', 'add.normal'),
                colorset = c('blue', 'green', 'red'))

chartSeries(return)

# Annualized volatility
chart.RollingPerformance(R = return["2008::2022"],
                         width = 252,
                         FUN = "sd.annualized",
                         scale = 252,
                         main = "Apple's yearly rolling volatility")

 
 
 

Models

 

Model 1 - sGARCH with constant mean

# 1. sGARCH model with constant mean
s <- ugarchspec(mean.model = list(armaOrder = c(0,0)),
                variance.model = list(model = "sGARCH"),
                distribution.model = 'norm')
m <- ugarchfit(data = return, spec = s)
m

*---------------------------------*
*          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.001907    0.000119  16.0364 0.000000
omega   0.000015    0.000008   1.9670 0.049182
alpha1  0.111925    0.034042   3.2879 0.001009
beta1   0.848945    0.004950 171.4986 0.000000

Robust Standard Errors:
        Estimate  Std. Error  t value Pr(>|t|)
mu      0.001907    0.002692  0.70857  0.47859
omega   0.000015    0.000085  0.17450  0.86147
alpha1  0.111925    0.389372  0.28745  0.77377
beta1   0.848945    0.128430  6.61019  0.00000

LogLikelihood : 9425.7 

Information Criteria
------------------------------------
                    
Akaike       -5.2430
Bayes        -5.2361
Shibata      -5.2430
Hannan-Quinn -5.2406

Weighted Ljung-Box Test on Standardized Residuals
------------------------------------
                        statistic p-value
Lag[1]                     0.7874  0.3749
Lag[2*(p+q)+(p+q)-1][2]    0.8855  0.5369
Lag[4*(p+q)+(p+q)-1][5]    3.8434  0.2743
d.o.f=0
H0 : No serial correlation

Weighted Ljung-Box Test on Standardized Squared Residuals
------------------------------------
                        statistic p-value
Lag[1]                     0.4318  0.5111
Lag[2*(p+q)+(p+q)-1][5]    1.8358  0.6577
Lag[4*(p+q)+(p+q)-1][9]    3.3107  0.7068
d.o.f=2

Weighted ARCH LM Tests
------------------------------------
            Statistic Shape Scale P-Value
ARCH Lag[3]     0.515 0.500 2.000  0.4730
ARCH Lag[5]     2.315 1.440 1.667  0.4058
ARCH Lag[7]     2.546 2.315 1.543  0.6030

Nyblom stability test
------------------------------------
Joint Statistic:  13.7842
Individual Statistics:             
mu     0.1172
omega  2.3145
alpha1 0.3385
beta1  0.3655

Asymptotic Critical Values (10% 5% 1%)
Joint Statistic:         1.07 1.24 1.6
Individual Statistic:    0.35 0.47 0.75

Sign Bias Test
------------------------------------
                   t-value    prob sig
Sign Bias           0.9598 0.33725    
Negative Sign Bias  1.0195 0.30803    
Positive Sign Bias  0.8238 0.41012    
Joint Effect        8.3625 0.03908  **


Adjusted Pearson Goodness-of-Fit Test:
------------------------------------
  group statistic p-value(g-1)
1    20     128.1    3.345e-18
2    30     142.6    5.924e-17
3    40     154.7    1.035e-15
4    50     157.0    3.076e-13


Elapsed time : 0.1765761 
plot(m, which = 'all')

please wait...calculating quantiles...

f <- ugarchforecast(fitORspec = m, n.ahead = 20)
plot(fitted(f))

plot(sigma(f))

Model 2 - GARCH with sstd

# 2. GARCH with sstd
s <- ugarchspec(mean.model = list(armaOrder = c(0,0)),
                variance.model = list(model = "sGARCH"),
                distribution.model = 'sstd')
m <- ugarchfit(data = return, spec = s)
m

*---------------------------------*
*          GARCH Model Fit        *
*---------------------------------*

Conditional Variance Dynamics   
-----------------------------------
GARCH Model : sGARCH(1,1)
Mean Model  : ARFIMA(0,0,0)
Distribution    : sstd 

Optimal Parameters
------------------------------------
        Estimate  Std. Error  t value Pr(>|t|)
mu      0.001569    0.000261   6.0182  0.00000
omega   0.000008    0.000005   1.5071  0.13179
alpha1  0.101071    0.018425   5.4855  0.00000
beta1   0.882488    0.016215  54.4245  0.00000
skew    1.008920    0.023274  43.3495  0.00000
shape   5.018493    0.622813   8.0578  0.00000

Robust Standard Errors:
        Estimate  Std. Error  t value Pr(>|t|)
mu      0.001569    0.000262  5.99954 0.000000
omega   0.000008    0.000022  0.37586 0.707023
alpha1  0.101071    0.063405  1.59406 0.110922
beta1   0.882488    0.044470 19.84464 0.000000
skew    1.008920    0.026011 38.78795 0.000000
shape   5.018493    2.015824  2.48955 0.012791

LogLikelihood : 9575.31 

Information Criteria
------------------------------------
                    
Akaike       -5.3252
Bayes        -5.3148
Shibata      -5.3252
Hannan-Quinn -5.3215

Weighted Ljung-Box Test on Standardized Residuals
------------------------------------
                        statistic p-value
Lag[1]                     0.8158  0.3664
Lag[2*(p+q)+(p+q)-1][2]    0.9074  0.5293
Lag[4*(p+q)+(p+q)-1][5]    4.0234  0.2512
d.o.f=0
H0 : No serial correlation

Weighted Ljung-Box Test on Standardized Squared Residuals
------------------------------------
                        statistic p-value
Lag[1]                     0.1463  0.7021
Lag[2*(p+q)+(p+q)-1][5]    1.3291  0.7818
Lag[4*(p+q)+(p+q)-1][9]    2.4764  0.8412
d.o.f=2

Weighted ARCH LM Tests
------------------------------------
            Statistic Shape Scale P-Value
ARCH Lag[3]    0.4874 0.500 2.000  0.4851
ARCH Lag[5]    1.9918 1.440 1.667  0.4731
ARCH Lag[7]    2.1879 2.315 1.543  0.6776

Nyblom stability test
------------------------------------
Joint Statistic:  6.329
Individual Statistics:             
mu     0.1189
omega  1.4391
alpha1 0.8697
beta1  0.9879
skew   0.0439
shape  1.2625

Asymptotic Critical Values (10% 5% 1%)
Joint Statistic:         1.49 1.68 2.12
Individual Statistic:    0.35 0.47 0.75

Sign Bias Test
------------------------------------
                   t-value    prob sig
Sign Bias           1.2249 0.22069    
Negative Sign Bias  0.7513 0.45253    
Positive Sign Bias  0.9037 0.36621    
Joint Effect        9.3635 0.02483  **


Adjusted Pearson Goodness-of-Fit Test:
------------------------------------
  group statistic p-value(g-1)
1    20     20.49       0.3656
2    30     38.07       0.1208
3    40     48.45       0.1427
4    50     56.47       0.2159


Elapsed time : 0.5704269 
plot(m, which = 'all')

please wait...calculating quantiles...

Model 3 - GJR-GARCH

# 3. GJR-GARCH
s <- ugarchspec(mean.model = list(armaOrder = c(0,0)),
                variance.model = list(model = "gjrGARCH"),
                distribution.model = 'sstd')
m <- ugarchfit(data = return, spec = s)
m

*---------------------------------*
*          GARCH Model Fit        *
*---------------------------------*

Conditional Variance Dynamics   
-----------------------------------
GARCH Model : gjrGARCH(1,1)
Mean Model  : ARFIMA(0,0,0)
Distribution    : sstd 

Optimal Parameters
------------------------------------
        Estimate  Std. Error  t value Pr(>|t|)
mu      0.001340    0.000250   5.3566        0
omega   0.000011    0.000001  11.3849        0
alpha1  0.034580    0.003705   9.3335        0
beta1   0.863740    0.010296  83.8882        0
gamma1  0.156976    0.022219   7.0648        0
skew    1.002508    0.022879  43.8175        0
shape   5.423806    0.421862  12.8568        0

Robust Standard Errors:
        Estimate  Std. Error  t value Pr(>|t|)
mu      0.001340    0.000267   5.0120 0.000001
omega   0.000011    0.000001   8.5961 0.000000
alpha1  0.034580    0.008938   3.8689 0.000109
beta1   0.863740    0.011114  77.7157 0.000000
gamma1  0.156976    0.026715   5.8760 0.000000
skew    1.002508    0.021652  46.3004 0.000000
shape   5.423806    0.514846  10.5348 0.000000

LogLikelihood : 9605.39 

Information Criteria
------------------------------------
                    
Akaike       -5.3413
Bayes        -5.3293
Shibata      -5.3413
Hannan-Quinn -5.3370

Weighted Ljung-Box Test on Standardized Residuals
------------------------------------
                        statistic p-value
Lag[1]                      1.551  0.2129
Lag[2*(p+q)+(p+q)-1][2]     1.569  0.3454
Lag[4*(p+q)+(p+q)-1][5]     4.349  0.2136
d.o.f=0
H0 : No serial correlation

Weighted Ljung-Box Test on Standardized Squared Residuals
------------------------------------
                        statistic p-value
Lag[1]                     0.5759  0.4479
Lag[2*(p+q)+(p+q)-1][5]    1.8611  0.6516
Lag[4*(p+q)+(p+q)-1][9]    3.0156  0.7564
d.o.f=2

Weighted ARCH LM Tests
------------------------------------
            Statistic Shape Scale P-Value
ARCH Lag[3]    0.5062 0.500 2.000  0.4768
ARCH Lag[5]    2.1756 1.440 1.667  0.4337
ARCH Lag[7]    2.4519 2.315 1.543  0.6224

Nyblom stability test
------------------------------------
Joint Statistic:  26.0128
Individual Statistics:              
mu     0.29656
omega  6.31097
alpha1 1.26489
beta1  1.29776
gamma1 1.03613
skew   0.03307
shape  1.61053

Asymptotic Critical Values (10% 5% 1%)
Joint Statistic:         1.69 1.9 2.35
Individual Statistic:    0.35 0.47 0.75

Sign Bias Test
------------------------------------
                   t-value   prob sig
Sign Bias           1.2980 0.1944    
Negative Sign Bias  0.9201 0.3576    
Positive Sign Bias  0.2156 0.8293    
Joint Effect        2.7163 0.4375    


Adjusted Pearson Goodness-of-Fit Test:
------------------------------------
  group statistic p-value(g-1)
1    20     23.21       0.2284
2    30     32.78       0.2868
3    40     38.74       0.4815
4    50     59.09       0.1532


Elapsed time : 1.112553 
plot(m, which = 'all')

please wait...calculating quantiles...

Model 4 - AR(1) GJR-GARCH

# 4. AR(1) GJR-GARCH
s <- ugarchspec(mean.model = list(armaOrder = c(1,0)),
                variance.model = list(model = "gjrGARCH"),
                distribution.model = 'sstd')
m <- ugarchfit(data = return, spec = s)
m

*---------------------------------*
*          GARCH Model Fit        *
*---------------------------------*

Conditional Variance Dynamics   
-----------------------------------
GARCH Model : gjrGARCH(1,1)
Mean Model  : ARFIMA(1,0,0)
Distribution    : sstd 

Optimal Parameters
------------------------------------
        Estimate  Std. Error  t value Pr(>|t|)
mu      0.001325    0.000253  5.23440  0.00000
ar1     0.012180    0.016871  0.72198  0.47031
omega   0.000011    0.000001 11.62291  0.00000
alpha1  0.033869    0.003776  8.96907  0.00000
beta1   0.863450    0.010291 83.90354  0.00000
gamma1  0.158980    0.022502  7.06529  0.00000
skew    1.002632    0.022907 43.76919  0.00000
shape   5.448357    0.425847 12.79418  0.00000

Robust Standard Errors:
        Estimate  Std. Error  t value Pr(>|t|)
mu      0.001325    0.000268  4.94671 0.000001
ar1     0.012180    0.015685  0.77654 0.437431
omega   0.000011    0.000001  8.77889 0.000000
alpha1  0.033869    0.008863  3.82141 0.000133
beta1   0.863450    0.011090 77.85576 0.000000
gamma1  0.158980    0.027009  5.88621 0.000000
skew    1.002632    0.021772 46.05116 0.000000
shape   5.448357    0.517558 10.52704 0.000000

LogLikelihood : 9605.649 

Information Criteria
------------------------------------
                    
Akaike       -5.3409
Bayes        -5.3272
Shibata      -5.3409
Hannan-Quinn -5.3360

Weighted Ljung-Box Test on Standardized Residuals
------------------------------------
                        statistic p-value
Lag[1]                     0.3745  0.5406
Lag[2*(p+q)+(p+q)-1][2]    0.3924  0.9845
Lag[4*(p+q)+(p+q)-1][5]    3.2007  0.3825
d.o.f=1
H0 : No serial correlation

Weighted Ljung-Box Test on Standardized Squared Residuals
------------------------------------
                        statistic p-value
Lag[1]                     0.6285  0.4279
Lag[2*(p+q)+(p+q)-1][5]    1.8839  0.6461
Lag[4*(p+q)+(p+q)-1][9]    3.0074  0.7577
d.o.f=2

Weighted ARCH LM Tests
------------------------------------
            Statistic Shape Scale P-Value
ARCH Lag[3]    0.5031 0.500 2.000  0.4781
ARCH Lag[5]    2.1078 1.440 1.667  0.4479
ARCH Lag[7]    2.3832 2.315 1.543  0.6366

Nyblom stability test
------------------------------------
Joint Statistic:  26.617
Individual Statistics:              
mu     0.29350
ar1    0.22416
omega  6.40647
alpha1 1.25673
beta1  1.29245
gamma1 1.03714
skew   0.03337
shape  1.59919

Asymptotic Critical Values (10% 5% 1%)
Joint Statistic:         1.89 2.11 2.59
Individual Statistic:    0.35 0.47 0.75

Sign Bias Test
------------------------------------
                   t-value   prob sig
Sign Bias           1.3342 0.1822    
Negative Sign Bias  0.9531 0.3406    
Positive Sign Bias  0.1807 0.8566    
Joint Effect        2.7686 0.4287    


Adjusted Pearson Goodness-of-Fit Test:
------------------------------------
  group statistic p-value(g-1)
1    20     23.48       0.2167
2    30     30.84       0.3729
3    40     42.28       0.3311
4    50     48.88       0.4781


Elapsed time : 1.105612 
plot(m, which = 'all')

please wait...calculating quantiles...

Model 5 - GJR-GARCH in mean

# 5. GJR-GARCH in mean
s <- ugarchspec(mean.model = list(armaOrder = c(0,0),
                                  archm =T,
                                  archpow = 2),
                variance.model = list(model = "gjrGARCH"),
                distribution.model = 'sstd')
m <- ugarchfit(data = return, spec = s)
m

*---------------------------------*
*          GARCH Model Fit        *
*---------------------------------*

Conditional Variance Dynamics   
-----------------------------------
GARCH Model : gjrGARCH(1,1)
Mean Model  : ARFIMA(0,0,0)
Distribution    : sstd 

Optimal Parameters
------------------------------------
        Estimate  Std. Error  t value Pr(>|t|)
mu      0.001312    0.000329  3.98771 0.000067
archm   0.117443    0.998232  0.11765 0.906344
omega   0.000011    0.000001 12.67795 0.000000
alpha1  0.034613    0.005833  5.93447 0.000000
beta1   0.863350    0.010308 83.75890 0.000000
gamma1  0.157047    0.022610  6.94606 0.000000
skew    1.002682    0.023071 43.46112 0.000000
shape   5.425795    0.431233 12.58206 0.000000

Robust Standard Errors:
        Estimate  Std. Error  t value Pr(>|t|)
mu      0.001312    0.000364  3.60264 0.000315
archm   0.117443    0.995217  0.11801 0.906062
omega   0.000011    0.000001 10.37463 0.000000
alpha1  0.034613    0.008853  3.90958 0.000092
beta1   0.863350    0.011216 76.97649 0.000000
gamma1  0.157047    0.027083  5.79884 0.000000
skew    1.002682    0.021506 46.62288 0.000000
shape   5.425795    0.499261 10.86766 0.000000

LogLikelihood : 9605.395 

Information Criteria
------------------------------------
                    
Akaike       -5.3408
Bayes        -5.3270
Shibata      -5.3408
Hannan-Quinn -5.3359

Weighted Ljung-Box Test on Standardized Residuals
------------------------------------
                        statistic p-value
Lag[1]                      1.595  0.2067
Lag[2*(p+q)+(p+q)-1][2]     1.610  0.3366
Lag[4*(p+q)+(p+q)-1][5]     4.390  0.2093
d.o.f=0
H0 : No serial correlation

Weighted Ljung-Box Test on Standardized Squared Residuals
------------------------------------
                        statistic p-value
Lag[1]                     0.5835  0.4450
Lag[2*(p+q)+(p+q)-1][5]    1.8698  0.6495
Lag[4*(p+q)+(p+q)-1][9]    3.0261  0.7546
d.o.f=2

Weighted ARCH LM Tests
------------------------------------
            Statistic Shape Scale P-Value
ARCH Lag[3]    0.5071 0.500 2.000  0.4764
ARCH Lag[5]    2.1781 1.440 1.667  0.4332
ARCH Lag[7]    2.4548 2.315 1.543  0.6218

Nyblom stability test
------------------------------------
Joint Statistic:  27.715
Individual Statistics:              
mu     0.29229
archm  0.18915
omega  6.45646
alpha1 1.26258
beta1  1.29694
gamma1 1.03261
skew   0.03298
shape  1.60982

Asymptotic Critical Values (10% 5% 1%)
Joint Statistic:         1.89 2.11 2.59
Individual Statistic:    0.35 0.47 0.75

Sign Bias Test
------------------------------------
                   t-value   prob sig
Sign Bias           1.2672 0.2052    
Negative Sign Bias  0.9175 0.3589    
Positive Sign Bias  0.2297 0.8184    
Joint Effect        2.6392 0.4507    


Adjusted Pearson Goodness-of-Fit Test:
------------------------------------
  group statistic p-value(g-1)
1    20     22.67       0.2521
2    30     32.08       0.3165
3    40     38.34       0.4997
4    50     59.98       0.1353


Elapsed time : 1.487473 
plot(m, which = 'all')

please wait...calculating quantiles...

Model 3 - GJR-GARCH is the best model.

 
 
 

Testing

# Simulation
s <- ugarchspec(mean.model = list(armaOrder = c(0,0)),
                variance.model = list(model = "gjrGARCH"),
                distribution.model = 'sstd')
m <- ugarchfit(data = return, spec = s)
sfinal <- s
setfixed(sfinal) <- as.list(coef(m))

sim <- ugarchpath(spec = sfinal,
                  m.sim = 3,
                  n.sim = 1*30,
                  rseed = 123)

tail(AAPL$AAPL.Close,1)
           AAPL.Close
2022-04-11     165.75
p <- 291.52*apply(fitted(sim), 2, 'cumsum') + 165.75
matplot(p, type = "l", lwd = 3)