library(quantmod)
## Warning: package 'quantmod' was built under R version 4.4.1
## Cargando paquete requerido: xts
## Cargando paquete requerido: zoo
##
## Adjuntando el paquete: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Cargando paquete requerido: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first() masks xts::first()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::last() masks xts::last()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(timeSeries)
## Cargando paquete requerido: timeDate
##
## Adjuntando el paquete: 'timeSeries'
##
## The following object is masked from 'package:dplyr':
##
## lag
##
## The following object is masked from 'package:zoo':
##
## time<-
##
## The following objects are masked from 'package:graphics':
##
## lines, points
library(tseries)
library(zoo)
library(xts)
library(readxl)
library(PerformanceAnalytics)
##
## Adjuntando el paquete: 'PerformanceAnalytics'
##
## The following objects are masked from 'package:timeDate':
##
## kurtosis, skewness
##
## The following object is masked from 'package:graphics':
##
## legend
library(ggplot2)
library(fPortfolio)
## Warning: package 'fPortfolio' was built under R version 4.4.1
## Cargando paquete requerido: fBasics
##
## Adjuntando el paquete: 'fBasics'
##
## The following objects are masked from 'package:PerformanceAnalytics':
##
## kurtosis, skewness
##
## The following object is masked from 'package:TTR':
##
## volatility
##
## Cargando paquete requerido: fAssets
library(PortfolioAnalytics)
## Cargando paquete requerido: foreach
##
## Adjuntando el paquete: 'foreach'
##
## The following objects are masked from 'package:purrr':
##
## accumulate, when
library(copula)
##
## Adjuntando el paquete: 'copula'
##
## The following object is masked from 'package:fPortfolio':
##
## getSigma
##
## The following object is masked from 'package:lubridate':
##
## interval
#install.packages("dygraphs")
library(dygraphs)
library(forecast)
## Warning: package 'forecast' was built under R version 4.4.1
library(ggplot.multistats)
## Warning: package 'ggplot.multistats' was built under R version 4.4.1
library(PerformanceAnalytics)
library(foreign)
library(ggfortify)
## Warning: package 'ggfortify' was built under R version 4.4.1
## Registered S3 methods overwritten by 'ggfortify':
## method from
## autoplot.Arima forecast
## autoplot.acf forecast
## autoplot.ar forecast
## autoplot.bats forecast
## autoplot.decomposed.ts forecast
## autoplot.ets forecast
## autoplot.forecast forecast
## autoplot.stl forecast
## autoplot.ts forecast
## fitted.ar forecast
## fortify.ts forecast
## residuals.ar forecast
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 4.4.1
##
## Adjuntando el paquete: 'gridExtra'
##
## The following object is masked from 'package:dplyr':
##
## combine
library(seasonal)
## Warning: package 'seasonal' was built under R version 4.4.1
##
## Adjuntando el paquete: 'seasonal'
##
## The following objects are masked from 'package:timeSeries':
##
## outlier, series
##
## The following object is masked from 'package:tibble':
##
## view
#install.packages("rugarch")
library(rugarch)
## Cargando paquete requerido: parallel
##
## Adjuntando el paquete: 'rugarch'
##
## The following objects are masked from 'package:fBasics':
##
## qgh, qnig
##
## The following object is masked from 'package:purrr':
##
## reduce
##
## The following object is masked from 'package:stats':
##
## sigma
library(ggplot2)
library(readxl)
library(tseries)
library(forecast)
library(ggplot.multistats)
library(PerformanceAnalytics)
library(foreign)
library(ggfortify)
library(gridExtra)
library(seasonal)
library(lattice)
library(zoo)
library(urca)
## Warning: package 'urca' was built under R version 4.4.1
library(dynlm)
## Warning: package 'dynlm' was built under R version 4.4.1
# Define el nuevo portafolio
cartera <- c("PM","NEM","KR", "PFE","AMZN","XOM","WMT","PG","CL","IBM")
Precios_i <- NULL
for (Cabecera_i in cartera)
Precios_i <- cbind(Precios_i, getSymbols(Cabecera_i, from = "2010-01-01",to="2024-05-31", auto.assign = FALSE)[, 6])
# Renombra las columnas con los nombres de las acciones
colnames(Precios_i) <- cartera
Retorno_i <- na.omit(Return.calculate(Precios_i,method = "log")) # Calculamos retornos mediante logaritmos
RetPort_i <- as.timeSeries(Retorno_i) # #Convertimos los retornos en series de tiempo
colnames(Retorno_i) <- c("PM","NEM","KR", "PFE","AMZN","XOM","WMT","PG","CL","IBM")
mean(Retorno_i$PM)
## [1] 0.0003804615
mean(Retorno_i$NEM)
## [1] 4.651159e-05
mean(Retorno_i$KR)
## [1] 0.0005226939
mean(Retorno_i$PFE)
## [1] 0.0002770015
mean(Retorno_i$AMZN)
## [1] 0.0009069823
mean(Retorno_i$XOM)
## [1] 0.000289488
mean(Retorno_i$WMT)
## [1] 0.0004403342
mean(Retorno_i$PG)
## [1] 0.0003875127
mean(Retorno_i$CL)
## [1] 0.0003115771
mean(Retorno_i$IBM)
## [1] 0.0002148563
sd(Retorno_i$PM)
## [1] 0.01351543
sd(Retorno_i$NEM)
## [1] 0.02224621
sd(Retorno_i$KR)
## [1] 0.01649461
sd(Retorno_i$PFE)
## [1] 0.01368278
sd(Retorno_i$AMZN)
## [1] 0.02063198
sd(Retorno_i$XOM)
## [1] 0.01583529
sd(Retorno_i$WMT)
## [1] 0.01210771
sd(Retorno_i$PG)
## [1] 0.01077572
sd(Retorno_i$CL)
## [1] 0.01128574
sd(Retorno_i$IBM)
## [1] 0.0140711
# Gráfico de Precios
ggplot(data = Precios_i, aes(x = index(Precios_i))) +
geom_line(aes(y = PM, color = "PM")) +
geom_line(aes(y = NEM, color = "NEM")) +
geom_line(aes(y = KR, color = "KR")) +
geom_line(aes(y = PFE, color = "PFE")) +
geom_line(aes(y = AMZN, color = "AMZN")) +
geom_line(aes(y = XOM, color = "XOM")) +
geom_line(aes(y = WMT, color = "WMT")) +
geom_line(aes(y = PG, color = "PG")) +
geom_line(aes(y = CL, color = "CL")) +
geom_line(aes(y = IBM, color = "IBM")) +
labs(x = "Periodos", y = "Precio Ajustado", color = "Acción") +
scale_color_manual(values = c("PM" = "#CD5B45", "NEM" = "#458B74","KR"="#CB977C","PFE"="#C41028","AMZN"="#3F0A18","XOM"="#A06444","WMT"="#8A1DC0",
"PG"="#9D8E8E","CL"="#11FFF4","IBM"="slategray3")) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.6), # Centrar el tÃtulo
) +
ggtitle("Precios de las accciones")

# Gráfico de Retornos
ggplot(data = Retorno_i, aes(x = index(Retorno_i))) +
geom_line(aes(y = PM, color = "PM")) +
geom_line(aes(y = NEM, color = "NEM")) +
geom_line(aes(y = KR, color = "KR")) +
geom_line(aes(y = PFE, color = "PFE")) +
geom_line(aes(y = AMZN, color = "AMZN")) +
geom_line(aes(y = XOM, color = "XOM")) +
geom_line(aes(y = WMT, color = "WMT")) +
geom_line(aes(y = PG, color = "PG")) +
geom_line(aes(y = CL, color = "CL")) +
geom_line(aes(y = IBM, color = "IBM")) +
labs(x = "Periodos", y = "Retornos", color = "Acción") +
scale_color_manual(values = c("PM" = "#CD5B45", "NEM" = "#458B74","KR"="#CB977C","PFE"="#C41028","AMZN"="#3F0A18","XOM"="#A06444","WMT"="#8A1DC0",
"PG"="#9D8E8E","CL"="#11FFF4","IBM"="#B4242B")) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.6), # Centrar el tÃtulo
) +
ggtitle("Retornos de la acciones")

dygraph(Precios_i,main = "Precios de las Acciones",ylab = "Precio Ajustado",xlab = "Periodo") %>% dyRangeSelector()
dygraph(Retorno_i$PM)
###############################################################################
###################### FRONTERA Y PORTAFOLIOS EFICIENTES ######################
###############################################################################
#Calculando la frontera eficiente
fronteraEff_i <- portfolioFrontier(RetPort_i, constraints= "LongOnly")
#Graficando la frontera
#En el gr?fico, se pueden incorporar los siguientes elementos:
# 1: Frontera Eficiente
# 2: El portafolio con la m?nima varianza global
# 3: L?nea tangente al portafolio
# 4: Riesgo y retorno de cada activo
# 5: Portafolio con activos del mismo peso
# 6: Fronteras de dos activos
# 7: Portafolios de Monte Carlo
# 8: Ratio de Sharpe
plot(fronteraEff_i,c(1,2,4,7,8))

#Riesgos y Retornos de la frontera
Riesgo_Retorno_i <- frontierPoints(fronteraEff_i)
#Matrices de correlaci?n y covarianza
MatrizCorr_i<-cor(RetPort_i)
MatrizCov_i<-cov(RetPort_i)
MatrizCov_i
## PM NEM KR PFE AMZN
## PM 1.826668e-04 5.020159e-05 4.387513e-05 6.800568e-05 5.828157e-05
## NEM 5.020159e-05 4.948940e-04 2.313044e-05 3.423571e-05 5.096645e-05
## KR 4.387513e-05 2.313044e-05 2.720723e-04 4.898566e-05 5.161739e-05
## PFE 6.800568e-05 3.423571e-05 4.898566e-05 1.872185e-04 7.041315e-05
## AMZN 5.828157e-05 5.096645e-05 5.161739e-05 7.041315e-05 4.256784e-04
## XOM 8.866565e-05 7.375508e-05 4.712172e-05 7.433727e-05 7.528219e-05
## WMT 5.205495e-05 2.758279e-05 7.509470e-05 4.990334e-05 6.056659e-05
## PG 7.088542e-05 3.749107e-05 5.014112e-05 5.812636e-05 5.014382e-05
## CL 7.643744e-05 3.881749e-05 5.036714e-05 5.739448e-05 5.997885e-05
## IBM 8.191937e-05 4.393280e-05 4.534028e-05 7.557131e-05 9.070024e-05
## XOM WMT PG CL IBM
## PM 8.866565e-05 5.205495e-05 7.088542e-05 7.643744e-05 8.191937e-05
## NEM 7.375508e-05 2.758279e-05 3.749107e-05 3.881749e-05 4.393280e-05
## KR 4.712172e-05 7.509470e-05 5.014112e-05 5.036714e-05 4.534028e-05
## PFE 7.433727e-05 4.990334e-05 5.812636e-05 5.739448e-05 7.557131e-05
## AMZN 7.528219e-05 6.056659e-05 5.014382e-05 5.997885e-05 9.070024e-05
## XOM 2.507565e-04 4.655407e-05 5.408623e-05 5.524335e-05 1.070857e-04
## WMT 4.655407e-05 1.465966e-04 5.926284e-05 5.750487e-05 5.140620e-05
## PG 5.408623e-05 5.926284e-05 1.161162e-04 8.569137e-05 6.375802e-05
## CL 5.524335e-05 5.750487e-05 8.569137e-05 1.273680e-04 6.378462e-05
## IBM 1.070857e-04 5.140620e-05 6.375802e-05 6.378462e-05 1.979957e-04
MatrizCorr_i
## PM NEM KR PFE AMZN XOM WMT
## PM 1.0000000 0.16696734 0.19680970 0.3677401 0.2090069 0.4142853 0.3181048
## NEM 0.1669673 1.00000000 0.06303556 0.1124731 0.1110421 0.2093677 0.1024048
## KR 0.1968097 0.06303556 1.00000000 0.2170463 0.1516747 0.1804068 0.3760151
## PFE 0.3677401 0.11247314 0.21704629 1.0000000 0.2494242 0.3430884 0.3012266
## AMZN 0.2090069 0.11104206 0.15167467 0.2494242 1.0000000 0.2304228 0.2424546
## XOM 0.4142853 0.20936773 0.18040681 0.3430884 0.2304228 1.0000000 0.2428117
## WMT 0.3181048 0.10240476 0.37601506 0.3012266 0.2424546 0.2428117 1.0000000
## PG 0.4867218 0.15639590 0.28210162 0.3942325 0.2255435 0.3169672 0.4542284
## CL 0.5011251 0.15461128 0.27056712 0.3716769 0.2575889 0.3091176 0.4208357
## IBM 0.4307536 0.14034758 0.19535034 0.3925135 0.3124207 0.4805931 0.3017350
## PG CL IBM
## PM 0.4867218 0.5011251 0.4307536
## NEM 0.1563959 0.1546113 0.1403476
## KR 0.2821016 0.2705671 0.1953503
## PFE 0.3942325 0.3716769 0.3925135
## AMZN 0.2255435 0.2575889 0.3124207
## XOM 0.3169672 0.3091176 0.4805931
## WMT 0.4542284 0.4208357 0.3017350
## PG 1.0000000 0.7046292 0.4204948
## CL 0.7046292 1.0000000 0.4016594
## IBM 0.4204948 0.4016594 1.0000000
#Pesos en las fronteras eficientes
fronteraPesos_i<-getWeights(fronteraEff_i)
colnames(fronteraPesos_i)<-c("PM","NEM","KR", "PFE","AMZN","XOM","WMT","PG","CL","IBM")
barplot(t(fronteraPesos_i), main="Pesos de los activos en la Frontera Eficiente",col=cm.colors(ncol(fronteraPesos_i)+2), legend=colnames(fronteraPesos_i))

MatrizCov_i
## PM NEM KR PFE AMZN
## PM 1.826668e-04 5.020159e-05 4.387513e-05 6.800568e-05 5.828157e-05
## NEM 5.020159e-05 4.948940e-04 2.313044e-05 3.423571e-05 5.096645e-05
## KR 4.387513e-05 2.313044e-05 2.720723e-04 4.898566e-05 5.161739e-05
## PFE 6.800568e-05 3.423571e-05 4.898566e-05 1.872185e-04 7.041315e-05
## AMZN 5.828157e-05 5.096645e-05 5.161739e-05 7.041315e-05 4.256784e-04
## XOM 8.866565e-05 7.375508e-05 4.712172e-05 7.433727e-05 7.528219e-05
## WMT 5.205495e-05 2.758279e-05 7.509470e-05 4.990334e-05 6.056659e-05
## PG 7.088542e-05 3.749107e-05 5.014112e-05 5.812636e-05 5.014382e-05
## CL 7.643744e-05 3.881749e-05 5.036714e-05 5.739448e-05 5.997885e-05
## IBM 8.191937e-05 4.393280e-05 4.534028e-05 7.557131e-05 9.070024e-05
## XOM WMT PG CL IBM
## PM 8.866565e-05 5.205495e-05 7.088542e-05 7.643744e-05 8.191937e-05
## NEM 7.375508e-05 2.758279e-05 3.749107e-05 3.881749e-05 4.393280e-05
## KR 4.712172e-05 7.509470e-05 5.014112e-05 5.036714e-05 4.534028e-05
## PFE 7.433727e-05 4.990334e-05 5.812636e-05 5.739448e-05 7.557131e-05
## AMZN 7.528219e-05 6.056659e-05 5.014382e-05 5.997885e-05 9.070024e-05
## XOM 2.507565e-04 4.655407e-05 5.408623e-05 5.524335e-05 1.070857e-04
## WMT 4.655407e-05 1.465966e-04 5.926284e-05 5.750487e-05 5.140620e-05
## PG 5.408623e-05 5.926284e-05 1.161162e-04 8.569137e-05 6.375802e-05
## CL 5.524335e-05 5.750487e-05 8.569137e-05 1.273680e-04 6.378462e-05
## IBM 1.070857e-04 5.140620e-05 6.375802e-05 6.378462e-05 1.979957e-04
MatrizCorr_i
## PM NEM KR PFE AMZN XOM WMT
## PM 1.0000000 0.16696734 0.19680970 0.3677401 0.2090069 0.4142853 0.3181048
## NEM 0.1669673 1.00000000 0.06303556 0.1124731 0.1110421 0.2093677 0.1024048
## KR 0.1968097 0.06303556 1.00000000 0.2170463 0.1516747 0.1804068 0.3760151
## PFE 0.3677401 0.11247314 0.21704629 1.0000000 0.2494242 0.3430884 0.3012266
## AMZN 0.2090069 0.11104206 0.15167467 0.2494242 1.0000000 0.2304228 0.2424546
## XOM 0.4142853 0.20936773 0.18040681 0.3430884 0.2304228 1.0000000 0.2428117
## WMT 0.3181048 0.10240476 0.37601506 0.3012266 0.2424546 0.2428117 1.0000000
## PG 0.4867218 0.15639590 0.28210162 0.3942325 0.2255435 0.3169672 0.4542284
## CL 0.5011251 0.15461128 0.27056712 0.3716769 0.2575889 0.3091176 0.4208357
## IBM 0.4307536 0.14034758 0.19535034 0.3925135 0.3124207 0.4805931 0.3017350
## PG CL IBM
## PM 0.4867218 0.5011251 0.4307536
## NEM 0.1563959 0.1546113 0.1403476
## KR 0.2821016 0.2705671 0.1953503
## PFE 0.3942325 0.3716769 0.3925135
## AMZN 0.2255435 0.2575889 0.3124207
## XOM 0.3169672 0.3091176 0.4805931
## WMT 0.4542284 0.4208357 0.3017350
## PG 1.0000000 0.7046292 0.4204948
## CL 0.7046292 1.0000000 0.4016594
## IBM 0.4204948 0.4016594 1.0000000
#Varianza m?nima global
VMG_i<-minvariancePortfolio(RetPort_i, spec=portfolioSpec(), constraints="LongOnly")
VMG_i
##
## Title:
## MV Minimum Variance Portfolio
## Estimator: covEstimator
## Solver: solveRquadprog
## Optimize: minRisk
## Constraints: LongOnly
##
## Portfolio Weights:
## PM NEM KR PFE AMZN XOM WMT PG CL IBM
## 0.0613 0.0749 0.0897 0.1169 0.0311 0.0499 0.1870 0.1963 0.1334 0.0596
##
## Covariance Risk Budgets:
## PM NEM KR PFE AMZN XOM WMT PG CL IBM
## 0.0613 0.0749 0.0897 0.1169 0.0311 0.0499 0.1870 0.1963 0.1334 0.0596
##
## Target Returns and Risks:
## mean Cov CVaR VaR
## 0.0004 0.0085 0.0196 0.0123
##
## Description:
## Tue Sep 10 15:39:16 2024 by user: KEVIN
#Reporte de los pesos asignados:
VMG_Pesos_i<-getWeights(VMG_i)
DF_VMG_Pesos_i<-data.frame(VMG_Pesos_i)
acciones<-colnames(fronteraPesos_i)
ggplot(data=DF_VMG_Pesos_i,aes(x=acciones,y=VMG_Pesos_i,fill=acciones))+
geom_bar(stat="identity",position=position_dodge(),colour="black")+
geom_text(aes(label=sprintf("%.02f %%",VMG_Pesos_i*100)),
position=position_dodge(width=0.9),vjust=-0.25,check_overlap=TRUE)+
ggtitle("Pesos de las acciones del portafolio de varianza minima global")+
theme(plot.title = element_text(hjust=0.5))+
labs(x="Acciones",y="Pesos (%)")

peso_i <-c(0.0612,0.0751,0.0898,0.1168,0.0314,0.0498,0.1868,0.1966,0.1327,0.0598)
Retorno_portafolio_i <- as.numeric(peso_i%*%t(Retorno_i))
Retorno_portafolio_i <- xts(Retorno_portafolio_i,order.by = index(Retorno_i))
names(Retorno_portafolio_i)<- c("Retorno del Portafolio")
ggplot(data = Retorno_portafolio_i, aes(x = index(Retorno_portafolio_i))) +
geom_line(aes(y = `Retorno del Portafolio`, color = "Retorno del Portafolio")) +
labs(x = "Periodos", y = "Retornos", color = "Acción") +
scale_color_manual(values = c("Retorno del Portafolio" = "slategray3")) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.6), # Centrar el tÃtulo
) +
ggtitle("Retornos del Portafolio")

mean <- mean(Retorno_portafolio_i)*100
sd_i <- sd(Retorno_portafolio_i)*100
cat("Media:", mean, "\n")
## Media: 0.03615988
cat("Desviación Estándar:", sd_i, "\n")
## Desviación Estándar: 0.8473781
# Definimos el indice
indice <- c("^GSPC")
Precio_indice <- NULL
for (Cabecera_indice in indice)
Precio_indice <- cbind(Precio_indice, getSymbols(Cabecera_indice, from = "2010-01-01",to="2024-05-31", auto.assign = FALSE)[, 6])
# Renombra las columnas con los nombres de las acciones
colnames(Precio_indice) <- indice
Retorno_indice <- na.omit(Return.calculate(Precio_indice,method = "log")) # Calculamos retornos mediante logaritmos
RetPort_indice <- as.timeSeries(Retorno_indice) # #Convertimos los retornos en series de tiempo
colnames(Retorno_indice) <- c("^GSPC")
# Gráfico de Precios
ggplot(data = Precio_indice, aes(x = index(Precio_indice))) +
geom_line(aes(y = Precio_indice$`^GSPC`, color = "^GSPC")) +
labs(x = "Periodos", y = "Precio Ajustado", color = "Indice") +
scale_color_manual(values = c("^GSPC" = "#A2CD5A")) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.6), # Centrar el tÃtulo
) +
ggtitle("Precio Ajustado: Indice S&P500")
## Don't know how to automatically pick scale for object of type <xts/zoo>.
## Defaulting to continuous.

# Gráfico de Retornos
ggplot(data = Retorno_indice, aes(x = index(Retorno_indice))) +
geom_line(aes(y = Retorno_indice$`^GSPC`, color = "^GSPC")) +
labs(x = "Periodos", y = "Retorno", color = "Indice") +
scale_color_manual(values = c( "^GSPC" = "#A2CD5A")) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.7), # Centrar el tÃtulo
) +
ggtitle("Retornos: Indice S&P500")
## Don't know how to automatically pick scale for object of type <xts/zoo>.
## Defaulting to continuous.

#Analsis de beta con respecto al mercado
beta_sistematico_1 <- lm(Retorno_portafolio_i ~ Retorno_indice)
summary(beta_sistematico_1)
##
## Call:
## lm(formula = Retorno_portafolio_i ~ Retorno_indice)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.033226 -0.002824 -0.000016 0.002921 0.053752
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.110e-04 9.011e-05 1.232 0.218
## Retorno_indice 5.935e-01 8.206e-03 72.319 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.005422 on 3623 degrees of freedom
## Multiple R-squared: 0.5908, Adjusted R-squared: 0.5906
## F-statistic: 5230 on 1 and 3623 DF, p-value: < 2.2e-16
#Portafolio con pesos iguales
peso_i <-c(0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1)
Retorno_portafolio_iguales <- as.numeric(peso_i%*%t(Retorno_i))
Retorno_portafolio_iguales <- xts(Retorno_portafolio_iguales,order.by = index(Retorno_i))
names(Retorno_portafolio_iguales)<- c("Retorno del Portafolio con Pesos Iguales")
ggplot(data = Retorno_portafolio_iguales, aes(x = index(Retorno_portafolio_iguales))) +
geom_line(aes(y = Retorno_portafolio_iguales$`Retorno del Portafolio con Pesos Iguales`, color = "Portafolio")) +
labs(x = "Periodos", y = "Retornos", color = "Acción") +
scale_color_manual(values = c("Portafolio" = "#11FFF4")) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5), # Centrar el tÃtulo
) +
ggtitle("Retornos del Portafolio con Pesos Iguales")
## Don't know how to automatically pick scale for object of type <xts/zoo>.
## Defaulting to continuous.

#--------------------------------------------------------------------
mean_portafolio_igual <- mean(Retorno_portafolio_iguales)*100
sd_portafolio_igual <- sd(Retorno_portafolio_iguales)*100
cat("Media:", mean_portafolio_igual, "\n")
## Media: 0.03777419
cat("Desviación Estándar:", sd_portafolio_igual, "\n")
## Desviación Estándar: 0.8782236
#beta sistematico del portafolio con pesos iguales
beta_sistematico_2 <- lm(Retorno_portafolio_iguales~Retorno_indice)
summary(beta_sistematico_2)
##
## Call:
## lm(formula = Retorno_portafolio_iguales ~ Retorno_indice)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.025046 -0.002702 0.000003 0.002740 0.043553
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 9.718e-05 8.136e-05 1.194 0.232
## Retorno_indice 6.645e-01 7.409e-03 89.678 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.004895 on 3623 degrees of freedom
## Multiple R-squared: 0.6894, Adjusted R-squared: 0.6893
## F-statistic: 8042 on 1 and 3623 DF, p-value: < 2.2e-16
#Pruebas necesarias para observar la estacionariedad e invertibilidad de nuestros portafolios
#PORTAFOLIO CON MINIMA VARIANZA
adf.test(Retorno_portafolio_i)
## Warning in adf.test(Retorno_portafolio_i): p-value smaller than printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: Retorno_portafolio_i
## Dickey-Fuller = -16.178, Lag order = 15, p-value = 0.01
## alternative hypothesis: stationary
acf(Retorno_portafolio_i,
main = 'Función de autocorrelación muestral',
xlab = 'Rezago',
ylab = 'ACF',
lwd = 3)
grid()

#----------------------------------------------------------------------
pacf(Retorno_portafolio_i,
main = 'Función de autocorrelación parcial muestral',
xlab = 'Rezago',
ylab = 'PACF',
lwd = 3)
grid()

#PORTAFOLIO CON PESOS IGUALES
adf.test(Retorno_portafolio_iguales)
## Warning in adf.test(Retorno_portafolio_iguales): p-value smaller than printed
## p-value
##
## Augmented Dickey-Fuller Test
##
## data: Retorno_portafolio_iguales
## Dickey-Fuller = -16.405, Lag order = 15, p-value = 0.01
## alternative hypothesis: stationary
acf(Retorno_portafolio_iguales,
main = 'Función de autocorrelación muestral',
xlab = 'Rezago',
ylab = 'ACF',
lwd = 3)
grid()

#----------------------------------------------------------------------
pacf(Retorno_portafolio_iguales,
main = 'Función de autocorrelación parcial muestral',
xlab = 'Rezago',
ylab = 'PACF',
lwd = 3)
grid()

hist(Retorno_portafolio_i,
main = "Histograma de los Retornos del Portafolio con Minima Varianza",
xlab = "Valores",
ylab = "Frecuencia",
col = "lightblue",
border = "black")
lines(density(Retorno_portafolio_i),col = "red", lwd = 2)

#-----------------------------------------------------------------------------------
hist(Retorno_portafolio_iguales,
main = "Histograma de los Retornos del Portafolio con Pesos Iguales",
xlab = "Valores",
ylab = "Frecuencia",
col = "lightblue",
border = "black")
lines(density(Retorno_portafolio_iguales),col = "red", lwd = 2)

#Estimacion del modelos VAR al portafolio con la minima Varianza
modelo_AR_MV <- arima(Retorno_portafolio_i,order = c(1,0,0))
modelo_MA_MV <- arima(Retorno_portafolio_i,order = c(0,0,1))
modelo_ARIMA_MV <- arima(Retorno_portafolio_i,order = c(1,1,1))
spec_arch_MV <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 0)),
mean.model = list(armaOrder = c(0, 0)))
modelo_ARCH_MV <- ugarchfit(spec = spec_arch_MV, data = Retorno_portafolio_i)
#-------------------------------------------------------------------------------------------
spec_GARCH_MV <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)),
mean.model = list(armaOrder = c(1, 1)))
# Ajustamos el modelo GARCH
modelo_GARCH_MV <- ugarchfit(spec = spec_GARCH_MV, data = Retorno_portafolio_i)
#-----------------------------------------------------------------------------------------------
#Mejor modelo que se adecua a nuestro Portafolio
modelo_MV<- auto.arima(Retorno_portafolio_i)
#------------------------------------------------------------------
modelo_AR_MV
##
## Call:
## arima(x = Retorno_portafolio_i, order = c(1, 0, 0))
##
## Coefficients:
## ar1 intercept
## -0.0937 4e-04
## s.e. 0.0165 1e-04
##
## sigma^2 estimated as 7.115e-05: log likelihood = 12166.92, aic = -24327.84
modelo_MA_MV
##
## Call:
## arima(x = Retorno_portafolio_i, order = c(0, 0, 1))
##
## Coefficients:
## ma1 intercept
## -0.0896 4e-04
## s.e. 0.0162 1e-04
##
## sigma^2 estimated as 7.118e-05: log likelihood = 12166.15, aic = -24326.31
modelo_ARIMA_MV
##
## Call:
## arima(x = Retorno_portafolio_i, order = c(1, 1, 1))
##
## Coefficients:
## ar1 ma1
## -0.0935 -1.000
## s.e. 0.0165 0.001
##
## sigma^2 estimated as 7.117e-05: log likelihood = 12158.88, aic = -24311.76
modelo_ARCH_MV
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(1,0)
## Mean Model : ARFIMA(0,0,0)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu -0.036156 0.000016 -2220.44024 0.00000
## omega 0.000000 0.000000 0.82983 0.40664
## alpha1 0.995821 0.008689 114.60086 0.00000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu -0.036156 0.000338 -106.975322 0.00000
## omega 0.000000 0.000019 0.017596 0.98596
## alpha1 0.995821 0.107000 9.306708 0.00000
##
## LogLikelihood : 5263.094
##
## Information Criteria
## ------------------------------------
##
## Akaike -2.9021
## Bayes -2.8970
## Shibata -2.9021
## Hannan-Quinn -2.9003
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 41.84 9.923e-11
## Lag[2*(p+q)+(p+q)-1][2] 115.64 0.000e+00
## Lag[4*(p+q)+(p+q)-1][5] 162.94 0.000e+00
## d.o.f=0
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.03819 8.451e-01
## Lag[2*(p+q)+(p+q)-1][2] 43.49356 3.123e-12
## Lag[4*(p+q)+(p+q)-1][5] 69.68784 0.000e+00
## d.o.f=1
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[2] 86.81 0.500 2.000 0
## ARCH Lag[4] 86.97 1.397 1.611 0
## ARCH Lag[6] 87.01 2.222 1.500 0
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 17.4942
## Individual Statistics:
## mu 2.232
## omega 2.852
## alpha1 5.327
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 0.846 1.01 1.35
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 1.205 2.284e-01
## Negative Sign Bias 1.476 1.401e-01
## Positive Sign Bias 10.021 2.472e-23 ***
## Joint Effect 108.263 2.595e-23 ***
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 11916 0
## 2 30 12155 0
## 3 40 12252 0
## 4 50 12350 0
##
##
## Elapsed time : 0.2695029
modelo_MV
## Series: Retorno_portafolio_i
## ARIMA(1,0,3) with non-zero mean
##
## Coefficients:
## ar1 ma1 ma2 ma3 mean
## 0.4190 -0.5129 0.0745 -0.0670 4e-04
## s.e. 0.1702 0.1697 0.0244 0.0169 1e-04
##
## sigma^2 = 7.097e-05: log likelihood = 12174
## AIC=-24335.99 AICc=-24335.97 BIC=-24298.82
modelo_GARCH_MV
##
## *---------------------------------*
## * 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.000515 0.000099 5.1871 0.000000
## ar1 0.454048 0.271118 1.6747 0.093988
## ma1 -0.500369 0.263090 -1.9019 0.057185
## omega 0.000003 0.000001 2.9243 0.003453
## alpha1 0.138312 0.011403 12.1295 0.000000
## beta1 0.812656 0.005800 140.1034 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.000515 0.000113 4.55923 0.000005
## ar1 0.454048 0.276850 1.64005 0.100995
## ma1 -0.500369 0.267072 -1.87353 0.060995
## omega 0.000003 0.000005 0.69971 0.484110
## alpha1 0.138312 0.026498 5.21982 0.000000
## beta1 0.812656 0.045178 17.98767 0.000000
##
## LogLikelihood : 12715.96
##
## Information Criteria
## ------------------------------------
##
## Akaike -7.0124
## Bayes -7.0021
## Shibata -7.0124
## Hannan-Quinn -7.0087
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 1.288 0.2564
## Lag[2*(p+q)+(p+q)-1][5] 2.705 0.6596
## Lag[4*(p+q)+(p+q)-1][9] 3.440 0.8133
## d.o.f=2
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.7429 0.3887
## Lag[2*(p+q)+(p+q)-1][5] 1.5733 0.7219
## Lag[4*(p+q)+(p+q)-1][9] 2.6923 0.8085
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.2458 0.500 2.000 0.6200
## ARCH Lag[5] 1.2093 1.440 1.667 0.6720
## ARCH Lag[7] 1.9467 2.315 1.543 0.7286
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 0.8922
## Individual Statistics:
## mu 0.02761
## ar1 0.23370
## ma1 0.23162
## omega 0.15857
## alpha1 0.15120
## beta1 0.27826
##
## 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.3187 0.749970
## Negative Sign Bias 2.0542 0.040031 **
## Positive Sign Bias 1.6903 0.091066 *
## Joint Effect 15.8591 0.001212 ***
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 79.35 2.409e-09
## 2 30 96.73 3.242e-09
## 3 40 114.62 2.288e-09
## 4 50 129.61 3.300e-09
##
##
## Elapsed time : 0.354795
#Estimacion de los modelos Var al portafolio con pesos iguales
modelo_AR_PI <- arima(Retorno_portafolio_iguales,order = c(1,0,0))
modelo_MA_PI <- arima(Retorno_portafolio_iguales,order = c(0,0,1))
modelo_ARIMA_PI <- arima(Retorno_portafolio_iguales,order = c(1,1,1))
#---------------------------------------------------------------------------------------------
spec_arch_PI <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 0)),
mean.model = list(armaOrder = c(0, 0)))
modelo_ARCH_PI <- ugarchfit(spec = spec_arch_PI, data = Retorno_portafolio_iguales)
#-------------------------------------------------------------------------------------------
spec_GARCH_PI <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)),
mean.model = list(armaOrder = c(1, 1)))
# Ajustamos el modelo GARCH
modelo_GARCH_PI <- ugarchfit(spec = spec_GARCH_PI, data = Retorno_portafolio_iguales)
#-----------------------------------------------------------------------------------------------
#Mejor modelo que se adecua a nuestro Portafolio
modelo_PI<- auto.arima(Retorno_portafolio_iguales)
#------------------------------------------------------------------------------------------------
modelo_AR_PI
##
## Call:
## arima(x = Retorno_portafolio_iguales, order = c(1, 0, 0))
##
## Coefficients:
## ar1 intercept
## -0.0845 4e-04
## s.e. 0.0165 1e-04
##
## sigma^2 estimated as 7.656e-05: log likelihood = 12034.3, aic = -24062.6
modelo_MA_PI
##
## Call:
## arima(x = Retorno_portafolio_iguales, order = c(0, 0, 1))
##
## Coefficients:
## ma1 intercept
## -0.079 4e-04
## s.e. 0.016 1e-04
##
## sigma^2 estimated as 7.659e-05: log likelihood = 12033.41, aic = -24060.83
modelo_ARIMA_PI
##
## Call:
## arima(x = Retorno_portafolio_iguales, order = c(1, 1, 1))
##
## Coefficients:
## ar1 ma1
## -0.0842 -1.000
## s.e. 0.0166 0.001
##
## sigma^2 estimated as 7.658e-05: log likelihood = 12026.3, aic = -24046.6
modelo_ARCH_PI
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(1,0)
## Mean Model : ARFIMA(0,0,0)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu -0.033897 0.000020 -1731.0582 0.00000
## omega 0.000000 0.000000 0.3425 0.73197
## alpha1 0.849189 0.000339 2505.7652 0.00000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu -0.033897 1.908855 -0.017758 0.98583
## omega 0.000000 0.047887 0.000003 1.00000
## alpha1 0.849189 35.567017 0.023876 0.98095
##
## LogLikelihood : 5296.001
##
## Information Criteria
## ------------------------------------
##
## Akaike -2.9203
## Bayes -2.9151
## Shibata -2.9203
## Hannan-Quinn -2.9185
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 5.655 1.740e-02
## Lag[2*(p+q)+(p+q)-1][2] 23.178 6.521e-07
## Lag[4*(p+q)+(p+q)-1][5] 95.806 0.000e+00
## d.o.f=0
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.005301 9.420e-01
## Lag[2*(p+q)+(p+q)-1][2] 0.405079 7.399e-01
## Lag[4*(p+q)+(p+q)-1][5] 29.822484 3.549e-08
## d.o.f=1
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[2] 0.7987 0.500 2.000 3.715e-01
## ARCH Lag[4] 37.2094 1.397 1.611 3.711e-10
## ARCH Lag[6] 49.3476 2.222 1.500 3.429e-13
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 1.2642
## Individual Statistics:
## mu 0.2916
## omega 0.3848
## alpha1 0.2916
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 0.846 1.01 1.35
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 38.979 8.017e-278 ***
## Negative Sign Bias 26.879 2.908e-145 ***
## Positive Sign Bias 1.039 2.990e-01
## Joint Effect 1601.171 0.000e+00 ***
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 10792 0
## 2 30 10900 0
## 3 40 11009 0
## 4 50 11037 0
##
##
## Elapsed time : 0.2736011
modelo_PI
## Series: Retorno_portafolio_iguales
## ARIMA(4,0,1) with non-zero mean
##
## Coefficients:
## ar1 ar2 ar3 ar4 ma1 mean
## -0.9050 -0.0306 -0.0098 -0.0796 0.8301 4e-04
## s.e. 0.0481 0.0227 0.0225 0.0172 0.0457 1e-04
##
## sigma^2 = 7.584e-05: log likelihood = 12054.4
## AIC=-24094.79 AICc=-24094.76 BIC=-24051.42
modelo_GARCH_PI
##
## *---------------------------------*
## * 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.000529 0.000105 5.0496 0.000000
## ar1 0.624707 0.300238 2.0807 0.037460
## ma1 -0.663682 0.287282 -2.3102 0.020877
## omega 0.000003 0.000002 1.1712 0.241502
## alpha1 0.126917 0.012608 10.0663 0.000000
## beta1 0.832164 0.022716 36.6336 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.000529 0.000298 1.77701 0.075567
## ar1 0.624707 0.744056 0.83960 0.401134
## ma1 -0.663682 0.711036 -0.93340 0.350613
## omega 0.000003 0.000028 0.10212 0.918662
## alpha1 0.126917 0.061945 2.04888 0.040474
## beta1 0.832164 0.231481 3.59495 0.000324
##
## LogLikelihood : 12572.47
##
## Information Criteria
## ------------------------------------
##
## Akaike -6.9332
## Bayes -6.9230
## Shibata -6.9332
## Hannan-Quinn -6.9296
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.8084 0.3686
## Lag[2*(p+q)+(p+q)-1][5] 1.9443 0.9676
## Lag[4*(p+q)+(p+q)-1][9] 2.4273 0.9581
## d.o.f=2
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.02103 0.8847
## Lag[2*(p+q)+(p+q)-1][5] 1.82821 0.6595
## Lag[4*(p+q)+(p+q)-1][9] 3.19245 0.7268
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 1.718 0.500 2.000 0.1900
## ARCH Lag[5] 1.860 1.440 1.667 0.5030
## ARCH Lag[7] 2.699 2.315 1.543 0.5721
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 1.3136
## Individual Statistics:
## mu 0.02286
## ar1 0.14585
## ma1 0.14242
## omega 0.43423
## alpha1 0.09119
## beta1 0.18167
##
## 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.2543 0.2097973
## Negative Sign Bias 0.9828 0.3257891
## Positive Sign Bias 1.6525 0.0985108 *
## Joint Effect 16.3183 0.0009757 ***
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 89.62 3.872e-11
## 2 30 109.97 2.340e-11
## 3 40 126.78 3.242e-11
## 4 50 149.58 4.115e-12
##
##
## Elapsed time : 0.304801
#Grafico de las raices inversas de los polinomios de los modelos del portafolio con pesos de la minima Varianza
plot(modelo_AR_MV,
main="Raiz inversa AR(1,0)",
xlab = 'Reales',
ylab = 'imaginarios',
lwd = 3,col="blue")

plot(modelo_MA_MV,
main="Raiz inversa MA(1,0)",
xlab = 'Reales',
ylab = 'imaginarios',
lwd = 3,col="red")

plot(modelo_MV,
main="Raiz inversa ARMA(1,0,3)",
xlab = 'Reales',
ylab = 'imaginarios',
lwd = 3,col="red")

plot(modelo_ARIMA_MV,
main="Raiz inversa ARIMA(1,1,1)",
xlab = 'Reales',
ylab = 'imaginarios',
lwd = 3,col="red")

#Graficos de los modelos arch y garch del portafolio de pesos con la Minima Varianza
plot(modelo_ARCH_MV, which = "all")
##
## please wait...calculating quantiles...

plot(modelo_GARCH_MV,which ="all")
##
## please wait...calculating quantiles...

#Grafico de las raices inversas de los polinomios de los modelos del portafolio con pesos iguales
plot(modelo_AR_PI,
main="Raiz inversa AR(1,0)",
xlab = 'Reales',
ylab = 'imaginarios',
lwd = 3,col="blue")

plot(modelo_MA_PI,
main="Raiz inversa MA(1,0)",
xlab = 'Reales',
ylab = 'imaginarios',
lwd = 3,col="red")

plot(modelo_PI,
main="Raiz inversa ARMA(4,0,1)",
xlab = 'Reales',
ylab = 'imaginarios',
lwd = 3,col="red")

plot(modelo_ARIMA_PI,
main="Raiz inversa ARIMA(1,1,1)",
xlab = 'Reales',
ylab = 'imaginarios',
lwd = 3,col="red")

#------------------------------------------------
#Graficos de los modelos arch y garch del portafolio de pesos iguales
plot(modelo_ARCH_PI, which = "all")
##
## please wait...calculating quantiles...

plot(modelo_GARCH_PI,which ="all")
##
## please wait...calculating quantiles...

#Proyecciones de los modelos de nuestro portafolio de pesos con minima Varianza
forecast(modelo_AR_MV)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 3626 0.0002298816 -0.01058037 0.01104013 -0.01630297 0.01676273
## 3627 0.0003740613 -0.01048359 0.01123171 -0.01623128 0.01697940
## 3628 0.0003605450 -0.01049752 0.01121861 -0.01624543 0.01696652
## 3629 0.0003618121 -0.01049625 0.01121988 -0.01624417 0.01696779
## 3630 0.0003616933 -0.01049637 0.01121976 -0.01624429 0.01696767
## 3631 0.0003617045 -0.01049636 0.01121977 -0.01624428 0.01696768
## 3632 0.0003617034 -0.01049636 0.01121977 -0.01624428 0.01696768
## 3633 0.0003617035 -0.01049636 0.01121977 -0.01624428 0.01696768
## 3634 0.0003617035 -0.01049636 0.01121977 -0.01624428 0.01696768
## 3635 0.0003617035 -0.01049636 0.01121977 -0.01624428 0.01696768
forecast(modelo_MA_MV)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 3626 0.0003157939 -0.01049675 0.01112834 -0.01622057 0.01685215
## 3627 0.0003616082 -0.01049426 0.01121748 -0.01624101 0.01696423
## 3628 0.0003616082 -0.01049426 0.01121748 -0.01624101 0.01696423
## 3629 0.0003616082 -0.01049426 0.01121748 -0.01624101 0.01696423
## 3630 0.0003616082 -0.01049426 0.01121748 -0.01624101 0.01696423
## 3631 0.0003616082 -0.01049426 0.01121748 -0.01624101 0.01696423
## 3632 0.0003616082 -0.01049426 0.01121748 -0.01624101 0.01696423
## 3633 0.0003616082 -0.01049426 0.01121748 -0.01624101 0.01696423
## 3634 0.0003616082 -0.01049426 0.01121748 -0.01624101 0.01696423
## 3635 0.0003616082 -0.01049426 0.01121748 -0.01624101 0.01696423
forecast(modelo_ARIMA_MV)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 3626 0.0002302060 -0.01058303 0.01104344 -0.01630721 0.01676762
## 3627 0.0003739631 -0.01048615 0.01123407 -0.01623514 0.01698307
## 3628 0.0003605231 -0.01050002 0.01122107 -0.01624924 0.01697029
## 3629 0.0003617796 -0.01049877 0.01122232 -0.01624799 0.01697155
## 3630 0.0003616621 -0.01049888 0.01122221 -0.01624811 0.01697143
## 3631 0.0003616731 -0.01049887 0.01122222 -0.01624810 0.01697144
## 3632 0.0003616721 -0.01049887 0.01122222 -0.01624810 0.01697144
## 3633 0.0003616722 -0.01049887 0.01122222 -0.01624810 0.01697144
## 3634 0.0003616722 -0.01049887 0.01122222 -0.01624810 0.01697144
## 3635 0.0003616722 -0.01049887 0.01122222 -0.01624810 0.01697144
forecast(modelo_MV)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 3626 0.0004440013 -0.010352606 0.01124061 -0.01606798 0.01695599
## 3627 0.0011451695 -0.009698891 0.01198923 -0.01543939 0.01772973
## 3628 0.0006643230 -0.010186369 0.01151501 -0.01593038 0.01725902
## 3629 0.0004895206 -0.010375837 0.01135488 -0.01612761 0.01710665
## 3630 0.0004162771 -0.010451654 0.01128421 -0.01620479 0.01703734
## 3631 0.0003855875 -0.010482795 0.01125397 -0.01623617 0.01700734
## 3632 0.0003727283 -0.010495733 0.01124119 -0.01624915 0.01699461
## 3633 0.0003673403 -0.010501135 0.01123582 -0.01625456 0.01698924
## 3634 0.0003650826 -0.010503396 0.01123356 -0.01625682 0.01698699
## 3635 0.0003641366 -0.010504342 0.01123262 -0.01625777 0.01698604
par(mfrow = c(2, 2))
plot(forecast(modelo_AR_MV,h=300),
main="Modelo AR(1,0)",
xlab = 'Horizonte',
ylab = 'Variacion [%]',
lwd = 2,col="blue")
plot(forecast(modelo_MA_MV,h=300),
main="Modelo MA(0,1)",
xlab = 'Horizonte',
ylab = 'Variacion [%]',
lwd = 2,col="red")
plot(forecast(modelo_ARIMA_MV,h=300),
main="Modelo ARIMA(1,1,1)",
xlab = 'Horizonte',
ylab = 'Variacion [%]',
lwd = 2,col="orange")
plot(forecast(modelo_MV,h=300),
main="Modelo ARMA(1,0,3)",
xlab = 'Horizonte',
ylab = 'Variacion [%]',
lwd = 2,col="green")

#-------------------------------------------------------------------------------------------
# Graficar la proyección del modelo AR
plot(forecast(modelo_AR_MV,h=300), main = "Proyección de Modelos AR, MA, ARIMA y ARMA", col = "blue", lwd = 2)
forecast_MA <- forecast(modelo_MA_MV,h=300)
forecast_arima <-forecast(modelo_ARIMA_MV,h=300)
forecast_arma_MV <-forecast(modelo_MV,h=300)
# Agregar la proyección del modelo MA
lines(forecast_MA$mean, col = "red", lwd = 2)
# Agregar la proyección del modelo ARIMA
lines(forecast_arima$mean, col = "orange", lwd = 2)
lines(forecast_arma_MV$mean, col = "green", lwd = 2)
# Agregar una leyenda
legend("topright", legend = c("AR", "MA", "ARIMA","ARMA"), col = c("blue", "red","orange", "green"), lwd = 2)

# Proyectar la volatilidad condicional y los valores de nuestro modelo con los pesos de la minima Varianza
forecast_arch_MV <- ugarchforecast(modelo_ARCH_MV, n.ahead = 20)
forecast_garch_MV <- ugarchforecast(modelo_GARCH_MV, n.ahead = 20)
#-----------------------------------------------------------------------------------------------------
par(mfrow = c(1, 1))
plot(sigma(forecast_arch_MV), type = "l", col = "blue", lwd = 2,
main = "Proyección de la Volatilidad condicional del ARCH y GARCH",
xlab = "Horizonte", ylab = "Volatilidad")
lines(sigma(forecast_garch_MV), col = "red", lwd = 2)
legend("topright", legend = c("Volatilidad de ARCH", "Volatilidad del GARCH"),
col = c("blue", "red"), lwd = 2)

#---------------------------------------------------------------------------------------------------------
plot(fitted(forecast_arch_MV), type = "l", col = "blue", lwd = 2,
main = "Valores Proyectados de los Modelos ARCH y GARCH",
xlab = "Horizonte", ylab = "Valores")
lines(fitted(forecast_garch_MV), col = "red", lwd = 2)
legend("topright", legend = c("Proyeccion de ARCH", "Proyeccion del GARCH"),
col = c("blue", "red"), lwd = 2)

#-------------------------------------------------------------------------------------
forecast_arch_MV
##
## *------------------------------------*
## * GARCH Model Forecast *
## *------------------------------------*
## Model: sGARCH
## Horizon: 20
## Roll Steps: 0
## Out of Sample: 0
##
## 0-roll forecast [T0=2024-05-30]:
## Series Sigma
## T+1 -0.03616 0.03785
## T+2 -0.03616 0.03777
## T+3 -0.03616 0.03770
## T+4 -0.03616 0.03763
## T+5 -0.03616 0.03755
## T+6 -0.03616 0.03748
## T+7 -0.03616 0.03740
## T+8 -0.03616 0.03733
## T+9 -0.03616 0.03726
## T+10 -0.03616 0.03718
## T+11 -0.03616 0.03711
## T+12 -0.03616 0.03704
## T+13 -0.03616 0.03696
## T+14 -0.03616 0.03689
## T+15 -0.03616 0.03682
## T+16 -0.03616 0.03675
## T+17 -0.03616 0.03667
## T+18 -0.03616 0.03660
## T+19 -0.03616 0.03653
## T+20 -0.03616 0.03646
forecast_garch_MV
##
## *------------------------------------*
## * GARCH Model Forecast *
## *------------------------------------*
## Model: sGARCH
## Horizon: 20
## Roll Steps: 0
## Out of Sample: 0
##
## 0-roll forecast [T0=2024-05-30]:
## Series Sigma
## T+1 0.0008021 0.007482
## T+2 0.0006453 0.007511
## T+3 0.0005741 0.007540
## T+4 0.0005417 0.007566
## T+5 0.0005271 0.007592
## T+6 0.0005204 0.007616
## T+7 0.0005174 0.007638
## T+8 0.0005160 0.007660
## T+9 0.0005154 0.007681
## T+10 0.0005151 0.007700
## T+11 0.0005149 0.007718
## T+12 0.0005149 0.007736
## T+13 0.0005149 0.007752
## T+14 0.0005149 0.007768
## T+15 0.0005148 0.007783
## T+16 0.0005148 0.007797
## T+17 0.0005148 0.007811
## T+18 0.0005148 0.007823
## T+19 0.0005148 0.007836
## T+20 0.0005148 0.007847
#plot(ugarchforecast(modelo_ARCH_MV),h=50,n.roll=0)
#Proyecciones de los modelos de nuestro portafolio de pesos Iguales
forecast(modelo_AR_PI)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 3626 0.0004065397 -0.01080653 0.01161961 -0.01674237 0.01755545
## 3627 0.0003754121 -0.01087762 0.01162844 -0.01683461 0.01758543
## 3628 0.0003780422 -0.01087527 0.01163136 -0.01683242 0.01758850
## 3629 0.0003778200 -0.01087550 0.01163114 -0.01683264 0.01758828
## 3630 0.0003778388 -0.01087548 0.01163115 -0.01683262 0.01758830
## 3631 0.0003778372 -0.01087548 0.01163115 -0.01683262 0.01758830
## 3632 0.0003778373 -0.01087548 0.01163115 -0.01683262 0.01758830
## 3633 0.0003778373 -0.01087548 0.01163115 -0.01683262 0.01758830
## 3634 0.0003778373 -0.01087548 0.01163115 -0.01683262 0.01758830
## 3635 0.0003778373 -0.01087548 0.01163115 -0.01683262 0.01758830
forecast(modelo_MA_PI)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 3626 0.0004670553 -0.01074876 0.01168287 -0.01668605 0.01762016
## 3627 0.0003777525 -0.01087302 0.01162852 -0.01682882 0.01758432
## 3628 0.0003777525 -0.01087302 0.01162852 -0.01682882 0.01758432
## 3629 0.0003777525 -0.01087302 0.01162852 -0.01682882 0.01758432
## 3630 0.0003777525 -0.01087302 0.01162852 -0.01682882 0.01758432
## 3631 0.0003777525 -0.01087302 0.01162852 -0.01682882 0.01758432
## 3632 0.0003777525 -0.01087302 0.01162852 -0.01682882 0.01758432
## 3633 0.0003777525 -0.01087302 0.01162852 -0.01682882 0.01758432
## 3634 0.0003777525 -0.01087302 0.01162852 -0.01682882 0.01758432
## 3635 0.0003777525 -0.01087302 0.01162852 -0.01682882 0.01758432
forecast(modelo_ARIMA_PI)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 3626 0.0004064509 -0.01080971 0.01162262 -0.01674719 0.01756010
## 3627 0.0003754243 -0.01088021 0.01163106 -0.01683858 0.01758943
## 3628 0.0003780380 -0.01087790 0.01163397 -0.01683643 0.01759251
## 3629 0.0003778178 -0.01087812 0.01163375 -0.01683665 0.01759229
## 3630 0.0003778364 -0.01087810 0.01163377 -0.01683663 0.01759231
## 3631 0.0003778348 -0.01087810 0.01163377 -0.01683663 0.01759230
## 3632 0.0003778350 -0.01087810 0.01163377 -0.01683663 0.01759230
## 3633 0.0003778349 -0.01087810 0.01163377 -0.01683663 0.01759230
## 3634 0.0003778349 -0.01087810 0.01163377 -0.01683663 0.01759230
## 3635 0.0003778349 -0.01087810 0.01163377 -0.01683663 0.01759230
forecast(modelo_PI)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 3626 -0.0003569571 -0.011517189 0.01080328 -0.01742506 0.01671115
## 3627 0.0015038119 -0.009687661 0.01269529 -0.01561207 0.01861969
## 3628 0.0001416363 -0.011057525 0.01134080 -0.01698600 0.01726928
## 3629 0.0005801305 -0.010628441 0.01178870 -0.01656190 0.01772216
## 3630 0.0002382144 -0.010980537 0.01145697 -0.01691939 0.01739581
## 3631 0.0003993632 -0.010830927 0.01162965 -0.01677588 0.01757461
## 3632 0.0003681836 -0.010872128 0.01160850 -0.01682239 0.01755876
## 3633 0.0003598949 -0.010889627 0.01160942 -0.01684476 0.01756455
## 3634 0.0003940043 -0.010861415 0.01164942 -0.01681967 0.01760768
## 3635 0.0003508599 -0.010908038 0.01160976 -0.01686814 0.01756986
par(mfrow = c(2, 2))
plot(forecast(modelo_AR_PI,h=300),
main="Modelo AR(1,0)",
xlab = 'Horizonte',
ylab = 'Variacion [%]',
lwd = 2,col="blue")
plot(forecast(modelo_MA_PI,h=300),
main="Modelo MA(0,1)",
xlab = 'Horizonte',
ylab = 'Variacion [%]',
lwd = 2,col="red")
plot(forecast(modelo_ARIMA_PI,h=300),
main="Modelo ARIMA(1,0,3)",
xlab = 'Horizonte',
ylab = 'Variacion [%]',
lwd = 2,col="orange")
plot(forecast(modelo_PI,h=300),
main="Modelo ARMA(4,0,1)",
xlab = 'Horizonte',
ylab = 'Variacion [%]',
lwd = 2,col="green")

#-------------------------------------------------------------------------------------------
# Graficar la proyección del modelo AR
plot(forecast(modelo_AR_PI,h=300), main = "Proyección de Modelos AR, MA, ARIMA y ARMA", col = "orange", lwd = 2)
forecast_MA_PI <- forecast(modelo_MA_PI,h=300)
forecast_arima_PI <-forecast(modelo_ARIMA_PI,h=300)
forecast_arma_PI <-forecast(modelo_PI,h=300)
# Agregar la proyección del modelo MA
lines(forecast_MA_PI$mean, col = "red", lwd = 2)
# Agregar la proyección del modelo ARIMA
lines(forecast_arima_PI$mean, col = "green", lwd = 2)
lines(forecast_arma_PI$mean, col = "blue", lwd = 2)
# Agregar una leyenda
legend("topright", legend = c("AR", "MA", "ARIMA","ARMA"), col = c("orange", "red", "green","blue"), lwd = 2)

# Proyectar la volatilidad condicional y los valores de nuestro modelo con los pesos iguales
forecast_arch_PI <- ugarchforecast(modelo_ARCH_PI, n.ahead = 20)
forecast_garch_PI <- ugarchforecast(modelo_GARCH_PI, n.ahead = 20)
#-----------------------------------------------------------------------------------------------------
par(mfrow = c(1, 1))
plot(sigma(forecast_arch_PI), type = "l", col = "blue", lwd = 2,
main = "Proyección de la Volatilidad condicional del ARCH y GARCH",
xlab = "Horizonte", ylab = "Volatilidad")
lines(sigma(forecast_garch_PI), col = "red", lwd = 2)
legend("topright", legend = c("Volatilidad de ARCH", "Volatilidad del GARCH"),
col = c("blue", "red"), lwd = 2)

#---------------------------------------------------------------------------------------------------------
plot(fitted(forecast_arch_PI),ylim=c(-0.05,0.02), type = "l", col = "blue", lwd = 2,
main = "Valores Proyectados de los Modelos ARCH y GARCH",
xlab = "Horizonte", ylab = "Valores")
lines(fitted(forecast_garch_PI),ylim=c(-0.05,0.02), col = "red", lwd = 2)
legend("topright", legend = c("Proyeccion de ARCH", "Proyeccion del GARCH"),
col = c("blue", "red"), lwd = 2)

#-------------------------------------------------------------------------------------
forecast_arch_PI
##
## *------------------------------------*
## * GARCH Model Forecast *
## *------------------------------------*
## Model: sGARCH
## Horizon: 20
## Roll Steps: 0
## Out of Sample: 0
##
## 0-roll forecast [T0=2024-05-30]:
## Series Sigma
## T+1 -0.0339 0.031274
## T+2 -0.0339 0.028822
## T+3 -0.0339 0.026562
## T+4 -0.0339 0.024480
## T+5 -0.0339 0.022561
## T+6 -0.0339 0.020794
## T+7 -0.0339 0.019165
## T+8 -0.0339 0.017665
## T+9 -0.0339 0.016282
## T+10 -0.0339 0.015008
## T+11 -0.0339 0.013835
## T+12 -0.0339 0.012754
## T+13 -0.0339 0.011759
## T+14 -0.0339 0.010842
## T+15 -0.0339 0.009997
## T+16 -0.0339 0.009219
## T+17 -0.0339 0.008503
## T+18 -0.0339 0.007844
## T+19 -0.0339 0.007237
## T+20 -0.0339 0.006679
forecast_garch_PI
##
## *------------------------------------*
## * GARCH Model Forecast *
## *------------------------------------*
## Model: sGARCH
## Horizon: 20
## Roll Steps: 0
## Out of Sample: 0
##
## 0-roll forecast [T0=2024-05-30]:
## Series Sigma
## T+1 0.0009829 0.007172
## T+2 0.0008127 0.007228
## T+3 0.0007063 0.007281
## T+4 0.0006399 0.007331
## T+5 0.0005984 0.007379
## T+6 0.0005725 0.007424
## T+7 0.0005563 0.007468
## T+8 0.0005462 0.007510
## T+9 0.0005399 0.007549
## T+10 0.0005359 0.007587
## T+11 0.0005335 0.007623
## T+12 0.0005319 0.007657
## T+13 0.0005310 0.007690
## T+14 0.0005304 0.007722
## T+15 0.0005300 0.007752
## T+16 0.0005298 0.007780
## T+17 0.0005296 0.007808
## T+18 0.0005295 0.007834
## T+19 0.0005295 0.007859
## T+20 0.0005294 0.007883
#plot(ugarchforecast(modelo_ARCH_MV),h=50,n.roll=0)
plot(sigma(forecast_arch_MV),ylim=c(0.002,0.03), type = "l", col = "blue", lwd = 3,
main = "Proyección de la Volatilidad condicional del ARCH y GARCH",
xlab = "Horizonte", ylab = "Volatilidad")
lines(sigma(forecast_garch_MV),ylim=c(0.002,0.03), col = "red", lwd = 3)
lines(sigma(forecast_arch_PI),ylim=c(0.002,0.03), col = "yellow", lwd = 3)
lines(sigma(forecast_garch_PI),ylim=c(0.002,0.03), col = "green", lwd = 3)
legend("topright", legend = c("ARCH con Minima Varianza", "GARCH con Minima Varianza","ARCH de Pesos Iguales","GARCH de Pesos Iguales"),
col = c("blue","red","yellow","green"), lwd = 3)

plot(fitted(forecast_arch_MV),ylim=c(-0.04,0.01), type = "l", col = "blue", lwd = 3,
main = "Valores Proyectados del ARCH y GARCH",
xlab = "Horizonte", ylab = "Volatilidad")
lines(fitted(forecast_garch_MV),ylim=c(-0.04, 0.01), col = "red", lwd = 3)
lines(fitted(forecast_arch_PI),ylim=c(-0.04, 0.01), col = "yellow", lwd = 3)
lines(fitted(forecast_garch_PI),ylim=c(-0.04, 0.01), col = "green", lwd = 3)
legend("topright", legend = c("ARCH con Minima Varianza", "GARCH con Minima Varianza","ARCH de Pesos Iguales","GARCH de Pesos Iguales"),
col = c("blue","red","yellow","green"), lwd = 3)

#plot(ugarchforecast(modelo_GARCH_MV),head=20)