This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.
#setwd(portfolio_frontier_master)
firm_data1 = read.csv('ABB.csv')
str(firm_data1)
## 'data.frame': 48 obs. of 4 variables:
## $ Date : Factor w/ 48 levels "1/1/2013","1/1/2014",..: 1 17 21 25 29 33 37 41 45 5 ...
## $ ABB : num 21.4 22.7 22.8 22.6 21.8 ...
## $ ROK : num 89.2 90.3 86.3 84.8 88 ...
## $ MIELY: num 16.4 16.4 16.4 19 19.2 ...
firm_data1$Date
## [1] 1/1/2013 2/1/2013 3/1/2013 4/1/2013 5/1/2013 6/1/2013 7/1/2013
## [8] 8/1/2013 9/1/2013 10/1/2013 11/1/2013 12/1/2013 1/1/2014 2/1/2014
## [15] 3/1/2014 4/1/2014 5/1/2014 6/1/2014 7/1/2014 8/1/2014 9/1/2014
## [22] 10/1/2014 11/1/2014 12/1/2014 1/1/2015 2/1/2015 3/1/2015 4/1/2015
## [29] 5/1/2015 6/1/2015 7/1/2015 8/1/2015 9/1/2015 10/1/2015 11/1/2015
## [36] 12/1/2015 1/1/2016 2/1/2016 3/1/2016 4/1/2016 5/1/2016 6/1/2016
## [43] 7/1/2016 8/1/2016 9/1/2016 10/1/2016 11/1/2016 12/1/2016
## 48 Levels: 1/1/2013 1/1/2014 1/1/2015 1/1/2016 10/1/2013 ... 9/1/2016
library(xts)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(PerformanceAnalytics)
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
date1 = as.Date(firm_data1[,1], "%Y/%m/%d")
#convert firm_data1 into time series data: xts
firm_data1.xts = as.xts(firm_data1[,-1], order.by = date1)
firm.data1<-coredata(firm_data1.xts)
summary(firm.data1)
## ABB ROK MIELY
## Min. :17.30 Min. : 83.14 Min. :16.39
## 1st Qu.:20.59 1st Qu.:105.85 1st Qu.:20.82
## Median :21.66 Median :113.67 Median :23.16
## Mean :21.68 Mean :110.91 Mean :22.69
## 3rd Qu.:22.76 3rd Qu.:118.27 3rd Qu.:25.02
## Max. :26.56 Max. :134.40 Max. :28.04
#skewness(firm.data1)
rbind(apply(firm.data1, 2, summary),
apply(firm.data1, 2, skewness),
apply(firm.data1, 2, kurtosis))
## ABB ROK MIELY
## Min. 17.2999990 83.13999900 16.3899990
## 1st Qu. 20.5950000 105.85250050 20.8200000
## Median 21.6650000 113.66500100 23.1550010
## Mean 21.6814582 110.91166681 22.6877084
## 3rd Qu. 22.7625000 118.27000250 25.0249990
## Max. 26.5599990 134.39999400 28.0400010
## 0.1042846 -0.62952642 -0.2671051
## -0.2109054 -0.07163436 -0.6558120
library(plyr)
library(quantmod)
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
tickers<-c("ABB", "ROK", "MIELY")
data.env<-new.env()
l_ply(tickers, function(sym) try(getSymbols(sym, env=data.env), silent=T))
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
stocks <- tickers[tickers %in% ls(data.env)]
data <- xts()
# i=1
for(i in seq_along(stocks)) {
symbol <- stocks[i]
data <- merge(data, Ad(get(symbol, envir=data.env)))
}
head(data)
## ABB.Adjusted ROK.Adjusted MIELY.Adjusted
## 2007-01-03 08:00:00 12.54934 45.85376 13.56393
## 2007-01-04 08:00:00 12.14793 45.26675 14.04969
## 2007-01-05 08:00:00 11.92258 45.00336 14.04969
## 2007-01-08 08:00:00 12.11272 45.14634 14.04969
## 2007-01-09 08:00:00 12.07047 45.10871 13.56393
## 2007-01-10 08:00:00 12.08455 45.15388 13.37710
str(data)
## An 'xts' object on 2007-01-03 08:00:00/2019-04-03 08:00:00 containing:
## Data: num [1:3084, 1:3] 12.5 12.1 11.9 12.1 12.1 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:3] "ABB.Adjusted" "ROK.Adjusted" "MIELY.Adjusted"
## Indexed by objects of class: [POSIXct,POSIXt] TZ:
## xts Attributes:
## NULL
data<-xts(coredata(data), order.by = as.Date(index(data), tz=""))
head(data)
## ABB.Adjusted ROK.Adjusted MIELY.Adjusted
## 2007-01-03 12.54934 45.85376 13.56393
## 2007-01-04 12.14793 45.26675 14.04969
## 2007-01-05 11.92258 45.00336 14.04969
## 2007-01-08 12.11272 45.14634 14.04969
## 2007-01-09 12.07047 45.10871 13.56393
## 2007-01-10 12.08455 45.15388 13.37710
tail(data)
## ABB.Adjusted ROK.Adjusted MIELY.Adjusted
## 2019-03-27 18.63 171.04 25.970
## 2019-03-28 18.67 173.84 25.870
## 2019-03-29 18.87 175.46 25.730
## 2019-04-01 19.14 180.91 26.710
## 2019-04-02 19.26 182.07 27.270
## 2019-04-03 19.63 183.48 27.595
library(fBasics)
## Loading required package: timeDate
##
## Attaching package: 'timeDate'
## The following objects are masked from 'package:PerformanceAnalytics':
##
## kurtosis, skewness
## Loading required package: timeSeries
##
## Attaching package: 'timeSeries'
## The following object is masked from 'package:zoo':
##
## time<-
##
## Attaching package: 'fBasics'
## The following object is masked from 'package:TTR':
##
## volatility
Sigma = cov(firm_data1[,2:4])
std = sqrt(diag(Sigma))
ones = rep(1,3)
one.vec = matrix(ones, ncol=1)
a = inv(Sigma)%*%one.vec
b = t(one.vec)%*%a
mvp.w =a / as.numeric(b)
mvp.w
## [,1]
## ABB 0.4795286
## ROK -0.1397710
## MIELY 0.6602423
mvp.ret<-sum((mvp.w)*colMeans(firm_data1[,2:4]))
mvp.ret
## [1] 9.874033
mu<-0.06/12
return <- firm_data1[,2:4]
Ax <- rbind(2*cov(return), colMeans(return), rep(1, ncol(return)))
Ax <- cbind(Ax, rbind(t(tail(Ax, 2)), matrix(0, 2, 2)))
b0 <- c(rep(0, ncol(return)), mu, 1)
out<-solve(Ax, b0)
wgt<-out[1:3]
wgt
## ABB ROK MIELY
## 0.3082468 -0.2535880 0.9453412
sum(wgt)
## [1] 1
ret.out<-sum(wgt*colMeans(return))
ret.out.annual<-ret.out*12
ret.out.annual
## [1] 0.06
std.out<-sqrt(t(wgt)%*%cov(return)%*%wgt)
std.out.annual<-std.out*sqrt(12)
std.out.annual
## [,1]
## [1,] 6.341398
return = firm_data1[,2:4]
#specified portfolio return: mu
mu=0.06/12
minvariance <- function(return, mu) {
#return <- log(tail(assets, -1) / head(assets, -1))
Ax <- rbind(2*cov(return), colMeans(return), rep(1, ncol(return)))
Ax <- cbind(Ax, rbind(t(tail(Ax, 2)), matrix(0, 2, 2)))
b0 <- c(rep(0, ncol(return)), mu, 1)
zx<-solve(Ax, b0)
weight<-zx[1:ncol(return)]
ret.out<-sum(weight*colMeans(return))
std.out<-sqrt(t(wgt)%*%cov(return)%*%wgt)
list(weight=weight, rtn=ret.out, sd=std.out)
}
minvariance(return, mu)
## $weight
## ABB ROK MIELY
## 0.3082468 -0.2535880 0.9453412
##
## $rtn
## [1] 0.005
##
## $sd
## [,1]
## [1,] 1.830604