Key Issues
portfolio returns = returns of basket of securities benchmark portfolio = hypothetical portfolio that we can compare the performance of our portf
Portfolio return is the weighted average of the returns of the individual securities in the portfolio. Suppose we invested $50K in AMZN, $10 in AAPL, $30 in MSFT, and $10 in IBM How much would this portfolio return b/t December 31, 2010 and December 31, 2013?
Step 0: Import Price Data
# To clean up the memory of your current R session run the following line
rm(list=ls(all=TRUE))
library(quantmod)
library(xts)
#Importing Price Data
data.AMZN <- getSymbols("AMZN", from = "2013-12-31", to = "2017-10-01", auto.assign = FALSE)
data.MSFT <- getSymbols("MSFT", from = "2013-12-31", to = "2017-10-01", auto.assign = FALSE)
data.IBM <- getSymbols("IBM", from = "2013-12-31", to = "2017-10-01", auto.assign = FALSE)
data.TSLA <- getSymbols("TSLA", from = "2013-12-31", to = "2017-10-01", auto.assign = FALSE)
data.FB <- getSymbols("FB", from = "2013-12-31", to = "2017-10-01", auto.assign = FALSE)
data.AAPL <- getSymbols("AAPL", from = "2013-12-31", to = "2017-10-01", auto.assign = FALSE)
#to = "2017-09-31" in the book was replaced by to = "2017-10-01"
#B/c that code doesn't produce the output that includes stock price of 2017-09-31the date.
multi <- data.AMZN[, 6] # Need to create a new data object before cbinding
multi <- cbind(multi, data.AAPL[, 6], data.TSLA[, 6], data.FB[, 6], data.MSFT[, 6], data.IBM[, 6])
Step 1: Find First and Last Adjusted Closing Price for Each Security Over the Investmet Period
period.ret <- multi[c(1, nrow(multi)), ]
period.ret
## AMZN.Adjusted AAPL.Adjusted TSLA.Adjusted FB.Adjusted
## 2013-12-31 398.79 74.57128 150.43 54.65
## 2017-09-29 961.35 154.11999 341.10 170.87
## MSFT.Adjusted IBM.Adjusted
## 2013-12-31 33.91713 166.089
## 2017-09-29 74.49000 145.080
Step 2: Calculate Returns for Each Security Over the Investment Period
rets <- lapply(period.ret, Delt) # lapply returns output in a list
rets
## $AMZN.Adjusted
## Delt.1.arithmetic
## 2013-12-31 NA
## 2017-09-29 1.410667
##
## $AAPL.Adjusted
## Delt.1.arithmetic
## 2013-12-31 NA
## 2017-09-29 1.066747
##
## $TSLA.Adjusted
## Delt.1.arithmetic
## 2013-12-31 NA
## 2017-09-29 1.2675
##
## $FB.Adjusted
## Delt.1.arithmetic
## 2013-12-31 NA
## 2017-09-29 2.126624
##
## $MSFT.Adjusted
## Delt.1.arithmetic
## 2013-12-31 NA
## 2017-09-29 1.196236
##
## $IBM.Adjusted
## Delt.1.arithmetic
## 2013-12-31 NA
## 2017-09-29 -0.1264924
#rets <- mapply(Delt, data.frame(period.ret)) #mapply does the same but returns output in DF
Step 3: Convert to a data.frame and Clean up Data
rets <- data.frame(rets)
rets
## Delt.1.arithmetic Delt.1.arithmetic.1 Delt.1.arithmetic.2
## 2013-12-31 NA NA NA
## 2017-09-29 1.410667 1.066747 1.2675
## Delt.1.arithmetic.3 Delt.1.arithmetic.4 Delt.1.arithmetic.5
## 2013-12-31 NA NA NA
## 2017-09-29 2.126624 1.196236 -0.1264924
rets <- rets[2, ] * 100
names(rets) <- paste(c("AMZN", "AAPL", "MSFT", "TSLA", "FB", "IBM"))
rets
## AMZN AAPL MSFT TSLA FB IBM
## 2017-09-29 141.0667 106.6747 126.75 212.6624 119.6236 -12.64924
Step 4: Calculate Weight of Each Security in the Portfolio
i.AMZN <- 40000
i.AAPL <- 10000
i.MSFT <- 10000
i.TSLA <- 20000
i.FB <- 10000
i.IBM <- 10000
w.AMZN <- i.AMZN / (i.AMZN + i.AAPL + i.MSFT + i.TSLA + i.FB + i.IBM)
w.AAPL <- i.AAPL / (i.AMZN + i.AAPL + i.MSFT + i.TSLA + i.FB + i.IBM)
w.MSFT<- i.MSFT / (i.AMZN + i.AAPL + i.MSFT + i.TSLA + i.FB + i.IBM)
w.TSLA<- i.TSLA / (i.AMZN + i.AAPL + i.MSFT + i.TSLA + i.FB + i.IBM)
w.FB<- i.FB / (i.AMZN + i.AAPL + i.MSFT + i.TSLA + i.FB + i.IBM)
w.IBM <- i.IBM / (i.AMZN + i.AAPL + i.MSFT + i.TSLA + i.FB + i.IBM)
w.AMZN
## [1] 0.4
w.AAPL
## [1] 0.1
w.MSFT
## [1] 0.1
w.TSLA
## [1] 0.2
w.FB
## [1] 0.1
w.IBM
## [1] 0.1
Step 5: Calculate Portfolio Return
port.ret.4asset <- w.AMZN * rets$AMZN + w.AAPL * rets$AAPL + w.MSFT * rets$MSFT + w.TSLA * rets$TSLA + w.FB * rets$FB + w.IBM * rets$IBM
port.ret.4asset
## [1] 132.9991
Interpretation
Step 0: Import Price Data
# To clean up the memory of your current R session run the following line
rm(list=ls(all=TRUE))
library(quantmod)
library(xts)
#Importing Price Data
data.AMZN <- getSymbols("AMZN", from = "2013-12-31", to = "2017-10-01", auto.assign = FALSE)
data.MSFT <- getSymbols("MSFT", from = "2013-12-31", to = "2017-10-01", auto.assign = FALSE)
data.IBM <- getSymbols("IBM", from = "2013-12-31", to = "2017-10-01", auto.assign = FALSE)
data.TSLA <- getSymbols("TSLA", from = "2013-12-31", to = "2017-10-01", auto.assign = FALSE)
data.FB <- getSymbols("FB", from = "2013-12-31", to = "2017-10-01", auto.assign = FALSE)
data.AAPL <- getSymbols("AAPL", from = "2013-12-31", to = "2017-10-01", auto.assign = FALSE)
#to = "2017-09-31" in the book was replaced by to = "2017-10-01"
#B/c that code doesn't produce the output that includes stock price of 2017-09-31the date.
multi <- data.AMZN[, 6] # Need to create a new data object before cbinding
multi <- cbind(multi, data.AAPL[, 6], data.TSLA[, 6], data.FB[, 6], data.MSFT[, 6], data.IBM[, 6])
Step 1: Find First and Last Adjusted Closing Price for Each Security Over the Investmet Period
period.ret <- multi[c(1, nrow(multi)), ]
period.ret
## AMZN.Adjusted AAPL.Adjusted TSLA.Adjusted FB.Adjusted
## 2013-12-31 398.79 74.57128 150.43 54.65
## 2017-09-29 961.35 154.11999 341.10 170.87
## MSFT.Adjusted IBM.Adjusted
## 2013-12-31 33.91713 166.089
## 2017-09-29 74.49000 145.080
Step 2: Calculate Returns for Each Security Over the Investment Period
rets <- lapply(period.ret, Delt) # lapply returns output in a list
rets
## $AMZN.Adjusted
## Delt.1.arithmetic
## 2013-12-31 NA
## 2017-09-29 1.410667
##
## $AAPL.Adjusted
## Delt.1.arithmetic
## 2013-12-31 NA
## 2017-09-29 1.066747
##
## $TSLA.Adjusted
## Delt.1.arithmetic
## 2013-12-31 NA
## 2017-09-29 1.2675
##
## $FB.Adjusted
## Delt.1.arithmetic
## 2013-12-31 NA
## 2017-09-29 2.126624
##
## $MSFT.Adjusted
## Delt.1.arithmetic
## 2013-12-31 NA
## 2017-09-29 1.196236
##
## $IBM.Adjusted
## Delt.1.arithmetic
## 2013-12-31 NA
## 2017-09-29 -0.1264924
#rets <- mapply(Delt, data.frame(period.ret)) #mapply does the same but returns output in DF
Step 3: Convert to a data.frame and Clean up Data
rets <- data.frame(rets)
rets
## Delt.1.arithmetic Delt.1.arithmetic.1 Delt.1.arithmetic.2
## 2013-12-31 NA NA NA
## 2017-09-29 1.410667 1.066747 1.2675
## Delt.1.arithmetic.3 Delt.1.arithmetic.4 Delt.1.arithmetic.5
## 2013-12-31 NA NA NA
## 2017-09-29 2.126624 1.196236 -0.1264924
rets <- rets[2, ] * 100
names(rets) <- paste(c("AMZN", "AAPL", "MSFT", "TSLA", "FB", "IBM"))
rets
## AMZN AAPL MSFT TSLA FB IBM
## 2017-09-29 141.0667 106.6747 126.75 212.6624 119.6236 -12.64924
Step 4: Calculate Weight of Each Security in the Portfolio
i.AMZN <- 10000
i.AAPL <- 10000
i.MSFT <- 10000
i.TSLA <- 30000
i.FB <- 10000
i.IBM <- 30000
w.AMZN <- i.AMZN / (i.AMZN + i.AAPL + i.MSFT + i.TSLA + i.FB + i.IBM)
w.AAPL <- i.AAPL / (i.AMZN + i.AAPL + i.MSFT + i.TSLA + i.FB + i.IBM)
w.MSFT<- i.MSFT / (i.AMZN + i.AAPL + i.MSFT + i.TSLA + i.FB + i.IBM)
w.TSLA<- i.TSLA / (i.AMZN + i.AAPL + i.MSFT + i.TSLA + i.FB + i.IBM)
w.FB<- i.FB / (i.AMZN + i.AAPL + i.MSFT + i.TSLA + i.FB + i.IBM)
w.IBM <- i.IBM / (i.AMZN + i.AAPL + i.MSFT + i.TSLA + i.FB + i.IBM)
w.AMZN
## [1] 0.1
w.AAPL
## [1] 0.1
w.MSFT
## [1] 0.1
w.TSLA
## [1] 0.3
w.FB
## [1] 0.1
w.IBM
## [1] 0.3
Step 5: Calculate Portfolio Return
port.ret.4asset <- w.AMZN * rets$AMZN + w.AAPL * rets$AAPL + w.MSFT * rets$MSFT + w.TSLA * rets$TSLA + w.FB * rets$FB + w.IBM * rets$IBM
port.ret.4asset
## [1] 109.4154
Interpretation