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 TSLA, $30 in AAPL, 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.AAPL <- getSymbols("AAPL", 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.MSFT <- getSymbols("MSFT", from = "2013-12-31", to = "2017-10-01", auto.assign = FALSE)
#to = "2013-12-31" in the book was replaced by to = "2014-01-01"
#B/c that code doesn't produce the output that includes stock price of 2013-12-31the date.
multi <- data.AMZN[, 6] # Need to create a new data object before cbinding
multi <- cbind(multi, data.TSLA[, 6], data.AAPL[, 6], data.IBM[, 6], data.FB[, 6], data.MSFT[, 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 TSLA.Adjusted AAPL.Adjusted IBM.Adjusted
## 2013-12-31 398.79 150.43 74.57128 166.089
## 2017-09-29 961.35 341.10 154.11999 145.080
## FB.Adjusted MSFT.Adjusted
## 2013-12-31 54.65 33.91713
## 2017-09-29 170.87 74.49000
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
##
## $TSLA.Adjusted
## Delt.1.arithmetic
## 2013-12-31 NA
## 2017-09-29 1.2675
##
## $AAPL.Adjusted
## Delt.1.arithmetic
## 2013-12-31 NA
## 2017-09-29 1.066747
##
## $IBM.Adjusted
## Delt.1.arithmetic
## 2013-12-31 NA
## 2017-09-29 -0.1264924
##
## $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
#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.2675 1.066747
## Delt.1.arithmetic.3 Delt.1.arithmetic.4 Delt.1.arithmetic.5
## 2013-12-31 NA NA NA
## 2017-09-29 -0.1264924 2.126624 1.196236
rets <- rets[2, ] * 100
names(rets) <- paste(c("AMZN", "TSLA", "AAPL", "IBM", "FB", "MSFT"))
rets
## AMZN TSLA AAPL IBM FB MSFT
## 2017-09-29 141.0667 126.75 106.6747 -12.64924 212.6624 119.6236
Step 4: Calculate Weight of Each Security in the Portfolio
i.AMZN <- 40000
i.TSLA <- 20000
i.AAPL <- 10000
i.IBM <- 10000
i.FB <- 10000
i.MSFT <- 10000
w.AMZN <- i.AMZN / (i.AMZN + i.TSLA + i.AAPL + i.IBM + i.FB + i.MSFT)
w.TSLA <- i.TSLA / (i.AMZN + i.TSLA + i.AAPL + i.IBM + i.FB + i.MSFT)
w.AAPL <- i.AAPL / (i.AMZN + i.TSLA + i.AAPL + i.IBM + i.FB + i.MSFT)
w.IBM <- i.IBM / (i.AMZN + i.TSLA + i.AAPL + i.IBM + i.FB + i.MSFT)
w.FB <- i.FB / (i.AMZN + i.TSLA + i.AAPL + i.IBM + i.FB + i.MSFT)
w.MSFT <- i.MSFT / (i.AMZN + i.TSLA + i.AAPL + i.IBM + i.FB + i.MSFT)
w.AMZN
## [1] 0.4
w.TSLA
## [1] 0.2
w.AAPL
## [1] 0.1
w.IBM
## [1] 0.1
w.FB
## [1] 0.1
w.MSFT
## [1] 0.1
Step 5: Calculate Portfolio Return
port.ret.4asset <- w.AMZN * rets$AMZN + w.TSLA * rets$TSLA + w.AAPL * rets$AAPL + w.IBM * rets$IBM + w.FB * rets$FB + w.MSFT + rets$MSFT
port.ret.4asset
## [1] 232.169
Interpretation
Alternate Portfolio
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 TSLA, $30 in AAPL, 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.AAPL <- getSymbols("AAPL", 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.MSFT <- getSymbols("MSFT", from = "2013-12-31", to = "2017-10-01", auto.assign = FALSE)
#to = "2013-12-31" in the book was replaced by to = "2014-01-01"
#B/c that code doesn't produce the output that includes stock price of 2013-12-31the date.
multi <- data.AMZN[, 6] # Need to create a new data object before cbinding
multi <- cbind(multi, data.TSLA[, 6], data.AAPL[, 6], data.IBM[, 6], data.FB[, 6], data.MSFT[, 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 TSLA.Adjusted AAPL.Adjusted IBM.Adjusted
## 2013-12-31 398.79 150.43 74.57128 166.089
## 2017-09-29 961.35 341.10 154.11999 145.080
## FB.Adjusted MSFT.Adjusted
## 2013-12-31 54.65 33.91713
## 2017-09-29 170.87 74.49000
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
##
## $TSLA.Adjusted
## Delt.1.arithmetic
## 2013-12-31 NA
## 2017-09-29 1.2675
##
## $AAPL.Adjusted
## Delt.1.arithmetic
## 2013-12-31 NA
## 2017-09-29 1.066747
##
## $IBM.Adjusted
## Delt.1.arithmetic
## 2013-12-31 NA
## 2017-09-29 -0.1264924
##
## $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
#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.2675 1.066747
## Delt.1.arithmetic.3 Delt.1.arithmetic.4 Delt.1.arithmetic.5
## 2013-12-31 NA NA NA
## 2017-09-29 -0.1264924 2.126624 1.196236
rets <- rets[2, ] * 100
names(rets) <- paste(c("AMZN", "TSLA", "AAPL", "IBM", "FB", "MSFT"))
rets
## AMZN TSLA AAPL IBM FB MSFT
## 2017-09-29 141.0667 126.75 106.6747 -12.64924 212.6624 119.6236
Step 4: Calculate Weight of Each Security in the Portfolio
i.AMZN <- 10000
i.TSLA <- 30000
i.AAPL <- 10000
i.IBM <- 30000
i.FB <- 10000
i.MSFT <- 10000
w.AMZN <- i.AMZN / (i.AMZN + i.TSLA + i.AAPL + i.IBM + i.FB + i.MSFT)
w.TSLA <- i.TSLA / (i.AMZN + i.TSLA + i.AAPL + i.IBM + i.FB + i.MSFT)
w.AAPL <- i.AAPL / (i.AMZN + i.TSLA + i.AAPL + i.IBM + i.FB + i.MSFT)
w.IBM <- i.IBM / (i.AMZN + i.TSLA + i.AAPL + i.IBM + i.FB + i.MSFT)
w.FB <- i.FB / (i.AMZN + i.TSLA + i.AAPL + i.IBM + i.FB + i.MSFT)
w.MSFT <- i.MSFT / (i.AMZN + i.TSLA + i.AAPL + i.IBM + i.FB + i.MSFT)
w.AMZN
## [1] 0.1
w.TSLA
## [1] 0.3
w.AAPL
## [1] 0.1
w.IBM
## [1] 0.3
w.FB
## [1] 0.1
w.MSFT
## [1] 0.1
Step 5: Calculate Portfolio Return
port.ret.4asset <- w.AMZN * rets$AMZN + w.TSLA * rets$TSLA + w.AAPL * rets$AAPL + w.IBM * rets$IBM + w.FB * rets$FB + w.MSFT + rets$MSFT
port.ret.4asset
## [1] 199.9942