ticks <- c("^GSPC", "CAT", "LLY")
retout <- NULL
retout <- xts(retout)
for(i in 1:length(ticks)){
prices <- getSymbols(ticks[i], auto.assign = F)
returns <- periodReturn(prices, period = "monthly",
type = "arithmetic")
retout <- merge.xts(retout, returns)
}
colnames(retout) <- ticks
retout = retout['2013/2016']
head(retout)
## ^GSPC CAT LLY
## 2013-01-30 19:00:00 0.05042810 0.09798011 0.08860501
## 2013-02-27 19:00:00 0.01106065 -0.06118504 0.01806670
## 2013-03-27 20:00:00 0.03598772 -0.05846056 0.03896819
## 2013-04-29 20:00:00 0.01808577 -0.02644594 -0.02482831
## 2013-05-30 20:00:00 0.02076281 0.01334599 -0.04008669
## 2013-06-27 20:00:00 -0.01499930 -0.03857815 -0.07599701
meanret <- apply(retout,2,mean)
meanret
## ^GSPC CAT LLY
## 0.009877323 0.002961310 0.009496715
covar <- var(retout)
covar
## ^GSPC CAT LLY
## ^GSPC 0.0009021826 1.151629e-03 2.715537e-04
## CAT 0.0011516291 4.592993e-03 -4.992334e-05
## LLY 0.0002715537 -4.992334e-05 2.324964e-03
weight <- c(0, .4, .6)
rp <- weight[2]*meanret[2] + weight[3]*meanret[3]
rp <- unname(rp) # otherwise picks up second ticker as dimname
rp
## [1] 0.006882553
sig2p <- weight[2]^2*covar[2,2]+weight[3]^2*covar[3,3]+2*weight[2]*weight[3]*covar[2,3]
sig2p
## [1] 0.001547903
sqrt(sig2p)
## [1] 0.03934339
weight <- as.matrix(weight)
dim(weight)
## [1] 3 1
meanret <- as.matrix(meanret)
dim(meanret)
## [1] 3 1
Use matrix multiplication to calculate portfolio metrics:
rp <- t(weight) %*% meanret
rp
## [,1]
## [1,] 0.006882553
sig2p <- t(weight) %*% covar %*% weight
sig2p
## [,1]
## [1,] 0.001547903
sigp <- sqrt(sig2p) # portfolio sigma
sigp
## [,1]
## [1,] 0.03934339
Matrix algebra is useful inorder to avoid complex for loops.
Now we are ready to do multiple computations. Simulate various portfolios and calcualte mean and sigma.
We still work with 2 securities for: . code simplicity . explanation of underlying concept of feasible set
But, it is easy to extend this to 3 or more securities using matrix math.
# initialize a counter and the results matrix
kount <- 0
Results <- matrix(data = NA, nrow = length(seq(.05,.95,.05)), ncol = 4)
for (i in seq(.05,0.95,.05)){
kount <- kount + 1 # counter for portfolio number
Results[kount,1] = weight[2,1] = i # weight of security 1
Results[kount,2] = weight[3,1] = 1-i # weight of security 2
Results[kount,3] <- t(weight) %*% meanret # portfolio mean
Results[kount,4] <- sqrt(t(weight) %*% covar %*% weight) # portfolio sigma
}
colnames(Results) <- c(paste0(ticks[2], '%'), paste0(ticks[3], '%'), "Port_Mean", "Port_Sigma")
Results
## CAT% LLY% Port_Mean Port_Sigma
## [1,] 0.05 0.95 0.009169945 0.04588049
## [2,] 0.10 0.90 0.008843174 0.04381968
## [3,] 0.15 0.85 0.008516404 0.04207610
## [4,] 0.20 0.80 0.008189634 0.04069055
## [5,] 0.25 0.75 0.007862864 0.03970054
## [6,] 0.30 0.70 0.007536094 0.03913609
## [7,] 0.35 0.65 0.007209323 0.03901569
## [8,] 0.40 0.60 0.006882553 0.03934339
## [9,] 0.45 0.55 0.006555783 0.04010824
## [10,] 0.50 0.50 0.006229013 0.04128592
## [11,] 0.55 0.45 0.005902242 0.04284243
## [12,] 0.60 0.40 0.005575472 0.04473822
## [13,] 0.65 0.35 0.005248702 0.04693221
## [14,] 0.70 0.30 0.004921932 0.04938467
## [15,] 0.75 0.25 0.004595162 0.05205908
## [16,] 0.80 0.20 0.004268391 0.05492302
## [17,] 0.85 0.15 0.003941621 0.05794841
## [18,] 0.90 0.10 0.003614851 0.06111127
## [19,] 0.95 0.05 0.003288081 0.06439135
Plot feasible set
plot(x = Results[,4], y = Results[,3], xlab = "Portfolio Sigma", ylab = "Portfolio Mean")