0% found this document useful (0 votes)
146 views4 pages

Single Index Model

The document describes a single index model for analyzing stock returns. It loads stock return data for several companies and the S&P 500 index. It then performs linear regressions to calculate each stock's beta and expected return based on the market. It constructs optimal portfolios both with and without shorting, and calculates the expected return and risk of these portfolios. Finally, it plots the stocks and portfolios on an efficient frontier graph.

Uploaded by

api-285777244
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
146 views4 pages

Single Index Model

The document describes a single index model for analyzing stock returns. It loads stock return data for several companies and the S&P 500 index. It then performs linear regressions to calculate each stock's beta and expected return based on the market. It constructs optimal portfolios both with and without shorting, and calculates the expected return and risk of these portfolios. Finally, it plots the stocks and portfolios on an efficient frontier graph.

Uploaded by

api-285777244
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Single Index Model

YIK LUN, KEI


allen29@ucla.edu
library("quantmod")
##
##
##
##
##
##
##
##
##
##
##

Loading required package: xts


Loading required package: zoo
Attaching package: 'zoo'
The following objects are masked from 'package:base':
as.Date, as.Date.numeric
Loading required package: TTR
Version 0.4-0 included new data defaults. See ?getSymbols.

symbols <-c("BABA","IBM","GOOG","MSFT","AMZN","AAPL","^GSPC")
days<-252;rf <- 0.001
stock <- list();daily.return<-list()
xx <- matrix(rep(0,6*(length(symbols)-1)),ncol=6, nrow=length(symbols)-1)
col <- matrix(rep(0,5*(length(symbols)-1)),ncol=5,nrow=length(symbols)-1)
for (i in 1:length(symbols)) {
stock[[i]] <- tail(getSymbols(symbols[i],src="yahoo",return.class="xts",auto.assign=FALSE),days)
daily.return[[i]] <- periodReturn(stock[[i]],period='monthly')
}
##
##
##
##
##
##
##
##
##

As of 0.4-0, 'getSymbols' uses env=parent.frame() and


auto.assign=TRUE by default.
This behavior will be phased out in 0.5-0 when the call will
default to use auto.assign=FALSE. getOption("getSymbols.env") and
getOptions("getSymbols.auto.assign") are now 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 more details.

daily.return<-as.data.frame(daily.return)
colnames(daily.return)<-symbols

for(i in 1:(length(daily.return)-1)){
xx[i,1] <- lm(data=daily.return,formula=daily.return[,i] ~
daily.return[,length(daily.return)])$coefficients[1]
xx[i,2] <- lm(data=daily.return,formula=daily.return[,i] ~
daily.return[,length(daily.return)])$coefficients[2]
xx[i,3] <- xx[i,1]+xx[i,2]*mean(daily.return[,length(daily.return)])
xx[i,4] <- sum(lm(data=daily.return, formula=daily.return[,i] ~ daily.return[,length(daily.return)])$res
xx[i,5] <- (xx[i,3]-rf)/xx[i,2]
1

xx[i,6] <- i
}
rownames(xx)<-symbols[1:(length(daily.return)-1)]
aaa <- xx[order(-xx[,5]),]
col[,1] <- (aaa[,3]-rf)*aaa[,2]/aaa[,4]
col[,3] <- aaa[,2]^2/aaa[,4]
for(i in 1:(length(daily.return)-1)) {
col[i,2] <- sum(col[1:i,1])
col[i,4] <- sum(col[1:i,3])
}
for(i in 1:(length(daily.return)-1)) {
col[i,5] <- var(daily.return[,length(daily.return)])*col[i,2]/
(1+var(daily.return[,length(daily.return)])*col[i,4])
}
z_short <- (aaa[,2]/aaa[,4])*(aaa[,5]-col[(length(daily.return)-1),5])
x_short <- z_short/sum(z_short)
noshort<-as.matrix(aaa[1:which(col[,5]==max(col[,5])), ])
z_no_short <- (noshort[,2]/noshort[,4])*(noshort[,5]-max(col[,5]))
x_no_short <- z_no_short/sum(z_no_short)
order<-as.numeric(aaa[,6])
expectation<-colMeans(daily.return[,order])%*%x_short
risk<-sd(as.matrix(daily.return[,order]) %*% x_short)
expectation
##
[,1]
## [1,] -0.4495219
risk
## [1] 0.5108066
order_no<-order[1:length(x_no_short)]
expectation<-colMeans(daily.return[,order_no])%*%x_no_short
risk<-sd(as.matrix(daily.return[,order_no]) %*% x_no_short)
expectation
##
[,1]
## [1,] 0.05670537
risk
## [1] 0.09146087
x_no_short
##
AMZN
GOOG
## 0.8124993 0.1875007

expectation
##
[,1]
## [1,] 0.05670537
risk
## [1] 0.09146087
plot(risk,expectation,col="red",pch=19,xlab="Risk",ylab="Return",
ylim=c(min(colMeans(daily.return),expectation),max(colMeans(daily.return),expectation)),
xlim=c(min(sqrt(diag(var(daily.return))),risk),max(sqrt(diag(var(daily.return))),risk)))
abline(h=0,col="red")
for (i in 1:length(symbols)){
points(sd(daily.return[,i]),mean(daily.return[,i]),col="blue",pch=17)
text(sd(daily.return[,i]),mean(daily.return[,i]),colnames(daily.return)[i])
}

0.02

GOOG

MSFT

0.00

AAPL
^GSPC

IBM
0.02

Return

0.04

0.06

AMZN

BABA
0.04

0.06

0.08

0.10
Risk

0.12

0.14

You might also like