2 Q Cummulative volatility and cummulative p%VaR

2.1 Download the daily IPyC index from 2018 to date and calculate daily returns.

# Set all the working directory 
setwd("~/Desktop/Financial Econometrics II ")
library(quantmod)
IPCindex<-getSymbols("^MXX", from="2018-01-01", to="2021-04-28",auto.assign=FALSE)[,6]
IPCreturn <- na.omit(diff(log(IPCindex)))
names(IPCreturn)<-c("return")

hist(IPCreturn, breaks=40)

2.2 Run a GARCH(1,1) model with the daily returns.

library(rugarch)
## Loading required package: parallel
## 
## Attaching package: 'rugarch'
## The following object is masked from 'package:stats':
## 
##     sigma
Especgarch1=ugarchspec(variance.model= list(model= "sGARCH", garchOrder= c(1, 1), 
submodel= NULL,  variance.targeting= FALSE), 
mean.model= list(armaOrder= c(0, 0), include.mean= TRUE, archm= FALSE, 
archpow= 0, arfima= FALSE,  external.regressors= NULL, archex= FALSE), 
distribution.model= "norm", start.pars= list(), fixed.pars= list())

#Estimación del GARCH(1,1)

garch1<- ugarchfit(spec=Especgarch1, data=IPCreturn$return)
garch1
## 
## *---------------------------------*
## *          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.000067    0.000323  0.20726 0.835809
## omega   0.000006    0.000002  3.13733 0.001705
## alpha1  0.143639    0.021486  6.68538 0.000000
## beta1   0.810415    0.019654 41.23353 0.000000
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.000067    0.000329  0.20332 0.838887
## omega   0.000006    0.000003  2.03500 0.041851
## alpha1  0.143639    0.019550  7.34725 0.000000
## beta1   0.810415    0.033569 24.14150 0.000000
## 
## LogLikelihood : 2623.694 
## 
## Information Criteria
## ------------------------------------
##                     
## Akaike       -6.3049
## Bayes        -6.2822
## Shibata      -6.3050
## Hannan-Quinn -6.2962
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic  p-value
## Lag[1]                      7.359 0.006672
## Lag[2*(p+q)+(p+q)-1][2]     7.578 0.008369
## Lag[4*(p+q)+(p+q)-1][5]     8.264 0.025327
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                     0.5599 0.45430
## Lag[2*(p+q)+(p+q)-1][5]    3.5506 0.31564
## Lag[4*(p+q)+(p+q)-1][9]   10.2552 0.04422
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale  P-Value
## ARCH Lag[3]     3.645 0.500 2.000 0.056230
## ARCH Lag[5]     5.272 1.440 1.667 0.088905
## ARCH Lag[7]    11.592 2.315 1.543 0.007767
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  6.369
## Individual Statistics:             
## mu     0.2703
## omega  0.5169
## alpha1 0.2165
## beta1  0.3623
## 
## 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.4834 0.6289    
## Negative Sign Bias  0.1837 0.8543    
## Positive Sign Bias  0.3662 0.7143    
## Joint Effect        1.4094 0.7033    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     26.83      0.10858
## 2    30     36.91      0.14864
## 3    40     56.36      0.03549
## 4    50     62.20      0.09755
## 
## 
## Elapsed time : 0.111738

2.3 Using this GARCH(1,1) model, forecast the daily volatility and 1%VaR for the next future 10 days (2 weeks from now). For the 1%VaR assume fat-tail distribution.

I do a prediction for the next 10 days

garch1.prediction <- ugarchforecast(garch1,n.ahead= 10)

garch1.prediction will be a very special R class: an uGARCHforecast class. This is special R class object that stores information about the GARCH prediction.

For this type of R class objects, we use the @ character to get access to the content of each element stored in this object.

The forecast for the daily return is stored in:

garch1.prediction@forecast$seriesFor
##        2021-04-27
## T+1  6.690189e-05
## T+2  6.690189e-05
## T+3  6.690189e-05
## T+4  6.690189e-05
## T+5  6.690189e-05
## T+6  6.690189e-05
## T+7  6.690189e-05
## T+8  6.690189e-05
## T+9  6.690189e-05
## T+10 6.690189e-05

The forecast for the standard deviation of returns (volatility) is stored in:

pred_vol <- garch1.prediction@forecast$sigmaFor
pred_vol
##       2021-04-27
## T+1  0.008016813
## T+2  0.008204170
## T+3  0.008379016
## T+4  0.008542493
## T+5  0.008695594
## T+6  0.008839190
## T+7  0.008974046
## T+8  0.009100844
## T+9  0.009220191
## T+10 0.009332631

I can get the cumulative volatility for the 10 days: I first get the variances from the forecasted standard deviations (volatilities):

pred_variance = pred_vol^2
pred_variance 
##        2021-04-27
## T+1  6.426929e-05
## T+2  6.730841e-05
## T+3  7.020791e-05
## T+4  7.297418e-05
## T+5  7.561335e-05
## T+6  7.813127e-05
## T+7  8.053350e-05
## T+8  8.282536e-05
## T+9  8.501192e-05
## T+10 8.709801e-05

Now I can ADD these 10 variances:

pred_variance10 <- sum(pred_variance)
pred_variance10
## [1] 0.0007639732

Now that I can have the 10-days VARIANCE, I easily can get the 10-day VOLATILITY:

pred_volatility10 <- sqrt(pred_variance10)
pred_volatility10
## [1] 0.02764007

Now I can get the 10-day 1%VaR p%VaR = mean(return) + t * volatility

mean_return=mean(IPCreturn$return)
# I calculate the kurtosis 
library(moments)
kurtosis = as.numeric(kurtosis(IPCreturn))
df = 6/ kurtosis + 4

# I calculate the t value for the VaR according to 1% probability
t1 = qt(0.01, df)

# Now I can calculate the cumulative  1%VaR for the next 10 days: 

VaR1 <- mean_return + t1 *pred_volatility10
VaR1
## [1] -0.09390613

INTERPRETATION OF 1%VaR

THIS 1%VaR CUMMULATIVE PREDICTION OF 10 DAYS IS - 9.390613 WHICH SAYS THAT ASSUMING FAT-TAIL DISTRIBUTION OF THE IPCyC, I CAN LOSE IN THE NEXT 10 DAYS - 9.390613% OR MORE WITH A PROBABILITY OF 1%.

2 Q ARMA mean model + volatility model

You have to design a Market model for a stock using an ARMA(1,1) model, specifying a GARCH(1,1) as the volatility model. Remember the market model uses the market returns as independent variable (explanatory or regressor), and the stock return as the dependent variable.

# I download daily data of stock 
sp1 <-getSymbols("AMXL.MX", from="2018-01-01", to="2021-04-28",auto.assign=FALSE)[,6]
AMXLreturn <- na.omit(diff(log(sp1)))
names(AMXLreturn)<-c("return")

hist(sp1, breaks=40)

Now I run the model including the mean model and the GARCH(1,1) volatility model.

prices<-merge(Ad(sp1),Ad(IPCindex))
r <- na.omit(diff(log(prices)))

Especgarch2=ugarchspec(variance.model= list(model= "sGARCH", garchOrder= c(1, 1), 
submodel= NULL,  variance.targeting= FALSE), 
mean.model= list(armaOrder= c(1, 1), include.mean= TRUE, archm= FALSE, 
archpow= 0, arfima= FALSE,  external.regressors= NULL, archex= FALSE), 
distribution.model= "norm", start.pars= list(), fixed.pars= list())

#Estimación del GARCH(1,1)

garch2<- ugarchfit(spec=Especgarch2, data=r$AMXL.MX.Adjusted)
garch2
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : sGARCH(1,1)
## Mean Model   : ARFIMA(1,0,1)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error   t value Pr(>|t|)
## mu      0.000033    0.000532  0.061914 0.950631
## ar1    -0.498611    0.320079 -1.557775 0.119287
## ma1     0.539548    0.309876  1.741176 0.081653
## omega   0.000005    0.000003  1.658450 0.097227
## alpha1  0.049666    0.008385  5.923487 0.000000
## beta1   0.931219    0.009896 94.096159 0.000000
## 
## Robust Standard Errors:
##         Estimate  Std. Error   t value Pr(>|t|)
## mu      0.000033    0.000479  0.068744 0.945194
## ar1    -0.498611    0.175364 -2.843285 0.004465
## ma1     0.539548    0.176364  3.059292 0.002219
## omega   0.000005    0.000007  0.718407 0.472507
## alpha1  0.049666    0.015111  3.286713 0.001014
## beta1   0.931219    0.021670 42.972248 0.000000
## 
## LogLikelihood : 2261.316 
## 
## Information Criteria
## ------------------------------------
##                     
## Akaike       -5.4280
## Bayes        -5.3939
## Shibata      -5.4281
## Hannan-Quinn -5.4149
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                     0.2249  0.6353
## Lag[2*(p+q)+(p+q)-1][5]    2.9437  0.5076
## Lag[4*(p+q)+(p+q)-1][9]    6.9205  0.1358
## d.o.f=2
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                  1.040e-07  0.9997
## Lag[2*(p+q)+(p+q)-1][5] 1.750e+00  0.6785
## Lag[4*(p+q)+(p+q)-1][9] 3.092e+00  0.7437
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]     2.749 0.500 2.000 0.09732
## ARCH Lag[5]     2.964 1.440 1.667 0.29501
## ARCH Lag[7]     3.765 2.315 1.543 0.38168
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  1.3438
## Individual Statistics:              
## mu     0.10644
## ar1    0.27092
## ma1    0.23713
## omega  0.10207
## alpha1 0.10418
## beta1  0.07832
## 
## 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.5383 0.5905    
## Negative Sign Bias  0.3828 0.7020    
## Positive Sign Bias  0.8400 0.4012    
## Joint Effect        0.8767 0.8310    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     37.18     0.007527
## 2    30     49.61     0.009937
## 3    40     65.03     0.005547
## 4    50     81.09     0.002667
## 
## 
## Elapsed time : 0.1984389
Especgarch3=ugarchspec(variance.model= list(model= "sGARCH", garchOrder= c(1, 1), 
submodel= NULL,external.regressors= r$MXX.Adjusted,  variance.targeting= FALSE), 
mean.model= list(armaOrder= c(1, 1), include.mean= TRUE, archm= FALSE, 
archpow= 0, arfima= FALSE,  external.regressors= NULL , archex= FALSE), 
distribution.model= "norm", start.pars= list(), fixed.pars= list())

#Estimación del GARCH(1,1)

garch3<- ugarchfit(spec=Especgarch3, data=r$AMXL.MX)
garch3
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : sGARCH(1,1)
## Mean Model   : ARFIMA(1,0,1)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error   t value Pr(>|t|)
## mu      0.000033    0.000547  0.060215 0.951984
## ar1    -0.498587    0.320102 -1.557587 0.119331
## ma1     0.539526    0.309922  1.740846 0.081711
## omega   0.000005    0.000003  1.928013 0.053853
## alpha1  0.049665    0.008909  5.574441 0.000000
## beta1   0.931220    0.012087 77.041507 0.000000
## vxreg1  0.000000    0.000364  0.000001 0.999999
## 
## Robust Standard Errors:
##         Estimate  Std. Error   t value Pr(>|t|)
## mu      0.000033    0.000462  0.071250 0.943199
## ar1    -0.498587    0.175692 -2.837852 0.004542
## ma1     0.539526    0.177024  3.047762 0.002306
## omega   0.000005    0.000006  0.869494 0.384577
## alpha1  0.049665    0.016624  2.987565 0.002812
## beta1   0.931220    0.017989 51.765212 0.000000
## vxreg1  0.000000    0.000534  0.000001 0.999999
## 
## LogLikelihood : 2261.316 
## 
## Information Criteria
## ------------------------------------
##                     
## Akaike       -5.4256
## Bayes        -5.3858
## Shibata      -5.4257
## Hannan-Quinn -5.4103
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      0.225  0.6353
## Lag[2*(p+q)+(p+q)-1][5]     2.944  0.5076
## Lag[4*(p+q)+(p+q)-1][9]     6.921  0.1358
## d.o.f=2
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                  9.704e-08  0.9998
## Lag[2*(p+q)+(p+q)-1][5] 1.750e+00  0.6785
## Lag[4*(p+q)+(p+q)-1][9] 3.092e+00  0.7437
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]     2.749 0.500 2.000 0.09731
## ARCH Lag[5]     2.964 1.440 1.667 0.29500
## ARCH Lag[7]     3.765 2.315 1.543 0.38167
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  3.4156
## Individual Statistics:              
## mu     0.10644
## ar1    0.27093
## ma1    0.23714
## omega  0.10205
## alpha1 0.10418
## beta1  0.07831
## vxreg1 1.39128
## 
## 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.5383 0.5905    
## Negative Sign Bias  0.3828 0.7020    
## Positive Sign Bias  0.8400 0.4012    
## Joint Effect        0.8767 0.8310    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     37.18     0.007527
## 2    30     49.61     0.009937
## 3    40     65.03     0.005547
## 4    50     81.09     0.002667
## 
## 
## Elapsed time : 0.164727

INTERPRETATION OF THE MODEL

WE ARE MODELING AN ARMA mean model + volatility model of AMERICA MOVIL STOCK.

  1. THE AR1 IS -0.498587 WICH IS NEGATIVE AND STATISTICALLY SIGNIFICANT WICH MEANS AFTER CONSIDERING THE EFFECTS OF THE ERRORS OF YESTERDAY THEN THE RETURNS WITH A LAG1 ARE CORRELATED WITH THE RETURNS OF TODAY. IN OTHER WORDS -49.85% OF THE INFORMATION OF ONE DAY AGO OF THE RETURNS ARE PASSSED TO TODAYS RETURNS.

  2. THE MA1 IS 0.539526 WICH IS POSITIVE AND STATISTICALLY SIGNIFICANT WICH MEANS AFTER CONSIDERING THE EFFECTS OF THE RETURNS OF YESTERDAY THE RANDOM SHOCKS OF ONE DAY AGO ARE CORRELATED WITH THE RETURNS OF TODAY. IN OTHER WORDS 53.95% OF THE INFORMATION OF THE ERRORS(RANDOM SHOCKS) IS PASSED TO THE RETURNS OF TODAY.

  3. ABOUT THE VARIANCE WE CAN SAY THAT AFTER CONSIDERING THE EFFECT OF THE ARMA MODEL, I SEE THAT THE beta1 COEFFICIENT IS POSITIVE 0.931220 AND STATISTICALLY SIGNIFICANT. THIS MEANS THAT ABOUT 93.122% OF THE 1 DAY AGO RETURN VARIANCE IS PASSED TO THE RETURN VARIANCE OF TODAY. AND SINCE THE TH GARCH COEFFICIENT IS GREATER THAN 0.9, AND IT IS VERY SIGNIFICANT I CAN SAY THAT THERE IS CLUSTERING OF VOLATILITY.

  4. WE ALSO SEE THAT THE SQUARED ERROR OF YESTERDAY IS POSITIVELY AND SIGNIFICANTLY RELATED TO THE RETURN VARIANCE OF TODAY. ABOUT 0.049665 or 4.9665% OF ONE DAY AGO IS PASSED TO THE RETURN VARIANCE OF TODAY.

Practicing with ARCH models and Value at Risk (optional)

Prediction of 10 days:

garch2.prediction <- ugarchforecast(garch3,n.ahead= 10)
pred_vol2 = garch2.prediction@forecast$sigmaFor
pred_vol2
##      2021-04-27
## T+1  0.01246632
## T+2  0.01255539
## T+3  0.01264214
## T+4  0.01272666
## T+5  0.01280902
## T+6  0.01288929
## T+7  0.01296755
## T+8  0.01304386
## T+9  0.01311827
## T+10 0.01319086

Then I calculate the 5%VaR assuming fat-tail distribution

kurtosis2 = as.numeric(kurtosis(sp1))
kurtosis2
## [1] 2.643347
mean_sp = mean(AMXLreturn)
df = (6/kurtosis2) + 4
df
## [1] 6.269849
t2 = qt(0.05,df)

VaR_5 = mean_sp + t2 * pred_vol2
VaR_5
##       2021-04-27
## T+1  -0.02412870
## T+2  -0.02430045
## T+3  -0.02446773
## T+4  -0.02463071
## T+5  -0.02478952
## T+6  -0.02494432
## T+7  -0.02509523
## T+8  -0.02524237
## T+9  -0.02538587
## T+10 -0.02552584

5%ES

t3 = qt(0.05/2,df)
ES_5 = mean_sp + t3*pred_vol2
ES_5
##       2021-04-27
## T+1  -0.03027839
## T+2  -0.03049407
## T+3  -0.03070415
## T+4  -0.03090882
## T+5  -0.03110827
## T+6  -0.03130266
## T+7  -0.03149218
## T+8  -0.03167696
## T+9  -0.03185717
## T+10 -0.03203294

Now I calculate the cumulative 5%VaR

garch2.prediction <- ugarchforecast(garch2,n.ahead= 5)
pred_vol3 = garch2.prediction@forecast$sigmaFor
pred_vol3
##     2021-04-27
## T+1 0.01246630
## T+2 0.01255536
## T+3 0.01264212
## T+4 0.01272664
## T+5 0.01280900

Now I can get the variances from the forecasted standard deviations (volatilities):

pred_variance2 = pred_vol3^2
pred_variance2
##       2021-04-27
## T+1 0.0001554087
## T+2 0.0001576372
## T+3 0.0001598231
## T+4 0.0001619673
## T+5 0.0001640704

Now I sum the variance to get a cummulative one

variance_5 = sum(pred_variance2)
variance_5
## [1] 0.0007989066

Now that I can have the 5-days VARIANCE, I easily can get the 5-day VOLATILITY:

vol_5 = sqrt(variance_5)
vol_5
## [1] 0.02826494

Now I can get the 5-day 5%VaR p%VaR = mean(return) + t * volatility

Cumu_VaR_5 = mean_sp + t2 * vol_5
Cumu_VaR_5
## [1] -0.05459325

Cumulative 5%ES for 5 days

Cumu_ES_5 = mean_sp + t3*vol_5
Cumu_ES_5
## [1] -0.06853646