You are on page 1of 10

C183 HW3

YIK LUN, KEI


704-115-065
2017/5/2

Exercise 1

source("/Users/air/Desktop/C183/hw3code.R")
set.seed(704115065)
myticker <- sample(ticker, 600)
market <- "^GSPC"
market1 <- qq(market,start = "2001-12-31",end = "2006-12-31");market1R<-market1$R
market2 <- qq(market,start = "2006-12-31",end = "2011-12-31");market2R<-market2$R
market3 <- qq(market,start = "2011-12-31",end = "2016-12-31");market3R<-market3$R

(a)

source("/Users/air/Desktop/C183/hw3code.R")
data1 <- qq(myticker,start = "2001-12-31",end = "2006-12-31");beta1 <- rep(0,600)
data2 <- qq(myticker,start = "2006-12-31",end = "2011-12-31");beta2 <- rep(0,600)
data3 <- qq(myticker,start = "2011-12-31",end = "2016-12-31");beta3 <- rep(0,600)
R1 <- data1$R
R2 <- data2$R
R3 <- data3$R

(b)

for(i in 1:600){beta1[i] <- lm(R1[,i] ~ market1R)$coefficients[2]}


for(i in 1:600){beta2[i] <- lm(R2[,i] ~ market2R)$coefficients[2]}
for(i in 1:600){beta3[i] <- lm(R3[,i] ~ market3R)$coefficients[2]}

(c)

#individual stock
period1eachstockMSE <- (1/600) * sum((beta2-beta1)^2);period1eachstockMSE

## [1] 1.005759
period2eachstockMSE <- (1/600) * sum((beta3-beta2)^2);period2eachstockMSE

## [1] 0.5460285
(mean(beta2) - mean(beta1))^2

## [1] 0.07274149

1
(1-lm(beta2~beta1)$coefficients[2])^2 * (sum((beta1 - mean(beta1))^2)/600)

## beta1
## 0.4520105
(1-summary(lm(beta2~beta1))$r.squared) * (sum((beta2 - mean(beta2))^2)/600)

## [1] 0.4810075
(mean(beta3) - mean(beta2))^2

## [1] 0.01711806
(1-lm(beta3~beta2)$coefficients[2])^2 * (sum((beta2 - mean(beta2))^2)/600)

## beta2
## 0.2224886
(1-summary(lm(beta3~beta2))$r.squared) * (sum((beta3 - mean(beta3))^2)/600)

## [1] 0.3064218
#equal weight portfolio of 5 stocks
betatable <- cbind(beta1,beta2,beta3)

beta1portfolio5 <- rep(0,120)


beta2portfolio5 <- rep(0,120)
beta3portfolio5 <- rep(0,120)

newtable1 <- betatable[order(-betatable[,1]),]


for(i in 1:120){
beta1portfolio5[i] <- sum(newtable1[(5*i-4):(5*i),1]/5)
beta2portfolio5[i] <- sum(newtable1[(5*i-4):(5*i),2]/5)
}
period1MSE5 <- (1/120) * sum((beta2portfolio5-beta1portfolio5)^2);period1MSE5

## [1] 0.6467624
(mean(beta2portfolio5) - mean(beta1portfolio5))^2

## [1] 0.07274149
(1-lm(beta2portfolio5~beta1portfolio5)$coefficients[2])^2 *
(sum((beta1portfolio5 - mean(beta1portfolio5))^2)/120)

## beta1portfolio5
## 0.4529662
(1-summary(lm(beta2portfolio5~beta1portfolio5))$r.squared) *
(sum((beta2portfolio5 - mean(beta2portfolio5))^2)/120)

## [1] 0.1210547
newtable2 <- betatable[order(-betatable[,2]),]
for(i in 1:120){
beta2portfolio5[i] <- sum(newtable2[(5*i-4):(5*i),2]/5)
beta3portfolio5[i] <- sum(newtable2[(5*i-4):(5*i),3]/5)
}
period2MSE5 <- (1/120) * sum((beta3portfolio5-beta2portfolio5)^2);period2MSE5

## [1] 0.294833

2
(mean(beta3portfolio5) - mean(beta2portfolio5))^2

## [1] 0.01711806
(1-lm(beta3portfolio5~beta2portfolio5)$coefficients[2])^2 *
(sum((beta2portfolio5 - mean(beta2portfolio5))^2)/120)

## beta2portfolio5
## 0.2214306
(1-summary(lm(beta3portfolio5~beta2portfolio5))$r.squared) *
(sum((beta3portfolio5 - mean(beta3portfolio5))^2)/120)

## [1] 0.05628427
#equal weight of portfolio of 10 stocks
beta1portfolio10 <- rep(0,60)
beta2portfolio10 <- rep(0,60)
beta3portfolio10 <- rep(0,60)

newtable1 <- betatable[order(-betatable[,1]),]


for(i in 1:60){
beta1portfolio10[i] <- sum(newtable1[(10*i-9):(10*i),1]/10)
beta2portfolio10[i] <- sum(newtable1[(10*i-9):(10*i),2]/10)
}
period1MSE10 <- (1/60) * sum((beta2portfolio10-beta1portfolio10)^2);period1MSE10

## [1] 0.5966137
(mean(beta2portfolio10) - mean(beta1portfolio10))^2

## [1] 0.07274149
(1-lm(beta2portfolio10~beta1portfolio10)$coefficients[2])^2 *
(sum((beta1portfolio10 - mean(beta1portfolio10))^2)/60)

## beta1portfolio10
## 0.4507542
(1-summary(lm(beta2portfolio10~beta1portfolio10))$r.squared) *
(sum((beta2portfolio10 - mean(beta2portfolio10))^2)/60)

## [1] 0.07311798
newtable2 <- betatable[order(-betatable[,2]),]
for(i in 1:60){
beta2portfolio10[i] <- sum(newtable2[(10*i-9):(10*i),2]/10)
beta3portfolio10[i] <- sum(newtable2[(10*i-9):(10*i),3]/10)
}
period2MSE10 <- (1/60) * sum((beta3portfolio10-beta2portfolio10)^2);period2MSE10

## [1] 0.2598748
(mean(beta3portfolio10) - mean(beta2portfolio10))^2

## [1] 0.01711806
(1-lm(beta3portfolio10~beta2portfolio10)$coefficients[2])^2 *
(sum((beta2portfolio10 - mean(beta2portfolio10))^2)/60)

3
## beta2portfolio10
## 0.2207386
(1-summary(lm(beta3portfolio10~beta2portfolio10))$r.squared) *
(sum((beta3portfolio10 - mean(beta3portfolio10))^2)/60)

## [1] 0.02201814

(d)

Forecast Errors For Portfolio of One to Ten Securities


1 5 10
Period 1
MSE 1.00576 0.6467624 0.5966137
Bias 0.07274149 0.07274149 0.07274149
Inefficiency 0.4520105 0.4529662 0.4507542
Random Errors 0.4810075 0.1210547 0.07311799
Period 2
MSE 0.5460342 0.2948351 0.259878
Bias 0.01711639 0.01711639 0.01711639
Inefficiency 0.2224945 0.2214365 0.2207446
Random Errors 0.3064234 0.05628223 0.02201707

Exercise 2

Single Index Model

source("/Users/air/Desktop/C183/hw3code.R")
ticker <- c("PEGA", "OME", "NWLI", "PLT", "UFCS", "TRC","CSFL", "UIS", "DGAS", "IBOC", "ANIK",
"SPTN","LTC", "CACI", "HRG", "^GSPC")
gr <- qq(ticker, start='2012-01-01', end='2017-04-30')
sm1SIM <-stockModel(gr, model="SIM",index=16,Rf = 0.001)
sm2SIM <-stockModel(gr, model="SIM",index=16,Rf = 0.001,shortSelling = FALSE)
op1SIM <- optimalPort(sm1SIM)
op2SIM <- optimalPort(sm2SIM)
op1SIM

## Model: single index model


## Expected return: 0.02624612
## Risk estimate: 0.04130974
##
## Portfolio allocation:
## PEGA OME NWLI PLT UFCS
## 0.072647935 0.066232360 0.066477200 -0.073942135 0.156247287
## TRC CSFL UIS DGAS IBOC
## -0.195158623 0.305948555 -0.071616936 0.142882175 0.048871676

4
## ANIK SPTN LTC CACI HRG
## 0.105276615 0.069821867 0.206230563 -0.003656691 0.103738151
op2SIM

## Model: single index model


## Expected return: 0.02024112
## Risk estimate: 0.03519294
##
## Portfolio allocation:
## PEGA OME NWLI PLT UFCS TRC
## 0.04653400 0.06077539 0.00000000 0.00000000 0.14130516 0.00000000
## CSFL UIS DGAS IBOC ANIK SPTN
## 0.26088668 0.00000000 0.10531987 0.00000000 0.07767249 0.04823521
## LTC CACI HRG
## 0.18550707 0.00000000 0.07376413
plot(op1SIM)
points(op2SIM, optPortOnly=TRUE, colOP="#888888", cex=2)
text(0.04130974+0.02,0.02624612,"Short Sale Allowed")
text(0.03519294+0.02,0.02024112,"Short Sale not Allowed")
portPossCurve(sm1SIM, add=TRUE,riskRange = 5)

Risk and Return of Stocks


0.03

Short Sale Allowed


0.02

Short Sale not Allowed


Return

0.01
0.00

0.04 0.06 0.08 0.10 0.12 0.14

Risk

Constant Correlation Model

sm1CCM <- stockModel(gr,model="CCM",drop=16)


sm2CCM <- stockModel(gr,model="CCM",drop=16,shortSelling = FALSE)
op1CCM <- optimalPort(sm1CCM)
op2CCM <- optimalPort(sm2CCM)

5
op1CCM

## Model: constant correlation model


## Expected return: 0.03183602
## Risk estimate: 0.05968929
##
## Portfolio allocation:
## PEGA OME NWLI PLT UFCS
## 0.082387686 0.005612029 0.123352382 -0.046582550 0.130911033
## TRC CSFL UIS DGAS IBOC
## -0.313163699 0.485725330 -0.182340708 0.157166407 0.068067623
## ANIK SPTN LTC CACI HRG
## 0.147246764 0.031496187 0.162888594 0.005931566 0.141301354
op2CCM

## Model: constant correlation model


## Expected return: 0.02184013
## Risk estimate: 0.0487549
##
## Portfolio allocation:
## PEGA OME NWLI PLT UFCS TRC
## 0.04545052 0.00000000 0.06112551 0.00000000 0.07778255 0.00000000
## CSFL UIS DGAS IBOC ANIK SPTN
## 0.40994136 0.00000000 0.09025357 0.01695000 0.11013475 0.00000000
## LTC CACI HRG
## 0.08725488 0.00000000 0.10110686
plot(op1CCM)
points(op2CCM,optPortOnly=T,colOP="#888888",cex=2)
text(0.05968929+0.02,0.03183602,"Short Sale Allowed")
text(0.0487549+0.02,0.02184013,"Short Sale not Allowed")
portPossCurve(sm1CCM,add=T,riskRange = 5)

6
Risk and Return of Stocks

Short Sale Allowed


0.03

Short Sale not Allowed


0.02
Return

0.01
0.00

0.06 0.08 0.10 0.12 0.14

Risk

Single Index Model without using stockProfolio

x <- rep(0, 15*6)


xx <- matrix(x, ncol=6, nrow=15)
stock <- rep(0,15)
alpha <- rep(0,15)
beta <- rep(0,15)
mse <- rep(0,15)
Rbar <- rep(0,15)
Ratio <- rep(0,15)
col1 <- rep(0,15)
col2 <- rep(0,15)
col3 <- rep(0,15)
col4 <- rep(0,15)
col5 <- rep(0,15)

rf <- 0.001

data<-as.data.frame(gr$R)

#Perform regression of each stock on the index and record alpha, beta, #sigma_e^2:
for(i in 1:15){
alpha[i] <- lm(data=data,formula=data[,i] ~ data[,16])$coefficients[1]
beta[i] <- lm(data=data,formula=data[,i] ~ data[,16])$coefficients[2]
Rbar[i] <- alpha[i]+beta[i]*mean(data[,16])
mse[i] <- sum(lm(data=data, formula=data[,i] ~ data[,16])$residuals^2)/(nrow(data)-2)
Ratio[i] <- (Rbar[i]-rf)/beta[i]
stock[i] <- i

7
}

#So far we have this table:


xx <- (cbind(stock,alpha, beta, Rbar, mse, Ratio))

#Order the table based on the excess return to beta ratio:


aaa <- xx[order(-Ratio),]

#Create the last 5 columns of the table:


col1 <- (aaa[,4]-rf)*aaa[,3]/aaa[,5]
col3 <- aaa[,3]^2/aaa[,5]
for(i in(1:15)) {
col2[i] <- sum(col1[1:i])
col4[i] <- sum(col3[1:i])
}

#Compute the Ci (col5):


for(i in (1:15)) {
col5[i] <- var(data[,16])*col2[i]/(1+var(data[,16])*col4[i])
}

#SHORT SALES ALLOWED:


#Compute the Zi:
z_short <- (aaa[,3]/aaa[,5])*(aaa[,6]-col5[5])
#Compute the xi:
x_short <- z_short/sum(z_short)
#The final table when short sales allowed:
aaaa <- cbind(aaa, col1, col2, col3, col4, col5, z_short, x_short)
aaaa

## stock alpha beta Rbar mse


## [1,] 2 0.0200543402 0.3097478 0.0231318926 0.020192035
## [2,] 5 0.0149555889 0.3181215 0.0181163397 0.006362061
## [3,] 13 0.0098735424 0.2239906 0.0120990392 0.003081622
## [4,] 7 0.0155563436 0.7318921 0.0228281822 0.003597510
## [5,] 11 0.0151381082 1.7741452 0.0327654307 0.011765266
## [6,] 9 0.0068254225 0.7340391 0.0141185934 0.003575215
## [7,] 15 0.0120940818 1.6256587 0.0282460916 0.009728021
## [8,] 12 0.0068768392 0.9612353 0.0164273563 0.007884988
## [9,] 1 0.0087877551 1.6405843 0.0250880607 0.010847636
## [10,] 10 0.0008497745 1.4834111 0.0155884589 0.004633129
## [11,] 3 0.0006694515 1.3714738 0.0142959636 0.002954209
## [12,] 14 -0.0039073500 1.9441780 0.0154093619 0.005917952
## [13,] 4 -0.0056324821 1.5144842 0.0094149339 0.002796503
## [14,] 6 -0.0146586078 1.4649783 -0.0001030658 0.004226254
## [15,] 8 -0.0202058261 1.9526300 -0.0008051378 0.015666364
## Ratio col1 col2 col3 col4
## [1,] 0.0714513237 0.3395054 0.3395054 4.751563 4.751563
## [2,] 0.0538044040 0.8558667 1.1953721 15.907000 20.658563
## [3,] 0.0495513613 0.8067441 2.0021162 16.280968 36.939531
## [4,] 0.0298243184 4.4408140 6.4429302 148.899094 185.838625
## [5,] 0.0179046397 4.7900733 11.2330035 267.532517 453.371142

8
## [6,] 0.0178717907 2.6934212 13.9264247 150.707964 604.079105
## [7,] 0.0167600314 4.5531201 18.4795448 271.665368 875.744473
## [8,] 0.0160495113 1.8807027 20.3602475 117.181307 992.925780
## [9,] 0.0146826105 3.6430515 24.0032991 248.120150 1241.045931
## [10,] 0.0098344003 4.6708566 28.6741557 474.950834 1715.996765
## [11,] 0.0096946537 6.1725712 34.8467269 636.698473 2352.695238
## [12,] 0.0074115447 4.7337936 39.5805205 638.705397 2991.400635
## [13,] 0.0055563036 4.5572216 44.1377421 820.189444 3811.590079
## [14,] -0.0007529571 -0.3823640 43.7553781 507.816444 4319.406523
## [15,] -0.0009244649 -0.2249894 43.5303887 243.372597 4562.779120
## col5 z_short x_short
## [1,] 0.0002858663 0.9907597 0.05788815
## [2,] 0.0009932098 2.3471034 0.13713665
## [3,] 0.0016413139 3.1026939 0.18128433
## [4,] 0.0047072516 4.6709246 0.27291297
## [5,] 0.0068650609 1.6647151 0.09726604
## [6,] 0.0077933398 2.2598276 0.13203730
## [7,] 0.0089766215 1.6535579 0.09661415
## [8,] 0.0093575416 1.1196488 0.06541889
## [9,] 0.0099026300 1.1823175 0.06908050
## [10,] 0.0098914513 0.9507076 0.05554798
## [11,] 0.0098560113 1.3136215 0.07675233
## [12,] 0.0094819848 0.1795320 0.01048970
## [13,] 0.0088373121 -0.7087752 -0.04141235
## [14,] 0.0079522089 -2.6406908 -0.15429039
## [15,] 0.0075762143 -0.9708737 -0.05672625
#SHORT SALES NOT ALLOWED:
#First create a matrix up to the maximum of col5:
table1 <- cbind(aaa, col1, col2, col3, col4, col5)
table2 <- table1[1:which(col5==max(col5)), ]

#Compute the Zi:


z_no_short <- (table2[,3]/table2[,5])*(table2[,6]-max(col5))

#Compute the xi:


x_no_short <- z_no_short/sum(z_no_short)

#The final table when short sales are not allowed:


aaaaa <- cbind(table2, z_no_short, x_no_short)
aaaaa

## stock alpha beta Rbar mse Ratio


## [1,] 2 0.020054340 0.3097478 0.02313189 0.020192035 0.07145132
## [2,] 5 0.014955589 0.3181215 0.01811634 0.006362061 0.05380440
## [3,] 13 0.009873542 0.2239906 0.01209904 0.003081622 0.04955136
## [4,] 7 0.015556344 0.7318921 0.02282818 0.003597510 0.02982432
## [5,] 11 0.015138108 1.7741452 0.03276543 0.011765266 0.01790464
## [6,] 9 0.006825423 0.7340391 0.01411859 0.003575215 0.01787179
## [7,] 15 0.012094082 1.6256587 0.02824609 0.009728021 0.01676003
## [8,] 12 0.006876839 0.9612353 0.01642736 0.007884988 0.01604951
## [9,] 1 0.008787755 1.6405843 0.02508806 0.010847636 0.01468261
## col1 col2 col3 col4 col5 z_no_short
## [1,] 0.3395054 0.3395054 4.751563 4.751563 0.0002858663 0.9441631
## [2,] 0.8558667 1.1953721 15.907000 20.658563 0.0009932098 2.1952161

9
## [3,] 0.8067441 2.0021162 16.280968 36.939531 0.0016413139 2.8819053
## [4,] 4.4408140 6.4429302 148.899094 185.838625 0.0047072516 4.0529492
## [5,] 4.7900733 11.2330035 267.532517 453.371142 0.0068650609 1.2066644
## [6,] 2.6934212 13.9264247 150.707964 604.079105 0.0077933398 1.6361744
## [7,] 4.5531201 18.4795448 271.665368 875.744473 0.0089766215 1.1459468
## [8,] 1.8807027 20.3602475 117.181307 992.925780 0.0093575416 0.7493479
## [9,] 3.6430515 24.0032991 248.120150 1241.045931 0.0099026300 0.7229189
## x_no_short
## [1,] 0.06077539
## [2,] 0.14130516
## [3,] 0.18550707
## [4,] 0.26088668
## [5,] 0.07767249
## [6,] 0.10531987
## [7,] 0.07376413
## [8,] 0.04823521
## [9,] 0.04653400

Exercise 3

#Vasicek's method:
q1 <- rep(0,15)
beta_adj <- rep(0,15)
for(i in 1:15){
q <- lm(data=data, formula=data[,i] ~ data[,16])
q1[i] <- vcov(q)[2,2]
beta_adj[i] <- q1[i]*mean(beta)/(var(beta)+q1[i]) +
var(beta)*beta[i]/(var(beta)+vcov(q)[2,2])
}
cbind(beta, beta_adj)

## beta beta_adj
## [1,] 1.6405843 1.4825784
## [2,] 0.3097478 0.7681785
## [3,] 1.3714738 1.3490258
## [4,] 1.5144842 1.4748744
## [5,] 0.3181215 0.5387204
## [6,] 1.4649783 1.4177191
## [7,] 0.7318921 0.8063957
## [8,] 1.9526300 1.6156642
## [9,] 0.7340391 0.8078161
## [10,] 1.4834111 1.4288997
## [11,] 1.7741452 1.5570586
## [12,] 0.9612353 1.0318080
## [13,] 0.2239906 0.3596339
## [14,] 1.9441780 1.7694200
## [15,] 1.6256587 1.4834956

10

You might also like