# Load libraries
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
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(PerformanceAnalytics)
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
library(readxl)
library(PortfolioAnalytics)
## Loading required package: foreach
#Question1
tickers <- c("SPY", "QQQ", "EEM", "IWM", "EFA", "TLT", "IYR", "GLD")
start_date <- "2010-01-01"
end_date <- Sys.Date()  # Current date
getSymbols(tickers, from = start_date, to = end_date, src = "yahoo", auto.assign = TRUE)
## [1] "SPY" "QQQ" "EEM" "IWM" "EFA" "TLT" "IYR" "GLD"
etf_data <- data.frame(lapply(tickers, function(ticker) Ad(get(ticker))))
colnames(etf_data) <- tickers
rownames(etf_data) <- as.Date(rownames(etf_data))
head(etf_data)
##                 SPY      QQQ      EEM      IWM      EFA      TLT      IYR
## 2010-01-04 86.86008 40.73328 31.82711 52.51540 37.52378 61.13185 28.10299
## 2010-01-05 87.09002 40.73328 32.05813 52.33483 37.55685 61.52668 28.17046
## 2010-01-06 87.15131 40.48758 32.12519 52.28558 37.71560 60.70302 28.15819
## 2010-01-07 87.51920 40.51390 31.93889 52.67136 37.57009 60.80513 28.40971
## 2010-01-08 87.81042 40.84735 32.19226 52.95863 37.86774 60.77790 28.21954
## 2010-01-11 87.93305 40.68063 32.12519 52.74525 38.17862 60.44436 28.35452
##               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
#Question2
etf_xts <- xts(etf_data, order.by = as.Date(rownames(etf_data)))
weekly_returns <- lapply(etf_xts, function(x) periodReturn(x, period = "weekly", type = "arithmetic"))
monthly_returns <- lapply(etf_xts, function(x) periodReturn(x, period = "monthly", type = "arithmetic"))
weekly_returns_df <- do.call(cbind, weekly_returns)
monthly_returns_df <- do.call(cbind, monthly_returns)
head(weekly_returns_df)
##            weekly.returns weekly.returns.1 weekly.returns.2 weekly.returns.3
## 2010-01-08    0.010940959      0.002800341       0.01147288      0.008439921
## 2010-01-15   -0.008116869     -0.015037421      -0.02893486     -0.013019072
## 2010-01-22   -0.038982956     -0.036859189      -0.05578099     -0.030621777
## 2010-01-29   -0.016665065     -0.031023854      -0.03357751     -0.026243171
## 2010-02-05   -0.006797519      0.004440426      -0.02821295     -0.013974758
## 2010-02-12    0.012937910      0.018147949       0.03333309      0.029525976
##            weekly.returns.4 weekly.returns.5 weekly.returns.6 weekly.returns.7
## 2010-01-08      0.009166551    -5.789952e-03      0.004147329      0.014298722
## 2010-01-15     -0.003493573     2.004725e-02     -0.006304356     -0.004579349
## 2010-01-22     -0.055740422     1.010161e-02     -0.041785231     -0.033285246
## 2010-01-29     -0.025802801     3.368454e-03     -0.008447304     -0.011290465
## 2010-02-05     -0.019054997    -5.427578e-05      0.003223236     -0.012080019
## 2010-02-12      0.005244492    -1.946056e-02     -0.007573504      0.022544905
head(monthly_returns_df)
##            monthly.returns monthly.returns.1 monthly.returns.2
## 2010-01-29     -0.05241352       -0.07819921      -0.103722748
## 2010-02-26      0.03119445        0.04603903       0.017764152
## 2010-03-31      0.06087975        0.07710902       0.081108587
## 2010-04-30      0.01546975        0.02242512      -0.001662001
## 2010-05-28     -0.07945428       -0.07392345      -0.093935538
## 2010-06-30     -0.05174073       -0.05975673      -0.013986522
##            monthly.returns.3 monthly.returns.4 monthly.returns.5
## 2010-01-29       -0.06048749      -0.074915878       0.027836275
## 2010-02-26        0.04475096       0.002667337      -0.003424596
## 2010-03-31        0.08230727       0.063854445      -0.020573345
## 2010-04-30        0.05678450      -0.028045786       0.033218671
## 2010-05-28       -0.07536650      -0.111927960       0.051083351
## 2010-06-30       -0.07743361      -0.020619584       0.057978873
##            monthly.returns.6 monthly.returns.7
## 2010-01-29       -0.05195384      -0.034972713
## 2010-02-26        0.05457051       0.032748219
## 2010-03-31        0.09748432      -0.004386396
## 2010-04-30        0.06388108       0.058834363
## 2010-05-28       -0.05683531       0.030513147
## 2010-06-30       -0.04670055       0.023553189
#Question3
file_path <- "F_F_Research_Data.xlsx"
factor_data <- read_excel(file_path)
factor_data[, -1] <- factor_data[, -1] / 100  # Divide by 100 to convert percentage to decimal
names(factor_data) <- c("Date", "Mkt-RF", "SMB", "HML", "RF")
head(factor_data)
## # A tibble: 6 × 5
##     Date `Mkt-RF`     SMB     HML     RF
##    <dbl>    <dbl>   <dbl>   <dbl>  <dbl>
## 1 192607   0.0296 -0.0256 -0.0243 0.0022
## 2 192608   0.0264 -0.0117  0.0382 0.0025
## 3 192609   0.0036 -0.014   0.0013 0.0023
## 4 192610  -0.0324 -0.0009  0.007  0.0032
## 5 192611   0.0253 -0.001  -0.0051 0.0031
## 6 192612   0.0262 -0.0003 -0.0005 0.0028
#Question4
etf_returns_df <- data.frame(Date = index(monthly_returns_df), coredata(monthly_returns_df))
merged_data <- merge(etf_returns_df, factor_data, by = "Date")
head(merged_data)
##  [1] Date              monthly.returns   monthly.returns.1 monthly.returns.2
##  [5] monthly.returns.3 monthly.returns.4 monthly.returns.5 monthly.returns.6
##  [9] monthly.returns.7 Mkt-RF            SMB               HML              
## [13] RF               
## <0 rows> (or 0-length row.names)
#Question5
tickers <- c("SPY", "QQQ", "EEM", "IWM", "EFA", "TLT", "IYR", "GLD")
start_date <- as.Date("2019-03-01")
end_date <- as.Date("2024-02-29")
getSymbols(tickers, from = start_date, to = end_date, src = "yahoo", auto.assign = TRUE)
## [1] "SPY" "QQQ" "EEM" "IWM" "EFA" "TLT" "IYR" "GLD"
etf_data <- data.frame(lapply(tickers, function(ticker) Ad(get(ticker))))
returns <- Return.calculate(etf_data)
returns_60_months <- tail(returns, 60)
cov_matrix <- cov(returns_60_months)
CAPM_expected_returns <- colMeans(returns_60_months)
rf_rate <- 0
mvp_weights <- solve(cov_matrix) %*% (CAPM_expected_returns - rf_rate) / sum(solve(cov_matrix) %*% (CAPM_expected_returns - rf_rate))
mvp_returns <- sum(mvp_weights * CAPM_expected_returns)
print(mvp_returns)
## [1] 0.002981569
#Question6
tickers <- c("SPY", "QQQ", "EEM", "IWM", "EFA", "TLT", "IYR", "GLD")
start_date <- as.Date("2019-03-01")
end_date <- as.Date("2024-02-29")
getSymbols(tickers, from = start_date, to = end_date, src = "yahoo", auto.assign = TRUE)
## [1] "SPY" "QQQ" "EEM" "IWM" "EFA" "TLT" "IYR" "GLD"
etf_data <- data.frame(lapply(tickers, function(ticker) Ad(get(ticker))))
returns <- Return.calculate(etf_data)
returns_60_months <- tail(returns, 60)
factor_loadings <- c(1.2, 0.8, 0.5)  
factor_cov_matrix <- matrix(c(0.02, 0.005, 0.003, 
                               0.005, 0.01, 0.001, 
                               0.003, 0.001, 0.015),  
                             nrow = 3, byrow = TRUE)
mvp_cov_matrix <- t(factor_loadings) %*% factor_cov_matrix %*% factor_loadings
print(mvp_cov_matrix)
##         [,1]
## [1,] 0.05295
#Question7
mvp_weights <- c(0.1, 0.2, 0.1, 0.1, 0.1, 0.1, 0.2, 0.1)  
asset_returns_march_2024 <- c(0.02, 0.01, 0.03, 0.005, 0.015, 0.02, 0.01, 0.025) 
portfolio_return_march_2024 <- sum(mvp_weights * asset_returns_march_2024)
print(portfolio_return_march_2024)
## [1] 0.0155