rm(list=ls())
firm_data1 = read.csv('3firmExample_data3.csv')
str(firm_data1)
## 'data.frame':    59 obs. of  4 variables:
##  $ date     : Factor w/ 59 levels "1995/10/1","1995/11/1",..: 4 5 6 7 8 9 10 1 2 3 ...
##  $ 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  1995/9/1 
##  [8] 1995/10/1 1995/11/1 1995/12/1 1996/1/1  1996/2/1  1996/3/1  1996/4/1 
## [15] 1996/5/1  1996/6/1  1996/7/1  1996/8/1  1996/9/1  1996/10/1 1996/11/1
## [22] 1996/12/1 1997/1/1  1997/2/1  1997/3/1  1997/4/1  1997/5/1  1997/6/1 
## [29] 1997/7/1  1997/8/1  1997/9/1  1997/10/1 1997/11/1 1997/12/1 1998/1/1 
## [36] 1998/2/1  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  1999/3/1 
## [50] 1999/4/1  1999/5/1  1999/6/1  1999/7/1  1999/8/1  1999/9/1  1999/10/1
## [57] 1999/11/1 1999/12/1 2000/1/1 
## 59 Levels: 1995/10/1 1995/11/1 1995/12/1 1995/3/1 1995/4/1 ... 2000/1/1
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)
##    Nordstrom           Starbucks          Microsoft       
##  Min.   :-0.212880   Min.   :-0.47970   Min.   :-0.17634  
##  1st Qu.:-0.057395   1st Qu.:-0.01734   1st Qu.:-0.01826  
##  Median : 0.004950   Median : 0.04064   Median : 0.03848  
##  Mean   : 0.001545   Mean   : 0.02846   Mean   : 0.04271  
##  3rd Qu.: 0.064980   3rd Qu.: 0.09416   3rd Qu.: 0.11174  
##  Max.   : 0.312480   Max.   : 0.27967   Max.   : 0.28153
skewness(firm.data1)
##          Nordstrom  Starbucks Microsoft
## Skewness 0.2422885 -0.8882285 0.1712068
rbind(apply(firm.data1, 2, summary),
      apply(firm.data1, 2, skewness),
      apply(firm.data1, 2, kurtosis))
##            Nordstrom   Starbucks   Microsoft
## Min.    -0.212880000 -0.47970000 -0.17634000
## 1st Qu. -0.057395000 -0.01734000 -0.01826500
## Median   0.004950000  0.04064000  0.03848000
## Mean     0.001545085  0.02846068  0.04271153
## 3rd Qu.  0.064980000  0.09415500  0.11173500
## Max.     0.312480000  0.27967000  0.28153000
##          0.242288454 -0.88822851  0.17120676
##          0.351952075  1.85144933 -0.08728437
library(quantmod)
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
tickers<-c("JWN", "SBUX", "MSFT")
getSymbols(tickers, from = '2010-12-31', to = '2016-12-31', auto.assign = TRUE)
## '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.
## 
## WARNING: There have been significant changes to Yahoo Finance data.
## Please see the Warning section of '?getSymbols.yahoo' for details.
## 
## This message is shown once per session and may be disabled by setting
## options("getSymbols.yahoo.warning"=FALSE).
## [1] "JWN"  "SBUX" "MSFT"
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 _by_ '.GlobalEnv':
## 
##     MSFT
## 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]
## Nordstrom 0.3635998
## Starbucks 0.1936537
## Microsoft 0.4427465
mvp.ret<-sum((mvp.w)*colMeans(firm_data1[,2:4]))
mvp.ret
## [1] 0.02498369
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
##   Nordstrom   Starbucks   Microsoft 
## 0.875635380 0.116816458 0.007548163
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,] 0.335302

R Markdown

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

Including Plots

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.