##Construcción del Modelo
library(zoo)
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
BTCSP <- read.csv("C:/Users/Personal/Desktop/BTCSP.csv", sep=";")
apple<-BTCSP
apple.zoo=zoo(apple[,-1], order.by=as.Date(strptime(as.character(apple[,1]), "%d.%m.%Y")))
library(rmgarch)
## Loading required package: rugarch
## Loading required package: parallel
##
## Attaching package: 'rugarch'
## The following object is masked from 'package:stats':
##
## sigma
library(PerformanceAnalytics)
## Loading required package: xts
##
## Attaching package: 'xts'
## The following objects are masked from 'package:rmgarch':
##
## first, last
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
#Declaramos el modelo
model<- window(apple.zoo [,"BTC.Close"], start="2014-01-01")
model<- na.approx(na.trim(CalculateReturns(model), side="both"))
##Modelo Garch (1,1)
spec = ugarchspec()
print(spec)
##
## *---------------------------------*
## * GARCH Model Spec *
## *---------------------------------*
##
## Conditional Variance Dynamics
## ------------------------------------
## GARCH Model : sGARCH(1,1)
## Variance Targeting : FALSE
##
## Conditional Mean Dynamics
## ------------------------------------
## Mean Model : ARFIMA(1,0,1)
## Include Mean : TRUE
## GARCH-in-Mean : FALSE
##
## Conditional Distribution
## ------------------------------------
## Distribution : norm
## Includes Skew : FALSE
## Includes Shape : FALSE
## Includes Lambda : FALSE
def.fit = ugarchfit(spec = spec, data = model)
print(def.fit)
##
## *---------------------------------*
## * 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.002540 0.001101 2.3077 0.021014
## ar1 0.814833 0.382341 2.1312 0.033075
## ma1 -0.791863 0.402468 -1.9675 0.049124
## omega 0.000125 0.000026 4.7669 0.000002
## alpha1 0.122571 0.020129 6.0893 0.000000
## beta1 0.825323 0.024812 33.2629 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.002540 0.001164 2.1824 0.029081
## ar1 0.814833 0.541378 1.5051 0.132296
## ma1 -0.791863 0.565336 -1.4007 0.161305
## omega 0.000125 0.000074 1.6862 0.091765
## alpha1 0.122571 0.033899 3.6158 0.000299
## beta1 0.825323 0.052460 15.7325 0.000000
##
## LogLikelihood : 2788.191
##
## Information Criteria
## ------------------------------------
##
## Akaike -3.4712
## Bayes -3.4511
## Shibata -3.4713
## Hannan-Quinn -3.4638
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.1034 0.7478
## Lag[2*(p+q)+(p+q)-1][5] 1.3352 0.9995
## Lag[4*(p+q)+(p+q)-1][9] 4.7756 0.5055
## d.o.f=2
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.3453 0.5568
## Lag[2*(p+q)+(p+q)-1][5] 1.0215 0.8548
## Lag[4*(p+q)+(p+q)-1][9] 1.3170 0.9692
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.2712 0.500 2.000 0.6025
## ARCH Lag[5] 0.3865 1.440 1.667 0.9165
## ARCH Lag[7] 0.4865 2.315 1.543 0.9796
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 1.3331
## Individual Statistics:
## mu 0.32344
## ar1 0.03306
## ma1 0.03301
## omega 0.10592
## alpha1 0.07153
## beta1 0.10984
##
## 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.0648 0.2871
## Negative Sign Bias 1.1088 0.2677
## Positive Sign Bias 0.7929 0.4279
## Joint Effect 1.9171 0.5898
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 286.9 9.624e-50
## 2 30 299.4 1.056e-46
## 3 40 316.5 3.768e-45
## 4 50 329.7 3.034e-43
##
##
## Elapsed time : 3.619996
##Modelo eGarch (2.1) con distribución de error T Student
garch112e2.2.2.spec = ugarchspec(mean.model = list(armaOrder = c(0,0)),
variance.model = list(garchOrder = c(2,1),
model = "eGARCH"), distribution.model = "std")
garch.fit112e2.2.2= ugarchfit(garch112e2.2.2.spec, data = model, fit.control=list(scale=TRUE))
print(garch.fit112e2.2.2)
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : eGARCH(2,1)
## Mean Model : ARFIMA(0,0,0)
## Distribution : std
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu 0.001261 0.000528 2.3895 0.016871
## omega -0.098617 0.052158 -1.8908 0.058657
## alpha1 -0.091854 0.062178 -1.4773 0.139602
## alpha2 0.112060 0.062577 1.7908 0.073330
## beta1 0.983072 0.008942 109.9359 0.000000
## gamma1 0.451433 0.099049 4.5577 0.000005
## gamma2 -0.137387 0.080693 -1.7026 0.088644
## shape 2.550534 0.186797 13.6540 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.001261 0.000509 2.4788 0.013183
## omega -0.098617 0.070233 -1.4041 0.160275
## alpha1 -0.091854 0.065685 -1.3984 0.161993
## alpha2 0.112060 0.064881 1.7272 0.084139
## beta1 0.983072 0.012533 78.4368 0.000000
## gamma1 0.451433 0.107693 4.1918 0.000028
## gamma2 -0.137387 0.085209 -1.6124 0.106885
## shape 2.550534 0.197290 12.9278 0.000000
##
## LogLikelihood : 3036.887
##
## Information Criteria
## ------------------------------------
##
## Akaike -3.7790
## Bayes -3.7522
## Shibata -3.7791
## Hannan-Quinn -3.7691
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 3.322 0.06836
## Lag[2*(p+q)+(p+q)-1][2] 4.493 0.05561
## Lag[4*(p+q)+(p+q)-1][5] 7.590 0.03721
## d.o.f=0
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.1405 0.7078
## Lag[2*(p+q)+(p+q)-1][8] 0.7473 0.9883
## Lag[4*(p+q)+(p+q)-1][14] 1.9573 0.9920
## d.o.f=3
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[4] 0.01235 0.500 2.000 0.9115
## ARCH Lag[6] 0.38116 1.461 1.711 0.9244
## ARCH Lag[8] 0.58042 2.368 1.583 0.9750
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 2.4443
## Individual Statistics:
## mu 0.51929
## omega 0.17655
## alpha1 0.40823
## alpha2 0.42594
## beta1 0.14497
## gamma1 0.09465
## gamma2 0.09181
## shape 0.13315
##
## 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.0846 0.2783
## Negative Sign Bias 0.4547 0.6494
## Positive Sign Bias 1.4585 0.1449
## Joint Effect 3.0063 0.3907
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 38.41 0.005261
## 2 30 38.43 0.113098
## 3 40 53.32 0.062957
## 4 50 50.24 0.423920
##
##
## Elapsed time : 8.069996
garch.fit = ugarchfit(garch112e2.2.2.spec, data = model, fit.control=list(scale=TRUE))
#Gráfico de la Varianza Condicional
plot(garch.fit, which=3)
##VaR Valor en Riesgo
plot(garch.fit, which=2)
##
## please wait...calculating quantiles...
garchroll <- ugarchroll(garch112e2.2.2.spec, data = model, n.start =500,
refit.window = "moving"
, refit.every = 100)
garchVaR <- quantile(garchroll, probs=0.01)
actual <- xts(as.data.frame(garchroll)$Realized, time(garchVaR))
mean(actual < garchVaR)
## [1] 0.008159565
VaRplot(alpha = 0.01, actual = actual, VaR = garchVaR)
report(garchroll, type = "VaR", VaR.alpha = 0.01, conf.level = 0.99)
## VaR Backtest Report
## ===========================================
## Model: eGARCH-std
## Backtest Length: 1103
## Data:
##
## ==========================================
## alpha: 1%
## Expected Exceed: 11
## Actual VaR Exceed: 9
## Actual %: 0.8%
##
## Unconditional Coverage (Kupiec)
## Null-Hypothesis: Correct Exceedances
## LR.uc Statistic: 0.403
## LR.uc Critical: 6.635
## LR.uc p-value: 0.526
## Reject Null: NO
##
## Conditional Coverage (Christoffersen)
## Null-Hypothesis: Correct Exceedances and
## Independence of Failures
## LR.cc Statistic: 0.551
## LR.cc Critical: 9.21
## LR.cc p-value: 0.759
## Reject Null: NO
##Condicional Superpuesta
plot(garch.fit, which=1)
##Residuos estandarizados
plot(garch.fit, which=8)
##Distribución de Probabilidad
plot(garch.fit, which=9)
##Curva de nuevos impactos
plot(garch.fit, which=12)
##Otros Gráficos
plot(garch.fit, which = "all")
##
## please wait...calculating quantiles...
forecast1<-ugarchforecast(garch.fit,n.ahead = 3,)
forecast1
##
## *------------------------------------*
## * GARCH Model Forecast *
## *------------------------------------*
## Model: eGARCH
## Horizon: 3
## Roll Steps: 0
## Out of Sample: 0
##
## 0-roll forecast [T0=2020-01-30]:
## Series Sigma
## T+1 0.001261 0.04461
## T+2 0.001261 0.04622
## T+3 0.001261 0.04635
plot(forecast1,which=1)
plot(forecast1,which=3)
modelfit=ugarchfit(garch112e2.2.2.spec,data=model, solver="hybrid", out.sample=5)
modelforecast<-ugarchforecast(modelfit, data = NULL, n.ahead = 10, n.roll = 5, external.forecasts = list(mregfor = NULL, vregfor = NULL))
modelforecast
##
## *------------------------------------*
## * GARCH Model Forecast *
## *------------------------------------*
## Model: eGARCH
## Horizon: 10
## Roll Steps: 5
## Out of Sample: 5
##
## 0-roll forecast [T0=2020-01-24]:
## Series Sigma
## T+1 0.001192 0.03927
## T+2 0.001192 0.04096
## T+3 0.001192 0.04116
## T+4 0.001192 0.04136
## T+5 0.001192 0.04155
## T+6 0.001192 0.04174
## T+7 0.001192 0.04193
## T+8 0.001192 0.04212
## T+9 0.001192 0.04230
## T+10 0.001192 0.04249
plot(modelforecast,which=2)
plot(modelforecast,which=4)
##DCC Garch Bitcoin y S&P 500
garch1 <- merge(apple.zoo[,"BTC.Close"], apple.zoo[,"SP.Close"])
colnames(garch1)<-c("BTC","SP")
tst <- na.approx(na.trim(CalculateReturns(garch1), side="both"))
dcc.garch12.1.spec = dccspec(uspec = multispec( replicate(2, garch112e2.2.2.spec )), dccOrder = c(1,1),model = "aDCC",distribution ="mvt")
dcc.fit.1 = dccfit(dcc.garch12.1.spec, data = tst, fit.control=list(scale=TRUE))
print(dcc.fit.1)
##
## *---------------------------------*
## * DCC GARCH Fit *
## *---------------------------------*
##
## Distribution : mvt
## Model : aDCC(1,1)
## No. Parameters : 21
## [VAR GARCH DCC UncQ] : [0+16+4+1]
## No. Series : 2
## No. Obs. : 1603
## Log-Likelihood : 8717.84
## Av.Log-Likelihood : 5.44
##
## Optimal Parameters
## -----------------------------------
## Estimate Std. Error t value Pr(>|t|)
## [BTC].mu 0.001261 0.000443 2.845261 0.004437
## [BTC].omega -0.098617 0.067614 -1.458534 0.144694
## [BTC].alpha1 -0.091854 0.059079 -1.554772 0.120000
## [BTC].alpha2 0.112060 0.059290 1.890041 0.058753
## [BTC].beta1 0.983072 0.012050 81.580138 0.000000
## [BTC].gamma1 0.451433 0.105853 4.264710 0.000020
## [BTC].gamma2 -0.137387 0.080808 -1.700162 0.089100
## [BTC].shape 2.550534 0.075947 33.583264 0.000000
## [SP].mu 0.000503 0.000472 1.065980 0.286433
## [SP].omega -0.588570 0.317145 -1.855837 0.063477
## [SP].alpha1 -0.213872 0.117771 -1.816000 0.069370
## [SP].alpha2 -0.042003 0.050273 -0.835489 0.403442
## [SP].beta1 0.941702 0.032926 28.600350 0.000000
## [SP].gamma1 0.220626 0.133934 1.647270 0.099503
## [SP].gamma2 -0.025682 0.122223 -0.210124 0.833571
## [SP].shape 4.896249 1.087639 4.501721 0.000007
## [Joint]dcca1 0.012674 0.008973 1.412385 0.157837
## [Joint]dccb1 0.947733 0.038296 24.747789 0.000000
## [Joint]dccg1 0.000410 0.010705 0.038321 0.969431
## [Joint]mshape 4.000001 0.329756 12.130200 0.000000
##
## Information Criteria
## ---------------------
##
## Akaike -10.851
## Bayes -10.780
## Shibata -10.851
## Hannan-Quinn -10.825
##
##
## Elapsed time : 21.75299
tst <- na.approx(na.trim(CalculateReturns(garch1), side="both"))
plot(tst, main="Retornos de Precios de BTC y S&P500", xlab="Date")
plot(dcc.fit.1, which=1)
## Warning in plot.window(...): relative range of values ( 50 * EPS) is small (axis
## 2)
## Warning in plot.window(...): relative range of values ( 27 * EPS) is small (axis
## 2)
plot(dcc.fit.1, which=2)
plot(dcc.fit.1, which=3)
plot(dcc.fit.1, which=4)
plot(dcc.fit.1, which=5)
fit01 = dccfit(dcc.garch12.1.spec, data = tst, out.sample = 15, fit.control = list(eval.se=FALSE))
forc2 = dccforecast(fit01, n.ahead = 1, n.roll = 10)
plot(forc2, which=1)
plot(forc2, which=2)
plot(forc2, which=3)
plot(forc2, which=5)
forc2
##
## *---------------------------------*
## * DCC GARCH Forecast *
## *---------------------------------*
##
## Distribution : mvt
## Model : aDCC(1,1)
## Horizon : 1
## Roll Steps : 10
## -----------------------------------
##
## 0-roll forecast:
## , , 1
##
## [,1] [,2]
## [1,] 1.00000 -0.07547
## [2,] -0.07547 1.00000
##Otros Gráficos
r1=rcor(dcc.fit.1, type="R")
r1.z=zoo(r1[1,2,], order.by=time(tst))
plot(r1.z, main=paste(colnames(garch1)[1],"-", colnames(garch1)[2], "Correlacion Condicional", sep=" "),
ylab="Correlacion Condicional",
xlab="Date")
abline(h=mean(r1.z), lty=2, lwd=1, col="blue")
abline(h=(mean(r1.z)+sd(r1.z)), lty=2, lwd=1, col="blue")
abline(h=(mean(r1.z)-sd(r1.z)), lty=2, lwd=1, col="blue")
mr1z <- mean(window(r1.z, start="2020-01-01"))
sr1z <- sd(window(r1.z, start="2020-01-01"))
plot(window(r1.z, start="2020-01-01"), main=paste(colnames(garch1)[1],"-", colnames(garch1)[2], "Correlacion Condicional", sep=" "),
ylab="Correlacion Condicional", sub=paste("mean:", round(mr1z,3),"sd:", round(sr1z,3)),
xlab="Date")
abline(h=mr1z, lty=2, lwd=1, col="blue")
abline(h=(mr1z+sr1z), lty=2, lwd=1, col="red")
abline(h=(mr1z-sr1z), lty=2, lwd=1, col="red")