install.packages(“tseries”) install.packages(“quantmod”)

library(tseries)
## Warning: package 'tseries' was built under R version 4.0.4
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(quantmod)
## Warning: package 'quantmod' was built under R version 4.0.4
## Loading required package: xts
## Warning: package 'xts' was built under R version 4.0.4
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 4.0.4

Go on yahoo finance, search GOOG (Google), FB (facebook), and ^GSPC (S&P 500). Obtain the prices from.

GoogData <- read.csv("C:/Users/Maria Elena Morinigo/Desktop/MSDA/DA 6813 - Data Analytics Applications/Week 6/GOOG.csv", header=TRUE, sep=",") 
FBData <- read.csv("C:/Users/Maria Elena Morinigo/Desktop/MSDA/DA 6813 - Data Analytics Applications/Week 6//FB.csv", header=TRUE, sep=",") 
SP500Data <- read.csv("C:/Users/Maria Elena Morinigo/Desktop/MSDA/DA 6813 - Data Analytics Applications/Week 6//^GSPC.csv", header=TRUE, sep=",")

Remove any missing values if needed

GoogData = na.omit(GoogData)
FBData = na.omit(FBData)
SP500Data = na.omit(SP500Data)

See what the data classes are

str(GoogData)
## 'data.frame':    250 obs. of  7 variables:
##  $ Date     : chr  "2019-04-15" "2019-04-16" "2019-04-17" "2019-04-18" ...
##  $ Open     : num  1218 1225 1233 1239 1236 ...
##  $ High     : num  1224 1231 1241 1242 1249 ...
##  $ Low      : num  1209 1220 1228 1235 1228 ...
##  $ Close    : num  1221 1227 1236 1236 1249 ...
##  $ Adj.Close: num  1221 1227 1236 1236 1249 ...
##  $ Volume   : int  1187400 856300 1221900 1331800 807300 1319900 1018800 1107300 1241400 2499400 ...
str(FBData)
## 'data.frame':    250 obs. of  7 variables:
##  $ Date     : chr  "2019-04-15" "2019-04-16" "2019-04-17" "2019-04-18" ...
##  $ Open     : num  178 179 180 179 178 ...
##  $ High     : num  180 180 181 179 182 ...
##  $ Low      : num  177 178 178 177 178 ...
##  $ Close    : num  180 179 179 178 181 ...
##  $ Adj.Close: num  180 179 179 178 181 ...
##  $ Volume   : int  10834800 11215200 9973700 11655600 13389900 19954800 37289900 54148800 22075000 19641300 ...
str(SP500Data)
## 'data.frame':    250 obs. of  7 variables:
##  $ ï..Date    : chr  "9-Apr-20" "8-Apr-20" "7-Apr-20" "6-Apr-20" ...
##  $ Open       : num  2777 2685 2739 2578 2515 ...
##  $ High       : num  2819 2761 2757 2677 2538 ...
##  $ Low        : num  2762 2663 2658 2575 2460 ...
##  $ Close.     : num  2790 2750 2659 2664 2489 ...
##  $ Adj.Close..: num  2790 2750 2659 2664 2489 ...
##  $ Volume     : chr  "7880140000.00" "5856370000.00" "7040720000.00" "6391860000.00" ...

** Compute the returns and remove any missing values.**

ReturnGoog = na.omit(Delt(GoogData[,5]))
ReturnFB = na.omit(Delt(FBData[,5]))
ReturnSP500 = na.omit(Delt(SP500Data[,5]))

Merge the returns of all three index into single data

MyData = cbind(ReturnSP500,ReturnGoog,ReturnFB)

Monthly Return of Investment of Interest.

colnames(MyData) = c("SP500", "Google", "Facebook")
head(MyData)
##             SP500        Google      Facebook
## [1,] -0.014280491  4.938194e-03 -0.0043417703
## [2,] -0.032934785  7.505285e-03 -0.0005031364
## [3,]  0.001605619  2.428863e-05 -0.0027967334
## [4,] -0.065709845  1.008595e-02  0.0177249440
## [5,]  0.015369779  1.257974e-02  0.0128968087
## [6,] -0.022319839 -6.761337e-03 -0.0065295299

See how the data looks. You can see the risk is much lesser in the S&P as this represents the market, compared to the individual stock.**

boxplot(MyData,main="Expected Return", xlab="Stock Picks", ylab="Return")

Compute mean and stdev for the returns.

DataMean=apply(MyData, 2, mean)
DataSD=apply(MyData, 2, sd)

Take a look at the means and standard deviations.

cbind(DataMean,DataSD)
##              DataMean     DataSD
## SP500    0.0003663344 0.02034639
## Google   0.0002210350 0.02249140
## Facebook 0.0001928108 0.02411799

According to the CAPM formula, we will first get the beta of each stock by regressions; then calculate the expected return of each stock and the covariance matrix of the four stocks; finally we can calculate the optimal asset allocations (weights) of the portfolio consisting of the 2 stocks.

Model for Google. Model fit can be judged by R-sq. Using LM here as interpretation is key. SVM is not so useful.

lm.Goog<- lm(Google ~ SP500, data = as.data.frame(MyData))
summary(lm.Goog)
## 
## Call:
## lm(formula = Google ~ SP500, data = as.data.frame(MyData))
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.109580 -0.007232  0.000686  0.008811  0.104633 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)
## (Intercept)  0.0002458  0.0014258   0.172    0.863
## SP500       -0.0676916  0.0702045  -0.964    0.336
## 
## Residual standard error: 0.02249 on 247 degrees of freedom
## Multiple R-squared:  0.00375,    Adjusted R-squared:  -0.0002836 
## F-statistic: 0.9297 on 1 and 247 DF,  p-value: 0.3359
BetaGoog <- summary(lm.Goog)$coefficients[2, 1]

Model for Facebook. Model fit can be judged by R-sq.

lm.FB<- lm(Facebook ~ SP500, data = as.data.frame(MyData))
summary(lm.FB)
## 
## Call:
## lm(formula = Facebook ~ SP500, data = as.data.frame(MyData))
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.140002 -0.009256  0.001145  0.011252  0.101228 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)
## (Intercept)  0.0002337  0.0015249   0.153    0.878
## SP500       -0.1116679  0.0750878  -1.487    0.138
## 
## Residual standard error: 0.02406 on 247 degrees of freedom
## Multiple R-squared:  0.008875,   Adjusted R-squared:  0.004862 
## F-statistic: 2.212 on 1 and 247 DF,  p-value: 0.1382
BetaFB <- summary(lm.FB)$coefficients[2, 1]

**Display betas*

paste("Beta of Google: ", BetaGoog)
## [1] "Beta of Google:  -0.0676915577875957"
paste("Beta of Facebook: ", BetaFB)
## [1] "Beta of Facebook:  -0.111667863075807"

If a stock has a beta of 1.00, it indicates that its price is correlated with the market. A stock like that has systemic risk, but the beta calculation cannot detect any unsystematic risk. Adding a stock to a portfolio with a beta of 1.00 does not add any risk to the portfolio, but it also does not increase the likelihood that the portfolio will provide excess return.

A beta of less than 1.00 means that the security is theoretically less volatile than the market which means the portfolio is less risky with the stock included than without it.

A beta that is greater than 1.00 indicates that the security’s price is theoretically more volatile than the market. For example, if a stock’s beta is 1.20, it is assumed to be 20% more volatile than the market. Technology stocks and small caps tend to have higher betas than the market benchmark. This indicates that adding the stock to a portfolio will increase the portfolio’s risk, but also increase its expected return.

For computation of return on investment, you can use US Treasury bill rates as the risk free rate, and the S&P as the market rate.