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


x<-c("quantmod", "tidyquant", "lubridate", "timetk", "purrr","tidyverse", "tibble", "readr", "xts", "performanceanalytics", "magrittr", "dplyr")

lapply(x, require, character.only = TRUE)
## Loading required package: 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
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Loading required package: tidyquant
## Warning: package 'tidyquant' was built under R version 4.4.3
## Warning: package 'PerformanceAnalytics' was built under R version 4.4.3
## ── Attaching core tidyquant packages ─────────────────────── tidyquant 1.0.11 ──
## ✔ PerformanceAnalytics 2.0.8
## ── Conflicts ────────────────────────────────────────── tidyquant_conflicts() ──
## ✖ zoo::as.Date()                 masks base::as.Date()
## ✖ zoo::as.Date.numeric()         masks base::as.Date.numeric()
## ✖ PerformanceAnalytics::legend() masks graphics::legend()
## ✖ quantmod::summary()            masks base::summary()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Loading required package: lubridate
## 
## 
## Attaching package: 'lubridate'
## 
## 
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
## 
## 
## Loading required package: timetk
## Warning: package 'timetk' was built under R version 4.4.3
## 
## Attaching package: 'timetk'
## 
## The following object is masked from 'package:tidyquant':
## 
##     FANG
## 
## Loading required package: purrr
## Loading required package: tidyverse
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr   1.1.4     ✔ stringr 1.5.1
## ✔ forcats 1.0.0     ✔ tibble  3.2.1
## ✔ ggplot2 3.5.1     ✔ tidyr   1.3.1
## ✔ readr   2.1.5     ── 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 conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errorsLoading required package: performanceanalytics
## Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
## logical.return = TRUE, : there is no package called 'performanceanalytics'
## Loading required package: magrittr
## 
## Attaching package: 'magrittr'
## 
## The following object is masked from 'package:tidyr':
## 
##     extract
## 
## The following object is masked from 'package:purrr':
## 
##     set_names
## [[1]]
## [1] TRUE
## 
## [[2]]
## [1] TRUE
## 
## [[3]]
## [1] TRUE
## 
## [[4]]
## [1] TRUE
## 
## [[5]]
## [1] TRUE
## 
## [[6]]
## [1] TRUE
## 
## [[7]]
## [1] TRUE
## 
## [[8]]
## [1] TRUE
## 
## [[9]]
## [1] TRUE
## 
## [[10]]
## [1] FALSE
## 
## [[11]]
## [1] TRUE
## 
## [[12]]
## [1] TRUE
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)
## [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 85.76846 40.48582 31.08410 51.92011 36.38437 58.68241 27.40289
## 2010-01-05 85.99548 40.48582 31.30972 51.74157 36.41643 59.06135 27.46869
## 2010-01-06 86.05604 40.24161 31.37521 51.69289 36.57036 58.27076 27.45672
## 2010-01-07 86.41930 40.26777 31.19326 52.07430 36.42926 58.36873 27.70199
## 2010-01-08 86.70688 40.59919 31.44071 52.35832 36.71787 58.34260 27.51655
## 2010-01-11 86.82797 40.43347 31.37521 52.14734 37.01932 58.02243 27.64814
##               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

2. Calculate weekly and monthly returns using discrete returns.


etf_data.xts <- xts(etf_data)
head(etf_data)
##                 SPY      QQQ      EEM      IWM      EFA      TLT      IYR
## 2010-01-04 85.76846 40.48582 31.08410 51.92011 36.38437 58.68241 27.40289
## 2010-01-05 85.99548 40.48582 31.30972 51.74157 36.41643 59.06135 27.46869
## 2010-01-06 86.05604 40.24161 31.37521 51.69289 36.57036 58.27076 27.45672
## 2010-01-07 86.41930 40.26777 31.19326 52.07430 36.42926 58.36873 27.70199
## 2010-01-08 86.70688 40.59919 31.44071 52.35832 36.71787 58.34260 27.51655
## 2010-01-11 86.82797 40.43347 31.37521 52.14734 37.01932 58.02243 27.64814
##               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

WEEKLY - Only count the last day of the week. exclude na values

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.008117313 -0.015037535 -0.02893523 -0.01301920 -0.003493580
## 2010-01-22 -0.038982871 -0.036859451 -0.05578065 -0.03062164 -0.055740338
## 2010-01-29 -0.016665322 -0.031023326 -0.03357724 -0.02624334 -0.025803083
## 2010-02-05 -0.006797297  0.004440172 -0.02821332 -0.01397428 -0.019054797
## 2010-02-12  0.012938499  0.018147851  0.03333344  0.02952568  0.005244899
## 2010-02-19  0.028692839  0.024451696  0.02445360  0.03343168  0.022995029
##                      TLT          IYR          GLD
## 2010-01-15  2.004732e-02 -0.006304396 -0.004579349
## 2010-01-22  1.010091e-02 -0.041785624 -0.033285246
## 2010-01-29  3.370149e-03 -0.008446762 -0.011290465
## 2010-02-05 -5.502338e-05  0.003223281 -0.012080019
## 2010-02-12 -1.946101e-02 -0.007573715  0.022544905
## 2010-02-19 -8.204916e-03  0.050184404  0.022701796
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.03119478  0.04603912  0.017763624  0.04475112  0.002667681
## 2010-03-31  0.06088000  0.07710873  0.081108750  0.08230691  0.063854219
## 2010-04-30  0.01546970  0.02242557 -0.001661597  0.05678457 -0.028045505
## 2010-05-28 -0.07945428 -0.07392414 -0.093936049 -0.07536604 -0.111927979
## 2010-06-30 -0.05174062 -0.05975628 -0.013986487 -0.07743426 -0.020619751
## 2010-07-30  0.06830001  0.07258186  0.109324931  0.06730951  0.116104407
##                     TLT         IYR          GLD
## 2010-02-26 -0.003424668  0.05457001  0.032748219
## 2010-03-31 -0.020573878  0.09748536 -0.004386396
## 2010-04-30  0.033218425  0.06388063  0.058834363
## 2010-05-28  0.051083026 -0.05683542  0.030513147
## 2010-06-30  0.057978818 -0.04670140  0.023553189
## 2010-07-30 -0.009464197  0.09404822 -0.050871157


3.Download Fama French 3 factors data and change to digit numbers (not inpercentage): •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).

fama <- read_csv("C:/Users/ramera/Downloads/F-F_Research_Data_Factors.csv")
## New names:
## Rows: 1288 Columns: 5
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (5): This file was created by CMPT_ME_BEME_RETS using the 202412 CRSP da...
## ℹ 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.
## • `` -> `...2`
## • `` -> `...3`
## • `` -> `...4`
## • `` -> `...5`
head(fama)
## # A tibble: 6 × 5
##   This file was created by CMPT_ME_BEME_RETS using the…¹ ...2  ...3  ...4  ...5 
##   <chr>                                                  <chr> <chr> <chr> <chr>
## 1 The 1-month TBill rate data until 202405 are from Ibb… the … <NA>  <NA>  <NA> 
## 2 <NA>                                                   <NA>  <NA>  <NA>  <NA> 
## 3 <NA>                                                   Mkt-… SMB   HML   RF   
## 4 192607                                                 2.96  -2.56 -2.43 0.22 
## 5 192608                                                 2.64  -1.17 3.82  0.25 
## 6 192609                                                 0.36  -1.4  0.13  0.23 
## # ℹ abbreviated name:
## #   ¹​`This file was created by CMPT_ME_BEME_RETS using the 202412 CRSP database.`

Format the data frame

colnames(fama) <- paste(c("date","MKT-RF","SMB","HML","RF"))
fama.digit <- fama %>% 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:
## ! 101 parsing failures.
##  row col       expected                                                                                      actual
##    1  -- date like %Y%m The 1-month TBill rate data until 202405 are from Ibbotson Associates. Starting from 202406
## 1187  -- date like %Y%m Annual Factors: January-December                                                           
## 1189  -- date like %Y%m 1927                                                                                       
## 1190  -- date like %Y%m 1928                                                                                       
## 1191  -- date like %Y%m 1929                                                                                       
## .... ... .............. ...........................................................................................
## See problems(...) for more details.
head(fama.digit)
## # A tibble: 6 × 5
##   date       `MKT-RF`                                          SMB   HML   RF   
##   <date>     <chr>                                             <chr> <chr> <chr>
## 1 NA         the 1-month TBill rate is from ICE BofA US 1-Mon… <NA>  <NA>  <NA> 
## 2 NA         <NA>                                              <NA>  <NA>  <NA> 
## 3 NA         Mkt-RF                                            SMB   HML   RF   
## 4 1926-06-30 2.96                                              -2.56 -2.43 0.22 
## 5 1926-07-31 2.64                                              -1.17 3.82  0.25 
## 6 1926-08-31 0.36                                              -1.4  0.13  0.23
fama.digit <- fama.digit[!is.na(fama.digit$date) & !is.infinite(as.numeric(fama.digit$date)), ]


fama.digit.xts <- xts(fama.digit[, -1], order.by = as.Date(fama.digit$date))


head(fama.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.4  0.13 0.23
## 1926-09-30  -3.24 -0.09   0.7 0.32
## 1926-10-31   2.53  -0.1 -0.51 0.31
## 1926-11-30   2.62 -0.03 -0.05 0.28
fama.digit.xts <- xts(fama.digit[,-1],order.by=as.Date(fama.digit$date))
head(fama.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.4  0.13 0.23
## 1926-09-30  -3.24 -0.09   0.7 0.32
## 1926-10-31   2.53  -0.1 -0.51 0.31
## 1926-11-30   2.62 -0.03 -0.05 0.28


4. Merge with ETF Monthly returns

merge_data <- merge(fama.digit, etf_monthly_returns)
tail(merge_data)
##              date MKT-RF   SMB   HML   RF        SPY        QQQ         EEM
## 159565 2024-06-30   1.24   6.8  5.74 0.45 0.04170751 0.06727652 0.002062213
## 159566 2024-07-31   1.61 -3.55 -1.13 0.48 0.04170751 0.06727652 0.002062213
## 159567 2024-08-31   1.74 -0.17 -2.59  0.4 0.04170751 0.06727652 0.002062213
## 159568 2024-09-30  -0.97 -1.01  0.89 0.39 0.04170751 0.06727652 0.002062213
## 159569 2024-10-31   6.51  4.63 -0.05  0.4 0.04170751 0.06727652 0.002062213
## 159570 2024-11-30  -3.17 -2.73 -2.95 0.37 0.04170751 0.06727652 0.002062213
##                 IWM        EFA        TLT        IYR        GLD
## 159565 0.0009053014 0.02820602 0.02376814 0.03371773 0.02169283
## 159566 0.0009053014 0.02820602 0.02376814 0.03371773 0.02169283
## 159567 0.0009053014 0.02820602 0.02376814 0.03371773 0.02169283
## 159568 0.0009053014 0.02820602 0.02376814 0.03371773 0.02169283
## 159569 0.0009053014 0.02820602 0.02376814 0.03371773 0.02169283
## 159570 0.0009053014 0.02820602 0.02376814 0.03371773 0.02169283
merge_data_tibble <- as_tibble(merge_data)
head(merge_data_tibble)
## # A tibble: 6 × 13
##   date       `MKT-RF` SMB   HML   RF       SPY    QQQ    EEM    IWM     EFA
##   <date>     <chr>    <chr> <chr> <chr>  <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
## # ℹ 3 more variables: TLT <dbl>, IYR <dbl>, GLD <dbl>


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.

calc_data <- merge_data_tibble[merge_data_tibble$date>="2019-03-01"&merge_data_tibble$date<="2024-02-01",]
head(calc_data)
## # A tibble: 6 × 13
##   date       `MKT-RF` SMB   HML   RF       SPY    QQQ    EEM    IWM     EFA
##   <date>     <chr>    <chr> <chr> <chr>  <dbl>  <dbl>  <dbl>  <dbl>   <dbl>
## 1 2019-03-31 3.97     -1.72 2.16  0.21  0.0312 0.0460 0.0178 0.0448 0.00267
## 2 2019-04-30 -6.94    -1.31 -2.37 0.21  0.0312 0.0460 0.0178 0.0448 0.00267
## 3 2019-05-31 6.93     0.28  -0.7  0.18  0.0312 0.0460 0.0178 0.0448 0.00267
## 4 2019-06-30 1.19     -1.93 0.47  0.19  0.0312 0.0460 0.0178 0.0448 0.00267
## 5 2019-07-31 -2.58    -2.39 -4.79 0.16  0.0312 0.0460 0.0178 0.0448 0.00267
## 6 2019-08-31 1.43     -0.97 6.77  0.18  0.0312 0.0460 0.0178 0.0448 0.00267
## # ℹ 3 more variables: TLT <dbl>, IYR <dbl>, GLD <dbl>
merge_data_tibble <- merge_data_tibble %>%
  mutate(across(c(SPY, QQQ, EEM, IWM, EFA, TLT, IYR, GLD, RF, `MKT-RF`), as.numeric))


spy_rf  <- merge_data_tibble$SPY - merge_data_tibble$RF
qqq_rf  <- merge_data_tibble$QQQ - merge_data_tibble$RF
eem_rf  <- merge_data_tibble$EEM - merge_data_tibble$RF
iwm_rf  <- merge_data_tibble$IWM - merge_data_tibble$RF
efa_rf  <- merge_data_tibble$EFA - merge_data_tibble$RF
tlt_rf  <- merge_data_tibble$TLT - merge_data_tibble$RF
iyr_rf  <- merge_data_tibble$IYR - merge_data_tibble$RF
gld_rf  <- merge_data_tibble$GLD - merge_data_tibble$RF


x <- cbind(spy_rf, qqq_rf, eem_rf, iwm_rf, efa_rf, tlt_rf, iyr_rf, gld_rf)
n <- nrow(x)


one.vec <- rep(1, n)
y <- cbind(one.vec, merge_data_tibble$`MKT-RF`)


x.mat <- as.matrix(x)
beta <- solve(t(x.mat) %*% x.mat) %*% t(x.mat) %*% y

beta
##           one.vec            
## spy_rf -2.4581205 -1.46855191
## qqq_rf  7.3647591  7.26912431
## eem_rf -0.3171686 -0.38399533
## iwm_rf -0.1165191 -0.05946168
## efa_rf -5.5125163 -5.55411091
## tlt_rf -1.3974355 -0.72564361
## iyr_rf  1.4466707  1.15965650
## gld_rf -0.8563238 -0.79763376
sapply(merge_data_tibble, class)
##        date      MKT-RF         SMB         HML          RF         SPY 
##      "Date"   "numeric" "character" "character"   "numeric"   "numeric" 
##         QQQ         EEM         IWM         EFA         TLT         IYR 
##   "numeric"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric" 
##         GLD 
##   "numeric"
summary(merge_data_tibble)
##       date                MKT-RF             SMB                HML           
##  Min.   :1926-06-30   Min.   :-29.1300   Length:159570      Length:159570     
##  1st Qu.:1951-01-31   1st Qu.: -2.0200   Class :character   Class :character  
##  Median :1975-09-15   Median :  1.0650   Mode  :character   Mode  :character  
##  Mean   :1975-09-15   Mean   :  0.6865                                        
##  3rd Qu.:2000-04-30   3rd Qu.:  3.6500                                        
##  Max.   :2024-11-30   Max.   : 38.8500                                        
##        RF               SPY                QQQ                EEM           
##  Min.   :-0.0600   Min.   :-0.12487   Min.   :-0.08661   Min.   :-0.178947  
##  1st Qu.: 0.0300   1st Qu.:-0.01019   1st Qu.:-0.01457   1st Qu.:-0.029055  
##  Median : 0.2300   Median : 0.01773   Median : 0.02074   Median : 0.004088  
##  Mean   : 0.2693   Mean   : 0.01248   Mean   : 0.01738   Mean   : 0.005588  
##  3rd Qu.: 0.4200   3rd Qu.: 0.03647   3rd Qu.: 0.04757   3rd Qu.: 0.037615  
##  Max.   : 1.3500   Max.   : 0.12698   Max.   : 0.14974   Max.   : 0.162678  
##       IWM                EFA                 TLT                 IYR           
##  Min.   :-0.21477   Min.   :-0.141067   Min.   :-0.082137   Min.   :-0.196324  
##  1st Qu.:-0.01794   1st Qu.:-0.021764   1st Qu.:-0.021123   1st Qu.:-0.012972  
##  Median : 0.01770   Median : 0.009208   Median : 0.002565   Median : 0.010133  
##  Mean   : 0.01242   Mean   : 0.006369   Mean   : 0.005973   Mean   : 0.009888  
##  3rd Qu.: 0.04659   3rd Qu.: 0.032912   3rd Qu.: 0.028185   3rd Qu.: 0.037719  
##  Max.   : 0.18244   Max.   : 0.142694   Max.   : 0.132061   Max.   : 0.131896  
##       GLD           
##  Min.   :-0.110623  
##  1st Qu.:-0.029437  
##  Median :-0.001480  
##  Mean   : 0.004297  
##  3rd Qu.: 0.032748  
##  Max.   : 0.122749
e.hat <- y - x%*%beta
res.var <- diag(t(e.hat)%*%e.hat)/(n-2)
d <- diag(res.var);d
##           [,1]    [,2]
## [1,] 0.4414904  0.0000
## [2,] 0.0000000 28.7215
cov.mat <- var(merge_data_tibble$`MKT-RF`)*t(beta)%*%beta + d
cov.mat
##          one.vec         
## one.vec 2710.212 2587.919
##         2587.919 2538.315
one.vec2 <- matrix(rep(1, 2), ncol = 1)

top <- solve(cov.mat) %*% one.vec2
bot <- t(one.vec2) %*% top

mvp_capm <- top / as.numeric(bot)
mvp_capm
##               [,1]
## one.vec -0.6824291
##          1.6824291

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(calc_data)[1]
markets <- calc_data[,c(2,3,4)]
FM <- calc_data[,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
str(FM)     
## tibble [7,965 × 8] (S3: tbl_df/tbl/data.frame)
##  $ SPY: num [1:7965] 0.0312 0.0312 0.0312 0.0312 0.0312 ...
##  $ QQQ: num [1:7965] 0.046 0.046 0.046 0.046 0.046 ...
##  $ EEM: num [1:7965] 0.0178 0.0178 0.0178 0.0178 0.0178 ...
##  $ IWM: num [1:7965] 0.0448 0.0448 0.0448 0.0448 0.0448 ...
##  $ EFA: num [1:7965] 0.00267 0.00267 0.00267 0.00267 0.00267 ...
##  $ TLT: num [1:7965] -0.00342 -0.00342 -0.00342 -0.00342 -0.00342 ...
##  $ IYR: num [1:7965] 0.0546 0.0546 0.0546 0.0546 0.0546 ...
##  $ GLD: num [1:7965] 0.0327 0.0327 0.0327 0.0327 0.0327 ...
dim(FM)         
## [1] 7965    8
str(markets)    
## tibble [7,965 × 3] (S3: tbl_df/tbl/data.frame)
##  $ MKT-RF: chr [1:7965] "3.97" "-6.94" "6.93" "1.19" ...
##  $ SMB   : chr [1:7965] "-1.72" "-1.31" "0.28" "-1.93" ...
##  $ HML   : chr [1:7965] "2.16" "-2.37" "-0.7" "0.47" ...
dim(markets)    
## [1] 7965    3
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
head(markets)
## # A tibble: 6 × 3
##   `MKT-RF` SMB   HML  
##   <chr>    <chr> <chr>
## 1 3.97     -1.72 2.16 
## 2 -6.94    -1.31 -2.37
## 3 6.93     0.28  -0.7 
## 4 1.19     -1.93 0.47 
## 5 -2.58    -2.39 -4.79
## 6 1.43     -0.97 6.77
FM <- as.matrix(FM)
n <- nrow(FM) 
p <- ncol(FM)  
one_vec <- rep(1, n)
markets <- as.matrix(markets) 
m <- ncol(markets) 
p <- cbind(one_vec, markets) 
sample.cov <- cov(FM)
sample.cor <- cor(FM)
sample.cov
##               SPY           QQQ           EEM           IWM           EFA
## SPY  0.0015923654  0.0016945299  0.0016038756  1.970835e-03  0.0015669173
## QQQ  0.0016945299  0.0021320310  0.0017133474  1.987147e-03  0.0016661944
## EEM  0.0016038756  0.0017133474  0.0028646586  2.086282e-03  0.0020376104
## IWM  0.0019708352  0.0019871471  0.0020862823  3.041479e-03  0.0019480242
## EFA  0.0015669173  0.0016661944  0.0020376104  1.948024e-03  0.0020264573
## TLT -0.0006831270 -0.0006298588 -0.0007368332 -1.044132e-03 -0.0007448935
## IYR  0.0012818696  0.0012509388  0.0014592186  1.685294e-03  0.0013375725
## GLD  0.0001024301  0.0002275234  0.0006600062  5.889005e-05  0.0001624475
##               TLT           IYR          GLD
## SPY -6.831270e-04  1.281870e-03 1.024301e-04
## QQQ -6.298588e-04  1.250939e-03 2.275234e-04
## EEM -7.368332e-04  1.459219e-03 6.600062e-04
## IWM -1.044132e-03  1.685294e-03 5.889005e-05
## EFA -7.448935e-04  1.337572e-03 1.624475e-04
## TLT  1.404729e-03 -8.521549e-05 4.421315e-04
## IYR -8.521549e-05  1.999622e-03 3.215517e-04
## GLD  4.421315e-04  3.215517e-04 2.174845e-03

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

calc_data2 <- tail(merge_data_tibble, n=1)
head(calc_data2)
## # A tibble: 1 × 13
##   date       `MKT-RF` SMB   HML      RF    SPY    QQQ     EEM      IWM    EFA
##   <date>        <dbl> <chr> <chr> <dbl>  <dbl>  <dbl>   <dbl>    <dbl>  <dbl>
## 1 2024-11-30    -3.17 -2.73 -2.95  0.37 0.0417 0.0673 0.00206 0.000905 0.0282
## # ℹ 3 more variables: TLT <dbl>, IYR <dbl>, GLD <dbl>
calc_data2 <- calc_data2[c(-1,-2,-3,-4,-5)]
calc_data2
## # 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

With Weights

returns_q5 <- rowSums(calc_data2*mvp_capm)
returns_q5
## [1] 0.1190676