library(readr)
library(xts)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(dplyr)
## 
## ######################### Warning from 'xts' package ##########################
## #                                                                             #
## # The dplyr lag() function breaks how base R's lag() function is supposed to  #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or       #
## # source() into this session won't work correctly.                            #
## #                                                                             #
## # 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.                                #
## #                                                                             #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #
## #                                                                             #
## ###############################################################################
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:xts':
## 
##     first, last
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(quantmod)
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
myetf4 <- read_csv("myetf4.csv",col_types = cols(Index = col_date(format = "%Y/%m/%d")))
                   
myetf4_xts <- xts(myetf4[, -1], order.by = myetf4$Index)
head(myetf4_xts)
##            tw0050 tw0056 tw006205 tw00646
## 2015-12-14  53.29  18.25    31.06   19.61
## 2015-12-15  53.33  18.38    31.59   19.63
## 2015-12-16  54.14  18.56    31.60   19.89
## 2015-12-17  54.77  18.81    32.23   20.05
## 2015-12-18  54.50  18.95    32.18   19.85
## 2015-12-21  54.41  19.02    33.00   19.64
#Q1
dailyreturns <- diff(myetf4_xts) / lag(myetf4_xts)
dailyreturns <- dailyreturns[-1, ]
daily_cova <- cov(dailyreturns)

top.mat = cbind(2*daily_cova, rep(1, 4))
bot.vec = c(rep(1, 4), 0)
Am.mat = rbind(top.mat, bot.vec)
b.vec = c(rep(0, 4), 1)
z.m.mat = solve(Am.mat)%*%b.vec
m.vec_daily = z.m.mat[1:4,1]
m.vec_daily
##     tw0050     tw0056   tw006205    tw00646 
## -0.2193579  0.7283718  0.1076234  0.3833627
expected_return_daily <- m.vec_daily %*% colMeans(dailyreturns)
print(expected_return_daily)
##              [,1]
## [1,] 0.0002536645
portfolio_variance_daily <- t(m.vec_daily) %*% daily_cova %*% m.vec_daily
portfolio_standard_deviation_daily <- sqrt(portfolio_variance_daily)
print(portfolio_standard_deviation_daily)
##             [,1]
## [1,] 0.005904941
#Q2
dailyreturns <- diff(myetf4_xts) / lag(myetf4_xts)
dailyreturns <- dailyreturns[-1, ]

asset1 <-monthlyReturn(myetf4_xts$tw0050, subset = NULL, type = 'arithmetic')
asset2 <-monthlyReturn(myetf4_xts$tw0056, subset = NULL, type = 'arithmetic')
asset3 <-monthlyReturn(myetf4_xts$tw006205, subset = NULL, type = 'arithmetic')
asset4 <-monthlyReturn(myetf4_xts$tw00646, subset = NULL, type = 'arithmetic')

xts_list <- list(asset1, asset2, asset3, asset4)
monthlyreturns <- do.call(merge, xts_list)

colnames(monthlyreturns) <- c('tw0050','tw0056','tw006205', 'tw00646')
monthly_cova <- cov(monthlyreturns)

top.mat = cbind(2*monthly_cova, rep(1, 4))
bot.vec = c(rep(1, 4), 0)
Am.mat = rbind(top.mat, bot.vec)
b.vec = c(rep(0, 4), 1)
z.m.mat = solve(Am.mat)%*%b.vec
m.vec_monthly = z.m.mat[1:4,1]
m.vec_monthly
##       tw0050       tw0056     tw006205      tw00646 
##  0.031577137  0.446628797 -0.008108622  0.529902688
expected_return_monthly <- m.vec_monthly %*% colMeans(monthlyreturns)
print(expected_return_monthly)
##             [,1]
## [1,] 0.006463207
portfolio_variance_monthly <- t(m.vec_daily) %*% daily_cova %*% m.vec_daily
portfolio_standard_deviation_monthly <- sqrt(portfolio_variance_monthly)
print(portfolio_standard_deviation_monthly)
##             [,1]
## [1,] 0.005904941