#BAsE DE DADOS:

library(GetBCBData)

cambio = gbcbd_get_series(id = 11753, first.date = "2008-01-01", last.date = Sys.Date())
cambio = cambio[,0:2]
cambio = data.frame(cambio, row.names = 1)
colnames(cambio) = c("Câmbio")
cambio = ts(cambio, start = 2008, frequency = 12)

plot(cambio)

#QUESTÃO A: Proponha um modelo GARCH para os dados. Estime utilizando a distribuicao normal e t-student.

library(rugarch)

#MODELO COM DISTRIBUIÇÃO NORMAL:
dist_norm = ugarchspec(mean.model = list(armaOrder =c(0 ,0)), 
                       variance.model = list(model = "sGARCH", 
                                             garchOrder = c(1,1)),
                       distribution.model = "norm")

fit = ugarchfit(data = cambio, spec = dist_norm, out.sample = 20); fit
## 
## *---------------------------------*
## *          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       75.5224     0.64129 117.766922 0.000000
## omega     4.1093     1.84192   2.230972 0.025683
## alpha1    0.9990     0.11703   8.536177 0.000000
## beta1     0.0000     0.02498   0.000001 0.999999
## 
## Robust Standard Errors:
##         Estimate  Std. Error   t value Pr(>|t|)
## mu       75.5224    1.971346 38.310057 0.000000
## omega     4.1093    1.485347  2.766535 0.005666
## alpha1    0.9990    0.076576 13.045916 0.000000
## beta1     0.0000    0.024231  0.000001 0.999999
## 
## LogLikelihood : -691.8694 
## 
## Information Criteria
## ------------------------------------
##                    
## Akaike       8.2351
## Bayes        8.3092
## Shibata      8.2341
## Hannan-Quinn 8.2652
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      118.6       0
## Lag[2*(p+q)+(p+q)-1][2]     161.2       0
## Lag[4*(p+q)+(p+q)-1][5]     259.4       0
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                     0.4157  0.5191
## Lag[2*(p+q)+(p+q)-1][5]    0.5610  0.9479
## Lag[4*(p+q)+(p+q)-1][9]    2.1761  0.8830
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]  0.007434 0.500 2.000  0.9313
## ARCH Lag[5]  0.279131 1.440 1.667  0.9458
## ARCH Lag[7]  1.469520 2.315 1.543  0.8275
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  4.1244
## Individual Statistics:              
## mu     1.09679
## omega  0.03229
## alpha1 0.56846
## beta1  0.86458
## 
## 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.5855 0.5590    
## Negative Sign Bias  0.1489 0.8818    
## Positive Sign Bias  0.7890 0.4312    
## Joint Effect        1.1619 0.7622    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     376.2    3.832e-68
## 2    30     467.4    1.401e-80
## 3    40     456.1    1.509e-72
## 4    50     453.8    5.906e-67
## 
## 
## Elapsed time : 0.09539509
#MODELO COM DISTRIBUIÇÃO T-STUDENT:
dist_t = ugarchspec(mean.model = list(armaOrder =c(0 ,0)), 
                       variance.model = list(model = "sGARCH", 
                                             garchOrder = c(1,1)),
                       distribution.model = "sstd")

fit2 = ugarchfit(data = cambio, spec = dist_t, out.sample = 20); fit2
## 
## *---------------------------------*
## *          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      97.22140     0.76277 127.4584 0.000000
## omega   16.44147     4.81211   3.4167 0.000634
## alpha1   0.97234     0.01309  74.2840 0.000000
## beta1    0.00000     0.15785   0.0000 1.000000
## skew     3.17646     0.53268   5.9632 0.000000
## shape    9.63767     8.93911   1.0781 0.280968
## 
## Robust Standard Errors:
##         Estimate  Std. Error   t value Pr(>|t|)
## mu      97.22140     0.91817 105.88561 0.000000
## omega   16.44147     6.90362   2.38157 0.017239
## alpha1   0.97234     0.41132   2.36394 0.018082
## beta1    0.00000     0.22636   0.00000 1.000000
## skew     3.17646     1.13886   2.78916 0.005284
## shape    9.63767    23.10696   0.41709 0.676613
## 
## LogLikelihood : -675.0958 
## 
## Information Criteria
## ------------------------------------
##                    
## Akaike       8.0603
## Bayes        8.1714
## Shibata      8.0579
## Hannan-Quinn 8.1054
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      148.2       0
## Lag[2*(p+q)+(p+q)-1][2]     212.2       0
## Lag[4*(p+q)+(p+q)-1][5]     383.4       0
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic   p-value
## Lag[1]                      33.87 5.898e-09
## Lag[2*(p+q)+(p+q)-1][5]     34.70 1.483e-09
## Lag[4*(p+q)+(p+q)-1][9]     35.34 1.465e-08
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]   0.07553 0.500 2.000  0.7834
## ARCH Lag[5]   0.36727 1.440 1.667  0.9219
## ARCH Lag[7]   0.66478 2.315 1.543  0.9611
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  16.9992
## Individual Statistics:             
## mu     0.7723
## omega  0.5180
## alpha1 0.1197
## beta1  1.3889
## skew   0.1006
## shape  1.4265
## 
## 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           3.1327 0.002052 ***
## Negative Sign Bias  2.8974 0.004278 ***
## Positive Sign Bias  0.1457 0.884353    
## Joint Effect       13.6182 0.003474 ***
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     211.0    2.183e-34
## 2    30     205.1    2.058e-28
## 3    40     218.5    7.990e-27
## 4    50     245.1    7.046e-28
## 
## 
## Elapsed time : 0.1993411

#QUESTÃO B: Proponha um modelo GJR para os dados. Estime utilizando a distribuição normal e t-student.

library(rugarch)

#MODELO COM DISTRIBUIÇÃO NORMAL:
gjr_norm = ugarchspec(mean.model = list(armaOrder =c(0 ,0)), 
                       variance.model = list(model = "gjrGARCH", 
                                             garchOrder = c(2,2)),
                       distribution.model = "norm")

fit3 = ugarchfit(data = cambio, spec = gjr_norm, out.sample = 20); fit3 
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : gjrGARCH(2,2)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## mu     76.960673    0.194913   394.85        0
## omega   1.990244    0.006826   291.56        0
## alpha1  0.331061    0.000963   343.65        0
## alpha2  0.029331    0.000105   279.78        0
## beta1   0.611169    0.001699   359.69        0
## beta2   0.071633    0.000231   310.74        0
## gamma1  0.608275    0.001685   361.04        0
## gamma2 -0.743058    0.001969  -377.36        0
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## mu     76.960673    4.034399  19.0761 0.000000
## omega   1.990244    0.025168  79.0793 0.000000
## alpha1  0.331061    0.095603   3.4629 0.000534
## alpha2  0.029331    0.001076  27.2545 0.000000
## beta1   0.611169    0.144171   4.2392 0.000022
## beta2   0.071633    0.063562   1.1270 0.259745
## gamma1  0.608275    0.143623   4.2352 0.000023
## gamma2 -0.743058    0.142508  -5.2141 0.000000
## 
## LogLikelihood : -701.998 
## 
## Information Criteria
## ------------------------------------
##                    
## Akaike       8.4023
## Bayes        8.5505
## Shibata      8.3981
## Hannan-Quinn 8.4625
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      144.3       0
## Lag[2*(p+q)+(p+q)-1][2]     200.4       0
## Lag[4*(p+q)+(p+q)-1][5]     337.2       0
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                          statistic   p-value
## Lag[1]                       52.13 5.196e-13
## Lag[2*(p+q)+(p+q)-1][11]     75.44 0.000e+00
## Lag[4*(p+q)+(p+q)-1][19]     90.15 0.000e+00
## d.o.f=4
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale  P-Value
## ARCH Lag[5]     2.713 0.500 2.000 0.099560
## ARCH Lag[7]     5.140 1.473 1.746 0.112985
## ARCH Lag[9]    15.285 2.402 1.619 0.001716
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  9.576
## Individual Statistics:             
## mu     0.6797
## omega  0.6805
## alpha1 0.6806
## alpha2 0.6805
## beta1  0.6770
## beta2  0.6796
## gamma1 0.6805
## gamma2 0.6792
## 
## 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           2.8690 0.004659 ***
## Negative Sign Bias  2.6889 0.007910 ***
## Positive Sign Bias  0.1826 0.855335    
## Joint Effect        9.4614 0.023745  **
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     245.3    2.740e-41
## 2    30     256.1    3.261e-38
## 3    40     268.6    4.554e-36
## 4    50     270.9    1.762e-32
## 
## 
## Elapsed time : 0.626899
#MODELO COM DISTRIBUIÇÃO T-STUDENT:
gjr_t = ugarchspec(mean.model = list(armaOrder =c(0 ,0)), 
                       variance.model = list(model = "gjrGARCH", 
                                             garchOrder = c(3,3)),
                       distribution.model = "sstd")

fit4 = ugarchfit(data = cambio, spec = gjr_t, out.sample = 30); fit4
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : gjrGARCH(3,3)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : sstd 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error   t value Pr(>|t|)
## mu     98.179244    0.245085  400.5931 0.000000
## omega   8.857862    0.028703  308.6065 0.000000
## alpha1  0.595140    0.002041  291.6560 0.000000
## alpha2  0.000016    0.000009    1.8240 0.068158
## alpha3  0.000013    0.000012    1.0532 0.292247
## beta1   0.101955    0.000338  301.6046 0.000000
## beta2   0.221331    0.000721  307.1210 0.000000
## beta3   0.000036    0.000000  238.7305 0.000000
## gamma1  0.824905    0.003006  274.4388 0.000000
## gamma2 -0.999949    0.003238 -308.7860 0.000000
## gamma3  0.273366    0.000981  278.8018 0.000000
## skew    3.507146    0.551088    6.3640 0.000000
## shape   7.342478    1.359707    5.4000 0.000000
## 
## Robust Standard Errors:
##         Estimate  Std. Error   t value Pr(>|t|)
## mu     98.179244  2.0254e+02  0.484744 0.627858
## omega   8.857862  1.3063e+02  0.067811 0.945936
## alpha1  0.595140  2.3224e+00  0.256255 0.797754
## alpha2  0.000016  3.8791e-02  0.000404 0.999678
## alpha3  0.000013  6.3687e-02  0.000202 0.999839
## beta1   0.101955  8.3314e-01  0.122374 0.902603
## beta2   0.221331  4.0640e+00  0.054461 0.956568
## beta3   0.000036  9.9300e-04  0.036612 0.970794
## gamma1  0.824905  2.1567e+00  0.382488 0.702099
## gamma2 -0.999949  5.7922e-01 -1.726367 0.084281
## gamma3  0.273366  2.8932e-02  9.448601 0.000000
## skew    3.507146  1.0658e+03  0.003291 0.997374
## shape   7.342478  7.5527e+02  0.009722 0.992243
## 
## LogLikelihood : -613.5045 
## 
## Information Criteria
## ------------------------------------
##                    
## Akaike       7.8806
## Bayes        8.1315
## Shibata      7.8685
## Hannan-Quinn 7.9825
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      130.8       0
## Lag[2*(p+q)+(p+q)-1][2]     187.1       0
## Lag[4*(p+q)+(p+q)-1][5]     336.3       0
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                          statistic   p-value
## Lag[1]                       25.81 3.764e-07
## Lag[2*(p+q)+(p+q)-1][17]     31.82 1.340e-05
## Lag[4*(p+q)+(p+q)-1][29]     35.48 2.987e-04
## d.o.f=6
## 
## Weighted ARCH LM Tests
## ------------------------------------
##              Statistic Shape Scale P-Value
## ARCH Lag[7]      1.076 0.500 2.000  0.2996
## ARCH Lag[9]      3.788 1.485 1.796  0.2348
## ARCH Lag[11]     4.197 2.440 1.677  0.3994
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  25.9836
## Individual Statistics:             
## mu     0.2640
## omega  0.3272
## alpha1 0.3270
## alpha2 0.3272
## alpha3 0.3267
## beta1  0.3270
## beta2  0.3271
## beta3  0.3270
## gamma1 0.3270
## gamma2 0.3273
## gamma3 0.3270
## skew   0.1527
## shape  0.6086
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          2.89 3.15 3.69
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value     prob sig
## Sign Bias           3.0984 0.002313 ***
## Negative Sign Bias  1.3587 0.176232    
## Positive Sign Bias  0.3405 0.733936    
## Joint Effect       15.5240 0.001419 ***
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     183.3    7.035e-29
## 2    30     228.0    8.974e-33
## 3    40     232.2    2.573e-29
## 4    50     224.9    2.274e-24
## 
## 
## Elapsed time : 4.681827

#QUESTÃO C: Compare a persistencia estimada em cada um dos casos.

library(rugarch)

#QUSTÃO A:
ugarchforecast(fit, n.ahead = 10)
## 
## *------------------------------------*
## *       GARCH Model Forecast         *
## *------------------------------------*
## Model: sGARCH
## Horizon: 10
## Roll Steps: 0
## Out of Sample: 10
## 
## 0-roll forecast [T0=jan 2022]:
##      Series Sigma
## T+1   75.52 70.94
## T+2   75.52 70.93
## T+3   75.52 70.93
## T+4   75.52 70.92
## T+5   75.52 70.92
## T+6   75.52 70.91
## T+7   75.52 70.90
## T+8   75.52 70.90
## T+9   75.52 70.89
## T+10  75.52 70.88
ugarchforecast(fit2, n.ahead = 10)
## 
## *------------------------------------*
## *       GARCH Model Forecast         *
## *------------------------------------*
## Model: sGARCH
## Horizon: 10
## Roll Steps: 0
## Out of Sample: 10
## 
## 0-roll forecast [T0=jan 2022]:
##      Series Sigma
## T+1   97.22 48.73
## T+2   97.22 48.22
## T+3   97.22 47.73
## T+4   97.22 47.23
## T+5   97.22 46.75
## T+6   97.22 46.28
## T+7   97.22 45.82
## T+8   97.22 45.36
## T+9   97.22 44.91
## T+10  97.22 44.47
#QUESTÃO B:
ugarchforecast(fit3, n.ahead = 10)
## 
## *------------------------------------*
## *       GARCH Model Forecast         *
## *------------------------------------*
## Model: gjrGARCH
## Horizon: 10
## Roll Steps: 0
## Out of Sample: 10
## 
## 0-roll forecast [T0=jan 2022]:
##      Series Sigma
## T+1   76.96 73.87
## T+2   76.96 85.65
## T+3   76.96 87.57
## T+4   76.96 87.03
## T+5   76.96 85.84
## T+6   76.96 84.48
## T+7   76.96 83.08
## T+8   76.96 81.70
## T+9   76.96 80.33
## T+10  76.96 78.99
ugarchforecast(fit4, n.ahead = 10)
## 
## *------------------------------------*
## *       GARCH Model Forecast         *
## *------------------------------------*
## Model: gjrGARCH
## Horizon: 10
## Roll Steps: 0
## Out of Sample: 10
## 
## 0-roll forecast [T0=mar 2021]:
##      Series Sigma
## T+1   98.18 49.38
## T+2   98.18 57.94
## T+3   98.18 55.54
## T+4   98.18 53.07
## T+5   98.18 52.42
## T+6   98.18 52.18
## T+7   98.18 51.72
## T+8   98.18 51.15
## T+9   98.18 50.60
## T+10  98.18 50.08

Conclusão: O modelo com distribuição normal apresentar uma previsão da valotilidade maior quando comparado com o modelo com distribuição t-student.