Librerias Requeridas

library(readxl)
library(tseries)
library(fGarch)

Importamos la base de Datos

La base de datos a importar tiene como fuente el Banco Central De Reserva Del Peru

TCM <- read_excel("C:/Users/aldair/Desktop/TCM.xlsx")
print(TCM)
## # A tibble: 344 x 2
##    Fecha                 TCM
##    <dttm>              <dbl>
##  1 1992-01-01 00:00:00 0.981
##  2 1992-02-01 00:00:00 0.966
##  3 1992-03-01 00:00:00 0.948
##  4 1992-04-01 00:00:00 1.01 
##  5 1992-05-01 00:00:00 1.12 
##  6 1992-06-01 00:00:00 1.17 
##  7 1992-07-01 00:00:00 1.23 
##  8 1992-08-01 00:00:00 1.28 
##  9 1992-09-01 00:00:00 1.36 
## 10 1992-10-01 00:00:00 1.54 
## # ... with 334 more rows

Graficamos La Base De Datos

plot(TCM$TCM, x=TCM$Fecha,type= "l", col="blue", xlab="Tiempo", ylab="TCM");grid()

Calculamos los Retornos

RTCM=ts(diff(log(TCM$TCM)))

Correlogramas

acf(RTCM,main="Funcion de Autocorrelacion", xlab="Rezagos");grid()

pacf(RTCM,main="Funcion  Parcial de Autocorrelacion", xlab="Rezagos");grid()

Estimacion del mejor modelo ARMA

Para estimar un modelo ARMA es necesario conocer los valores que puede tomar \(p\), en la parte Autoregresiva \(AR(p)\), estos valores se pueden visualizar en el correlograma de la funcion de autocorrelacion parcial.

\[p=[1;3;5]\] Y para la parte de media movil \(MA(q)\), sus valores se pueden apreciar en el correlograma de funcion de autocorrelacion. \[ q= [1;2;3]\]

ARMA11=arma(RTCM,order = c(1,1))
print(summary(ARMA11)$aic)
## [1] -1960.628
ARMA12=arma(RTCM,order = c(1,2))
print(summary(ARMA12)$aic)
## [1] -1969.886
ARMA13=arma(RTCM,order = c(1,3))
print(summary(ARMA13)$aic)
## [1] -2056.494
ARMA31=arma(RTCM,order = c(3,1))
print(summary(ARMA31)$aic)
## [1] -2062.07
ARMA32=arma(RTCM,order = c(3,2))
print(summary(ARMA32)$aic)
## [1] -2060.116
ARMA33=arma(RTCM,order = c(3,3))
print(summary(ARMA33)$aic)
## [1] -2056.937
ARMA51=arma(RTCM,order = c(5,1))
print(summary(ARMA51)$aic)
## [1] -2054.448
ARMA52=arma(RTCM,order = c(5,2))
print(summary(ARMA52)$aic)
## [1] -2062.453
ARMA55=arma(RTCM,order = c(5,5))
print(summary(ARMA55)$aic)
## [1] -2051.857

El mejor modelo arma considernado el factor de \(AIC = -2062.453\), es \(MA(5,2)\).

Rescatamos los ERRORES del mejor modelo ARMA

e <- ARMA52$residuals
e2 = e^2
e_r=e2[6:343]
acf(e_r)

pacf(e_r)

GARCH11= garchFit(formula = ~garch(1,1),data = e_r,trace = T)
## 
## Series Initialization:
##  ARMA Model:                arma
##  Formula Mean:              ~ arma(0, 0)
##  GARCH Model:               garch
##  Formula Variance:          ~ garch(1, 1)
##  ARMA Order:                0 0
##  Max ARMA Order:            0
##  GARCH Order:               1 1
##  Max GARCH Order:           1
##  Maximum Order:             1
##  Conditional Dist:          norm
##  h.start:                   2
##  llh.start:                 1
##  Length of Series:          338
##  Recursion Init:            mci
##  Series Scale:              0.000355476
## 
## Parameter Initialization:
##  Initial Parameters:          $params
##  Limits of Transformations:   $U, $V
##  Which Parameters are Fixed?  $includes
##  Parameter Matrix:
##                      U          V    params includes
##     mu     -3.83536499   3.835365 0.3835365     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.8000000     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 
##      1      2      3      5 
##  Persistence:                  0.9 
## 
## 
## --- START OF TRACE ---
## Selected Algorithm: nlminb 
## 
## R coded nlminb Solver: 
## 
##   0:     474.84084: 0.383536 0.100000 0.100000 0.800000
##   1:     474.40333: 0.381980 0.0798362 0.0923533 0.793462
##   2:     472.17676: 0.379060 0.0789948 0.0895957 0.814863
##   3:     468.21951: 0.373808 0.0504275 0.0709053 0.842140
##   4:     465.53658: 0.371894 0.0453946 0.0604258 0.856885
##   5:     459.15911: 0.368293 0.0376594 0.0386724 0.886696
##   6:     455.61917: 0.366799 0.0338170 0.0285883 0.898910
##   7:     445.32413: 0.363812 0.0261418 0.00841111 0.923335
##   8:     440.25458: 0.363292 0.0259014 0.00130428 0.929121
##   9:     438.87718: 0.363135 0.0237917 1.00000e-08 0.933161
##  10:     437.39096: 0.366584 0.0247185 1.00000e-08 0.936130
##  11:     436.69509: 0.369938 0.0248800 1.00000e-08 0.939877
##  12:     435.79197: 0.376914 0.0210338 1.00000e-08 0.943950
##  13:     434.49009: 0.391144 0.0195353 1.00000e-08 0.952327
##  14:     433.77874: 0.404581 0.0118008 1.00000e-08 0.965039
##  15:     433.01276: 0.390028 0.0138592 1.00000e-08 0.967234
##  16:     431.92336: 0.375437 0.0126650 1.00000e-08 0.966871
##  17:     431.66360: 0.360851 0.0118755 1.00000e-08 0.968269
##  18:     431.60899: 0.358116 0.0113143 1.00000e-08 0.970067
##  19:     431.58932: 0.355288 0.0111505 1.00000e-08 0.970090
##  20:     431.58541: 0.352463 0.0113312 1.00000e-08 0.969764
##  21:     431.57833: 0.349913 0.0109612 1.00000e-08 0.970708
##  22:     431.57774: 0.349427 0.0109791 1.00000e-08 0.970566
##  23:     431.57756: 0.349819 0.0110264 1.00000e-08 0.970478
##  24:     431.57756: 0.349759 0.0110218 1.00000e-08 0.970489
##  25:     431.57756: 0.349761 0.0110217 1.00000e-08 0.970490
## 
## Final Estimate of the Negative LLH:
##  LLH:  -2252.836    norm LLH:  -6.665196 
##           mu        omega       alpha1        beta1 
## 1.243317e-04 1.392731e-09 1.000000e-08 9.704896e-01 
## 
## R-optimhess Difference Approximated Hessian Matrix:
##                   mu         omega        alpha1         beta1
## mu     -4.914942e+09  1.702144e+13 -5.133143e+05  1.198461e+06
## omega   1.702144e+13 -7.094317e+19 -3.864144e+12 -3.326719e+12
## alpha1 -5.133143e+05 -3.864144e+12 -2.758660e+05 -3.007409e+05
## beta1   1.198461e+06 -3.326719e+12 -3.007409e+05 -1.803259e+05
## attr(,"time")
## Time difference of 0.01595807 secs
## 
## --- END OF TRACE ---
## Warning in sqrt(diag(fit$cvar)): Se han producido NaNs
## 
## Time to Estimate Parameters:
##  Time difference of 0.131649 secs
summary(GARCH11)
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(1, 1), data = e_r, trace = T) 
## 
## Mean and Variance Equation:
##  data ~ garch(1, 1)
## <environment: 0x0000000018cff058>
##  [data = e_r]
## 
## Conditional Distribution:
##  norm 
## 
## Coefficient(s):
##         mu       omega      alpha1       beta1  
## 1.2433e-04  1.3927e-09  1.0000e-08  9.7049e-01  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##         Estimate  Std. Error  t value Pr(>|t|)    
## mu     1.243e-04   1.425e-05    8.727  < 2e-16 ***
## omega  1.393e-09   2.127e-10    6.547 5.87e-11 ***
## alpha1 1.000e-08          NA       NA       NA    
## beta1  9.705e-01          NA       NA       NA    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  2252.836    normalized:  6.665196 
## 
## Description:
##  Thu Sep 17 18:48:37 2020 by user: ALDAIR EMERSON 
## 
## 
## Standardised Residuals Tests:
##                                 Statistic  p-Value     
##  Jarque-Bera Test   R    Chi^2  61551.51   0           
##  Shapiro-Wilk Test  R    W      0.4555192  0           
##  Ljung-Box Test     R    Q(10)  31.11023   0.0005626288
##  Ljung-Box Test     R    Q(15)  32.78485   0.005026209 
##  Ljung-Box Test     R    Q(20)  35.23174   0.01890419  
##  Ljung-Box Test     R^2  Q(10)  0.09394783 1           
##  Ljung-Box Test     R^2  Q(15)  0.14117    1           
##  Ljung-Box Test     R^2  Q(20)  0.1630713  1           
##  LM Arch Test       R    TR^2   19.47445   0.07770388  
## 
## Information Criterion Statistics:
##       AIC       BIC       SIC      HQIC 
## -13.30672 -13.26148 -13.30700 -13.28869

Por lo tanto el mejor modelo Arima Grach es \(ARIMA (5,1,2)~ GARCH(1,1)\)