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.
firm_data1 = read.csv('Data-hw5-csv.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 ...
## $ AAPL: num 65.1 63.1 63.2 63.3 64.2 ...
## $ PEP : num 72.8 75.8 79.1 82.5 80.8 ...
## $ SNE : num 14.9 14.6 17.4 16.4 20.1 ...
firm_data1$date
## NULL
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)
## AAPL PEP SNE
## Min. : 56.65 Min. : 72.85 Min. :14.58
## 1st Qu.: 76.30 1st Qu.: 83.53 1st Qu.:18.23
## Median :100.31 Median : 94.04 Median :21.75
## Mean : 96.53 Mean : 92.60 Mean :23.05
## 3rd Qu.:113.17 3rd Qu.:100.10 3rd Qu.:28.10
## Max. :130.28 Max. :108.92 Max. :33.41
#skewness(firm.data1)
rbind(apply(firm.data1, 2, summary),
apply(firm.data1, 2, skewness),
apply(firm.data1, 2, kurtosis))
## AAPL PEP SNE
## Min. 56.6471440 72.8499980 14.5800000
## 1st Qu. 76.3021390 83.5300007 18.2349995
## Median 100.3050005 94.0400010 21.7550000
## Mean 96.5262796 92.6010412 23.0550001
## 3rd Qu. 113.1725025 100.0999980 28.1025007
## Max. 130.2799990 108.9199980 33.4100000
## -0.2955043 -0.1509707 0.2533172
## -1.1851237 -1.0595008 -1.1745961
library(plyr)
library(quantmod)
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
tickers<-c("AAPL", "PEP", "SNE")
data.env<-new.env()
# here we use l_ply so that we don't double save the data
# getSymbols() does this already so we just want to be memory efficient
# go through every stock and try to use getSymbols()
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.
# now we only want the stocks that got stored from getSymbols()
# basically we drop all "bad" tickers
stocks <- tickers[tickers %in% ls(data.env)]
# now we just loop through and merge our good stocks
# if you prefer to use an lapply version here, that is also fine
# since now we are just collecting all the good stock xts() objects
data <- xts()
# i=1
for(i in seq_along(stocks)) {
symbol <- stocks[i]
data <- merge(data, Ad(get(symbol, envir=data.env)))
}
head(data)
## AAPL.Adjusted PEP.Adjusted SNE.Adjusted
## 2007-01-03 08:00:00 7.982585 43.76391 38.33660
## 2007-01-04 08:00:00 8.159763 44.06393 39.13174
## 2007-01-05 08:00:00 8.101658 43.92438 40.02516
## 2007-01-08 08:00:00 8.141665 44.02206 40.03409
## 2007-01-09 08:00:00 8.817995 44.20349 41.45463
## 2007-01-10 08:00:00 9.239983 44.76170 40.90070
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] 7.98 8.16 8.1 8.14 8.82 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:3] "AAPL.Adjusted" "PEP.Adjusted" "SNE.Adjusted"
## Indexed by objects of class: [POSIXct,POSIXt] TZ:
## xts Attributes:
## NULL
# convert POSIXct into date series
data<-xts(coredata(data), order.by = as.Date(index(data), tz=""))
head(data)
## AAPL.Adjusted PEP.Adjusted SNE.Adjusted
## 2007-01-03 7.982585 43.76391 38.33660
## 2007-01-04 8.159763 44.06393 39.13174
## 2007-01-05 8.101658 43.92438 40.02516
## 2007-01-08 8.141665 44.02206 40.03409
## 2007-01-09 8.817995 44.20349 41.45463
## 2007-01-10 9.239983 44.76170 40.90070
tail(data)
## AAPL.Adjusted PEP.Adjusted SNE.Adjusted
## 2019-03-27 188.4700 121.89 42.7900
## 2019-03-28 188.7200 121.84 42.4000
## 2019-03-29 189.9500 122.55 42.2400
## 2019-04-01 191.2400 122.00 42.9800
## 2019-04-02 194.0200 121.68 42.1700
## 2019-04-03 195.7365 122.11 42.6115
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]
## AAPL -0.1458493
## PEP -0.1268216
## SNE 1.2726709
mvp.ret<-sum((mvp.w)*colMeans(firm_data1[,2:4]))
mvp.ret
## [1] 3.519326
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
## AAPL PEP SNE
## -0.1394899 -0.1840723 1.3235622
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,] 15.40483