# Bibliotecas
#------------------
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(xts)
library(PerformanceAnalytics)
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
library(rugarch)
## Warning: package 'rugarch' was built under R version 4.3.3
## Loading required package: parallel
##
## Attaching package: 'rugarch'
## The following object is masked from 'package:stats':
##
## sigma
#Datos
getSymbols("AAPL",from = "2008-01-01",to = "2024-01-01")
## [1] "AAPL"
# Plot
#-------------------
chartSeries(AAPL)
# Rentabilidades diarias
#----------------------
return <- CalculateReturns(AAPL$AAPL.Close)
return <- return[-1]
hist(return)
chart.Histogram(return,
methods = c('add.density', 'add.normal'),
colorset = c('lightblue', 'blue', 'red'))
Se observa que los rendimientos se ajustan adecuadamente a la normal y prsenta un alto grado de apuntamiento (hiperleptocurtosis).
chartSeries(return)
Vemos periodos con una alta voltailidad y otros con una dispersión menos
elevada que conduce a utilizar modelos de heterocedasticidad
condicionada.
# Annualized volatility
chart.RollingPerformance(R = return["2008::2024"],
width = 252,
FUN = "sd.annualized",
scale = 252,
main = "Apple's yearly rolling volatility")
Se confirma alta volatilidad en 2008 y 2021. Verificándose en el empleo de modelos ARCH-GARCH.
Model 1 - sGARCH with constant mean
# 1. sGARCH model with constant mean
#---------------------------------------
s1 <- ugarchspec(mean.model = list(armaOrder = c(0,0)),
variance.model = list(model = "sGARCH"),
distribution.model = 'norm')
m1 <- ugarchfit(data = return, spec = s1)
m1
##
## *---------------------------------*
## * 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.001855 0.000245 7.5845 0
## omega 0.000013 0.000001 15.2248 0
## alpha1 0.106128 0.003736 28.4063 0
## beta1 0.858152 0.008090 106.0786 0
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.001855 0.000289 6.4082 0
## omega 0.000013 0.000002 6.6472 0
## alpha1 0.106128 0.010623 9.9905 0
## beta1 0.858152 0.011109 77.2467 0
##
## LogLikelihood : 10576.44
##
## Information Criteria
## ------------------------------------
##
## Akaike -5.2521
## Bayes -5.2458
## Shibata -5.2521
## Hannan-Quinn -5.2499
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.5038 0.4778
## Lag[2*(p+q)+(p+q)-1][2] 0.7862 0.5731
## Lag[4*(p+q)+(p+q)-1][5] 2.9618 0.4141
## d.o.f=0
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.2282 0.6329
## Lag[2*(p+q)+(p+q)-1][5] 1.8041 0.6654
## Lag[4*(p+q)+(p+q)-1][9] 3.0697 0.7474
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.7576 0.500 2.000 0.3841
## ARCH Lag[5] 2.3682 1.440 1.667 0.3955
## ARCH Lag[7] 2.5549 2.315 1.543 0.6012
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 19.3278
## Individual Statistics:
## mu 0.08776
## omega 4.29976
## alpha1 0.28302
## beta1 0.30009
##
## 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 1.2476 0.21224
## Negative Sign Bias 1.1151 0.26486
## Positive Sign Bias 0.5894 0.55565
## Joint Effect 9.9634 0.01888 **
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 127.2 4.878e-18
## 2 30 138.7 2.956e-16
## 3 40 156.5 5.277e-16
## 4 50 158.3 1.977e-13
##
##
## Elapsed time : 0.213887
plot(m1, which = 'all')
##
## please wait...calculating quantiles...
# Prediccion
# -----------------------------
f <- ugarchforecast(fitORspec = m1, n.ahead = 30)
plot(fitted(f))
Model 2 - GARCH with sstd
# 2. GARCH with sstd
#------------------------------------------------
s1 <- ugarchspec(mean.model = list(armaOrder = c(0,0)),
variance.model = list(model = "sGARCH"),
distribution.model = 'sstd')
m1 <- ugarchfit(data = return, spec = s1)
m1
##
## *---------------------------------*
## * 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.001514 0.000245 6.1710 0.000000
## omega 0.000007 0.000003 2.3044 0.021199
## alpha1 0.090689 0.011298 8.0267 0.000000
## beta1 0.895856 0.013341 67.1491 0.000000
## skew 1.007190 0.021849 46.0987 0.000000
## shape 5.154061 0.436765 11.8005 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.001514 0.000244 6.1985 0.000
## omega 0.000007 0.000006 1.0603 0.289
## alpha1 0.090689 0.014126 6.4202 0.000
## beta1 0.895856 0.021122 42.4143 0.000
## skew 1.007190 0.021488 46.8714 0.000
## shape 5.154061 0.580612 8.8769 0.000
##
## LogLikelihood : 10735.8
##
## Information Criteria
## ------------------------------------
##
## Akaike -5.3303
## Bayes -5.3209
## Shibata -5.3303
## Hannan-Quinn -5.3269
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.5119 0.4743
## Lag[2*(p+q)+(p+q)-1][2] 0.7614 0.5825
## Lag[4*(p+q)+(p+q)-1][5] 3.0780 0.3930
## d.o.f=0
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.009186 0.9236
## Lag[2*(p+q)+(p+q)-1][5] 1.081228 0.8410
## Lag[4*(p+q)+(p+q)-1][9] 2.035102 0.9008
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.5746 0.500 2.000 0.4484
## ARCH Lag[5] 1.7289 1.440 1.667 0.5343
## ARCH Lag[7] 1.9781 2.315 1.543 0.7220
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 2.3605
## Individual Statistics:
## mu 0.08682
## omega 0.44654
## alpha1 0.60432
## beta1 0.65995
## skew 0.03744
## shape 0.92801
##
## 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.4316 0.15235
## Negative Sign Bias 0.9230 0.35609
## Positive Sign Bias 0.6655 0.50579
## Joint Effect 10.7643 0.01307 **
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 24.32 0.18406
## 2 30 40.14 0.08167
## 3 40 50.42 0.10401
## 4 50 52.86 0.32732
##
##
## Elapsed time : 0.5776751
plot(m1, which = 'all')
##
## please wait...calculating quantiles...
Model 3 - GJR-GARCH
# 3. GJR-GARCH
s1 <- ugarchspec(mean.model = list(armaOrder = c(0,0)),
variance.model = list(model = "gjrGARCH"),
distribution.model = 'sstd')
m1 <- ugarchfit(data = return, spec = s1)
m1
##
## *---------------------------------*
## * 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.001294 0.000235 5.5139 0
## omega 0.000010 0.000001 9.1962 0
## alpha1 0.029653 0.002605 11.3829 0
## beta1 0.876709 0.009205 95.2402 0
## gamma1 0.145258 0.019299 7.5265 0
## skew 1.002832 0.021686 46.2442 0
## shape 5.548672 0.422472 13.1338 0
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.001294 0.000252 5.1285 0.00000
## omega 0.000010 0.000001 6.6392 0.00000
## alpha1 0.029653 0.008707 3.4056 0.00066
## beta1 0.876709 0.010243 85.5921 0.00000
## gamma1 0.145258 0.024169 6.0100 0.00000
## skew 1.002832 0.021431 46.7929 0.00000
## shape 5.548672 0.510407 10.8711 0.00000
##
## LogLikelihood : 10768.71
##
## Information Criteria
## ------------------------------------
##
## Akaike -5.3461
## Bayes -5.3352
## Shibata -5.3461
## Hannan-Quinn -5.3422
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 1.040 0.3079
## Lag[2*(p+q)+(p+q)-1][2] 1.143 0.4541
## Lag[4*(p+q)+(p+q)-1][5] 3.188 0.3738
## d.o.f=0
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.2599 0.6102
## Lag[2*(p+q)+(p+q)-1][5] 1.4049 0.7633
## Lag[4*(p+q)+(p+q)-1][9] 2.3115 0.8647
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.5543 0.500 2.000 0.4565
## ARCH Lag[5] 1.8390 1.440 1.667 0.5079
## ARCH Lag[7] 2.0819 2.315 1.543 0.7000
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 23.5834
## Individual Statistics:
## mu 0.24756
## omega 5.35244
## alpha1 0.99502
## beta1 0.97609
## gamma1 0.79650
## skew 0.03107
## shape 1.22359
##
## 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.61550 0.1063
## Negative Sign Bias 0.81261 0.4165
## Positive Sign Bias 0.05342 0.9574
## Joint Effect 3.46592 0.3252
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 21.17 0.3273
## 2 30 25.56 0.6486
## 3 40 36.04 0.6058
## 4 50 54.05 0.2874
##
##
## Elapsed time : 0.7437539
plot(m1, which = 'all')
##
## please wait...calculating quantiles...
Model 4 - AR(1) GJR-GARCH
# 4. AR(1) GJR-GARCH
#---------------------------------------------------
s1 <- ugarchspec(mean.model = list(armaOrder = c(1,0)),
variance.model = list(model = "gjrGARCH"),
distribution.model = 'sstd')
m1 <- ugarchfit(data = return, spec = s1)
m1
##
## *---------------------------------*
## * 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.001280 0.000237 5.39407 0.00000
## ar1 0.011388 0.015820 0.71984 0.47163
## omega 0.000010 0.000001 9.39846 0.00000
## alpha1 0.028997 0.002698 10.74856 0.00000
## beta1 0.876559 0.009193 95.34764 0.00000
## gamma1 0.146909 0.019514 7.52843 0.00000
## skew 1.002968 0.021715 46.18708 0.00000
## shape 5.565488 0.424988 13.09564 0.00000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.001280 0.000253 5.0600 0.000000
## ar1 0.011388 0.014415 0.7900 0.429526
## omega 0.000010 0.000001 6.7944 0.000000
## alpha1 0.028997 0.008595 3.3735 0.000742
## beta1 0.876559 0.010207 85.8772 0.000000
## gamma1 0.146909 0.024323 6.0401 0.000000
## skew 1.002968 0.021576 46.4859 0.000000
## shape 5.565488 0.511074 10.8898 0.000000
##
## LogLikelihood : 10768.97
##
## Information Criteria
## ------------------------------------
##
## Akaike -5.3457
## Bayes -5.3332
## Shibata -5.3457
## Hannan-Quinn -5.3413
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.1493 0.6992
## Lag[2*(p+q)+(p+q)-1][2] 0.2532 0.9970
## Lag[4*(p+q)+(p+q)-1][5] 2.3021 0.6235
## d.o.f=1
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.3023 0.5825
## Lag[2*(p+q)+(p+q)-1][5] 1.4204 0.7595
## Lag[4*(p+q)+(p+q)-1][9] 2.3029 0.8659
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.5459 0.500 2.000 0.4600
## ARCH Lag[5] 1.7897 1.440 1.667 0.5196
## ARCH Lag[7] 2.0305 2.315 1.543 0.7109
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 24.1903
## Individual Statistics:
## mu 0.24566
## ar1 0.20348
## omega 5.44358
## alpha1 0.99043
## beta1 0.97333
## gamma1 0.79912
## skew 0.03106
## shape 1.21781
##
## 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.65008 0.0990 *
## Negative Sign Bias 0.85085 0.3949
## Positive Sign Bias 0.08454 0.9326
## Joint Effect 3.52827 0.3171
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 19.06 0.4532
## 2 30 27.79 0.5294
## 3 40 39.36 0.4540
## 4 50 49.86 0.4391
##
##
## Elapsed time : 0.917182
plot(m1, which = 'all')
##
## please wait...calculating quantiles...
Model 5 - GJR-GARCH in mean
# 5. GJR-GARCH in mean
s1 <- ugarchspec(mean.model = list(armaOrder = c(0,0),
archm =T,
archpow = 2),
variance.model = list(model = "gjrGARCH"),
distribution.model = 'sstd')
m1 <- ugarchfit(data = return, spec = s1)
m1
##
## *---------------------------------*
## * 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.001335 0.000310 4.30786 0.000016
## archm -0.176054 0.963674 -0.18269 0.855041
## omega 0.000009 0.000001 9.65644 0.000000
## alpha1 0.029537 0.004891 6.03903 0.000000
## beta1 0.877160 0.009207 95.27420 0.000000
## gamma1 0.145336 0.019824 7.33143 0.000000
## skew 1.002489 0.021878 45.82154 0.000000
## shape 5.549960 0.431325 12.86725 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.001335 0.000354 3.77040 0.000163
## archm -0.176054 0.992370 -0.17741 0.859188
## omega 0.000009 0.000001 7.56471 0.000000
## alpha1 0.029537 0.008342 3.54070 0.000399
## beta1 0.877160 0.010356 84.69781 0.000000
## gamma1 0.145336 0.024533 5.92416 0.000000
## skew 1.002489 0.021286 47.09542 0.000000
## shape 5.549960 0.494399 11.22566 0.000000
##
## LogLikelihood : 10768.73
##
## Information Criteria
## ------------------------------------
##
## Akaike -5.3456
## Bayes -5.3331
## Shibata -5.3456
## Hannan-Quinn -5.3412
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.9877 0.3203
## Lag[2*(p+q)+(p+q)-1][2] 1.1021 0.4663
## Lag[4*(p+q)+(p+q)-1][5] 3.1494 0.3804
## d.o.f=0
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.2528 0.6151
## Lag[2*(p+q)+(p+q)-1][5] 1.3938 0.7660
## Lag[4*(p+q)+(p+q)-1][9] 2.2988 0.8665
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.5513 0.500 2.000 0.4578
## ARCH Lag[5] 1.8339 1.440 1.667 0.5091
## ARCH Lag[7] 2.0775 2.315 1.543 0.7009
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 23.9325
## Individual Statistics:
## mu 0.25391
## archm 0.18568
## omega 5.15901
## alpha1 0.99833
## beta1 0.97705
## gamma1 0.79979
## skew 0.03108
## shape 1.22410
##
## 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.65107 0.0988 *
## Negative Sign Bias 0.80885 0.4186
## Positive Sign Bias 0.07609 0.9393
## Joint Effect 3.58666 0.3097
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 21.59 0.3051
## 2 30 25.21 0.6675
## 3 40 40.91 0.3868
## 4 50 58.45 0.1671
##
##
## Elapsed time : 1.783028
Modelo 3 - GJ-GARCH es el modelo óptimo
# Simulation Montecarlo
s <- ugarchspec(mean.model = list(armaOrder = c(0,0)),
variance.model = list(model = "gjrGARCH"),
distribution.model = 'sstd')
m <- ugarchfit(data = return, spec = s1)
#-----------------------------------------------------------
sfinal <- s
setfixed(sfinal) <- as.list(coef(m))
## Warning in `setfixed<-`(`*tmp*`, value = list(mu = 0.00133487179973344, :
## Unrecognized Parameter in Fixed Values: archm...Ignored
#-----------------------------------------------------------
sim <- ugarchpath(spec = sfinal,
m.sim = 3,
n.sim = 1*30,
rseed = 123)
tail(AAPL$AAPL.Close,2)
## AAPL.Close
## 2023-12-28 193.58
## 2023-12-29 192.53
p <- 193.58*apply(fitted(sim), 2, 'cumsum') + 192.53
matplot(p, type = "l", lwd = 3)