data <- read_excel("3firmExample_data1.xlsx")
Compute the optimal weights for the global minimum variance portfolio based on three assets.
cov_matrix <- cov(data[, 2:4])
one <- rep(1,3)
one_31 <- matrix(one,ncol=1)
a <- inv(cov_matrix)%*%one_31 # 3x3 x 3x1 = 3x1
b <- t(one_31)%*%inv(cov_matrix)%*%one_31 # 1x3 x 3x3 x 3x1 = 1x1
mvp <- a/as.vector(b)
as.tibble(mvp)
## Warning: `as.tibble()` was deprecated in tibble 2.0.0.
## ℹ Please use `as_tibble()` instead.
## ℹ The signature and semantics have changed, see `?as_tibble`.
## Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if
## `.name_repair` is omitted as of tibble 2.0.0.
## ℹ Using compatibility `.name_repair`.
## ℹ The deprecated feature was likely used in the tibble package.
## Please report the issue at <]8;;https://github.com/tidyverse/tibble/issueshttps://github.com/tidyverse/tibble/issues]8;;>.
## # A tibble: 3 × 1
## V1
## <dbl>
## 1 0.364
## 2 0.194
## 3 0.443
colnames(mvp)="Weight"
mvp
## Weight
## Nordstrom 0.3635964
## Starbucks 0.1936574
## Microsoft 0.4427462
For a given portfolio return of 4.5%, compute the optimal weights for the minimum variance portfolio based on the three assets and portfolio risk (standard deviation).
return <- data[,2:4]
mean_return <-colMeans(return)
mean_return
## Nordstrom Starbucks Microsoft
## 0.001545065 0.028460847 0.042711830
A <-rbind(2*cov_matrix,mean_return,t(one_31)) #nối hàng
A <- cbind(A, rbind(t(tail(A, 2)), matrix(0, 2, 2))) #nối cột
# A 5x5, b0 1x5
b0 <- c(rep(0, ncol(return)), 0.045/12, 1)
x <- solve(A,b0)
port_weight <- as.matrix(x[1:3])
as.tibble(port_weight)
## # A tibble: 3 × 1
## V1
## <dbl>
## 1 0.908
## 2 0.112
## 3 -0.0197
colnames(port_weight) = "Weight"
port_weight
## Weight
## Nordstrom 0.90766134
## Starbucks 0.11201674
## Microsoft -0.01967808