Importind data
library(readxl)
hw4 <- read_excel("C:/Users/DELL/Downloads/hw4.xlsx")
head(hw4)
## # A tibble: 6 × 4
## date Nordstrom Starbucks Microsoft
## <dttm> <dbl> <dbl> <dbl>
## 1 1995-03-01 00:00:00 -0.0362 0.00521 0.121
## 2 1995-04-01 00:00:00 -0.0568 -0.0211 0.139
## 3 1995-05-01 00:00:00 0.0782 0.212 0.0353
## 4 1995-06-01 00:00:00 -0.00302 0.204 0.0650
## 5 1995-07-01 00:00:00 -0.0276 0.0480 0.00138
## 6 1995-08-01 00:00:00 0.0277 0.0679 0.0219
Computing optimal weight
library(fBasics)
Sigma <- cov(hw4[, 2:4])
ones <- rep(1, 3)
one.vec <- matrix(ones, ncol =1)
a <- inv(Sigma)%*%one.vec
b <- t(one.vec)%*%a
mvp.w <- a / as.numeric(b)
head(mvp.w)
## [,1]
## 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).
mu <- 0.045/12
return <- hw4[,2:4]
Ax <- rbind(2*cov(return), colMeans(return), rep(1, ncol(return)))
Ax <- cbind(Ax, rbind(t(tail(Ax, 2)), matrix(0, 2, 2)))
b0 <- c(rep(0, ncol(return)), mu, 1)
out <- solve(Ax, b0)
wgt <- out[1:3]
wgt
## Nordstrom Starbucks Microsoft
## 0.90766134 0.11201674 -0.01967808
sum(wgt)
## [1] 1