1. Try to find the MVP for 4 Taiwan ETFs: “tw0050”,“tw0056”,“tw006205”,“tw00646”. Using 2015/12/14-2018/12/28 daily returns as the insample data, you have to compute optimal weights for 4 ETFs based on daily returns of the period. Given the derived optimal weights, compute realized returns of MVP.
rm(list = ls())
library(readr)
library(xts)
## Warning: package 'xts' was built under R version 4.2.2
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.2.2
## 
## 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 ###################################
etf4 <- read_csv("C:/Users/admin/Documents/myetf4(1).csv")
## Rows: 751 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Index
## dbl (4): tw0050, tw0056, tw006205, tw00646
## 
## ℹ 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.
colnames(etf4) <- c("Index","0050","0056","006205","00646")
head(etf4)
## # A tibble: 6 × 5
##   Index      `0050` `0056` `006205` `00646`
##   <chr>       <dbl>  <dbl>    <dbl>   <dbl>
## 1 2015/12/14   53.3   18.2     31.1    19.6
## 2 2015/12/15   53.3   18.4     31.6    19.6
## 3 2015/12/16   54.1   18.6     31.6    19.9
## 4 2015/12/17   54.8   18.8     32.2    20.0
## 5 2015/12/18   54.5   19.0     32.2    19.8
## 6 2015/12/21   54.4   19.0     33      19.6
etf4.xts <- as.xts(etf4[,-1], order.by = as.POSIXct(etf4$Index) )
head(etf4.xts)
##             0050  0056 006205 00646
## 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
library(magrittr)
library(PerformanceAnalytics)
## Warning: package 'PerformanceAnalytics' was built under R version 4.2.2
## 
## Attaching package: 'PerformanceAnalytics'
## 
## The following object is masked from 'package:graphics':
## 
##     legend
etf4.ret <- etf4.xts %>% Return.calculate() %>% na.omit() 
head(etf4.ret)
##                     0050         0056        006205        00646
## 2015-12-15  0.0007506099  0.007123288  0.0170637476  0.001019888
## 2015-12-16  0.0151884493  0.009793254  0.0003165559  0.013245033
## 2015-12-17  0.0116364980  0.013469828  0.0199367089  0.008044243
## 2015-12-18 -0.0049297060  0.007442850 -0.0015513497 -0.009975062
## 2015-12-21 -0.0016513761  0.003693931  0.0254816656 -0.010579345
## 2015-12-22  0.0023892667 -0.003680336  0.0030303030  0.004073320
  1. By Q1, use monthly returns to recalculate the answers to Q1.
etf4.mon.ret <- etf4.xts %>% to.monthly(indexAt = "lastof", OHLC = FALSE) %>% Return.calculate() %>% na.omit()
head(etf4.mon.ret)
##                   0050         0056       006205        00646
## 2016-01-31 -0.01981651 -0.013785790 -0.173070915 -0.038883350
## 2016-02-29  0.02864096  0.043548387 -0.027578391 -0.003630705
## 2016-03-31  0.05550500 -0.002575992  0.082750583  0.026028110
## 2016-04-30 -0.04724138 -0.037190083 -0.024757804  0.009639777
## 2016-05-31  0.02515382  0.016630901  0.004415011  0.022110553
## 2016-06-30  0.03636364  0.029551451 -0.025641026 -0.026057030
  1. Find the tangency portfolio based on Q2. Risk-free rate is assumed to be zero