Investment Portfolio Analysis Mid Term

library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## ################################### WARNING ###################################
## # We noticed you have dplyr installed. The dplyr lag() function breaks how    #
## # base R's lag() function is supposed to work, which breaks lag(my_xts).      #
## #                                                                             #
## # If you call library(dplyr) later in this session, then calls to lag(my_xts) #
## # that you enter or source() into this session won't work correctly.          #
## #                                                                             #
## # All package code is unaffected because it is protected by the R namespace   #
## # mechanism.                                                                  #
## #                                                                             #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #
## #                                                                             #
## # You can use stats::lag() to make sure you're not using dplyr::lag(), or you #
## # can add conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop   #
## # dplyr from breaking base R's lag() function.                                #
## ################################### WARNING ###################################
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(tidyquant)
## Loading required package: lubridate
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
## Loading required package: PerformanceAnalytics
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
library(lubridate)
library(timetk)
library(purrr)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr   1.1.0     ✔ stringr 1.5.0
## ✔ forcats 1.0.0     ✔ tibble  3.1.8
## ✔ ggplot2 3.4.1     ✔ tidyr   1.3.0
## ✔ readr   2.1.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first()  masks xts::first()
## ✖ dplyr::lag()    masks stats::lag()
## ✖ dplyr::last()   masks xts::last()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(tibble)
library(readr)
library(xts)
library(PerformanceAnalytics)
library(magrittr)
## 
## Attaching package: 'magrittr'
## 
## The following object is masked from 'package:tidyr':
## 
##     extract
## 
## The following object is masked from 'package:purrr':
## 
##     set_names
library(dplyr)

Question 1

Download ETF daily data from yahoo with ticker names of SPY, QQQ, EEM, IWM, EFA, TLT, IYR and GLD from 2010 to current date (See http://etfdb.com/ for ETF information). (Hint: Use library quantmod to help you to download these prices and use adjusted prices for your computation.)

tickers <- c("SPY", "QQQ", "EEM", "IWM", "EFA", "TLT", "IYR", "GLD")
data = new.env()
getSymbols(tickers, src = 'yahoo', from = '2010-01-01', to = '2021-04-14', auto.assign = TRUE)
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## [1] "SPY" "QQQ" "EEM" "IWM" "EFA" "TLT" "IYR" "GLD"
ETF_Data<- merge(Ad(SPY), Ad(QQQ),Ad(EEM), Ad(IWM),Ad(EFA),Ad(TLT),Ad(IYR),Ad(GLD))
colnames(ETF_Data) <- c("SPY", "QQQ", "EEM", "IWM", "EFA", "TLT", "IYR", "GLD")
head(ETF_Data)
##                 SPY      QQQ      EEM      IWM      EFA      TLT      IYR
## 2010-01-04 86.86007 40.73329 31.82711 52.51540 37.52378 61.13187 28.10298
## 2010-01-05 87.08998 40.73329 32.05812 52.33483 37.55686 61.52665 28.17048
## 2010-01-06 87.15130 40.48757 32.12519 52.28557 37.71560 60.70303 28.15819
## 2010-01-07 87.51920 40.51389 31.93890 52.67136 37.57008 60.80515 28.40971
## 2010-01-08 87.81046 40.84735 32.19225 52.95864 37.86773 60.77791 28.21953
## 2010-01-11 87.93309 40.68064 32.12519 52.74524 38.17862 60.44436 28.35450
##               GLD
## 2010-01-04 109.80
## 2010-01-05 109.70
## 2010-01-06 111.51
## 2010-01-07 110.82
## 2010-01-08 111.37
## 2010-01-11 112.85
ETF_Data.xts <- xts(ETF_Data)
head(ETF_Data.xts)
##                 SPY      QQQ      EEM      IWM      EFA      TLT      IYR
## 2010-01-04 86.86007 40.73329 31.82711 52.51540 37.52378 61.13187 28.10298
## 2010-01-05 87.08998 40.73329 32.05812 52.33483 37.55686 61.52665 28.17048
## 2010-01-06 87.15130 40.48757 32.12519 52.28557 37.71560 60.70303 28.15819
## 2010-01-07 87.51920 40.51389 31.93890 52.67136 37.57008 60.80515 28.40971
## 2010-01-08 87.81046 40.84735 32.19225 52.95864 37.86773 60.77791 28.21953
## 2010-01-11 87.93309 40.68064 32.12519 52.74524 38.17862 60.44436 28.35450
##               GLD
## 2010-01-04 109.80
## 2010-01-05 109.70
## 2010-01-06 111.51
## 2010-01-07 110.82
## 2010-01-08 111.37
## 2010-01-11 112.85

Question 2

Calculate weekly and monthly returns using discrete returns

weekly_returns <- to.weekly(ETF_Data.xts, indexAt = "last", OHLC = FALSE)
ETF_weekly_returns <- na.omit(Return.calculate(weekly_returns, method = "discrete"))
head(ETF_weekly_returns)
##                     SPY          QQQ         EEM         IWM          EFA
## 2010-01-15 -0.008117291 -0.015037230 -0.02893497 -0.01301907 -0.003493581
## 2010-01-22 -0.038982959 -0.036859359 -0.05578071 -0.03062199 -0.055740123
## 2010-01-29 -0.016665160 -0.031023671 -0.03357730 -0.02624317 -0.025803223
## 2010-02-05 -0.006797515  0.004439813 -0.02821308 -0.01397438 -0.019054673
## 2010-02-12  0.012938098  0.018148474  0.03333336  0.02952581  0.005244720
## 2010-02-19  0.028692959  0.024451765  0.02445335  0.03343146  0.022995204
##                      TLT          IYR          GLD
## 2010-01-15  2.004649e-02 -0.006304073 -0.004579348
## 2010-01-22  1.010162e-02 -0.041785190 -0.033285251
## 2010-01-29  3.369057e-03 -0.008447703 -0.011290464
## 2010-02-05 -5.349054e-05  0.003223858 -0.012080021
## 2010-02-12 -1.946114e-02 -0.007573999  0.022544908
## 2010-02-19 -8.205260e-03  0.050184743  0.022701794
monthly_returns <- to.monthly(ETF_Data.xts, indexAt = "last", OHLC = FALSE)
ETF_monthly_returns <- na.omit(Return.calculate(monthly_returns, method = "discrete"))
head(ETF_monthly_returns)
##                    SPY         QQQ          EEM         IWM         EFA
## 2010-02-26  0.03119473  0.04603872  0.017763809  0.04475079  0.00266776
## 2010-03-31  0.06087965  0.07710921  0.081108579  0.08230713  0.06385434
## 2010-04-30  0.01547025  0.02242522 -0.001661556  0.05678459 -0.02804589
## 2010-05-28 -0.07945456 -0.07392379 -0.093935991 -0.07536625 -0.11192786
## 2010-06-30 -0.05174144 -0.05975656 -0.013986406 -0.07743382 -0.02061989
## 2010-07-30  0.06830093  0.07258253  0.109324720  0.06730879  0.11610458
##                     TLT         IYR          GLD
## 2010-02-26 -0.003424477  0.05457082  0.032748217
## 2010-03-31 -0.020573453  0.09748493 -0.004386393
## 2010-04-30  0.033218409  0.06388087  0.058834366
## 2010-05-28  0.051083793 -0.05683502  0.030513141
## 2010-06-30  0.057978401 -0.04670136  0.023553189
## 2010-07-30 -0.009464147  0.09404774 -0.050871154

Question 3

Download Fama French 3 factors data and change to digit numbers (not in percentage): •Go to http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data_library.html •Download Fama/French 3 factor returns’ monthly data (Mkt-RF, SMB and HML).

famafrench <- read_csv("C:\\Users\\emman\\Downloads\\F-F_Research_Data_Factors_CSV\\F-F_Research_Data_Factors.csv")
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 1272 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): DATE
## dbl (4): Mkt-RF, SMB, HML, RF
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(famafrench)
## # A tibble: 6 × 5
##   DATE   `Mkt-RF`   SMB   HML    RF
##   <chr>     <dbl> <dbl> <dbl> <dbl>
## 1 192607     2.96 -2.56 -2.43  0.22
## 2 192608     2.64 -1.17  3.82  0.25
## 3 192609     0.36 -1.4   0.13  0.23
## 4 192610    -3.24 -0.09  0.7   0.32
## 5 192611     2.53 -0.1  -0.51  0.31
## 6 192612     2.62 -0.03 -0.05  0.28
colnames(famafrench) <- paste(c("date","Mkt-RF","SMB","HML","RF"))
famafrench.digit <- famafrench %>% mutate(date = as.character(date))%>% 
  mutate(date=ymd(parse_date(date,format="%Y%m"))) %>%
  mutate(date=rollback(date))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `date = ymd(parse_date(date, format = "%Y%m"))`.
## Caused by warning:
## ! 99 parsing failures.
##  row col       expected                           actual
## 1173  -- date like %Y%m Annual Factors: January-December
## 1175  -- date like %Y%m 1927                            
## 1176  -- date like %Y%m 1928                            
## 1177  -- date like %Y%m 1929                            
## 1178  -- date like %Y%m 1930                            
## .... ... .............. ................................
## See problems(...) for more details.
head(famafrench.digit)
## # A tibble: 6 × 5
##   date       `Mkt-RF`   SMB   HML    RF
##   <date>        <dbl> <dbl> <dbl> <dbl>
## 1 1926-06-30     2.96 -2.56 -2.43  0.22
## 2 1926-07-31     2.64 -1.17  3.82  0.25
## 3 1926-08-31     0.36 -1.4   0.13  0.23
## 4 1926-09-30    -3.24 -0.09  0.7   0.32
## 5 1926-10-31     2.53 -0.1  -0.51  0.31
## 6 1926-11-30     2.62 -0.03 -0.05  0.28
famafrench.digit <- famafrench.digit[complete.cases(famafrench.digit), ]
famafrench.digit.xts <- xts(famafrench.digit[,-1],order.by=as.Date(famafrench.digit$date))
head(famafrench.digit.xts)
##            Mkt-RF   SMB   HML   RF
## 1926-06-30   2.96 -2.56 -2.43 0.22
## 1926-07-31   2.64 -1.17  3.82 0.25
## 1926-08-31   0.36 -1.40  0.13 0.23
## 1926-09-30  -3.24 -0.09  0.70 0.32
## 1926-10-31   2.53 -0.10 -0.51 0.31
## 1926-11-30   2.62 -0.03 -0.05 0.28

Question 4

Merge monthly return data in question 2 and 3.

merged_data <- merge(famafrench.digit,ETF_monthly_returns)
tail(merged_data)
##              date Mkt-RF   SMB   HML   RF        SPY        QQQ         EEM
## 158215 2023-08-31  -5.24 -2.51  1.52 0.43 0.04170778 0.06727648 0.002062358
## 158216 2023-09-30  -3.19 -3.87  0.19 0.47 0.04170778 0.06727648 0.002062358
## 158217 2023-10-31   8.84 -0.02  1.64 0.44 0.04170778 0.06727648 0.002062358
## 158218 2023-11-30   4.87  6.34  4.93 0.43 0.04170778 0.06727648 0.002062358
## 158219 2023-12-31   0.71 -5.09 -2.38 0.47 0.04170778 0.06727648 0.002062358
## 158220 2024-01-31   5.06 -0.24 -3.48 0.42 0.04170778 0.06727648 0.002062358
##                IWM        EFA        TLT        IYR        GLD
## 158215 0.000905095 0.02820616 0.02376805 0.03371777 0.02169283
## 158216 0.000905095 0.02820616 0.02376805 0.03371777 0.02169283
## 158217 0.000905095 0.02820616 0.02376805 0.03371777 0.02169283
## 158218 0.000905095 0.02820616 0.02376805 0.03371777 0.02169283
## 158219 0.000905095 0.02820616 0.02376805 0.03371777 0.02169283
## 158220 0.000905095 0.02820616 0.02376805 0.03371777 0.02169283
final_data_as_tibble <- as_tibble(merged_data)
head(final_data_as_tibble)
## # A tibble: 6 × 13
##   date       `Mkt-RF`   SMB   HML    RF    SPY    QQQ    EEM    IWM     EFA
##   <date>        <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>  <dbl>  <dbl>   <dbl>
## 1 1926-06-30     2.96 -2.56 -2.43  0.22 0.0312 0.0460 0.0178 0.0448 0.00267
## 2 1926-07-31     2.64 -1.17  3.82  0.25 0.0312 0.0460 0.0178 0.0448 0.00267
## 3 1926-08-31     0.36 -1.4   0.13  0.23 0.0312 0.0460 0.0178 0.0448 0.00267
## 4 1926-09-30    -3.24 -0.09  0.7   0.32 0.0312 0.0460 0.0178 0.0448 0.00267
## 5 1926-10-31     2.53 -0.1  -0.51  0.31 0.0312 0.0460 0.0178 0.0448 0.00267
## 6 1926-11-30     2.62 -0.03 -0.05  0.28 0.0312 0.0460 0.0178 0.0448 0.00267
## # … with 3 more variables: TLT <dbl>, IYR <dbl>, GLD <dbl>

Question 5

Based on CAPM model, compute MVP monthly returns based on estimated covariance matrix for the 8-asset portfolio by using past 60-month returns from 2019/03 - 2024/02.

final_data2 <- final_data_as_tibble[final_data_as_tibble$date>="2019-03-01"&final_data_as_tibble$date<="2024-02-01",]
head(final_data2)
## # A tibble: 6 × 13
##   date       `Mkt-RF`   SMB   HML    RF    SPY    QQQ    EEM    IWM     EFA
##   <date>        <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>  <dbl>  <dbl>   <dbl>
## 1 2019-03-31     3.97 -1.74  2.15  0.21 0.0312 0.0460 0.0178 0.0448 0.00267
## 2 2019-04-30    -6.94 -1.32 -2.37  0.21 0.0312 0.0460 0.0178 0.0448 0.00267
## 3 2019-05-31     6.93  0.29 -0.71  0.18 0.0312 0.0460 0.0178 0.0448 0.00267
## 4 2019-06-30     1.19 -1.93  0.48  0.19 0.0312 0.0460 0.0178 0.0448 0.00267
## 5 2019-07-31    -2.58 -2.38 -4.78  0.16 0.0312 0.0460 0.0178 0.0448 0.00267
## 6 2019-08-31     1.43 -0.96  6.75  0.18 0.0312 0.0460 0.0178 0.0448 0.00267
## # … with 3 more variables: TLT <dbl>, IYR <dbl>, GLD <dbl>
spy_rf <- final_data_as_tibble$SPY-final_data_as_tibble$RF
qqq_rf <- final_data_as_tibble$QQQ-final_data_as_tibble$RF
eem_rf <- final_data_as_tibble$EEM-final_data_as_tibble$RF
iwm_rf <- final_data_as_tibble$IWM-final_data_as_tibble$RF
efa_rf <- final_data_as_tibble$EFA-final_data_as_tibble$RF
tlt_rf <- final_data_as_tibble$TLT-final_data_as_tibble$RF
iyr_rf <- final_data_as_tibble$IYR-final_data_as_tibble$RF
gld_rf <- final_data_as_tibble$GLD-final_data_as_tibble$RF
y <- cbind(spy_rf,qqq_rf,eem_rf,iwm_rf,efa_rf,tlt_rf,iyr_rf,gld_rf)
n <- nrow(y)
one.vec <- rep(1,n)
x <- cbind(one.vec,final_data_as_tibble$`Mkt-RF`)
x.mat <- as.matrix(x)
beta <- solve(t(x)%*%x)%*%t(x)%*%y
beta
##               spy_rf       qqq_rf       eem_rf       iwm_rf       efa_rf
## one.vec -0.257580973 -0.252687282 -0.264474097 -0.257645508 -0.263693113
##          0.003069948  0.003069948  0.003069948  0.003069948  0.003069948
##               tlt_rf       iyr_rf       gld_rf
## one.vec -0.264089533 -0.260174432 -0.265765506
##          0.003069948  0.003069948  0.003069948
e.hat <- y - x%*%beta
res.var <- diag(t(e.hat)%*%e.hat)/(n-2)
d <- diag(res.var);d
##            [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
## [1,] 0.06407155 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
## [2,] 0.00000000 0.06461115 0.00000000 0.00000000 0.00000000 0.00000000
## [3,] 0.00000000 0.00000000 0.06534369 0.00000000 0.00000000 0.00000000
## [4,] 0.00000000 0.00000000 0.00000000 0.06552049 0.00000000 0.00000000
## [5,] 0.00000000 0.00000000 0.00000000 0.00000000 0.06450559 0.00000000
## [6,] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.06388393
## [7,] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
## [8,] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
##            [,7]       [,8]
## [1,] 0.00000000 0.00000000
## [2,] 0.00000000 0.00000000
## [3,] 0.00000000 0.00000000
## [4,] 0.00000000 0.00000000
## [5,] 0.00000000 0.00000000
## [6,] 0.00000000 0.00000000
## [7,] 0.06447875 0.00000000
## [8,] 0.00000000 0.06465395
cov.mat <- var(final_data_as_tibble$`Mkt-RF`)*t(beta)%*%beta + d
cov.mat
##          spy_rf   qqq_rf   eem_rf   iwm_rf   efa_rf   tlt_rf   iyr_rf   gld_rf
## spy_rf 1.955798 1.855791 1.942343 1.892200 1.936609 1.939520 1.910770 1.951826
## qqq_rf 1.855791 1.885150 1.905447 1.856256 1.899821 1.902676 1.874473 1.914750
## eem_rf 1.942343 1.905447 2.059659 1.942830 1.988427 1.991416 1.961897 2.004052
## iwm_rf 1.892200 1.856256 1.942830 1.958195 1.937094 1.940005 1.911249 1.952315
## efa_rf 1.936609 1.899821 1.988427 1.937094 2.047062 1.985536 1.956105 1.998135
## tlt_rf 1.939520 1.902676 1.991416 1.940005 1.985536 2.052405 1.959045 2.001138
## iyr_rf 1.910770 1.874473 1.961897 1.911249 1.956105 1.959045 1.994485 1.971476
## gld_rf 1.951826 1.914750 2.004052 1.952315 1.998135 2.001138 1.971476 2.078490
one.vec2 <- rep(1,8)
top <- solve(cov.mat)%*%one.vec2
bot <- t(one.vec2)%*%top
mvp_capm <- top/as.numeric(bot)
mvp_capm
##              [,1]
## spy_rf  0.4735263
## qqq_rf  0.9990351
## eem_rf -0.2731200
## iwm_rf  0.4561692
## efa_rf -0.1920332
## tlt_rf -0.2372801
## iyr_rf  0.1893654
## gld_rf -0.4156627

Question 6

Based on FF 3-factor model, compute MVP monthly returns covariance matrix for the 8-asset portfolio by using past 60-month returns from 2019/03 -2024/02.

t <- dim(final_data2)[1]
markets <- final_data2[,c(2,3,4)]
FM <- final_data2[,c(-1,-2,-3,-4,-5)]
head(FM)
## # A tibble: 6 × 8
##      SPY    QQQ    EEM    IWM     EFA      TLT    IYR    GLD
##    <dbl>  <dbl>  <dbl>  <dbl>   <dbl>    <dbl>  <dbl>  <dbl>
## 1 0.0312 0.0460 0.0178 0.0448 0.00267 -0.00342 0.0546 0.0327
## 2 0.0312 0.0460 0.0178 0.0448 0.00267 -0.00342 0.0546 0.0327
## 3 0.0312 0.0460 0.0178 0.0448 0.00267 -0.00342 0.0546 0.0327
## 4 0.0312 0.0460 0.0178 0.0448 0.00267 -0.00342 0.0546 0.0327
## 5 0.0312 0.0460 0.0178 0.0448 0.00267 -0.00342 0.0546 0.0327
## 6 0.0312 0.0460 0.0178 0.0448 0.00267 -0.00342 0.0546 0.0327
FM <- as.matrix(FM)
n <- dim(FM)[2]
one_vec <- rep(1,t)
p <- cbind(one_vec,markets)
p <- as.matrix(p)
b.hat <- solve(t(p)%*%p)%*%t(p)%*%FM
res <- FM-p%*%b.hat
diag.d <- diag(t(res)%*%res)/(t-6)
diag.d
##         SPY         QQQ         EEM         IWM         EFA         TLT 
## 0.001593373 0.002133371 0.002866460 0.003043388 0.002027735 0.001405613 
##         IYR         GLD 
## 0.002000875 0.002176211
retvar <- apply(FM,2,var)
rsq <- 1-diag(t(res)%*%res)/((t-1)/retvar)
res.stdev <- sqrt(diag.d)
factor.cov <- var(FM)*t(b.hat)%*%b.hat+diag(diag.d)
stdev <- sqrt(diag(factor.cov))
factor.cor <- factor.cov/(stdev%*%t(stdev))
factor.cor
##               SPY           QQQ           EEM           IWM           EFA
## SPY  1.000000e+00  1.992778e-04  5.234299e-05  1.386861e-04  6.929616e-05
## QQQ  1.992778e-04  1.000000e+00  6.726480e-05  1.682160e-04  8.864295e-05
## EEM  5.234299e-05  6.726480e-05  1.000000e+00  4.901045e-05  3.008275e-05
## IWM  1.386861e-04  1.682160e-04  4.901045e-05  1.000000e+00  6.201341e-05
## EFA  6.929616e-05  8.864295e-05  3.008275e-05  6.201341e-05  1.000000e+00
## TLT -3.402736e-05 -3.774208e-05 -1.225265e-05 -3.743786e-05 -1.678536e-05
## IYR  8.859360e-05  1.040039e-04  3.366757e-05  8.384221e-05  4.182007e-05
## GLD  2.949977e-06  7.882589e-06  6.345593e-06  1.220868e-06  2.116499e-06
##               TLT           IYR          GLD
## SPY -3.402736e-05  8.859360e-05 2.949977e-06
## QQQ -3.774208e-05  1.040039e-04 7.882589e-06
## EEM -1.225265e-05  3.366757e-05 6.345593e-06
## IWM -3.743786e-05  8.384221e-05 1.220868e-06
## EFA -1.678536e-05  4.182007e-05 2.116499e-06
## TLT  1.000000e+00 -3.000744e-06 6.488119e-06
## IYR -3.000744e-06  1.000000e+00 6.547089e-06
## GLD  6.488119e-06  6.547089e-06 1.000000e+00
sample.cov <- cov(FM)
sample.cor <- cor(FM)
sample.cov
##               SPY           QQQ           EEM           IWM           EFA
## SPY  0.0015923727  0.0016945338  0.0016038794  0.0019708392  0.0015669222
## QQQ  0.0016945338  0.0021320317  0.0017133455  0.0019871446  0.0016661971
## EEM  0.0016038794  0.0017133455  0.0028646601  0.0020862809  0.0020376119
## IWM  0.0019708392  0.0019871446  0.0020862809  0.0030414775  0.0019480259
## EFA  0.0015669222  0.0016661971  0.0020376119  0.0019480259  0.0020264619
## TLT -0.0006831249 -0.0006298565 -0.0007368303 -0.0010441279 -0.0007448921
## IYR  0.0012818711  0.0012509378  0.0014592159  0.0016852937  0.0013375734
## GLD  0.0001024304  0.0002275218  0.0006600064  0.0000588911  0.0001624498
##               TLT           IYR          GLD
## SPY -6.831249e-04  1.281871e-03 0.0001024304
## QQQ -6.298565e-04  1.250938e-03 0.0002275218
## EEM -7.368303e-04  1.459216e-03 0.0006600064
## IWM -1.044128e-03  1.685294e-03 0.0000588911
## EFA -7.448921e-04  1.337573e-03 0.0001624498
## TLT  1.404730e-03 -8.521095e-05 0.0004421335
## IYR -8.521095e-05  1.999619e-03 0.0003215533
## GLD  4.421335e-04  3.215533e-04 0.0021748446
sample.cor
##             SPY        QQQ        EEM         IWM         EFA         TLT
## SPY  1.00000000  0.9196679  0.7509531  0.89554317  0.87227983 -0.45675284
## QQQ  0.91966794  1.0000000  0.6932846  0.78035164  0.80160484 -0.36395550
## EEM  0.75095306  0.6932846  1.0000000  0.70679574  0.84569878 -0.36731144
## IWM  0.89554317  0.7803516  0.7067957  1.00000000  0.78466305 -0.50514369
## EFA  0.87227983  0.8016048  0.8456988  0.78466305  1.00000000 -0.44149692
## TLT -0.45675284 -0.3639555 -0.3673114 -0.50514369 -0.44149692  1.00000000
## IYR  0.71837036  0.6058503  0.6096905  0.68337599  0.66446916 -0.05084232
## GLD  0.05504176  0.1056604  0.2644219  0.02289777  0.07738125  0.25295475
##             IYR        GLD
## SPY  0.71837036 0.05504176
## QQQ  0.60585026 0.10566035
## EEM  0.60969050 0.26442195
## IWM  0.68337599 0.02289777
## EFA  0.66446916 0.07738125
## TLT -0.05084232 0.25295475
## IYR  1.00000000 0.15419324
## GLD  0.15419324 1.00000000
one <- rep(1,8)
top.mat <- solve(factor.cov)%*%one
bot.mat <- t(one)%*%top.mat
MVP_FF3 <- top.mat/as.numeric(bot.mat)
MVP_FF3
##           [,1]
## SPY 0.15935085
## QQQ 0.11897834
## EEM 0.08860295
## IWM 0.08341610
## EFA 0.12524994
## TLT 0.18075352
## IYR 0.12691443
## GLD 0.11673388

#Question 7 You can invest in the 8-asset portfolio in 2024/03 based on the optimal weights of MVP from question 5 and 6. What are the realized portfolio returns in the March of 2024 using the weights from question 5 and 6?

final_data3 <- tail(final_data_as_tibble, n=1)
final_data3
## # A tibble: 1 × 13
##   date       `Mkt-RF`   SMB   HML    RF    SPY    QQQ     EEM      IWM    EFA
##   <date>        <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>   <dbl>    <dbl>  <dbl>
## 1 2024-01-31     5.06 -0.24 -3.48  0.42 0.0417 0.0673 0.00206 0.000905 0.0282
## # … with 3 more variables: TLT <dbl>, IYR <dbl>, GLD <dbl>
final_data3 <- final_data3[,c(-1,-2,-3,-4,-5)]
final_data3
## # A tibble: 1 × 8
##      SPY    QQQ     EEM      IWM    EFA    TLT    IYR    GLD
##    <dbl>  <dbl>   <dbl>    <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
## 1 0.0417 0.0673 0.00206 0.000905 0.0282 0.0238 0.0337 0.0217
returnsQ5 <- rowSums(final_data3 * mvp_capm) #QUESTION 5
returnsQ5
## [1] 0.07312278
returnsQ6 <- rowSums(final_data3 * MVP_FF3) #QUESTION 6
returnsQ6
## [1] 0.02954938

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.