# 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
#Q1
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.86007 40.73328 31.82712 52.51540 37.52379 61.13186 28.10298
## 2010-01-05 87.08998 40.73328 32.05812 52.33482 37.55685 61.52663 28.17046
## 2010-01-06 87.15131 40.48758 32.12519 52.28556 37.71560 60.70305 28.15820
## 2010-01-07 87.51922 40.51391 31.93888 52.67135 37.57009 60.80513 28.40973
## 2010-01-08 87.81043 40.84736 32.19225 52.95864 37.86774 60.77792 28.21954
## 2010-01-11 87.93304 40.68064 32.12519 52.74524 38.17862 60.44438 28.35451
## 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
#Q2
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.010941312 0.002800622 0.01147239 0.008440358
## 2010-01-15 -0.008117302 -0.015037977 -0.02893475 -0.013019284
## 2010-01-22 -0.038982353 -0.036859010 -0.05578105 -0.030621921
## 2010-01-29 -0.016665697 -0.031023463 -0.03357731 -0.026243474
## 2010-02-05 -0.006797428 0.004440221 -0.02821321 -0.013974222
## 2010-02-12 0.012938006 0.018148251 0.03333364 0.029525815
## weekly.returns.4 weekly.returns.5 weekly.returns.6 weekly.returns.7
## 2010-01-08 0.009166039 -0.0057897023 0.004147466 0.014298722
## 2010-01-15 -0.003493472 0.0200473659 -0.006304222 -0.004579349
## 2010-01-22 -0.055740624 0.0100996968 -0.041785571 -0.033285246
## 2010-01-29 -0.025802700 0.0033701035 -0.008447165 -0.011290465
## 2010-02-05 -0.019054889 -0.0000543972 0.003224025 -0.012080019
## 2010-02-12 0.005244492 -0.0194605022 -0.007574356 0.022544905
head(monthly_returns_df)
## monthly.returns monthly.returns.1 monthly.returns.2
## 2010-01-29 -0.05241362 -0.07819893 -0.103722950
## 2010-02-26 0.03119502 0.04603881 0.017763815
## 2010-03-31 0.06087974 0.07710873 0.081108668
## 2010-04-30 0.01546957 0.02242585 -0.001661515
## 2010-05-28 -0.07945411 -0.07392438 -0.093935991
## 2010-06-30 -0.05174136 -0.05975638 -0.013986390
## monthly.returns.3 monthly.returns.4 monthly.returns.5
## 2010-01-29 -0.06048772 -0.074916356 0.027836398
## 2010-02-26 0.04475136 0.002667557 -0.003424109
## 2010-03-31 0.08230697 0.063854438 -0.020574124
## 2010-04-30 0.05678452 -0.028045680 0.033218488
## 2010-05-28 -0.07536645 -0.111928307 0.051084630
## 2010-06-30 -0.07743376 -0.020619468 0.057977102
## monthly.returns.6 monthly.returns.7
## 2010-01-29 -0.05195379 -0.034972713
## 2010-02-26 0.05457074 0.032748219
## 2010-03-31 0.09748479 -0.004386396
## 2010-04-30 0.06388087 0.058834363
## 2010-05-28 -0.05683588 0.030513147
## 2010-06-30 -0.04670057 0.023553189
#Q3
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
#Q4
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)
#Q5
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.002981573
#Q6
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
#Q7
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)