INDICE S&P 5OO

library(tseries)
library(fPortfolio)
library(kableExtra)
library(tseries)
library(fPortfolio)
library(gplots)
library(readxl)
library(quantmod)
library(PerformanceAnalytics)
## Índice S&P 500
Indice<- get.hist.quote(instrument = "^GSPC", 
                        start=as.Date("2016-01-04"), 
                        end=as.Date("2021-08-31"), quote = "AdjClose")
## time series ends   2021-08-30
## Grafico
plot(Indice, col="red", xlab="Fecha", ylab="Precio_cierre"); title(main="Evolución S&P 5OO") 

summary(Indice)->DATEPRINT
DATEPRINT%>% head()%>% kable(caption = "Datos",
                                    aling ="c")%>%
  add_footnote(label ="Datos portafolio",
               notation = "symbol"
               )%>% kable_material()
Datos
 Index </th>
Adjusted </th>
Min. :2016-01-04 Min. :1829
1st Qu.:2017-06-02 1st Qu.:2409
Median :2018-10-30 Median :2768
Mean :2018-10-31 Mean :2852
3rd Qu.:2020-04-01 3rd Qu.:3128
Max. :2021-08-30 Max. :4529
* Datos portafolio

PORTAFOLIO DE ACCIONES

acciones<-c("JPM","AAPL","MRVL","BAYN.DE","PFE","BABA","AAL","GOOGL","TSLA","AMZN","DIS")
getSymbols(acciones, src = "yahoo", from = "2015-01-01", to = "2020-11-30", periodicity = "daily")
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
##  [1] "JPM"     "AAPL"    "MRVL"    "BAYN.DE" "PFE"     "BABA"    "AAL"    
##  [8] "GOOGL"   "TSLA"    "AMZN"    "DIS"
list <- lapply(acciones, function(x) Cl(get(x)))

PRECIO DE CIERRE

precio.cierre <- do.call(merge,list)
precio.cierre<-na.omit(precio.cierre)
names(precio.cierre)<-c("JPM","AAPL","MRVL","BAYN.DE","PFE","BABA","AAL","GOOGL","TSLA","AMZN","DIS")

CONSRTRUCCION DE CARTERA

cartera_portaf<- merge(precio.cierre, all=FALSE)
rendimientos<- diff(log(cartera_portaf))
rendimientos<- na.omit(rendimientos)

MATRICES DE COVARIANZAS Y CORRELACION

cov <- cov(rendimientos)*100
cov%>%kable(caption = "Matriz de covarianza")%>%kable_styling()
Matriz de covarianza
JPM AAPL MRVL BAYN.DE PFE BABA AAL GOOGL TSLA AMZN DIS
JPM 0.0350926 0.0168066 0.0179318 0.0139736 0.0124575 0.0128851 0.0333521 0.0153409 0.0176563 0.0109081 0.0195710
AAPL 0.0168066 0.0355489 0.0221613 0.0098526 0.0098716 0.0185539 0.0179067 0.0197266 0.0255509 0.0206385 0.0141358
MRVL 0.0179318 0.0221613 0.0607706 0.0128127 0.0085927 0.0220659 0.0222311 0.0181430 0.0254369 0.0183321 0.0151510
BAYN.DE 0.0139736 0.0098526 0.0128127 0.0368799 0.0072005 0.0117913 0.0169744 0.0089347 0.0111964 0.0076927 0.0093516
PFE 0.0124575 0.0098716 0.0085927 0.0072005 0.0191008 0.0077528 0.0116197 0.0100497 0.0086436 0.0081770 0.0090491
BABA 0.0128851 0.0185539 0.0220659 0.0117913 0.0077528 0.0443536 0.0195282 0.0180941 0.0231415 0.0203429 0.0106203
AAL 0.0333521 0.0179067 0.0222311 0.0169744 0.0116197 0.0195282 0.1124896 0.0169978 0.0243381 0.0118013 0.0253342
GOOGL 0.0153409 0.0197266 0.0181430 0.0089347 0.0100497 0.0180941 0.0169978 0.0290343 0.0214402 0.0219885 0.0133760
TSLA 0.0176563 0.0255509 0.0254369 0.0111964 0.0086436 0.0231415 0.0243381 0.0214402 0.1208509 0.0243085 0.0162704
AMZN 0.0109081 0.0206385 0.0183321 0.0076927 0.0081770 0.0203429 0.0118013 0.0219885 0.0243085 0.0386075 0.0108798
DIS 0.0195710 0.0141358 0.0151510 0.0093516 0.0090491 0.0106203 0.0253342 0.0133760 0.0162704 0.0108798 0.0281199

analisis: donde se observa que el activo más riesgoso es la acción JPM, con una varianza de 0.035%, mientras que el activo que presenta la menor varianza es la acción de Disney con 0.0281199%, estando igual que el riesgo individual del mercado, pues el índice S&P 500 tiene una varianza de 0,009%.

corr <- cor(rendimientos) * 100
corr%>%kable(caption = "Matriz de Correlaciones")%>%kable_styling()
Matriz de Correlaciones
JPM AAPL MRVL BAYN.DE PFE BABA AAL GOOGL TSLA AMZN DIS
JPM 100.00000 47.58375 38.83006 38.84240 48.11665 32.65982 53.08341 48.06054 27.11225 29.63494 62.30146
AAPL 47.58375 100.00000 47.67998 27.21091 37.88335 46.72588 28.31692 61.40221 38.98241 55.70947 44.70975
MRVL 38.83006 47.67998 100.00000 27.06439 25.22065 42.50210 26.88796 43.19234 29.68194 37.84685 36.65105
BAYN.DE 38.84240 27.21091 27.06439 100.00000 27.12936 29.15438 26.35383 27.30416 16.77092 20.38683 29.03918
PFE 48.11665 37.88335 25.22065 27.12936 100.00000 26.63610 25.06756 42.67481 17.99054 30.11146 39.04544
BABA 32.65982 46.72588 42.50210 29.15438 26.63610 100.00000 27.64658 50.42171 31.60834 49.16006 30.07233
AAL 53.08341 28.31692 26.88796 26.35383 25.06756 27.64658 100.00000 29.74277 20.87401 17.90756 45.04471
GOOGL 48.06054 61.40221 43.19234 27.30416 42.67481 50.42171 29.74277 100.00000 36.19501 65.67562 46.81285
TSLA 27.11225 38.98241 29.68194 16.77092 17.99054 31.60834 20.87401 36.19501 100.00000 35.58751 27.91045
AMZN 29.63494 55.70947 37.84685 20.38683 30.11146 49.16006 17.90756 65.67562 35.58751 100.00000 33.02011
DIS 62.30146 44.70975 36.65105 29.03918 39.04544 30.07233 45.04471 46.81285 27.91045 33.02011 100.00000
espcartera_portaf<-portfolioSpec()
setRiskFreeRate(espcartera_portaf)<- -0.001 ##Rentabilidad Activo Libre de Riesgo
setNFrontierPoints(espcartera_portaf) <- 20
constraints="LongOnly"
Frontera <- portfolioFrontier(as.timeSeries(rendimientos),spec=espcartera_portaf,constraints)
summary(Frontera)
## 
## Title:
##  MV Portfolio Frontier 
##  Estimator:         covEstimator 
##  Solver:            solveRquadprog 
##  Optimize:          minRisk 
##  Constraints:       LongOnly 
##  Portfolio Points:  5 of 19 
## 
## Portfolio Weights:
##       JPM   AAPL   MRVL BAYN.DE    PFE   BABA    AAL  GOOGL   TSLA   AMZN
## 1  0.0000 0.0000 0.0000  0.4323 0.0000 0.0000 0.5677 0.0000 0.0000 0.0000
## 5  0.0000 0.0000 0.0000  0.3804 0.4664 0.0000 0.0738 0.0000 0.0000 0.0000
## 10 0.0000 0.0443 0.0112  0.0899 0.3996 0.0498 0.0000 0.0293 0.0157 0.1991
## 14 0.0480 0.1007 0.0124  0.0000 0.2301 0.0000 0.0000 0.0000 0.0743 0.4746
## 19 0.0000 0.0000 0.0000  0.0000 0.0000 0.0000 0.0000 0.0000 1.0000 0.0000
##       DIS
## 1  0.0000
## 5  0.0794
## 10 0.1611
## 14 0.0598
## 19 0.0000
## 
## Covariance Risk Budgets:
##       JPM   AAPL   MRVL BAYN.DE    PFE   BABA    AAL  GOOGL   TSLA   AMZN
## 1  0.0000 0.0000 0.0000  0.2149 0.0000 0.0000 0.7851 0.0000 0.0000 0.0000
## 5  0.0000 0.0000 0.0000  0.4573 0.3826 0.0000 0.1016 0.0000 0.0000 0.0000
## 10 0.0000 0.0484 0.0117  0.0711 0.3713 0.0513 0.0000 0.0311 0.0196 0.2409
## 14 0.0334 0.0966 0.0106  0.0000 0.1265 0.0000 0.0000 0.0000 0.0996 0.5959
## 19 0.0000 0.0000 0.0000  0.0000 0.0000 0.0000 0.0000 0.0000 1.0000 0.0000
##       DIS
## 1  0.0000
## 5  0.0585
## 10 0.1545
## 14 0.0374
## 19 0.0000
## 
## Target Returns and Risks:
##       mean     Cov    CVaR     VaR
## 1  -0.0007  0.0227  0.0545  0.0351
## 5  -0.0002  0.0127  0.0309  0.0194
## 10  0.0005  0.0117  0.0287  0.0169
## 14  0.0011  0.0142  0.0352  0.0220
## 19  0.0018  0.0348  0.0800  0.0478
## 
## Description:
##  Sun Sep 05 21:58:55 2021 by user: melvi

FRONTERA DE EFICIENCIA DE PORTAFOLIO

frontierPlot(Frontera)
grid()
tangencyPoints(Frontera, pch = 19, col = "red", cex=2)
tangencyLines(Frontera, col="black", pch=19, cex=2)
minvariancePoints(Frontera, col="blue", pch=19, cex=2)
monteCarloPoints(Frontera, mCsteps=500, col="#0098D5", cex=0.001)

col <- qualiPalette(ncol(rendimientos), "Dark2")
weightsPlot(Frontera, col=col)

efPortfolio <- efficientPortfolio(as.timeSeries(rendimientos),espcartera_portaf,constraints)
print(efPortfolio)
## 
## Title:
##  MV Efficient Portfolio 
##  Estimator:         covEstimator 
##  Solver:            solveRquadprog 
##  Optimize:          minRisk 
##  Constraints:       LongOnly 
## 
## Portfolio Weights:
##     JPM    AAPL    MRVL BAYN.DE     PFE    BABA     AAL   GOOGL    TSLA    AMZN 
##  0.0000  0.0154  0.0041  0.1680  0.4381  0.0613  0.0000  0.0397  0.0000  0.0962 
##     DIS 
##  0.1773 
## 
## Covariance Risk Budgets:
##     JPM    AAPL    MRVL BAYN.DE     PFE    BABA     AAL   GOOGL    TSLA    AMZN 
##  0.0000  0.0154  0.0041  0.1680  0.4381  0.0613  0.0000  0.0397  0.0000  0.0962 
##     DIS 
##  0.1773 
## 
## Target Returns and Risks:
##   mean    Cov   CVaR    VaR 
## 0.0003 0.0114 0.0277 0.0169 
## 
## Description:
##  Sun Sep 05 21:59:06 2021 by user: melvi
tgPortfolio <- tangencyPortfolio(as.timeSeries(rendimientos), espcartera_portaf,constraints)
tgPortfolio
## 
## Title:
##  MV Tangency Portfolio 
##  Estimator:         covEstimator 
##  Solver:            solveRquadprog 
##  Optimize:          minRisk 
##  Constraints:       LongOnly 
## 
## Portfolio Weights:
##     JPM    AAPL    MRVL BAYN.DE     PFE    BABA     AAL   GOOGL    TSLA    AMZN 
##  0.0470  0.0998  0.0129  0.0000  0.2359  0.0000  0.0000  0.0000  0.0729  0.4682 
##     DIS 
##  0.0632 
## 
## Covariance Risk Budgets:
##     JPM    AAPL    MRVL BAYN.DE     PFE    BABA     AAL   GOOGL    TSLA    AMZN 
##  0.0331  0.0963  0.0111  0.0000  0.1319  0.0000  0.0000  0.0000  0.0979  0.5896 
##     DIS 
##  0.0401 
## 
## Target Returns and Risks:
##   mean    Cov   CVaR    VaR 
## 0.0011 0.0142 0.0350 0.0219 
## 
## Description:
##  Sun Sep 05 21:59:06 2021 by user: melvi
##Gráficos de portafolios eficiente y tangente
weightsPie(efPortfolio, col=col, radius = 0.5)
mtext(text = "Portafolio eficiente", side = 3, line = 1.5,
      font = 2, cex = 1, adj = 0)

weightsPie(tgPortfolio, col=col, radius = 0.5)
mtext(text = "Portafolio tangente", side = 3, line = 1.5,
      font = 2, cex = 1, adj = 0)