#===========================================================
# Importing Data
#===========================================================
library(xts)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(readr)
etf4 <- read_csv("D:/(1) ADRIN/myetf4.csv")
## 
## -- Column specification --------------------------------------------------------
## cols(
##   Index = col_date(format = ""),
##   `0050` = col_double(),
##   `0056` = col_double(),
##   `006205` = col_double(),
##   `00646` = col_double()
## )
str(etf4)
## spec_tbl_df [751 x 5] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ Index : Date[1:751], format: "2015-12-14" "2015-12-15" ...
##  $ 0050  : num [1:751] 53.3 53.3 54.1 54.8 54.5 ...
##  $ 0056  : num [1:751] 18.2 18.4 18.6 18.8 18.9 ...
##  $ 006205: num [1:751] 31.1 31.6 31.6 32.2 32.2 ...
##  $ 00646 : num [1:751] 19.6 19.6 19.9 20.1 19.9 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   Index = col_date(format = ""),
##   ..   `0050` = col_double(),
##   ..   `0056` = col_double(),
##   ..   `006205` = col_double(),
##   ..   `00646` = col_double()
##   .. )
head(etf4)
## # A tibble: 6 x 5
##   Index      `0050` `0056` `006205` `00646`
##   <date>      <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
#
library(magrittr)

# Using Pipe %>%
library(PerformanceAnalytics)
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
# Converting Data into xts
etf4.xts <- xts(etf4[, -1], order.by = 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
#
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
#===========================================================
# Computing Average Returns & Covariance Matrix
#===========================================================
colMeans(etf4.ret, na.rm = FALSE, dims = 1)
##          0050          0056        006205         00646 
##  0.0004632227  0.0003846366 -0.0002118311  0.0002554122
cov(etf4.ret)
##                0050         0056       006205        00646
## 0050   7.837060e-05 4.559164e-05 4.467258e-05 3.663388e-05
## 0056   4.559164e-05 4.526413e-05 2.673674e-05 2.353543e-05
## 006205 4.467258e-05 2.673674e-05 1.304184e-04 2.910367e-05
## 00646  3.663388e-05 2.353543e-05 2.910367e-05 5.902892e-05
avg.return <- colMeans(etf4.ret, na.rm = FALSE, dims = 1)
sigma.mat <- cov(etf4.ret)

#===========================================================
# Question 1 Daily Returns
# etf4 Portfolio Weights Daily Returns
#===========================================================
top.mat = cbind(2*sigma.mat, rep(1, 4))
bot.vec = c(rep(1, 4), 0)
matrix.a = rbind(top.mat, bot.vec)
vector.b = c(rep(0,4), 1)
matrix.z = solve(matrix.a)%*%vector.b
etf4.portf = matrix.z[1:4,1]
etf4.portf
##       0050       0056     006205      00646 
## -0.2193579  0.7283718  0.1076234  0.3833627
#===========================================================
# etf4 Portfolio Return and Standard Deviation
# MVP for 4 Taiwan ETFs Daily Returns
#===========================================================
etf4min = as.numeric(crossprod(etf4.portf, avg.return))
etf4min
## [1] 0.0002536645
sig2.etf4min = as.numeric(t(etf4.portf)%*%sigma.mat%*%etf4.portf)
stadev.etf4min = sqrt(sig2.etf4min)
stadev.etf4min
## [1] 0.005904941
# Converting xts Data into Monthly Frequency
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
#===========================================================
# Computing Average Returns & Covariance Matrix
#===========================================================
colMeans(etf4.mon.ret, na.rm = FALSE, dims = 1)
##         0050         0056       006205        00646 
##  0.008819836  0.007086721 -0.005355481  0.004510630
cov(etf4.mon.ret)
##                0050         0056       006205        00646
## 0050   0.0011751458 0.0008661004 0.0008472189 0.0003928466
## 0056   0.0008661004 0.0009080806 0.0005553289 0.0003572509
## 006205 0.0008472189 0.0005553289 0.0024412877 0.0006736296
## 00646  0.0003928466 0.0003572509 0.0006736296 0.0008605161
avg.return2 <- colMeans(etf4.mon.ret, na.rm = FALSE, dims = 1)
sigma.mat2 <- cov(etf4.mon.ret)

#===========================================================
# Question 2 Monthly Returns
# etf4 Portfolio Weights Monthly Returns
#===========================================================
top.mat2 = cbind(2*sigma.mat2, rep(1, 4))
bot.vec2 = c(rep(1, 4), 0)
matrix.c = rbind(top.mat2, bot.vec2)
vector.d = c(rep(0,4), 1)
matrix.w = solve(matrix.c)%*%vector.d
etf4.mon.portf = matrix.w[1:4,1]
etf4.mon.portf
##        0050        0056      006205       00646 
## 0.003183861 0.474049044 0.001203750 0.521563345
#===========================================================
# etf4 Portfolio Return and Standard Deviation
# MVP for 4 Taiwan ETFs Monthly Returns
#===========================================================
etf4min2 = as.numeric(crossprod(etf4.mon.portf, avg.return2))
etf4min2
## [1] 0.005733667
sig2.etf4min2 = as.numeric(t(etf4.mon.portf)%*%sigma.mat2%*%etf4.mon.portf)
stadev.etf4min2 = sqrt(sig2.etf4min2)
stadev.etf4min2
## [1] 0.02490441
#===========================================================
# Efficient Frontier
#===========================================================
return <- etf4.mon.ret[,2:4]

frontier <- function(return){
  #return <- log(tail(assets, -1) / head(assets, -1))
  n <-  ncol(return)
  top.mat2 = cbind(2*sigma.mat2, rep(1, 4))
  bot.vec2 = c(rep(1, 4), 0)
  matrix.c = rbind(top.mat2, bot.vec2)
  rbase <- seq(min(avg.return2), max(avg.return2), length = 100)
  s <- sapply(rbase, function(x) {
    b.vec.mon <- c(rep(0, n), x, 1)
    z.mat.mon <- solve(matrix.c)%*%b.vec.mon
    w.etf4 <- z.mat.mon[1:4,]
    sqrt(w.etf4%*%sigma.mat2%*%w.etf4)
  })
  plot(s, rbase, xlab = 'Std', ylab = 'Return')
}
frontier(return)

#===========================================================
# Tangency Portfolio Based on Q2 Monthly Returns
#===========================================================
rf = 0.000
sigma.inv.mat = solve(sigma.mat2)
one.vec = rep(1, 4)
mu.minus.rf = avg.return2 - rf*one.vec
top.mat.t = sigma.inv.mat%*%mu.minus.rf
bot.val.t = as.numeric(t(one.vec)%*%top.mat.t)
t.vec = top.mat.t[,1]/bot.val.t
t.vec
##       0050       0056     006205      00646 
##  1.3050535 -0.1576807 -0.8475320  0.7001591