library(readxl)
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
Portafolio <- read_excel("Portafolio.xlsx")
fig <- plot_ly(Portafolio, x = ~Fecha, y = ~ECOPETROL, name = 'ECOPETROL', type = 'scatter', mode = 'lines+markers',ylabel="Precios Acciones")
fig <- fig %>% add_trace(y = ~PFBCOLOM, name = 'PFBCOLOM', mode = 'lines+markers')
fig <- fig %>% add_trace(y = ~PFGRUPSURA, name = 'PFGRUPSURA', mode = 'lines+markers')
fig <- fig %>% add_trace(y = ~CEMARGOS, name = 'CEMARGOS', mode = 'lines+markers')
fig
n<-length(Portafolio$Fecha)
rendimiento_Ecopetrol<-c()
for (i in 1:n) {
rendimiento_Ecopetrol[i]<-(Portafolio$ECOPETROL[i+1]-Portafolio$ECOPETROL[i])/(Portafolio$ECOPETROL[i])
}
rendimiento_Ecopetrol<-na.omit(rendimiento_Ecopetrol)
rendPFBCOLOM<-c()
for (i in 1:n) {
rendPFBCOLOM[i]<-(Portafolio$PFBCOLOM[i+1]-Portafolio$PFBCOLOM[i])/(Portafolio$PFBCOLOM[i])
}
rendPFBCOLOM<-na.omit(rendPFBCOLOM)
rendPFGRUPSURA<-c()
for (i in 1:n) {
rendPFGRUPSURA[i]<-(Portafolio$PFGRUPSURA[i+1]-Portafolio$PFGRUPSURA[i])/(Portafolio$PFGRUPSURA[i])
}
rendPFGRUPSURA<-na.omit(rendPFGRUPSURA)
rendCEMARGOS<-c()
for (i in 1:n) {
rendCEMARGOS[i]<-(Portafolio$CEMARGOS[i+1]-Portafolio$CEMARGOS[i])/(Portafolio$CEMARGOS[i])
}
rendCEMARGOS<-na.omit(rendCEMARGOS)
Fecha1<-Portafolio$Fecha[2:n]
datos<-data.frame(Fecha1,rendimiento_Ecopetrol,rendPFBCOLOM,rendPFGRUPSURA,rendCEMARGOS)
g1<-plot_ly(datos, x = ~Fecha1, y = ~rendimiento_Ecopetrol, name = 'Rendimiento ECOPETROL', type = 'scatter', mode = 'lines+markers')
g1
g2<-plot_ly(datos, x = ~Fecha1, y = ~rendPFBCOLOM, name = 'Rendimiento PFBCOLOM', type = 'scatter', mode = 'lines+markers')
g2
g3<-plot_ly(datos, x = ~Fecha1, y = ~rendPFGRUPSURA, name = 'Rendimiento PFGRUPSURA', type = 'scatter', mode = 'lines+markers')
g3
g4<-plot_ly(datos, x = ~Fecha1, y = ~rendCEMARGOS, name = 'Rendimiento CEMARGOS', type = 'scatter', mode = 'lines+markers')
g4
Veamos los datos de los rendimientos
library(knitr)
kable(datos)
Fecha1 | rendimiento_Ecopetrol | rendPFBCOLOM | rendPFGRUPSURA | rendCEMARGOS |
---|---|---|---|---|
2023-01-03 | -0.0399355 | -0.0136296 | -0.0265152 | -0.0243243 |
2023-01-04 | 0.0147059 | 0.0273355 | -0.0038911 | 0.0135734 |
2023-01-05 | 0.0393375 | 0.0116959 | -0.0101562 | 0.0057393 |
2023-01-06 | 0.0541833 | 0.0028902 | 0.0102605 | 0.0000000 |
2023-01-10 | 0.0147392 | -0.0057637 | 0.0156250 | 0.0309783 |
2023-01-11 | -0.0067039 | 0.0086957 | -0.0007692 | -0.0060622 |
2023-01-12 | 0.0101237 | -0.0100575 | 0.0092379 | -0.0238663 |
2023-01-13 | 0.0074239 | 0.0040639 | -0.0083905 | 0.0051616 |
2023-01-16 | -0.0051584 | 0.0002891 | 0.0000000 | 0.0000000 |
2023-01-17 | 0.0059259 | -0.0037572 | 0.0076923 | -0.0135135 |
2023-01-18 | 0.0320324 | -0.0049318 | -0.0122137 | -0.0180822 |
2023-01-19 | -0.0132001 | 0.0000000 | -0.0069552 | -0.0066964 |
2023-01-20 | -0.0061461 | 0.0084548 | 0.0015564 | 0.0075843 |
2023-01-23 | -0.0043652 | -0.0034692 | -0.0054390 | -0.0242543 |
2023-01-24 | -0.0244794 | -0.0023209 | 0.0062500 | -0.0285714 |
2023-01-25 | -0.0022472 | -0.0055249 | -0.0295031 | -0.0438235 |
2023-01-26 | 0.0108859 | 0.0146199 | 0.0000000 | -0.0003076 |
2023-01-27 | -0.0319346 | 0.0025937 | -0.0016000 | 0.0000000 |
2023-01-30 | -0.0065209 | -0.0037367 | -0.0384615 | -0.0153846 |
2023-01-31 | 0.0305019 | 0.0239469 | 0.0233333 | 0.0040625 |
2023-02-01 | -0.0359685 | -0.0253593 | -0.0228013 | -0.0040461 |
2023-02-02 | -0.0186553 | -0.0112749 | 0.0150000 | 0.0000000 |
2023-02-03 | 0.0047525 | 0.0172515 | -0.0139573 | 0.0000000 |
2023-02-06 | 0.0157667 | 0.0074734 | -0.0008326 | -0.0312500 |
2023-02-07 | 0.0128056 | -0.0171184 | -0.0050000 | 0.0161290 |
2023-02-08 | -0.0107280 | 0.0043541 | -0.0192630 | -0.0473016 |
2023-02-09 | -0.0317583 | -0.0297688 | -0.0034159 | -0.0369877 |
2023-02-10 | 0.0516000 | 0.0020852 | 0.0102828 | -0.0103806 |
2023-02-13 | -0.0110308 | 0.0166468 | -0.0161154 | 0.0129371 |
2023-02-14 | 0.0057692 | -0.0058480 | -0.0603448 | -0.0334829 |
Calculemos el rendimiento esperado
\[\overline{r}_p=W'\overline{R}\]
Para ello minimicemos el siguiente problema:
minimizar: \[\sigma^2_p=W'\Sigma W\] sujeto a: \[\begin{align*} \sum_{i=1}^{n}w_i&=1 \end{align*}\]
Usando multiplicadores de Lagrange, tenemos que:
\[L=\sum_{i=1}^n\sum_{j=1}^nw_iw_j\sigma_{ij}+\lambda\left(\sum_{i=1}^nw_i-1\right)\]
Derivando e igualando a cero, se tiene el siguiente sistema de ecuaciones lineales:
\[\left[\begin{array}{cccccc} 2\sigma^2_1&2\sigma_{12}&2\sigma_{13}&\cdots&2\sigma_{1n}&1\\ 2\sigma_{12}&2\sigma^2_2&2\sigma_{23}&\cdots&2\sigma_{2n}&1\\ 2\sigma_{13}&2\sigma_{23}&2\sigma^2_{3}&\cdots&2\sigma_{3n}&1\\ \vdots&\vdots&\vdots&\vdots&\vdots&\vdots\\ 2\sigma_{1n}&2\sigma_{2n}&2\sigma_{3n}&\cdots&2\sigma^2_{n}&1\\ 1&1&1&\cdots&1&0 \end{array}\right]\cdot\left[\begin{array}{c} w_1 \\ w_2\\ w_3\\ \vdots\\ w_m\\ \lambda \end{array}\right]=\left[\begin{array}{c} 0 \\ 0\\ 0\\ \vdots\\ 0\\ 1 \end{array}\right]\]
Para nuestros datos tenemos que:
a<-datos %>%
summarise(
cov_11 = cov(rendimiento_Ecopetrol, rendimiento_Ecopetrol),
cov_12 = cov(rendimiento_Ecopetrol, rendPFBCOLOM),
cov_13= cov(rendimiento_Ecopetrol,rendPFGRUPSURA),
cov_14= cov(rendimiento_Ecopetrol,rendCEMARGOS),
cov_22= cov(rendPFBCOLOM,rendPFBCOLOM),
cov_23= cov(rendPFBCOLOM,rendPFGRUPSURA),
cov_24= cov(rendPFBCOLOM,rendCEMARGOS),
cov_33= cov(rendPFGRUPSURA,rendPFGRUPSURA),
cov_34= cov(rendPFGRUPSURA,rendCEMARGOS),
cov_44= cov(rendCEMARGOS,rendCEMARGOS)
)
attach(a)
SIG<-matrix(c(2*a$cov_11,2*a$cov_12,2*a$cov_13,2*a$cov_14,1,2*a$cov_12,2*a$cov_22,2*a$cov_23,2*a$cov_24,1,2*a$cov_13,2*a$cov_23,2*a$cov_33,2*a$cov_34,1,2*a$cov_14,2*a$cov_24,2*a$cov_34,2*a$cov_44,1,1,1,1,1,0),5,5,T)
SIG
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0011475631 2.690791e-04 2.460458e-04 0.0002363984 1
## [2,] 0.0002690791 3.382611e-04 8.862087e-05 0.0001733240 1
## [3,] 0.0002460458 8.862087e-05 6.033568e-04 0.0002728494 1
## [4,] 0.0002363984 1.733240e-04 2.728494e-04 0.0007156860 1
## [5,] 1.0000000000 1.000000e+00 1.000000e+00 1.0000000000 0
wi<-solve(SIG)%*%matrix(c(0,0,0,0,1),5,1,T)
wi
## [,1]
## [1,] -0.0085294183
## [2,] 0.6286897038
## [3,] 0.2864716751
## [4,] 0.0933680394
## [5,] -0.0002519365
Luego el valor esperado del portafolio es:
\[\overline{r}_p=\sum_{i=1}^{n}w_i\overline{r}_i\]
rp<-matrix(c(mean(rendimiento_Ecopetrol),mean(rendPFBCOLOM),mean(rendPFGRUPSURA),mean(rendCEMARGOS)),1,4,T)%*%wi[1:4,]
rp
## [,1]
## [1,] -0.002438312