Escolar Documentos
Profissional Documentos
Cultura Documentos
Assignments
Walkthroughs and R
Demo
W4290 Statistical Methods in Finance | Spring 2010 | Columbia University
+ 2
Outline
Assignment 1
Assignment 2
Assignment 1
+ 4
Assignment 1
Problem 3.5
returns<-read.table("m_logret_10stocks.txt",
header=TRUE)[1:156,2:11]
+ 5
Assignment 1
Problem 3.5
Assignment 1
Problem 3.5
#R code:
%MATLAB code:
no.of.stocks<-dim(returns)[2]
size (M, i)
# For an m x n matrix M, dim(M) gives % i: ith dim
# >> [1] m n
# Using “dim” can help you extract the size of the
# matrix.
Assignment 1| Functions
One of the great strengths of R is the user's ability to add
functions. In fact, many of the functions in R are actually
functions of functions. The structure of a function is
given below.
myfunction <- function(arg1, arg2, ... ){
statements
return(object)
}
Assignment 1| Markowitz
#R code:
no.of.stocks<-dim(returns)[2]
Markowitz<-function(return.set, tar.re){ %MATLAB code:
% Compare “*”
mu<-mean(return.set)
Sigma<-var(return.set) and “.*”
inv.Sigma<-solve(Sigma) # solve % inv(Sigma)
A<-sum(mu%*%inv.Sigma) # %*%
B<-mu%*%inv.Sigma%*%mu
C<-sum(inv.Sigma)
D<-B*C - A*A
vol.eff<-sqrt((C*tar.re^2 - 2*tar.re*A + B)/D)
weights<-matrix(NA, length(tar.re), no.of.stocks)
for (i in 1:length(tar.re))
weights[i,]<-(as.numeric(B/D)*inv.Sigma%*%rep(1,10)
-as.numeric(A/D)*inv.Sigma%*%mu
+tar.re[i]*(as.numeric(C/D)*inv.Sigma%*%mu
-as.numeric(A/D)*inv.Sigma%*%rep(1,10)))
list(vol.eff=vol.eff,weights=weights) #list
}
+ 9
Assignment 1| Markowitz
#R code:
target.return<-log(1+0.003)
eff.frontier<-Markowitz(returns, target.return)
eff.frontier$weights
mean.sigma<-sqrt(
diag(eff.frontier$weights%*%var(returns)
%*%t(eff.frontier$weights))
)
# ------------------------------------------------------------
# >eff.frontier$weights
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 0.07135497 -0.02974003 0.6499748 -0.02362635 -0.1913992 0.07879041
# [,7] [,8] [,9] [,10]
# [1,] 0.1617200 0.0964938 0.1630675 0.02336413
Assignment 1| Bootstrap
Problem 3.5
Bootstrap, Algorithm:
1. Set up an index vector: index={1,2,…,no.of.obs (156)}
2. From the index set generated, we randomly draw m
samples (with replacement)
3. Suppose {2, 5, 1, 156, 2, 4, … 20} is drawn, we copy
different days of returns of ALL 10 stocks from the
original data set according to the sequence of indices
drawn. In this case, Day 2, 5, 1… of the original data
set will become the first, second, third, … row of our
new, resampled dataset.
+ 11
Assignment 1| Bootstrap
To implement it in R…
#R code:
n.sims <- 500
index <- seq(1,dim(returns)[1])
draws<-matrix(sample(index,size=n.sims*length(index)
,replace=TRUE),n.sims)
weights.Michaud<-matrix(NA, n.sims, no.of.stocks)
for (b in 1:n.sims){
portfolio<-returns[draws[b,],]
weights.Michaud[b,]<-t(Markowitz(portfolio,
target.return)$weights)}
apply(weights.Michaud,2,mean)
}
Assignment 1| Plots
Problem 3.5
%MATLAB code:
mus=linspace(-0.01,0.015,100)
%...
plot(mus,vol)
hold on
+ 13
Assignment 1| Plots
Problem 3.5
Assignment 1| Plots
Assignment 2
+ 16
Assignment 2
Problem 3.6
(a) Fit CAPM to the ten stocks. Give point estimates and
95% confidence intervals of α,β, the Sharpe index, and
the Treynor index.
Assignment 2
The parameters in CAPM can be obtained by running
linear regressions lm(y~x) of the excess equity returns
on the excess market returns for each of the ten stocks
in the data set.
#R code:
for(i in 1:n.of.stocks){
llm <- summary(lm(stockexcess[,i] ~ marketexcess))
alpha.coef[i] <-
# try to use names(llm) and use llm$<…> to extract useful info.
alpha.sd[i] <- #...
beta.coef[i] <- #...
beta.sd[i] <- #...
rres[i,] <- #...
sharpe.coef[i] <- mean(stockexcess[,i])/sd(stockexcess[,i])
treynor.coef[i] <- mean(stockexcess[,i])/beta.coef[i]
#...
#names(llm)
+ 18
… =_=’’
+ 19
If we take variance on both sides and treat φ(n) (θ0 ) ≈ φ(n) (θ̂)
then,
Assignment 2
The variance of the Treynor ratio can be computed in a
similar fashion. Notice that for this ratio, the covariance
term also vanishes1.
#R code:
# This part will be very similar to part (c) in Problem 3.5. Make
# sure that you have set up matrices to store the estimated
# values. It is a combination of the bootstrap technique that you
# just learnt and the code for part (a) of Problem 3.6
1
The covariance between the mean of excess stock return and the beta estimate should also be 0.
+ 22
Assignment 2
Problem 3.6
(d) Use the regression model (3.24) to test for the ten
stocks the null hypothesis α= 0.
−1
Pn
V̂ = n i=1 (y i − α̂ − β̂xi )(y i − α̂ − β̂xi )0
and
n o−1
n−q−1 0 −1 Pn x̄
2
α̂ V̂ α̂ 1+ 2 ∼ Fq,n−q−1
q n1 t=1 (xt −x̄)
under H0.
+ 23
Assignment 2
The corresponding R code is shown as follows. Notice
that we can extract the residual from the regression
result –rres.
#R code:
alpha.level <- 0.05
#what is rres?
Vhat <- 1/n.of.obs * rres %*% t(rres) # Formula 3.26
nnumerator <- t(alpha.coef) %*% solve(Vhat) %*% alpha.coef
ddenominator <- 1 + mean(marketexcess)^2
/(var(marketexcess)*(n.of.obs-1)/n.of.obs)
dof <- n.of.obs-n.of.stocks-1
sstat <- dof/n.of.stocks * nnumerator / ddenominator
# Formula 3.28
fstat <- qf(1-alpha.level, n.of.stocks, dof)
print(sstat)
print(fstat)
+ 24
Assignment 2
Problem 3.6
#R code:
ana2<-factanal(stockexcess, factors=2, rotation="none")
rot2<-varimax(loadings(ana2), normalize = TRUE)
Assignment 2 | Testing
Problem 3.6
rte = β1 1{t<t0 } rM
e e
+ β2 1{t≥t0 } rM + ²t ,
in which rte = rt − rf and rM e
= rM − rf are the excess
returns of the stock and the S&P500 index. The model suggests
that the βin the CAPM might not be a constant (i.e. β1 ≠ β2).
Taking February 2001 as the month t0, test for each stock the
number hypothesis that β1 = β2.
Hint:
Transform the model into
#R code:
changepoint <- function(i, t0)
{
x2 <- c(rep(0,t0-1), rep(1, n.of.obs-t0+1))
#set up the indicator function
R printout:
cor(d) Æ
+ 32
PCA using R
Å R printout:
pca$loadings[,1:4]
+ 35
#R code:
screeplot(pca,type="lines")
+ 37
Scree Plot.
+ 38