Využijeme GARCH modely pro modelování volatility výnosů akcie Tesly (TSLA) podle článku Idrisse Tsafacka.
navíc zohledňuje volatilitu z předchozích period
často používaný pro modelovaní volatility výnosů akcií, indexů
model:
\[ h_t = \omega + \alpha_1 e_{t-1}^{2} + \cdots + \alpha_q e_{t-q}^{2} + \beta_1 h_{t-1} + \cdots + \beta_p h_{t-p}, \]
kde platí
\[ \omega > 0,\quad \alpha_i, \beta_j \ge 0. \]
“Standardní GARCH (sGARCH)”
“ARIMA-GARCH”
“EGARCH”
“Integrovaný GARCH (IGARCH)”
“GARCH in mean (GARCH-M)”
“GJR-GARCH”
library(quantmod) # Stahování finančních dat
library(dplyr)
library(tidyverse)
library(tseries)
library(xts)
library(rugarch) # GARCH model
library(PerformanceAnalytics)
TSLA = getSymbols("TSLA", from = "2010-06-29", to = "2020-12-31", auto.assign = FALSE) # stažení dat od 29.6.2010 do 31.12.2020 z Yahoo
head(TSLA)
## TSLA.Open TSLA.High TSLA.Low TSLA.Close TSLA.Volume TSLA.Adjusted
## 2010-06-29 1.266667 1.666667 1.169333 1.592667 281494500 1.592667
## 2010-06-30 1.719333 2.028000 1.553333 1.588667 257806500 1.588667
## 2010-07-01 1.666667 1.728000 1.351333 1.464000 123282000 1.464000
## 2010-07-02 1.533333 1.540000 1.247333 1.280000 77097000 1.280000
## 2010-07-06 1.333333 1.333333 1.055333 1.074000 103003500 1.074000
## 2010-07-07 1.093333 1.108667 0.998667 1.053333 103825500 1.053333
TSLA.Adjusted je upravená uzavírací cena - bere v úvahu dividendy, rozdělení akcií…
chartSeries(TSLA)
chartSeries(TSLA["2020-12"])
return = CalculateReturns(TSLA$TSLA.Adjusted)
return = return[-c(1), ] # odstranění první hodnoty
chart_Series(return)
chart.Histogram(return, method = c('add.density', 'add.normal'), colorset = c('blue','red','black'))
legend("topright", legend = c("return", "kernel", "normal dist"), fill = c("blue", "red", "black"))
chart.RollingPerformance(R = return["2010::2020"], width = 22, FUN = "sd.annualized", scale = 252, main = "Měsíční volatilita akcií TESLY")
# Nastavení modelu
mod_specify = ugarchspec(mean.model = list(armaOrder = c(0,0)),
variance.model = list(model = "sGARCH", garchOrder = c(1,1)),
distribution.model = "norm")
# Model fitting
mod_fitting = ugarchfit(data = return, spec = mod_specify, out.sample = 20)
mod_fitting
##
## *---------------------------------*
## * 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.001658 0.000608 2.7283 0.006367
## omega 0.000007 0.000005 1.3638 0.172640
## alpha1 0.024882 0.003031 8.2083 0.000000
## beta1 0.969754 0.001409 688.2476 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.001658 0.000630 2.63307 0.008462
## omega 0.000007 0.000027 0.25634 0.797688
## alpha1 0.024882 0.018149 1.37097 0.170384
## beta1 0.969754 0.008170 118.69202 0.000000
##
## LogLikelihood : 5207.72
##
## Information Criteria
## ------------------------------------
##
## Akaike -3.9647
## Bayes -3.9558
## Shibata -3.9647
## Hannan-Quinn -3.9615
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.4477 0.5034
## Lag[2*(p+q)+(p+q)-1][2] 0.5302 0.6796
## Lag[4*(p+q)+(p+q)-1][5] 0.6436 0.9334
## d.o.f=0
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 14.86 1.158e-04
## Lag[2*(p+q)+(p+q)-1][5] 21.25 8.754e-06
## Lag[4*(p+q)+(p+q)-1][9] 23.64 2.259e-05
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 1.205 0.500 2.000 0.2724
## ARCH Lag[5] 2.098 1.440 1.667 0.4501
## ARCH Lag[7] 3.441 2.315 1.543 0.4344
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 1.0389
## Individual Statistics:
## mu 0.09049
## omega 0.14437
## alpha1 0.13560
## beta1 0.12654
##
## 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 8.194e-04 0.999346
## Negative Sign Bias 3.126e+00 0.001793 ***
## Positive Sign Bias 3.127e-01 0.754506
## Joint Effect 1.329e+01 0.004056 ***
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 202.6 1.025e-32
## 2 30 243.1 1.080e-35
## 3 40 242.0 4.074e-31
## 4 50 268.9 4.058e-32
##
##
## Elapsed time : 0.2936511
plot(mod_fitting, which = 8)
plot(mod_fitting, which = 9)
mod_specify = ugarchspec(mean.model = list(armaOrder = c(0,0)),
variance.model = list(model = "sGARCH", garchOrder = c(1,1)),
distribution.model = "sstd")
mod_fitting = ugarchfit(data = return, spec = mod_specify, out.sample = 20)
mod_fitting
##
## *---------------------------------*
## * 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.001910 0.000592 3.2247 0.001261
## omega 0.000020 0.000010 2.0599 0.039405
## alpha1 0.042897 0.012651 3.3909 0.000697
## beta1 0.944849 0.017251 54.7707 0.000000
## skew 1.032497 0.026370 39.1550 0.000000
## shape 3.504814 0.254154 13.7901 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.001910 0.000596 3.2070 0.001341
## omega 0.000020 0.000017 1.1377 0.255267
## alpha1 0.042897 0.022254 1.9276 0.053902
## beta1 0.944849 0.032351 29.2066 0.000000
## skew 1.032497 0.027171 37.9998 0.000000
## shape 3.504814 0.247869 14.1398 0.000000
##
## LogLikelihood : 5449.655
##
## Information Criteria
## ------------------------------------
##
## Akaike -4.1475
## Bayes -4.1341
## Shibata -4.1476
## Hannan-Quinn -4.1427
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.3255 0.5683
## Lag[2*(p+q)+(p+q)-1][2] 0.4065 0.7392
## Lag[4*(p+q)+(p+q)-1][5] 0.4893 0.9593
## d.o.f=0
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 7.459 0.006310
## Lag[2*(p+q)+(p+q)-1][5] 9.885 0.009855
## Lag[4*(p+q)+(p+q)-1][9] 12.318 0.015350
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.287 0.500 2.000 0.5921
## ARCH Lag[5] 1.453 1.440 1.667 0.6047
## ARCH Lag[7] 3.248 2.315 1.543 0.4681
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 0.6624
## Individual Statistics:
## mu 0.10937
## omega 0.11787
## alpha1 0.17331
## beta1 0.12387
## skew 0.02821
## shape 0.08105
##
## 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 0.1173 0.90660
## Negative Sign Bias 2.3413 0.01929 **
## Positive Sign Bias 0.1744 0.86155
## Joint Effect 8.7886 0.03224 **
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 15.98 0.6584
## 2 30 35.79 0.1798
## 3 40 33.06 0.7369
## 4 50 50.71 0.4058
##
##
## Elapsed time : 0.5733631
plot(mod_fitting, which = 8)
plot(mod_fitting, which = 9)
mod_specify = ugarchspec(mean.model = list(armaOrder = c(0,0)),
variance.model = list(model = "gjrGARCH", garchOrder = c(1,1)),
distribution.model = "sstd")
mod_fitting = ugarchfit(data = return, spec = mod_specify, out.sample = 20)
mod_fitting
##
## *---------------------------------*
## * 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.001873 0.000594 3.15575 0.001601
## omega 0.000021 0.000014 1.49337 0.135340
## alpha1 0.041112 0.015924 2.58178 0.009829
## beta1 0.940564 0.026545 35.43223 0.000000
## gamma1 0.011906 0.015547 0.76581 0.443789
## skew 1.031598 0.026405 39.06841 0.000000
## shape 3.506313 0.254251 13.79077 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.001873 0.000604 3.10087 0.001929
## omega 0.000021 0.000038 0.55482 0.579020
## alpha1 0.041112 0.038923 1.05626 0.290851
## beta1 0.940564 0.074141 12.68615 0.000000
## gamma1 0.011906 0.030492 0.39046 0.696199
## skew 1.031598 0.027573 37.41276 0.000000
## shape 3.506313 0.248340 14.11900 0.000000
##
## LogLikelihood : 5450.033
##
## Information Criteria
## ------------------------------------
##
## Akaike -4.1471
## Bayes -4.1314
## Shibata -4.1471
## Hannan-Quinn -4.1414
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.3621 0.5474
## Lag[2*(p+q)+(p+q)-1][2] 0.4465 0.7193
## Lag[4*(p+q)+(p+q)-1][5] 0.5402 0.9513
## d.o.f=0
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 5.876 0.01535
## Lag[2*(p+q)+(p+q)-1][5] 8.486 0.02229
## Lag[4*(p+q)+(p+q)-1][9] 11.015 0.03017
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.2765 0.500 2.000 0.5990
## ARCH Lag[5] 1.4943 1.440 1.667 0.5938
## ARCH Lag[7] 3.3891 2.315 1.543 0.4432
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 0.8719
## Individual Statistics:
## mu 0.11631
## omega 0.13151
## alpha1 0.19408
## beta1 0.12823
## gamma1 0.08395
## skew 0.02740
## shape 0.08005
##
## 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 0.29968 0.76445
## Negative Sign Bias 1.87523 0.06087 *
## Positive Sign Bias 0.09153 0.92708
## Joint Effect 6.40752 0.09338 *
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 15.85 0.6675
## 2 30 38.51 0.1114
## 3 40 37.51 0.5380
## 4 50 52.43 0.3425
##
##
## Elapsed time : 1.310739
plot(mod_fitting, which = 1)
mod_specify = ugarchspec(mean.model = list(armaOrder=c(0,0)),
variance.model = list(model = "gjrGARCH", garchOrder = c(1,0)),
distribution.model = "sstd")
# Model fitting code
mod_fitting = ugarchfit(data=return, spec = mod_specify, out.sample = 20)
mod_fitting
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : gjrGARCH(1,0)
## Mean Model : ARFIMA(0,0,0)
## Distribution : sstd
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu 0.002375 0.000644 3.6872 0.000227
## omega 0.001128 0.000111 10.1421 0.000000
## alpha1 0.143930 0.047053 3.0589 0.002222
## gamma1 0.215124 0.094040 2.2876 0.022163
## skew 1.049853 0.026621 39.4363 0.000000
## shape 3.156063 0.217222 14.5292 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.002375 0.000645 3.6798 0.000233
## omega 0.001128 0.000128 8.8290 0.000000
## alpha1 0.143930 0.053959 2.6674 0.007645
## gamma1 0.215124 0.112314 1.9154 0.055444
## skew 1.049853 0.026655 39.3864 0.000000
## shape 3.156063 0.235542 13.3991 0.000000
##
## LogLikelihood : 5401.529
##
## Information Criteria
## ------------------------------------
##
## Akaike -4.1109
## Bayes -4.0975
## Shibata -4.1109
## Hannan-Quinn -4.1060
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 1.367 0.2423
## Lag[2*(p+q)+(p+q)-1][2] 1.375 0.3910
## Lag[4*(p+q)+(p+q)-1][5] 1.950 0.6304
## d.o.f=0
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.5745 4.485e-01
## Lag[2*(p+q)+(p+q)-1][2] 13.9223 1.764e-04
## Lag[4*(p+q)+(p+q)-1][5] 34.2485 1.994e-09
## d.o.f=1
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[2] 26.65 0.500 2.000 2.432e-07
## ARCH Lag[4] 40.12 1.397 1.611 6.277e-11
## ARCH Lag[6] 47.52 2.222 1.500 1.108e-12
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 3.0294
## Individual Statistics:
## mu 0.1115
## omega 1.5333
## alpha1 0.9231
## gamma1 0.5204
## skew 0.0329
## shape 1.1376
##
## 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 0.01374 0.9890
## Negative Sign Bias 0.61886 0.5361
## Positive Sign Bias 0.69428 0.4876
## Joint Effect 0.86948 0.8328
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 23.05 0.2350
## 2 30 27.72 0.5329
## 3 40 46.38 0.1943
## 4 50 42.87 0.7188
##
##
## Elapsed time : 1.383046
plot(mod_fitting, which = 8)
plot(mod_fitting, which = 9)
plot(mod_fitting, which = 1)
plot(mod_fitting, which = 3)
plot(mod_fitting, which = 12)
forc = ugarchforecast(fitORspec = mod_fitting, n.ahead = 20)
par(mfrow = c(1,1))
plot(sigma(forc))
TSLA2 = getSymbols("TSLA", from = "2015-11-18", to = "2025-11-18", auto.assign = FALSE)
head(TSLA2)
## TSLA.Open TSLA.High TSLA.Low TSLA.Close TSLA.Volume TSLA.Adjusted
## 2015-11-18 14.30000 14.75867 14.16800 14.73800 42178500 14.73800
## 2015-11-19 14.70267 15.07933 14.68667 14.78667 37566000 14.78667
## 2015-11-20 14.89933 15.00000 14.23867 14.66733 66010500 14.66733
## 2015-11-23 14.49000 14.61200 14.31200 14.51667 37893000 14.51667
## 2015-11-24 14.35800 14.73333 14.33333 14.55000 37204500 14.55000
## 2015-11-25 14.75600 15.38867 14.69200 15.30933 59862000 15.30933
chartSeries(TSLA2)
return2 = CalculateReturns(TSLA2$TSLA.Adjusted)
return2 = return2[-c(1), ] # odstranění první hodnoty
mod_specify = ugarchspec(mean.model = list(armaOrder=c(0,0)),
variance.model = list(model = "sGARCH", garchOrder = c(1,1)),
distribution.model = "sstd")
# Model fitting
mod_fitting = ugarchfit(data=return2, spec = mod_specify, out.sample = 20)
mod_fitting
##
## *---------------------------------*
## * 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.001735 0.000645 2.6921 0.007101
## omega 0.000010 0.000005 1.8014 0.071643
## alpha1 0.041422 0.007062 5.8653 0.000000
## beta1 0.954891 0.003931 242.9329 0.000000
## skew 1.019269 0.026886 37.9114 0.000000
## shape 4.154731 0.375221 11.0728 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.001735 0.000722 2.4024 0.016287
## omega 0.000010 0.000009 1.0218 0.306860
## alpha1 0.041422 0.011371 3.6428 0.000270
## beta1 0.954891 0.002105 453.6849 0.000000
## skew 1.019269 0.027243 37.4133 0.000000
## shape 4.154731 0.417555 9.9501 0.000000
##
## LogLikelihood : 4961.201
##
## Information Criteria
## ------------------------------------
##
## Akaike -3.9753
## Bayes -3.9613
## Shibata -3.9753
## Hannan-Quinn -3.9702
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.5702 0.4502
## Lag[2*(p+q)+(p+q)-1][2] 1.4736 0.3672
## Lag[4*(p+q)+(p+q)-1][5] 2.4679 0.5127
## d.o.f=0
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.4378 0.5082
## Lag[2*(p+q)+(p+q)-1][5] 1.5358 0.7311
## Lag[4*(p+q)+(p+q)-1][9] 3.3575 0.6988
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.3949 0.500 2.000 0.5297
## ARCH Lag[5] 0.6866 1.440 1.667 0.8275
## ARCH Lag[7] 1.9294 2.315 1.543 0.7323
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 2.9279
## Individual Statistics:
## mu 0.10380
## omega 0.14943
## alpha1 0.40246
## beta1 0.37381
## skew 0.07591
## shape 0.55032
##
## 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 0.3611 0.7181
## Negative Sign Bias 0.1700 0.8650
## Positive Sign Bias 0.3976 0.6910
## Joint Effect 0.9963 0.8021
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 23.56 0.2136
## 2 30 31.90 0.3244
## 3 40 42.76 0.3130
## 4 50 59.45 0.1458
##
##
## Elapsed time : 0.434052
plot(mod_fitting, which = 8)
plot(mod_fitting, which = 9)
plot(mod_fitting, which = 1)
forc = ugarchforecast(fitORspec = mod_fitting, n.ahead = 20)
par(mfrow = c(1,1))
plot(sigma(forc))