WEBINAR GRATIS - CARTERAS DE INVERSIÓN
DCC-GARCH COPULAS
👨💻 Los Modelos DCC-GARCH-Cópulas pueden ser usados para crear carteras de inversión en diferentes activos financieros o también para hacer simulaciones a partir de ellos.
La idea con estos modelos es conjuntar los modelos de volatilidad condicional por un lado con las cópulas por otro,todo ello permitiendo crear una estructura que nos permita medir y simular el riesgo de manera multidimensional.
Aquí tienes el Webinar Completo donde explico u método para crear carteras usando estos modelos 👇👇
CODIGO R DEL WEBINAR
### CLASE CARTERAS VIDEO DCC-GARCH- COPULAS
## INDICES- 10
#### CODIGO CARTERA INDICES #######
#install.packages(«quantmod»)
library(quantmod)
#install.packages(«tseries»)
library(tseries)
#install.packages(«fImport»)
library(fImport)
#install.packages(«rugarch») #para ugarchspec
library(rugarch) #rugarch: a package for univariate GARCH fitting, simulation and forecast
#install.packages(«rmgarch»)
library(rmgarch) #dccspec
#install.packages(«ccgarch»)
library(ccgarch)
#install.packages(«HAC»)
library(HAC)#
#install.packages(«expm»)
library(expm)
#install.packages(«fPortfolio»)
library(fPortfolio)
#install.packages(«gtools)
library(gtools)
#?get.hist.quote
CAC40=get.hist.quote(«^FCHI»,quote=»Close»,start = «2016-06-04″,end=»2017-06-04»)
DAX=get.hist.quote(«^GDAXI»,quote=»Close»,start = «2016-06-04″,end=»2017-06-04»)
#FTSEMIB=get.hist.quote(«FTSEMIB.MI»,quote=»Close»,start = «2016-03-13″,end=»2017-03-13»)
FTSE=get.hist.quote(«^FTSE»,quote=»Close»,start = «2016-06-04″,end=»2017-06-04»)
SWISS=get.hist.quote(«^SSMI»,quote=»Close»,start =»2016-06-04″,end=»2017-06-04″)
IBEX=get.hist.quote(«^IBEX»,quote=»Close»,start = «2016-06-04″,end=»2017-06-04»)
BELG=get.hist.quote(«^BFX»,quote=»Close»,start =»2016-06-04″,end=»2017-06-04″)
DOW=get.hist.quote(«^DJI»,quote=»Close»,start = «2016-06-04″,end=»2017-06-04»)
NASDAQ=get.hist.quote(«^NDX»,quote=»Close»,start = «2016-06-04″,end=»2017-06-04»)
NIKKEI=get.hist.quote(«^N225″,quote=»Close»,start =»2016-06-04″,end=»2017-06-04″)
SINGAPUR=get.hist.quote(«^STI»,quote=»Close»,start = «2016-06-04″,end=»2017-06-04»)
HONG=get.hist.quote(«^HSI»,quote=»Close»,start = «2016-06-04″,end=»2017-06-04»)
HONG1=get.hist.quote(«^HSI»,quote=»Close»,start = «2016-06-04″,end=»2017-06-04»)
SP500=get.hist.quote(«^GSPC»,quote=»Close»,start = «2016-06-04″,end=»2017-06-04»)
STOXX50=get.hist.quote(«^STOXX50E»,quote=»Close»,start = «2016-06-04″,end=»2017-06-04»)
#HOLANDA=get.hist.quote(«^STOXX50E»,quote=»Close»,start = «2016-03-28″,end=»2017-03-28»)
HOLANDA<- get.hist.quote(«^AEX»,quote=»Close»,start = «2016-06-04″,end=»2017-06-04»)
#getSymbols(«EURUSD=X»,src=»yahoo»,from = «2016-06-04»)
#getSymbols(«CADCHF=X»,src=»yahoo»,from = «2016-06-04»)
CAC40=getSymbols(«^FCHI»,quote=»Close»,from = «2019-10-06»)
CAC40= FCHI[,4]
getSymbols(«^GDAXI»,quote=»Close»,from = «2019-10-06»)
head(GDAXI)
DAX= GDAXI[,4]
#FTSEMIB=get.hist.quote(«FTSEMIB.MI»,quote=»Close»,start = «2016-03-13″,end=»2017-03-13»)
#FTSE=getSymbols(«^FTSE»,quote=»Close»,from = «2016-06-04″,end=»2017-06-04»)
getSymbols(«^SSMI»,quote=»Close»,from =»2019-10-06″)
SWISS = SSMI[,4]
getSymbols(«^IBEX»,quote=»Close»,from = «2019-10-06»)
IBEX= IBEX[,4]
#BELG=get.hist.quote(«^BFX»,quote=»Close»,start =»2016-06-04″,end=»2017-06-04″)
getSymbols(«^DJI»,quote=»Close»,from =»2019-10-06″)
DOW= DJI[,4]
getSymbols(«^NDX»,quote=»Close»,from = «2019-10-06»)
NASDAQ=NDX[,4]
getSymbols(«^N225″,quote=»Close»,from =»2019-10-06″)
NIKKEI=N225[,4]
getSymbols(«^STI»,quote=»Close»,from = «2019-03-04»)
SINGAPUR=STI[,4]
getSymbols(«^HSI»,quote=»Close»,from = «2019-10-06»)
HONG=HSI[,4]
plot(HONG,type=»l»)
HONG1=getSymbols(«^HSI»,quote=»Close»,from =»2019-10-06″)
getSymbols(«^AEX»,quote=»Close»,from =»2019-10-06″)
HOLANDA=AEX[,4]
#SP500=get.hist.quote(«^GSPC»,quote=»Close»,start = «2016-06-04″,end=»2017-06-04»)
#STOXX50=get.hist.quote(«^STOXX50E»,quote=»Close»,start = «2016-06-04″,end=»2017-06-04»)
#HOLANDA=get.hist.quote(«^STOXX50E»,quote=»Close»,start = «2016-03-28″,end=»2017-03-28»)
#X<-timeSequence(from=»2017-04-18″,to =»2017-04-18″,by=»day»)
#X
#X2<-23924.54
#P<-timeSeries(X2,X)
#P
#cHONG<-rbind(HONG,P,HONG1)
#plot(cHONG,type=»l»)
#1. Elegir 10 indicies por independecia lineal
FX1=na.omit(cbind(CAC40,DAX,SWISS,IBEX,DOW,NASDAQ,NIKKEI,SINGAPUR,HONG,HOLANDA))
head(FX1)
FX1=returns(FX1)
FX1=100*FX1[,1:ncol(FX1)]
FX<-as.timeSeries(FX1)
FX<-na.omit(FX)
head(FX)
apply(FX,2,mean)
mu<-matrix(apply(FX,2,mean))
mu1<-t(mu) #media simple Esta media podría calcularse por el metodo de rendimiento simple
mu1
plot(FX)
head(FX)
plot(FX[,9],xlab=»Retornos HONG KONG»)
cor(FX)
#Asumimos una distribución t-student multivariada/ Ver Normal multivariada
#install.packages(«rugarch») #para ugarchspec
#library(rugarch) #rugarch: a package for univariate GARCH fitting, simulation and forecast
#install.packages(«rmgarch»)
#library(rmgarch) #dccspec
#install.packages(«ccgarch»)
#library(ccgarch)
garch11.spec<-ugarchspec(mean.model = list(armaOrder = c(0,0), include.mean=T),
variance.model = list(garchOrder = c(1,1),model = «sGARCH»))
#DCC-GARCH — VARIAS DISTRIBUIONES
dcc.garch11.spec<-dccspec(uspec = multispec(replicate(10, garch11.spec)),
dccOrder = c(1,1),distribution = «mvt»)
# APLICAMOS MODELO A NUESTRA TABLA
garch11.dcc.fit<-dccfit(dcc.garch11.spec, data = FX)
garch11.dcc.fit
#max<-garch11.dcc.fit@mfit$log.likelihoods
AICdcc<-infocriteria(garch11.dcc.fit)[1]
AICdcc
#???plot(garch11.dcc.fit)
#?dccfit
#fcst.h<-(rcov(garch11.dcc.fit))
#last(fcst.h)
#Predicción con dcc. Está por defecto 1 pero se puede poner las que se desee
fcst<-(dccforecast(garch11.dcc.fit))
fcst
fcst.h<-matrix(fcst@mforecast$H[[1]],nrow=10)#Matriz covarianzas pred
fcst.h
fcst.mu<-matrix(fcst@mforecast$mu,nrow=1) #medias arma prediccion
fcst.mu
#######3
#1. activos
# DCC-GARCH — Predecir y varianzas y media predichas( Modelización
#volatilidad)
#Sacamos los residuos y modelizar con Las copulas
garch11.dcc.res<-(garch11.dcc.fit@mfit$stdresid)
#garch11.dcc.fit@mfit$stdresid
res1<-garch11.dcc.res[,1]
res2<-garch11.dcc.res[,2]
res3<-garch11.dcc.res[,3]
res4<-garch11.dcc.res[,4]
res5<-garch11.dcc.res[,5]
res6<-garch11.dcc.res[,6]
res7<-garch11.dcc.res[,7]
res8<-garch11.dcc.res[,8]
res9<-garch11.dcc.res[,9]
res10<-garch11.dcc.res[,10]
plot(res10,type=»l»)
######################## Copula Fitting and Simulation ##########################
#Aplicación de copulas
#aquí fija una copula arquimediana de la forma Hierarquical
#install.packages(«HAC»)
library(HAC)#HAC: a package for hierarchical Archimedean copula
results<-estimate.copula(cbind(res1,res2,res3,res4,res5,res6,res7,res8,res9,res10),type=3,margins=’t’)
results
#?estimate.copula#The function estimates the parameters and determines the structure of Hierarchical Archimedean Copulae.
plot(results)
##SIMULAMOS COPULA
#??hac2nacopula
na.cop<-hac2nacopula(results)
na.cop
boo<-length(res1)
###
set.seed(2)
cop.sim=rnacopula(boo,na.cop)
head(cop.sim)
##simulate residuals using the simulated copula distribution
#Simulación de nuevos resiudos(serie) con los parámetros de la cópula
res.sim= cbind(qt(cop.sim[,1],df=length(res1)-1),qt(cop.sim[,2],df=length(res2)-1),
qt(cop.sim[,3],df=length(res3)-1),qt(cop.sim[,4],df=length(res4)-1),
qt(cop.sim[,5],df=length(res5)-1),qt(cop.sim[,6],df=length(res6)-1),
qt(cop.sim[,7],df=length(res7)-1),qt(cop.sim[,8],df=length(res8)-1),
qt(cop.sim[,9],df=length(res9)-1), qt(cop.sim[,10],df=length(res10)-1))
res.sim
plot(res.sim[,3],type=»l»)
######### SIMULACION DE RETORNOS DCC-GARCH-COPULA ##########
#simulamos los nuevos retornos con los que calcularemos var y la cartera
#install.packages(«expm»)
library(expm)
logret<-matrix(,nrow=boo,ncol=10)
logret
sqrt.h<-sqrtm(fcst.h)
sqrt.h
#logret<-matrix(rep(mu1,each=boo),ncol=10)+t(sqrt.h%*%t(res.sim))
logret<-matrix(rep(fcst.mu,each=boo),ncol=10)+t(sqrt.h%*%t(res.sim))
medias<-rbind(mu1,fcst.mu)
medias
plot(logret[,1],type=»l»)
plot(logret[,2],type=»l»)
#FX1<-logret
acum<- apply(logret,2,cumsum)
acum
plot(acum[,10],type=»l»)
FX1<-rbind(FX,logret)
colnames(FX1)<-c(«CAC40″,»DAX»,»SWISS»,»IBEX»,»DOW»,»NASDAQ»,»NIKKEI»,»SINGAPUR»,»HONG»,»HOLANDA»)
plot(FX1)
##########################
apply(FX1,2,mean)
A<-matrix(apply(FX1,2,mean))
A1<-t(A) #media simple Esta media podría calcularse por el metodo de rendimiento simple
A1
reb3<-A1<0
reb3
rebb<-replace(reb3,reb3==TRUE,-1)
rebb
REBfinal<-replace(rebb,rebb==0,1)
REBfinal
REBfinal<-t(matrix(REBfinal))
REBfinal
########################################
V1<-as.timeSeries(rep(REBfinal[1,1],nrow(FX1)))
V2<-as.timeSeries(rep(REBfinal[1,2],nrow(FX1)))
V3<-as.timeSeries(rep(REBfinal[1,3],nrow(FX1)))
V4<-as.timeSeries(rep(REBfinal[1,4],nrow(FX1)))
V5<-as.timeSeries(rep(REBfinal[1,5],nrow(FX1)))
V6<-as.timeSeries(rep(REBfinal[1,6],nrow(FX1)))
V7<-as.timeSeries(rep(REBfinal[1,7],nrow(FX1)))
V8<-as.timeSeries(rep(REBfinal[1,8],nrow(FX1)))
V9<-as.timeSeries(rep(REBfinal[1,9],nrow(FX1)))
V10<-as.timeSeries(rep(REBfinal[1,10],nrow(FX1)))
buena<- cbind(V1,V2,V3,V4,V5,V6,V7,V8,V9,V10)
buena
FXX<- FX1*buena
colnames(FXX)<-c(«CAC40″,»DAX»,»SWISS»,»IBEX»,»DOW»,»NASDAQ»,»NIKKEI»,»SINGAPUR»,»HONG»,»HOLANDA»)
apply(FXX,2,mean)
#install.packages(«fPortfolio»)
library(fPortfolio)
x=portfolioSpec()
#setOptimize(x)=»maxReturn»
setType(x)=»CVAR»
setAlpha(x)=0.05
setSolver(x)=»solveRglpk.CVAR»
#setSolver(x)=» solveRquadprog»
rest=c(«minW[1:10]=-0.25″,»maxW[1:10]=0.25»)
y=tangencyPortfolio(data=FXX,spec=x,constraints=rest)
print(y)
pesos<- y@portfolio@portfolio$weights
pesos
Apalancamiento<-sum(abs(pesos))
posicionAy<- (pesos/Apalancamiento)*2
AYONDOWEIGHT<-posicionAy*REBfinal
colnames(AYONDOWEIGHT)<-c(«CAC40″,»DAX»,»SWISS»,»IBEX»,»DOW»,»NASDAQ»,»NIKKEI»,»SINGAPUR»,»HONG»,»HOLANDA»)
AYONDOWEIGHT
##########################
# Transformamos
a = apply(FX1,2,mean)
a
negativos = c()
positivos = c()
for (i in 1:length(a))
{
if (a[i] < 0)
{
negativos = c(negativos,i)
}
else
{
positivos = c(positivos,i)
}
}
positivar = function(x)
{
x*-1
}
# Positivemos y cbindemos
apply(FX1[,negativos],2,mean)
FXc = positivar(FX1[,negativos])
apply(FXc,2,mean)
FXc1 = cbind(FXc,FX1[,positivos])
apply(FXc1,2,mean)
FXX<-as.timeSeries(FXc1)
head(FXX)
FXX<-FX1
#FECHA <-timeSequence(from=»2012-01-02″,to=»2016-12-31″,by= «day»)
#fxx<-as.xts(FXc1,FECHA)
#FXX<-as.timeSeries(fxx[«2012-01-02::2012-12-31»])
#colnames(FXX)<-c(«CADCHF»,»AUDNZD»,»AUDUSD»,»EURCHF»,»EURGBP»,»EURJPY»,»EURNZD»,»EURUSD»,»GBPCHF»,»NZDCAD»)
#CREaMOS LA CARTERA
#install.packages(«fPortfolio»)
library(fPortfolio)
x=portfolioSpec()
setType(x)=»CVAR»
setAlpha(x)=0.05
setSolver(x)=»solveRglpk.CVAR»
rest=c(«minW[1:10]=-0.25″,»maxW[1:10]=0.25»)
y=tangencyPortfolio(data=FXX,spec=x,constraints=rest)
print(y)
a