1.- INTRODUCCIÓN

Procesos estocásticos

Como hemos mencionado anteriormente, nosotros no nos centraremos en el estucio clásico de series temporales, sino que consideraremos nuestros datos como fenómenos que evolucionan en el tiempo. Para ello, los modelos adecuados son los llamados procesos estocásticos.

Procesos estacionarios

Cada serie temporal tiene su particular forma, cosa que se puede observar al hacer su respectiva gráfica. Necesitamos que los procesos estocásticos sean la representación matemática de algún tipo de regularidad, para que puedan ser utilizados como modelos de nuestras series.

Modelos ARCH

La heterocedasticidad condicional auto-regresiva (en inglés, Autoregressive conditional heteroscedasticity; ARCH) es la condición de que hay uno o más puntos de datos en una serie para los cuales la varianza del término de error actual o innovación es una función de los tamaños reales de los términos de error de los períodos de tiempo anteriores: se relaciona con los cuadrados de las innovaciones anteriores. En la econometría, los modelos ARCH se utilizan para caracterizar y modelar series temporales. Una variedad de otras siglas se aplican a las estructuras particulares que tienen una base similar. Los modelos de ARCH se emplean comúnmente en el modelado de series de tiempo financieras que presentan agrupaciones de volatilidad variables en el tiempo, es decir, períodos de oscilaciones entremezclados con períodos de relativa calma. A veces se considera que los modelos de tipo ARCH pertenecen a la familia de modelos de volatilidad estocástica , aunque esto es estrictamente incorrecto ya que en el tiempo t la volatilidad es completamente predeterminada (determinista) dados los valores anteriores.

2.- EJERCICIO

# Bibliotecas
#---------------------
library(tidyquant)
## Loading required package: lubridate
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(TTR)
library(ggplot2)
library(knitr)
library(nortest)
library(MASS)
library(stats)
library(moments)
## 
## Attaching package: 'moments'
## The following objects are masked from 'package:PerformanceAnalytics':
## 
##     kurtosis, skewness
library(fGarch)
## NOTE: Packages 'fBasics', 'timeDate', and 'timeSeries' are no longer
## attached to the search() path when 'fGarch' is attached.
## 
## If needed attach them yourself in your R script by e.g.,
##         require("timeSeries")
## 
## Attaching package: 'fGarch'
## The following object is masked from 'package:TTR':
## 
##     volatility
library(fBasics)
## 
## Attaching package: 'fBasics'
## The following objects are masked from 'package:moments':
## 
##     kurtosis, skewness
## The following object is masked from 'package:TTR':
## 
##     volatility
## The following objects are masked from 'package:PerformanceAnalytics':
## 
##     kurtosis, skewness
# Datos
# -------------------------
AAPL <- tq_get("AAPL", get = "stock.prices", from = "2020-01-01", to = "2024-01-01")
AAPL <- data.frame(AAPL)
# Plot
#------------------------
plot(AAPL$adjusted)

precio <- AAPL$adjusted
n <- length(precio)
rendimiento <- c()
for (i in 1:n) {
  rendimiento[i] <- (precio[i+1]-precio[i])/(precio[i])
}
rendimiento<-na.omit(rendimiento)
summary(rendimiento)
##       Min.    1st Qu.     Median       Mean    3rd Qu.       Max. 
## -0.1286470 -0.0091270  0.0009882  0.0011866  0.0127428  0.1198085

Modelo de heterocedasticidad condicional

modelo <- garchFit(~garch(1,0), data = rendimiento)
## 
## Series Initialization:
##  ARMA Model:                arma
##  Formula Mean:              ~ arma(0, 0)
##  GARCH Model:               garch
##  Formula Variance:          ~ garch(1, 0)
##  ARMA Order:                0 0
##  Max ARMA Order:            0
##  GARCH Order:               1 0
##  Max GARCH Order:           1
##  Maximum Order:             1
##  Conditional Dist:          norm
##  h.start:                   2
##  llh.start:                 1
##  Length of Series:          1005
##  Recursion Init:            mci
##  Series Scale:              0.02114593
## 
## Parameter Initialization:
##  Initial Parameters:          $params
##  Limits of Transformations:   $U, $V
##  Which Parameters are Fixed?  $includes
##  Parameter Matrix:
##                      U           V     params includes
##     mu     -0.56113285   0.5611329 0.05611329     TRUE
##     omega   0.00000100 100.0000000 0.10000000     TRUE
##     alpha1  0.00000001   1.0000000 0.10000000     TRUE
##     gamma1 -0.99999999   1.0000000 0.10000000    FALSE
##     delta   0.00000000   2.0000000 2.00000000    FALSE
##     skew    0.10000000  10.0000000 1.00000000    FALSE
##     shape   1.00000000  10.0000000 4.00000000    FALSE
##  Index List of Parameters to be Optimized:
##     mu  omega alpha1 
##      1      2      3 
##  Persistence:                  0.1 
## 
## 
## --- START OF TRACE ---
## Selected Algorithm: nlminb 
## 
## R coded nlminb Solver: 
## 
##   0:     3006.7556: 0.0561133 0.100000 0.100000
##   1:     1412.0659: 0.0561624  1.06199 0.373067
##   2:     1408.4169: 0.0563679  1.08020 0.284060
##   3:     1388.8408: 0.0564495 0.918388 0.201101
##   4:     1387.9049: 0.0566401 0.697330 0.146242
##   5:     1382.1955: 0.0567111 0.765909 0.237166
##   6:     1381.9079: 0.0629987 0.745913 0.241227
##   7:     1381.7314: 0.0782486 0.750404 0.240933
##   8:     1381.7102: 0.0820689 0.736148 0.258305
##   9:     1381.6984: 0.0815047 0.739781 0.249762
##  10:     1381.6983: 0.0813011 0.740303 0.250176
##  11:     1381.6983: 0.0813450 0.740163 0.250153
##  12:     1381.6983: 0.0813447 0.740164 0.250150
## 
## Final Estimate of the Negative LLH:
##  LLH:  -2493.891    norm LLH:  -2.481484 
##           mu        omega       alpha1 
## 0.0017201098 0.0003309645 0.2501501114 
## 
## R-optimhess Difference Approximated Hessian Matrix:
##                  mu         omega       alpha1
## mu     -2745212.135    -2709482.3    2700.2551
## omega  -2709482.309 -3397897076.7 -609158.9362
## alpha1     2700.255     -609158.9    -470.5157
## attr(,"time")
## Time difference of 0.005178928 secs
## 
## --- END OF TRACE ---
## 
## 
## Time to Estimate Parameters:
##  Time difference of 0.02600312 secs
# Coeficientes
#------------
coef(modelo)
##           mu        omega       alpha1 
## 0.0017201098 0.0003309645 0.2501501114

h^2_t = 0.00033+0.25015u^2_(t-1)

predict(modelo,n.ahead=5)
##   meanForecast  meanError standardDeviation
## 1   0.00172011 0.01854001        0.01854001
## 2   0.00172011 0.02041933        0.02041933
## 3   0.00172011 0.02086299        0.02086299
## 4   0.00172011 0.02097250        0.02097250
## 5   0.00172011 0.02099981        0.02099981
predict(modelo,n.ahead=30, plot = TRUE)

##    meanForecast  meanError standardDeviation lowerInterval upperInterval
## 1    0.00172011 0.01854001        0.01854001   -0.03461765    0.03805787
## 2    0.00172011 0.02041933        0.02041933   -0.03830104    0.04174126
## 3    0.00172011 0.02086299        0.02086299   -0.03917060    0.04261082
## 4    0.00172011 0.02097250        0.02097250   -0.03938524    0.04282546
## 5    0.00172011 0.02099981        0.02099981   -0.03943876    0.04287898
## 6    0.00172011 0.02100663        0.02100663   -0.03945214    0.04289236
## 7    0.00172011 0.02100834        0.02100834   -0.03945548    0.04289570
## 8    0.00172011 0.02100877        0.02100877   -0.03945632    0.04289654
## 9    0.00172011 0.02100887        0.02100887   -0.03945653    0.04289675
## 10   0.00172011 0.02100890        0.02100890   -0.03945658    0.04289680
## 11   0.00172011 0.02100891        0.02100891   -0.03945659    0.04289681
## 12   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
## 13   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
## 14   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
## 15   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
## 16   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
## 17   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
## 18   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
## 19   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
## 20   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
## 21   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
## 22   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
## 23   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
## 24   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
## 25   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
## 26   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
## 27   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
## 28   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
## 29   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
## 30   0.00172011 0.02100891        0.02100891   -0.03945660    0.04289682
modelo2 <- garchFit(~garch(2,0), data=rendimiento)
## 
## Series Initialization:
##  ARMA Model:                arma
##  Formula Mean:              ~ arma(0, 0)
##  GARCH Model:               garch
##  Formula Variance:          ~ garch(2, 0)
##  ARMA Order:                0 0
##  Max ARMA Order:            0
##  GARCH Order:               2 0
##  Max GARCH Order:           2
##  Maximum Order:             2
##  Conditional Dist:          norm
##  h.start:                   3
##  llh.start:                 1
##  Length of Series:          1005
##  Recursion Init:            mci
##  Series Scale:              0.02114593
## 
## Parameter Initialization:
##  Initial Parameters:          $params
##  Limits of Transformations:   $U, $V
##  Which Parameters are Fixed?  $includes
##  Parameter Matrix:
##                      U           V     params includes
##     mu     -0.56113285   0.5611329 0.05611329     TRUE
##     omega   0.00000100 100.0000000 0.10000000     TRUE
##     alpha1  0.00000001   1.0000000 0.05000000     TRUE
##     alpha2  0.00000001   1.0000000 0.05000000     TRUE
##     gamma1 -0.99999999   1.0000000 0.10000000    FALSE
##     gamma2 -0.99999999   1.0000000 0.10000000    FALSE
##     delta   0.00000000   2.0000000 2.00000000    FALSE
##     skew    0.10000000  10.0000000 1.00000000    FALSE
##     shape   1.00000000  10.0000000 4.00000000    FALSE
##  Index List of Parameters to be Optimized:
##     mu  omega alpha1 alpha2 
##      1      2      3      4 
##  Persistence:                  0.1 
## 
## 
## --- START OF TRACE ---
## Selected Algorithm: nlminb 
## 
## R coded nlminb Solver: 
## 
##   0:     2610.3924: 0.0561133 0.100000 0.0500000 0.0500000
##   1:     1417.0998: 0.0561402 0.927139 0.436566 0.457930
##   2:     1416.0398: 0.0590162  1.16464 0.0115681 0.356221
##   3:     1410.5148: 0.0648157  1.13385 0.0576209 1.00000e-08
##   4:     1369.2830: 0.0713194 0.799310 0.191323 0.326758
##   5:     1351.4299: 0.0713323 0.670094 0.133769 0.260115
##   6:     1350.5955: 0.0713662 0.527033 0.144175 0.197862
##   7:     1347.6678: 0.0757933 0.526817 0.245121 0.287505
##   8:     1347.0655: 0.0755089 0.537257 0.223162 0.262900
##   9:     1346.8973: 0.0765494 0.548343 0.204386 0.242826
##  10:     1346.8969: 0.0765520 0.549064 0.204558 0.243111
##  11:     1346.8969: 0.0765627 0.549305 0.203866 0.242869
##  12:     1346.8968: 0.0766056 0.549452 0.204005 0.242959
##  13:     1346.8966: 0.0770283 0.549287 0.204009 0.243028
##  14:     1346.8965: 0.0772244 0.549488 0.204143 0.242658
##  15:     1346.8965: 0.0772264 0.549362 0.204119 0.242962
##  16:     1346.8965: 0.0771960 0.549377 0.204107 0.242910
##  17:     1346.8965: 0.0772016 0.549378 0.204110 0.242910
## 
## Final Estimate of the Negative LLH:
##  LLH:  -2528.693    norm LLH:  -2.516112 
##           mu        omega       alpha1       alpha2 
## 0.0016325002 0.0002456544 0.2041101569 0.2429101343 
## 
## R-optimhess Difference Approximated Hessian Matrix:
##                   mu         omega       alpha1       alpha2
## mu     -3208472.4513    -1261073.4    1416.2322    -768.6621
## omega  -1261073.4481 -4448270059.8 -796212.3152 -700590.6077
## alpha1     1416.2322     -796212.3    -568.3013    -151.2328
## alpha2     -768.6621     -700590.6    -151.2328    -541.3589
## attr(,"time")
## Time difference of 0.0109868 secs
## 
## --- END OF TRACE ---
## 
## 
## Time to Estimate Parameters:
##  Time difference of 0.03432512 secs
coef(modelo2)
##           mu        omega       alpha1       alpha2 
## 0.0016325002 0.0002456544 0.2041101569 0.2429101343

h^2_t = 0.00024 + 0.2041u^2_(t-1) + 0.2491u^2_(t-2)

predict(modelo2, n.ahead=30, plot = TRUE)

##    meanForecast  meanError standardDeviation lowerInterval upperInterval
## 1     0.0016325 0.01599700        0.01599700   -0.02972104    0.03298604
## 2     0.0016325 0.01760633        0.01760633   -0.03287528    0.03614028
## 3     0.0016325 0.01926361        0.01926361   -0.03612349    0.03938849
## 4     0.0016325 0.01991720        0.01991720   -0.03740450    0.04066950
## 5     0.0016325 0.02041481        0.02041481   -0.03837980    0.04164480
## 6     0.0016325 0.02066595        0.02066595   -0.03887202    0.04213702
## 7     0.0016325 0.02083417        0.02083417   -0.03920172    0.04246672
## 8     0.0016325 0.02092829        0.02092829   -0.03938620    0.04265120
## 9     0.0016325 0.02098789        0.02098789   -0.03950301    0.04276801
## 10    0.0016325 0.02102276        0.02102276   -0.03957135    0.04283635
## 11    0.0016325 0.02104429        0.02104429   -0.03961355    0.04287855
## 12    0.0016325 0.02105713        0.02105713   -0.03963872    0.04290372
## 13    0.0016325 0.02106497        0.02106497   -0.03965409    0.04291909
## 14    0.0016325 0.02106969        0.02106969   -0.03966334    0.04292834
## 15    0.0016325 0.02107256        0.02107256   -0.03966896    0.04293396
## 16    0.0016325 0.02107429        0.02107429   -0.03967235    0.04293735
## 17    0.0016325 0.02107534        0.02107534   -0.03967441    0.04293941
## 18    0.0016325 0.02107597        0.02107597   -0.03967565    0.04294065
## 19    0.0016325 0.02107636        0.02107636   -0.03967640    0.04294140
## 20    0.0016325 0.02107659        0.02107659   -0.03967686    0.04294186
## 21    0.0016325 0.02107673        0.02107673   -0.03967713    0.04294213
## 22    0.0016325 0.02107682        0.02107682   -0.03967730    0.04294230
## 23    0.0016325 0.02107687        0.02107687   -0.03967740    0.04294240
## 24    0.0016325 0.02107690        0.02107690   -0.03967746    0.04294246
## 25    0.0016325 0.02107692        0.02107692   -0.03967750    0.04294250
## 26    0.0016325 0.02107693        0.02107693   -0.03967752    0.04294252
## 27    0.0016325 0.02107694        0.02107694   -0.03967754    0.04294254
## 28    0.0016325 0.02107694        0.02107694   -0.03967755    0.04294255
## 29    0.0016325 0.02107694        0.02107694   -0.03967755    0.04294255
## 30    0.0016325 0.02107695        0.02107695   -0.03967755    0.04294255
modelo3 <- garchFit(~garch(3,0), data=rendimiento)
## 
## Series Initialization:
##  ARMA Model:                arma
##  Formula Mean:              ~ arma(0, 0)
##  GARCH Model:               garch
##  Formula Variance:          ~ garch(3, 0)
##  ARMA Order:                0 0
##  Max ARMA Order:            0
##  GARCH Order:               3 0
##  Max GARCH Order:           3
##  Maximum Order:             3
##  Conditional Dist:          norm
##  h.start:                   4
##  llh.start:                 1
##  Length of Series:          1005
##  Recursion Init:            mci
##  Series Scale:              0.02114593
## 
## Parameter Initialization:
##  Initial Parameters:          $params
##  Limits of Transformations:   $U, $V
##  Which Parameters are Fixed?  $includes
##  Parameter Matrix:
##                      U           V     params includes
##     mu     -0.56113285   0.5611329 0.05611329     TRUE
##     omega   0.00000100 100.0000000 0.10000000     TRUE
##     alpha1  0.00000001   1.0000000 0.03333333     TRUE
##     alpha2  0.00000001   1.0000000 0.03333333     TRUE
##     alpha3  0.00000001   1.0000000 0.03333333     TRUE
##     gamma1 -0.99999999   1.0000000 0.10000000    FALSE
##     gamma2 -0.99999999   1.0000000 0.10000000    FALSE
##     gamma3 -0.99999999   1.0000000 0.10000000    FALSE
##     delta   0.00000000   2.0000000 2.00000000    FALSE
##     skew    0.10000000  10.0000000 1.00000000    FALSE
##     shape   1.00000000  10.0000000 4.00000000    FALSE
##  Index List of Parameters to be Optimized:
##     mu  omega alpha1 alpha2 alpha3 
##      1      2      3      4      5 
##  Persistence:                  0.1 
## 
## 
## --- START OF TRACE ---
## Selected Algorithm: nlminb 
## 
## R coded nlminb Solver: 
## 
##   0:     2503.8299: 0.0561133 0.100000 0.0333333 0.0333333 0.0333333
##   1:     1435.0575: 0.0561416 0.802406 0.442460 0.452097 0.438151
##   2:     1429.9499: 0.0611083 0.454254 0.645210 0.715825 0.539621
##   3:     1413.0418: 0.101433 0.377225 0.899510 0.815030 1.00000e-08
##   4:     1392.2585: 0.101313 0.289345 0.767193 0.675706 0.0563440
##   5:     1376.3048: 0.101242 0.378746 0.638832 0.543140 0.132349
##   6:     1363.9229: 0.100392 0.286200 0.490063 0.428129 0.0717916
##   7:     1349.5369: 0.0970964 0.410114 0.335915 0.422858 0.144097
##   8:     1345.6688: 0.0939061 0.363362 0.171001 0.305754 0.105455
##   9:     1338.1959: 0.0917166 0.454763 0.292651 0.163775 0.160117
##  10:     1336.0887: 0.0913317 0.443119 0.219164 0.195502 0.165619
##  11:     1335.7694: 0.0912271 0.457722 0.185604 0.186574 0.178194
##  12:     1335.7620: 0.0891443 0.466506 0.180782 0.182940 0.168736
##  13:     1335.7558: 0.0890660 0.464138 0.184378 0.188418 0.176099
##  14:     1335.7548: 0.0889203 0.455278 0.184333 0.184155 0.177237
##  15:     1335.7408: 0.0887611 0.458925 0.185309 0.186050 0.177779
##  16:     1335.7397: 0.0885805 0.459519 0.183136 0.185564 0.174538
##  17:     1335.7362: 0.0883334 0.461476 0.184090 0.186707 0.175394
##  18:     1335.7122: 0.0837498 0.461183 0.183532 0.187875 0.173271
##  19:     1335.7121: 0.0835007 0.459391 0.184574 0.184837 0.176664
##  20:     1335.7108: 0.0833335 0.460397 0.184303 0.185844 0.176208
##  21:     1335.7103: 0.0831580 0.460854 0.183560 0.185705 0.175417
##  22:     1335.7100: 0.0829735 0.460664 0.183671 0.186240 0.175248
##  23:     1335.7098: 0.0825297 0.460913 0.183722 0.186287 0.174935
##  24:     1335.7098: 0.0824564 0.460758 0.183509 0.186378 0.175086
##  25:     1335.7098: 0.0824885 0.460825 0.183605 0.186341 0.175013
##  26:     1335.7098: 0.0824888 0.460826 0.183603 0.186341 0.175014
## 
## Final Estimate of the Negative LLH:
##  LLH:  -2539.88    norm LLH:  -2.527243 
##           mu        omega       alpha1       alpha2       alpha3 
## 0.0017443017 0.0002060584 0.1836034994 0.1863414510 0.1750137965 
## 
## R-optimhess Difference Approximated Hessian Matrix:
##                  mu         omega       alpha1       alpha2       alpha3
## mu     -3352659.431    -2646659.5    1022.5125   -1049.3707    1130.8191
## omega  -2646659.454 -4996717261.7 -872340.8088 -766413.3188 -886211.4623
## alpha1     1022.513     -872340.8    -632.2097    -162.2344    -185.4468
## alpha2    -1049.371     -766413.3    -162.2344    -697.5366    -228.9762
## alpha3     1130.819     -886211.5    -185.4468    -228.9762    -590.2607
## attr(,"time")
## Time difference of 0.02141404 secs
## 
## --- END OF TRACE ---
## 
## 
## Time to Estimate Parameters:
##  Time difference of 0.06105399 secs
coef(modelo3)
##           mu        omega       alpha1       alpha2       alpha3 
## 0.0017443017 0.0002060584 0.1836034994 0.1863414510 0.1750137965

h^2_t = 0.000206 + 0.1836u^2_(t-1) + 0.1836u^2_(t-2) + 0.175u^2_(t-3)

predict(modelo3, n.ahead=30, plot = TRUE)

##    meanForecast  meanError standardDeviation lowerInterval upperInterval
## 1   0.001744302 0.01469012        0.01469012   -0.02704780    0.03053640
## 2   0.001744302 0.01597798        0.01597798   -0.02957197    0.03306057
## 3   0.001744302 0.01738210        0.01738210   -0.03232398    0.03581259
## 4   0.001744302 0.01862450        0.01862450   -0.03475905    0.03824765
## 5   0.001744302 0.01925425        0.01925425   -0.03599334    0.03948195
## 6   0.001744302 0.01978989        0.01978989   -0.03704318    0.04053178
## 7   0.001744302 0.02019292        0.02019292   -0.03783309    0.04132169
## 8   0.001744302 0.02046422        0.02046422   -0.03836484    0.04185344
## 9   0.001744302 0.02067541        0.02067541   -0.03877875    0.04226736
## 10  0.001744302 0.02083131        0.02083131   -0.03908431    0.04257291
## 11  0.001744302 0.02094471        0.02094471   -0.03930657    0.04279518
## 12  0.001744302 0.02103038        0.02103038   -0.03947449    0.04296309
## 13  0.001744302 0.02109390        0.02109390   -0.03959898    0.04308758
## 14  0.001744302 0.02114103        0.02114103   -0.03969135    0.04317995
## 15  0.001744302 0.02117632        0.02117632   -0.03976051    0.04324912
## 16  0.001744302 0.02120259        0.02120259   -0.03981200    0.04330061
## 17  0.001744302 0.02122218        0.02122218   -0.03985040    0.04333900
## 18  0.001744302 0.02123681        0.02123681   -0.03987908    0.04336769
## 19  0.001744302 0.02124773        0.02124773   -0.03990048    0.04338908
## 20  0.001744302 0.02125588        0.02125588   -0.03991645    0.04340506
## 21  0.001744302 0.02126196        0.02126196   -0.03992838    0.04341699
## 22  0.001744302 0.02126651        0.02126651   -0.03993729    0.04342589
## 23  0.001744302 0.02126990        0.02126990   -0.03994394    0.04343254
## 24  0.001744302 0.02127244        0.02127244   -0.03994891    0.04343751
## 25  0.001744302 0.02127433        0.02127433   -0.03995261    0.04344122
## 26  0.001744302 0.02127574        0.02127574   -0.03995538    0.04344399
## 27  0.001744302 0.02127680        0.02127680   -0.03995745    0.04344606
## 28  0.001744302 0.02127758        0.02127758   -0.03995900    0.04344760
## 29  0.001744302 0.02127817        0.02127817   -0.03996015    0.04344875
## 30  0.001744302 0.02127861        0.02127861   -0.03996101    0.04344962

EJERCICIO GARCH

#Bibliotecas
#----------------------
library(tidyquant)
library(TTR)
library(ggplot2)
library(knitr)
library(nortest)
library(MASS)
library(stats)
library(moments)
library(fGarch)
library(fBasics)
AAPL <- tq_get("AAPL", get = "stock.prices", from = "2021-07-01", to = "2021-07-31")
AAPL <- data.frame(AAPL)
precio <- AAPL$close
n<-length(precio)
rendimiento <- c()
for (i in 1:n) {
  rendimiento[i] <- (precio[i+1]-precio[i])/(precio[i])
}
rendimiento<-na.omit(rendimiento)

modelo

modeloo<-garchFit(~garch(1,2), data = rendimiento)
## 
## Series Initialization:
##  ARMA Model:                arma
##  Formula Mean:              ~ arma(0, 0)
##  GARCH Model:               garch
##  Formula Variance:          ~ garch(1, 2)
##  ARMA Order:                0 0
##  Max ARMA Order:            0
##  GARCH Order:               1 2
##  Max GARCH Order:           2
##  Maximum Order:             2
##  Conditional Dist:          norm
##  h.start:                   3
##  llh.start:                 1
##  Length of Series:          20
##  Recursion Init:            mci
##  Series Scale:              0.01437149
## 
## Parameter Initialization:
##  Initial Parameters:          $params
##  Limits of Transformations:   $U, $V
##  Which Parameters are Fixed?  $includes
##  Parameter Matrix:
##                      U          V    params includes
##     mu     -2.18314624   2.183146 0.2183146     TRUE
##     omega   0.00000100 100.000000 0.1000000     TRUE
##     alpha1  0.00000001   1.000000 0.1000000     TRUE
##     gamma1 -0.99999999   1.000000 0.1000000    FALSE
##     beta1   0.00000001   1.000000 0.4000000     TRUE
##     beta2   0.00000001   1.000000 0.4000000     TRUE
##     delta   0.00000000   2.000000 2.0000000    FALSE
##     skew    0.10000000  10.000000 1.0000000    FALSE
##     shape   1.00000000  10.000000 4.0000000    FALSE
##  Index List of Parameters to be Optimized:
##     mu  omega alpha1  beta1  beta2 
##      1      2      3      5      6 
##  Persistence:                  0.9 
## 
## 
## --- START OF TRACE ---
## Selected Algorithm: nlminb 
## 
## R coded nlminb Solver: 
## 
##   0:     27.974431: 0.218315 0.100000 0.100000 0.400000 0.400000
##   1:     27.963262: 0.218306 0.0984128 0.0925684 0.394741 0.395780
##   2:     27.949471: 0.218248 0.107334 0.0894441 0.396070 0.399248
##   3:     27.940618: 0.218211 0.110453 0.0808451 0.392095 0.397322
##   4:     27.928170: 0.218156 0.119269 0.0774193 0.393413 0.400781
##   5:     27.919535: 0.218119 0.123056 0.0688020 0.389868 0.399361
##   6:     27.909087: 0.218070 0.131684 0.0646854 0.390955 0.402615
##   7:     27.901178: 0.218039 0.136025 0.0560397 0.387975 0.401763
##   8:     27.892706: 0.218006 0.144265 0.0510329 0.388924 0.404818
##   9:     27.886067: 0.217995 0.148984 0.0422444 0.386999 0.404815
##  10:     27.865705: 0.218103 0.180245 1.00000e-08 0.391414 0.417108
##  11:     27.865680: 0.217653 0.178890 1.00000e-08 0.390705 0.419596
##  12:     27.865657: 0.217347 0.176312 1.00000e-08 0.389171 0.423840
##  13:     27.865303: 0.215217 0.149752 1.00000e-08 0.373197 0.467314
##  14:     27.858681: 0.207000 0.0431420 1.00000e-08 0.309068 0.641202
##  15:     27.848277: 0.198671 1.00000e-06 1.00000e-08 0.282134 0.709992
##  16:     27.847857: 0.200997 1.00000e-06 1.00000e-08 0.284222 0.707978
##  17:     27.846795: 0.208486 1.00000e-06 1.00000e-08 0.295922 0.696009
##  18:     27.846438: 0.209728 1.00000e-06 1.00000e-08 0.303411 0.687955
##  19:     27.846178: 0.208801 1.00000e-06 1.00000e-08 0.313022 0.677416
##  20:     27.846156: 0.207771 1.00000e-06 1.00000e-08 0.314588 0.675702
##  21:     27.846150: 0.207330 1.00000e-06 1.00000e-08 0.315936 0.674337
##  22:     27.846141: 0.206868 1.00000e-06 1.00000e-08 0.319768 0.670502
##  23:     27.846108: 0.205879 1.00000e-06 1.00000e-08 0.335296 0.654995
##  24:     27.846012: 0.203901 1.00000e-06 1.00000e-08 0.385385 0.605019
##  25:     27.845645: 0.197609 1.00000e-06 1.00000e-08 0.598791 0.392167
##  26:     27.844462: 0.189820 1.00000e-06 1.00000e-08 0.992277 1.00000e-08
##  27:     27.843174: 0.195193 1.00000e-06 1.00000e-08 0.993031 1.00000e-08
##  28:     27.842168: 0.206263 1.00000e-06 1.00000e-08 0.992926 1.00000e-08
##  29:     27.842136: 0.205194 1.00000e-06 1.00000e-08 0.993082 1.00000e-08
##  30:     27.842136: 0.205169 1.00000e-06 1.00000e-08 0.993072 1.00000e-08
##  31:     27.842136: 0.205171 1.00000e-06 1.00000e-08 0.993072 1.00000e-08
## 
## Final Estimate of the Negative LLH:
##  LLH:  -57.00804    norm LLH:  -2.850402 
##           mu        omega       alpha1        beta1        beta2 
## 2.948617e-03 2.065397e-10 1.000000e-08 9.930725e-01 1.000000e-08 
## 
## R-optimhess Difference Approximated Hessian Matrix:
##                  mu        omega        alpha1        beta1        beta2
## mu      -102523.279     15878301     2606.8527     2951.804     2971.167
## omega  15878301.125 -26388621834 -4828637.5637 -5420415.441 -5442427.330
## alpha1     2606.853     -4828638     -910.9959    -1027.417    -1020.912
## beta1      2951.804     -5420415    -1027.4172    -1102.926    -1108.068
## beta2      2971.167     -5442427    -1020.9117    -1108.068    -1113.228
## attr(,"time")
## Time difference of 0.003675938 secs
## 
## --- END OF TRACE ---
## Warning in sqrt(diag(fit$cvar)): Se han producido NaNs
## 
## Time to Estimate Parameters:
##  Time difference of 0.01845503 secs
coef(modeloo)
##           mu        omega       alpha1        beta1        beta2 
## 2.948617e-03 2.065397e-10 1.000000e-08 9.930725e-01 1.000000e-08

varianza(t) = 2.06e-10 + 9.93R^2_(t-1) + 1e-0.8R^2(t-2) + 1e-0.8u^2_(t-1)

predict(modeloo, n.ahead=5, plot=TRUE)

##   meanForecast  meanError standardDeviation lowerInterval upperInterval
## 1  0.002948617 0.01306825        0.01306825   -0.02266468    0.02856192
## 2  0.002948617 0.01302291        0.01302291   -0.02257582    0.02847306
## 3  0.002948617 0.01297773        0.01297773   -0.02248728    0.02838451
## 4  0.002948617 0.01293271        0.01293271   -0.02239903    0.02829627
## 5  0.002948617 0.01288785        0.01288785   -0.02231110    0.02820833
modeloo2 <- garchFit(~garch(2,2), data=rendimiento)
## 
## Series Initialization:
##  ARMA Model:                arma
##  Formula Mean:              ~ arma(0, 0)
##  GARCH Model:               garch
##  Formula Variance:          ~ garch(2, 2)
##  ARMA Order:                0 0
##  Max ARMA Order:            0
##  GARCH Order:               2 2
##  Max GARCH Order:           2
##  Maximum Order:             2
##  Conditional Dist:          norm
##  h.start:                   3
##  llh.start:                 1
##  Length of Series:          20
##  Recursion Init:            mci
##  Series Scale:              0.01437149
## 
## Parameter Initialization:
##  Initial Parameters:          $params
##  Limits of Transformations:   $U, $V
##  Which Parameters are Fixed?  $includes
##  Parameter Matrix:
##                      U          V    params includes
##     mu     -2.18314624   2.183146 0.2183146     TRUE
##     omega   0.00000100 100.000000 0.1000000     TRUE
##     alpha1  0.00000001   1.000000 0.0500000     TRUE
##     alpha2  0.00000001   1.000000 0.0500000     TRUE
##     gamma1 -0.99999999   1.000000 0.1000000    FALSE
##     gamma2 -0.99999999   1.000000 0.1000000    FALSE
##     beta1   0.00000001   1.000000 0.4000000     TRUE
##     beta2   0.00000001   1.000000 0.4000000     TRUE
##     delta   0.00000000   2.000000 2.0000000    FALSE
##     skew    0.10000000  10.000000 1.0000000    FALSE
##     shape   1.00000000  10.000000 4.0000000    FALSE
##  Index List of Parameters to be Optimized:
##     mu  omega alpha1 alpha2  beta1  beta2 
##      1      2      3      4      7      8 
##  Persistence:                  0.9 
## 
## 
## --- START OF TRACE ---
## Selected Algorithm: nlminb 
## 
## R coded nlminb Solver: 
## 
##   0:     28.170183: 0.218315 0.100000 0.0500000 0.0500000 0.400000 0.400000
##   1:     27.924754: 0.211097 0.481954 1.00000e-08 1.00000e-08 0.222692 0.329487
##   2:     27.922363: 0.211343 0.445889 1.00000e-08 1.00000e-08 0.184822 0.291770
##   3:     27.814952: 0.211349 0.457380 0.0267217 1.00000e-08 0.194658 0.301610
##   4:     27.703492: 0.212334 0.427057 0.138508 1.00000e-08 0.152600 0.263799
##   5:     27.691258: 0.212541 0.422030 0.134272 1.00000e-08 0.140332 0.254599
##   6:     27.679154: 0.213447 0.442141 0.156872 1.00000e-08 0.124708 0.253676
##   7:     27.654597: 0.213564 0.454349 0.158592 1.00000e-08 0.0972009 0.237290
##   8:     27.615387: 0.216835 0.519803 0.208701 1.00000e-08 0.0334769 0.228921
##   9:     27.562178: 0.218340 0.560548 0.205485 1.00000e-08 1.00000e-08 0.154663
##  10:     27.474363: 0.259219 0.729793 0.215570 1.00000e-08 1.00000e-08 1.00000e-08
##  11:     27.463940: 0.282105 0.706564 0.252274 1.00000e-08 1.00000e-08 1.00000e-08
##  12:     27.461109: 0.276170 0.692816 0.265528 1.00000e-08 1.00000e-08 1.00000e-08
##  13:     27.460400: 0.275302 0.682440 0.269024 1.00000e-08 1.00000e-08 1.00000e-08
##  14:     27.460358: 0.275302 0.681351 0.267558 1.00000e-08 1.00000e-08 1.00000e-08
##  15:     27.460357: 0.275366 0.681484 0.267124 1.00000e-08 1.00000e-08 1.00000e-08
##  16:     27.460357: 0.275371 0.681500 0.267113 1.00000e-08 1.00000e-08 1.00000e-08
## 
## Final Estimate of the Negative LLH:
##  LLH:  -57.38982    norm LLH:  -2.869491 
##           mu        omega       alpha1       alpha2        beta1        beta2 
## 0.0039574883 0.0001407567 0.2671125192 0.0000000100 0.0000000100 0.0000000100 
## 
## R-optimhess Difference Approximated Hessian Matrix:
##                   mu         omega        alpha1        alpha2         beta1
## mu     -109295.61079    -387096.35    116.902458   -138.231690    -86.293074
## omega  -387096.35164 -317763896.55 -37126.647129 -34668.561113 -53439.972054
## alpha1     116.90246     -37126.65    -12.791437     -3.714648     -8.755150
## alpha2    -138.23169     -34668.56     -3.714648     -5.585587     -8.966858
## beta1      -86.29307     -53439.97     -8.755150     -8.966858    -10.527384
## beta2     -386.92494     -51715.58    -10.580606     -7.666944    -10.172590
##                beta2
## mu       -386.924942
## omega  -51715.581804
## alpha1    -10.580606
## alpha2     -7.666944
## beta1     -10.172590
## beta2      -8.903082
## attr(,"time")
## Time difference of 0.00468421 secs
## 
## --- END OF TRACE ---
## Warning in sqrt(diag(fit$cvar)): Se han producido NaNs
## 
## Time to Estimate Parameters:
##  Time difference of 0.01228309 secs
coef(modeloo2)
##           mu        omega       alpha1       alpha2        beta1        beta2 
## 0.0039574883 0.0001407567 0.2671125192 0.0000000100 0.0000000100 0.0000000100

varianza(t) = 0.00014 + 0.0000000001R^2_(t-1) + 0.000000001R^2_(t-1) + 0.2671u^2_(t-1) + 0.000000001u^2(t-2)

predict(modeloo2, n.ahead=5, plot=TRUE)

##   meanForecast  meanError standardDeviation lowerInterval upperInterval
## 1  0.003957488 0.01193131        0.01193131   -0.01942744    0.02734242
## 2  0.003957488 0.01337093        0.01337093   -0.02224906    0.03016403
## 3  0.003957488 0.01372995        0.01372995   -0.02295273    0.03086770
## 4  0.003957488 0.01382427        0.01382427   -0.02313759    0.03105257
## 5  0.003957488 0.01384936        0.01384936   -0.02318676    0.03110173