library(tseries)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## BIMBO CEMEX MSFT AMZN SHELL ASML
## 2011-02-01 26.00 8.75 27.99 8.61 26.49 41.18
## 2011-02-02 25.51 8.70 27.94 8.68 26.59 41.19
## 2011-02-03 25.25 8.82 27.65 8.69 26.00 40.70
## BIMBO CEMEX MSFT AMZN SHELL ASML
## 2024-01-29 79.54 14.32 409.72 161.26 29.10 804.8
## 2024-01-30 77.59 14.35 408.59 159.00 29.25 801.3
## 2024-01-31 78.26 14.27 397.58 155.20 29.07 798.2
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
Return <- diff(log(Portfolio))
head(Return, n = 2)
## BIMBO CEMEX MSFT AMZN SHELL
## 2011-02-02 -0.01892982 -0.006095012 -0.001787922 0.008216699 0.004144644
## 2011-02-03 -0.01024439 0.013877061 -0.010433659 0.001036727 -0.022626664
## ASML
## 2011-02-02 0.0001577375
## 2011-02-03 -0.0118945617
plot(Return, main = "Graph of my Portfolio returns", xlab = "Date", col = 2:7)
R_average <- apply(Return, 2, mean) #Average return
Volatility <- apply(Return, 2, sd)
Risk <- apply(Return, 2, var)
Table <- data.frame(rbind(R_average, Volatility, Risk))
names(Table) <- assets
Table <- t(round(Table, 6))
Table
## R_average Volatility Risk
## BIMBO 0.000350 0.018407 0.000339
## CEMEX 0.000155 0.024449 0.000598
## MSFT 0.000842 0.016594 0.000275
## AMZN 0.000917 0.021063 0.000444
## SHELL 0.000029 0.017608 0.000310
## ASML 0.000940 0.020288 0.000412
boxplot(Return, col = 2:7, horizontal = TRUE)
legend("topright", assets, fill=2:7)
C <- cov(Return)
round(C,7)
## BIMBO CEMEX MSFT AMZN SHELL ASML
## BIMBO 0.0003388 0.0001211 0.0000692 0.0000621 0.0000518 0.0000615
## CEMEX 0.0001211 0.0005978 0.0001200 0.0001269 0.0001256 0.0001360
## MSFT 0.0000692 0.0001200 0.0002754 0.0001979 0.0000567 0.0001259
## AMZN 0.0000621 0.0001269 0.0001979 0.0004437 0.0000385 0.0001240
## SHELL 0.0000518 0.0001256 0.0000567 0.0000385 0.0003100 0.0001156
## ASML 0.0000615 0.0001360 0.0001259 0.0001240 0.0001156 0.0004116
cat("The minimum covariance is:", min(C))
## The minimum covariance is: 3.845899e-05
which(C == min(C), arr.ind = TRUE)
## row col
## SHELL 5 4
## AMZN 4 5
#The two companies with the minimal covariance are SHELL and AMAZN
EY1 <- Table["SHELL","R_average"]
cat("EY1 =", EY1, "\n")
## EY1 = 2.9e-05
VY1 <- Table["SHELL","Risk"]
cat("VY1 =", VY1, "\n")
## VY1 = 0.00031
EY2 <- Table["AMZN","R_average"]
cat("EY2 =", EY2, "\n")
## EY2 = 0.000917
VY2 <- Table["AMZN","Risk"]
cat("VY2 =", VY2, "\n")
## VY2 = 0.000444
CovY1Y2 <- min(C)
cat("Cov(Y1,Y2) =", CovY1Y2)
## Cov(Y1,Y2) = 3.845899e-05
cat("EY1 =", EY1, "\n")
## EY1 = 2.9e-05
cat("EY2 =", EY2, "\n")
## EY2 = 0.000917
cat("VY1 =", VY1, "\n")
## VY1 = 0.00031
cat("VY2 =", VY2, "\n")
## VY2 = 0.000444
cat("Cov(Y1,Y2) =", CovY1Y2)
## Cov(Y1,Y2) = 3.845899e-05
Rm <- function(a){
a*EY1 + (1-a)*EY2
}
Vr <- function(a){
a^2*VY1 + (1-a)^2*VY2 + 2*a*(1-a)*CovY1Y2
}
#Ahora vamos a calcular el average return and risk
a_optima = (VY2 - CovY1Y2) / (VY1 + VY2 - 2 * CovY1Y2)
b_optima = 1 - a_optima
#Function that calculates the average return based on a
Rm <- function(a) {a*EY1 + (1-a)*EY2}
Rm_optimo = Rm(a_optima) #optimal return
#Function that calculates the risk based on a
Vr <- function(a) {a^2*VY1 + (1-a)^2*VY2 + 2*a*(1-a)*CovY1Y2}
Vr_optimo = Vr(a_optima) #optimal risk
cat("a_optima =", a_optima, "\n")
## a_optima = 0.598954
cat("b_optima =", b_optima, "\n")
## b_optima = 0.401046
cat("Optimal return =", Rm_optimo, "\n")
## Optimal return = 0.0003851288
cat("Optimal risk =", Vr_optimo, "\n")
## Optimal risk = 0.0002010996
# Illustrae Risk vs Return graph
a = seq(0,1,0.01)
b = 1-a
plot(Vr(a), Rm(a), type = "l", col = "red", xlab = "Risk", ylab = "Return", lwd =2)
# Efficient frontier graph and optimal point
plot(Vr(a), Rm(a), type = "l", col = "red", xlab = "Risk", ylab = "Return", lwd = 2)
abline(v = Vr_optimo, col = "blue", lty = 3, lwd = 2)
abline(h = Rm_optimo, col = "black", lty = 3, lwd = 2)
points(Vr_optimo, Rm_optimo, pch = 20, col = "black", lwd = 7)
# Efficient frontier
a1 = seq(0, a_optima, 0.001)
lines(Vr(a1), Rm(a1), col = "blue", xlab = "Risk", ylab = "Return", lwd =3)
# Arrow marking the efficient frontier
arrows(0.00025, 0.00045, 0.00020, 0.00038, code = 2)
# Label
text(0.00025, 0.00045, "Efficient frontier", pos = 3)