etf4 <- read.csv('myetf4.csv')
str(etf4)
## 'data.frame':    751 obs. of  5 variables:
##  $ Index  : chr  "2015-12-14" "2015-12-15" "2015-12-16" "2015-12-17" ...
##  $ X0050  : num  53.3 53.3 54.1 54.8 54.5 ...
##  $ X0056  : num  18.2 18.4 18.6 18.8 18.9 ...
##  $ X006205: num  31.1 31.6 31.6 32.2 32.2 ...
##  $ X00646 : num  19.6 19.6 19.9 20.1 19.9 ...
library(xts)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
etf4.xts <- as.xts(etf4[, -1],
                   order.by = as.Date(etf4$Index, "%Y-%m-%d"))
head(etf4.xts)
##            X0050 X0056 X006205 X00646
## 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
class(etf4.xts)
## [1] "xts" "zoo"
firm_data1 = read.csv('3firmExample.csv')
str(firm_data1)
## 'data.frame':    59 obs. of  4 variables:
##  $ date     : chr  "1995/3/1" "1995/4/1" "1995/5/1" "1995/6/1" ...
##  $ Nordstrom: num  -0.03615 -0.0568 0.07821 -0.00302 -0.02757 ...
##  $ Starbucks: num  0.00521 -0.02105 0.21244 0.2036 0.04797 ...
##  $ Microsoft: num  0.1213 0.13923 0.03529 0.06501 0.00138 ...
firm_data1$date
##  [1] "1995/3/1"  "1995/4/1"  "1995/5/1"  "1995/6/1"  "1995/7/1"  "1995/8/1" 
##  [7] "1995/9/1"  "1995/10/1" "1995/11/1" "1995/12/1" "1996/1/1"  "1996/2/1" 
## [13] "1996/3/1"  "1996/4/1"  "1996/5/1"  "1996/6/1"  "1996/7/1"  "1996/8/1" 
## [19] "1996/9/1"  "1996/10/1" "1996/11/1" "1996/12/1" "1997/1/1"  "1997/2/1" 
## [25] "1997/3/1"  "1997/4/1"  "1997/5/1"  "1997/6/1"  "1997/7/1"  "1997/8/1" 
## [31] "1997/9/1"  "1997/10/1" "1997/11/1" "1997/12/1" "1998/1/1"  "1998/2/1" 
## [37] "1998/3/1"  "1998/4/1"  "1998/5/1"  "1998/6/1"  "1998/7/1"  "1998/8/1" 
## [43] "1998/9/1"  "1998/10/1" "1998/11/1" "1998/12/1" "1999/1/1"  "1999/2/1" 
## [49] "1999/3/1"  "1999/4/1"  "1999/5/1"  "1999/6/1"  "1999/7/1"  "1999/8/1" 
## [55] "1999/9/1"  "1999/10/1" "1999/11/1" "1999/12/1" "2000/1/1"
library(xts)
date1 = as.Date(firm_data1[,1], "%Y/%m/%d")

# convert firm_data1 into time series data
firm_data1.xts = as.xts(firm_data1[,-1], order.by = date1)
head(firm_data1.xts)
##            Nordstrom Starbucks Microsoft
## 1995-03-01  -0.03615   0.00521   0.12130
## 1995-04-01  -0.05680  -0.02105   0.13923
## 1995-05-01   0.07821   0.21244   0.03529
## 1995-06-01  -0.00302   0.20360   0.06501
## 1995-07-01  -0.02757   0.04797   0.00138
## 1995-08-01   0.02768   0.06787   0.02186
# minimum variance portfolio
library(fBasics)
## Loading required package: timeDate
## Loading required package: timeSeries
## 
## Attaching package: 'timeSeries'
## The following object is masked from 'package:zoo':
## 
##     time<-
Sigma <- cov(firm_data1[,2:4])
one.vec <- rep(1,3)
Sigma.inv.mat <- solve(Sigma)
top.mat <- Sigma.inv.mat%*%one.vec
bot.val <- as.numeric(t(one.vec)%*%top.mat)
mvp.w <-  top.mat / bot.val
mvp.w
##                [,1]
## Nordstrom 0.3635998
## Starbucks 0.1936537
## Microsoft 0.4427465
mu.vec <- colMeans(firm_data1[, 2:4])
mvp.ret <- as.numeric(crossprod(mvp.w, mu.vec))
mvp.ret
## [1] 0.02498369
mvp.sig2 <- as.numeric(t(mvp.w)%*%Sigma%*%mvp.w)
mvp.sig <- sqrt(mvp.sig2)
mvp.sig
## [1] 0.073302
# optimal minimum variance portfolio
r0 <- 0.06/12
top.mat <- cbind(2*Sigma, mu.vec, rep(1, 3))
mid.vec <- c(mu.vec, 0, 0)
bot.vec <- c(rep(1, 3), 0, 0)
A.mat <- rbind(top.mat, mid.vec, bot.vec)
b.vec <- c(rep(0, 3), r0, 1)
z.mat <- solve(A.mat)%*%b.vec
w.r0 <- z.mat[1:3,]
mu.r0 <- as.numeric(crossprod(w.r0, mu.vec))
sig2.r0 <- as.numeric(t(w.r0)%*%Sigma%*%w.r0)
sig.r0 <- sqrt(sig2.r0)
sig.r0
## [1] 0.09679334
return <- firm_data1[,2:4]

#Minimum Variance 
minvariance <- function(return, r0) {
  Sigma <-  cov(return)
  mu.vec <- colMeans(return)
  #one.vec <-  rep(1, 3)
  top.mat <- cbind(2*Sigma, mu.vec, rep(1, 3))
  mid.vec <- c(mu.vec, 0, 0)
  bot.vec <- c(rep(1, 3), 0, 0)
  A.mat <- rbind(top.mat, mid.vec, bot.vec)
  b.vec <- c(rep(0, 3), r0, 1)
  z.mat <- solve(A.mat)%*%b.vec
  w.r0 <- z.mat[1:3,]
  mu.r0 <- as.numeric(crossprod(w.r0, mu.vec))
  sig2.r0 <- as.numeric(t(w.r0)%*%Sigma%*%w.r0)
  sig.r0 <- sqrt(sig2.r0)
  # Output Function:
   list(weight = w.r0, rt = mu.r0, sd = sig.r0)
}
minvariance(return, 0.005)
## $weight
##   Nordstrom   Starbucks   Microsoft 
## 0.875635380 0.116816458 0.007548163 
## 
## $rt
## [1] 0.005
## 
## $sd
## [1] 0.09679334
# plot efficient frontier function
frontier <- function(return){
  Sigma <-  cov(return)
  mu.vec <- colMeans(return)
  #return <- log(tail(assets, -1) / head(assets, -1))
  n <-  ncol(return)
  top.mat <- cbind(2*Sigma, mu.vec, rep(1, 3))
  mid.vec <- c(mu.vec, 0, 0)
  bot.vec <- c(rep(1, 3), 0, 0)
  A.mat <- rbind(top.mat, mid.vec, bot.vec)
  rbase <- seq(min(mu.vec), max(mu.vec), length = 100)
  s <- sapply(rbase, function(x) {
    b.vec <- c(rep(0, n), x, 1)
    z.mat <- solve(A.mat)%*%b.vec
    w.r0 <- z.mat[1:3,]
    sqrt(w.r0%*%Sigma%*%w.r0)
  })
  plot(s, rbase, xlab = 'Std', ylab = 'Return')
}
frontier(return)

# tangency portfolio by closed form
rf = 0.01/12
mr = colMeans(firm_data1[,2:4])
mr.mtx = matrix(mr, ncol=1)
mr_rf = mr - rf
mr_rf = matrix(mr_rf, ncol=1)
mr_rf
##              [,1]
## [1,] 0.0007117514
## [2,] 0.0276273446
## [3,] 0.0418781921
a1 = inv(Sigma)%*%mr_rf
b1 = t(one.vec)%*%a1
tp = a1 / as.numeric(b1)
tp
##                 [,1]
## Nordstrom -0.2061492
## Starbucks  0.2791516
## Microsoft  0.9269976
# portfolio expected return
ret.tp  = sum(mr.mtx*tp)
ret.tp
## [1] 0.04721981
# portfolio standard deviation
std.tp = sqrt((t(tp)%*%Sigma)%*%tp)
std.tp
##           [,1]
## [1,] 0.1015897
# sharpe ratio
sharpe.tp = (ret.tp - rf)/std.tp
sharpe.tp
##          [,1]
## [1,] 0.456606
# risk Free Asset
rf = 0.01/12
Sigma.inv.mat = solve(Sigma.inv.mat)
one.vec = rep(1, 3)
mu.minus.rf = mu.vec - rf*one.vec
top.mat = Sigma.inv.mat%*%mu.minus.rf
bot.val = as.numeric(t(one.vec)%*%top.mat)
t.vec = top.mat[,1]/bot.val
t.vec
## Nordstrom Starbucks Microsoft 
## 0.1294474 0.4971193 0.3734332
# expected return & Standard Deviation
mu.t = as.numeric(crossprod(t.vec , mu.vec))
mu.t
## [1] 0.03029826
sig2.t = as.numeric(t(t.vec)%*%Sigma.inv.mat%*%t.vec)
sig.t = sqrt(sig2.t)
sig.t
## [1] 0.08678011
# 10% Standard Deviation
x.t.01 = 0.01/sig.t
x.t.01
## [1] 0.1152338
1-x.t.01
## [1] 0.8847662
#88.5% on Free Risk Asset & 11.5 for Tangency Portfolio
devtools::install_github('joshuaulrich/xts')
## Error in get(genname, envir = envir) : object 'testthat_print' not found
## Skipping install of 'xts' from a github remote, the SHA1 (38772558) has not changed since last install.
##   Use `force = TRUE` to force installation
devtools::install_github('joshuaulrich/quantmod')
## Skipping install of 'quantmod' from a github remote, the SHA1 (a2aad169) has not changed since last install.
##   Use `force = TRUE` to force installation
#

devtools::install_github('systematicinvestor/SIT.date')
## Skipping install of 'SIT.date' from a github remote, the SHA1 (6263da60) has not changed since last install.
##   Use `force = TRUE` to force installation
curl::curl_download('https://github.com/systematicinvestor/SIT/raw/master/SIT.tar.gz', 'SIT.tar.gz',mode = 'wb',quiet=T)

install.packages('SIT.tar.gz', repos = NULL, type='source')
## Installing package into '/home/rstudio/R/x86_64-pc-linux-gnu-library/4.0'
## (as 'lib' is unspecified)
#
library(SIT)
## Loading required package: SIT.date
## Loading required package: quantmod
## Loading required package: TTR
## 
## Attaching package: 'TTR'
## The following object is masked from 'package:fBasics':
## 
##     volatility
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## 
## Attaching package: 'SIT'
## The following object is masked from 'package:TTR':
## 
##     DVI
## The following objects are masked from 'package:fBasics':
## 
##     inv, vec
## The following object is masked from 'package:base':
## 
##     close
n<-dim (firm_data1.xts)[2]
constraints = new.constraints(n, lb = -Inf, ub = +Inf)
# SUM x.i = 1
constraints = add.constraints(rep(1, n), 1, type = '=', constraints)
ia <- create.historical.ia(firm_data1.xts, 12)
ia
## $hist.returns
##            Nordstrom Starbucks Microsoft
## 1995-03-01  -0.03615   0.00521   0.12130
## 1995-04-01  -0.05680  -0.02105   0.13923
## 1995-05-01   0.07821   0.21244   0.03529
## 1995-06-01  -0.00302   0.20360   0.06501
## 1995-07-01  -0.02757   0.04797   0.00138
## 1995-08-01   0.02768   0.06787   0.02186
## 1995-09-01   0.01205  -0.05458  -0.02186
## 1995-10-01  -0.11910   0.03565   0.09982
## 1995-11-01   0.06045   0.07365  -0.13783
## 1995-12-01   0.03135  -0.00593   0.00715
## 1996-01-01  -0.03135  -0.22612   0.05271
## 1996-02-01   0.14237   0.05092   0.06475
## 1996-03-01   0.07084   0.27967   0.04398
## 1996-04-01   0.04909   0.15147   0.09365
## 1996-05-01   0.00495   0.00000   0.04743
## 1996-06-01  -0.13634   0.04064   0.01151
## 1996-07-01  -0.06980  -0.08300  -0.01890
## 1996-08-01  -0.05887   0.23081   0.03848
## 1996-09-01  -0.02598   0.00760   0.07374
## 1996-10-01  -0.05233  -0.01527   0.03995
## 1996-11-01   0.19043   0.06334   0.13365
## 1996-12-01  -0.20499  -0.19030   0.05201
## 1997-01-01   0.04652   0.17941   0.21066
## 1997-02-01  -0.00673  -0.01842  -0.04512
## 1997-03-01   0.03015  -0.12665  -0.06147
## 1997-04-01   0.03566   0.00840   0.28153
## 1997-05-01   0.20388   0.05297   0.02037
## 1997-06-01   0.02189   0.21197   0.01897
## 1997-07-01   0.14446   0.05009   0.11216
## 1997-08-01   0.03387   0.00152  -0.06720
## 1997-09-01   0.08594   0.01962   0.00094
## 1997-10-01  -0.04000  -0.23669  -0.01763
## 1997-11-01  -0.03504   0.05526   0.08477
## 1997-12-01   0.02304   0.09564  -0.09055
## 1998-01-01  -0.17120  -0.04839   0.14346
## 1998-02-01   0.12211   0.07886   0.12765
## 1998-03-01   0.10689   0.13570   0.05453
## 1998-04-01   0.02515   0.06022   0.00696
## 1998-05-01   0.09840  -0.00260  -0.06076
## 1998-06-01   0.06951   0.10731   0.24516
## 1998-07-01  -0.21288  -0.24383   0.01432
## 1998-08-01  -0.03962  -0.28272  -0.13621
## 1998-09-01  -0.19028   0.13675   0.13735
## 1998-10-01   0.09852   0.18117  -0.03879
## 1998-11-01   0.31248   0.06147   0.14176
## 1998-12-01  -0.07127   0.19623   0.12820
## 1999-01-01   0.18232  -0.07514   0.23256
## 1999-02-01  -0.03167   0.01549  -0.15332
## 1999-03-01   0.01541   0.05965   0.17731
## 1999-04-01  -0.14983   0.27479  -0.09733
## 1999-05-01   0.01126  -0.00169  -0.00772
## 1999-06-01  -0.05799   0.01847   0.11131
## 1999-07-01  -0.06354  -0.47970  -0.04973
## 1999-08-01  -0.10217  -0.01626   0.07572
## 1999-09-01  -0.04747   0.08004  -0.02184
## 1999-10-01  -0.07946   0.09267   0.02184
## 1999-11-01   0.11217  -0.02326  -0.01651
## 1999-12-01  -0.05544  -0.09108   0.24866
## 2000-01-01  -0.17900   0.27732  -0.17634
## 
## $nperiod
## [1] 59
## 
## $index
## [1] 1 2 3
## 
## $n
## [1] 3
## 
## $symbols
## [1] "Nordstrom" "Starbucks" "Microsoft"
## 
## $risk
## Nordstrom Starbucks Microsoft 
## 0.3653093 0.4924912 0.3485204 
## 
## $correlation
##           Nordstrom  Starbucks  Microsoft
## Nordstrom 1.0000000 0.17582856 0.17437668
## Starbucks 0.1758286 1.00000000 0.07903199
## Microsoft 0.1743767 0.07903199 1.00000000
## 
## $cov
##            Nordstrom  Starbucks  Microsoft
## Nordstrom 0.13345086 0.03163360 0.02220124
## Starbucks 0.03163360 0.24254757 0.01356531
## Microsoft 0.02220124 0.01356531 0.12146648
## 
## $expected.return
##  Nordstrom  Starbucks  Microsoft 
## 0.01869939 0.40040064 0.65184802 
## 
## $annual.factor
## [1] 12
## 
## $arithmetic.return
##  Nordstrom  Starbucks  Microsoft 
## 0.01869939 0.40040064 0.65184802 
## 
## $geometric.return
##   Nordstrom   Starbucks   Microsoft 
## -0.04582436  0.23057832  0.56338528
weight <- min.risk.portfolio(ia, constraints)
## Loading required package: kernlab
## 
## Attaching package: 'kernlab'
## The following object is masked from 'package:SIT':
## 
##     cross
weight
## [1] 0.3635998 0.1936537 0.4427465
library(SIT)

# plot efficient frontier using SIT
ia <- create.historical.ia(firm_data1.xts, 12)
# create long-only, fully invested efficient frontier
# 0 <= x.i <= 1
# if short sale allowed: constraints = new.constraints(n, lb = -Inf, ub = +Inf)
constraints = new.constraints(n, lb = 0, ub = 1)
constraints = add.constraints(diag(n), type='>=', b=0, constraints)
constraints = add.constraints(diag(n), type='<=', b=1, constraints)
# SUM x.i = 1
constraints = add.constraints(rep(1, n), 1, type = '=', constraints)

# create efficient frontier
ef = portopt(ia, constraints, 50,'Efficient Frontier') 
## 
## Attaching package: 'corpcor'
## The following object is masked from 'package:SIT':
## 
##     cov.shrink
## Warning in if (class(val) == "try-error") return(FALSE) else return(TRUE): the
## condition has length > 1 and only the first element will be used
ef
## $weight
##         Nordstrom  Starbucks Microsoft
##  [1,] 0.363599832 0.19365370 0.4427465
##  [2,] 0.354174974 0.19474884 0.4510762
##  [3,] 0.344750115 0.19584397 0.4594059
##  [4,] 0.335325256 0.19693910 0.4677356
##  [5,] 0.325900397 0.19803423 0.4760654
##  [6,] 0.316475538 0.19912936 0.4843951
##  [7,] 0.307050679 0.20022449 0.4927248
##  [8,] 0.297625821 0.20131963 0.5010546
##  [9,] 0.288200962 0.20241476 0.5093843
## [10,] 0.278776103 0.20350989 0.5177140
## [11,] 0.269351244 0.20460502 0.5260437
## [12,] 0.259926385 0.20570015 0.5343735
## [13,] 0.250501527 0.20679529 0.5427032
## [14,] 0.241076668 0.20789042 0.5510329
## [15,] 0.231651809 0.20898555 0.5593626
## [16,] 0.222226950 0.21008068 0.5676924
## [17,] 0.212802091 0.21117581 0.5760221
## [18,] 0.203377232 0.21227094 0.5843518
## [19,] 0.193952374 0.21336608 0.5926816
## [20,] 0.184527515 0.21446121 0.6010113
## [21,] 0.175102656 0.21555634 0.6093410
## [22,] 0.165677797 0.21665147 0.6176707
## [23,] 0.156252938 0.21774660 0.6260005
## [24,] 0.146828079 0.21884173 0.6343302
## [25,] 0.137403221 0.21993687 0.6426599
## [26,] 0.127978362 0.22103200 0.6509896
## [27,] 0.118553503 0.22212713 0.6593194
## [28,] 0.109128644 0.22322226 0.6676491
## [29,] 0.099703785 0.22431739 0.6759788
## [30,] 0.090278927 0.22541252 0.6843085
## [31,] 0.080854068 0.22650766 0.6926383
## [32,] 0.071429209 0.22760279 0.7009680
## [33,] 0.062004350 0.22869792 0.7092977
## [34,] 0.052579491 0.22979305 0.7176275
## [35,] 0.043154632 0.23088818 0.7259572
## [36,] 0.033729774 0.23198331 0.7342869
## [37,] 0.024304915 0.23307845 0.7426166
## [38,] 0.014880056 0.23417358 0.7509464
## [39,] 0.005455197 0.23526871 0.7592761
## [40,] 0.000000000 0.22636817 0.7736318
## [41,] 0.000000000 0.20373135 0.7962686
## [42,] 0.000000000 0.18109453 0.8189055
## [43,] 0.000000000 0.15845772 0.8415423
## [44,] 0.000000000 0.13582090 0.8641791
## [45,] 0.000000000 0.11318408 0.8868159
## [46,] 0.000000000 0.09054727 0.9094527
## [47,] 0.000000000 0.06791045 0.9320895
## [48,] 0.000000000 0.04527363 0.9547264
## [49,] 0.000000000 0.02263682 0.9773632
## [50,] 0.000000000 0.00000000 1.0000000
## 
## $return
##            [,1]
##  [1,] 0.3729416
##  [2,] 0.3786335
##  [3,] 0.3843255
##  [4,] 0.3900175
##  [5,] 0.3957094
##  [6,] 0.4014014
##  [7,] 0.4070934
##  [8,] 0.4127853
##  [9,] 0.4184773
## [10,] 0.4241693
## [11,] 0.4298613
## [12,] 0.4355532
## [13,] 0.4412452
## [14,] 0.4469372
## [15,] 0.4526291
## [16,] 0.4583211
## [17,] 0.4640131
## [18,] 0.4697050
## [19,] 0.4753970
## [20,] 0.4810890
## [21,] 0.4867809
## [22,] 0.4924729
## [23,] 0.4981649
## [24,] 0.5038568
## [25,] 0.5095488
## [26,] 0.5152408
## [27,] 0.5209327
## [28,] 0.5266247
## [29,] 0.5323167
## [30,] 0.5380087
## [31,] 0.5437006
## [32,] 0.5493926
## [33,] 0.5550846
## [34,] 0.5607765
## [35,] 0.5664685
## [36,] 0.5721605
## [37,] 0.5778524
## [38,] 0.5835444
## [39,] 0.5892364
## [40,] 0.5949283
## [41,] 0.6006203
## [42,] 0.6063123
## [43,] 0.6120042
## [44,] 0.6176962
## [45,] 0.6233882
## [46,] 0.6290801
## [47,] 0.6347721
## [48,] 0.6404641
## [49,] 0.6461561
## [50,] 0.6518480
## 
## $risk
##  [1] 0.2539256 0.2539584 0.2540569 0.2542210 0.2544506 0.2547454 0.2551053
##  [8] 0.2555300 0.2560192 0.2565724 0.2571893 0.2578695 0.2586123 0.2594174
## [15] 0.2602840 0.2612117 0.2621997 0.2632473 0.2643540 0.2655188 0.2667411
## [22] 0.2680201 0.2693549 0.2707448 0.2721888 0.2736862 0.2752361 0.2768376
## [29] 0.2784897 0.2801917 0.2819425 0.2837413 0.2855873 0.2874794 0.2894167
## [36] 0.2913985 0.2934237 0.2954915 0.2976009 0.2997974 0.3024626 0.3056698
## [43] 0.3094021 0.3136409 0.3183658 0.3235556 0.3291882 0.3352415 0.3416929
## [50] 0.3485204
## 
## $name
## [1] "Efficient Frontier"
plot.ef(ia, list(ef), portfolio.risk, T)