-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathtestPortfolioOptim.r
84 lines (66 loc) · 2.14 KB
/
testPortfolioOptim.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# testPortfolioOptim.r
#
# load tseries library
library(tseries)
?portfolio.optim
methods(portfolio.optim)
# notice how the function is not displayed
portfolio.optim.default
# use getS3method to display function
getS3method("portfolio.optim","default")
getS3method("portfolio.optim","ts")
# use lab7 example data to test functions
lab7.df = read.csv("C:/classes/econ424/fall2008/econ424lab7returns.csv",
stringsAsFactors=F)
colnames(lab7.df)
ret.mat = as.matrix(lab7.df[,-1])
# estimate mu and Sigma
mu.hat = colMeans(ret.mat)
cov.hat = var(ret.mat)
mu.hat
cov.hat
# compute efficient portfolio with target return equal to mean
# of msft using portfolio function
ep = efficient.portfolio(mu.hat, cov.hat, mu.hat[1])
ep
# compute tangency port
tp = tangency.portfolio(mu.hat, cov.hat, 0.005)
tp
# compute global min port
gp = globalMin.portfolio(mu.hat, cov.hat)
gp
# compute efficient frontier
ef = efficient.frontier(mu.hat, cov.hat,
alpha.min=0, alpha.max=1)
# compute efficient portfolio using portfolio.optim
ep2 = portfolio.optim(ret.mat, pm = mu.hat[1], shorts=T)
names(ep2)
ep2$pm
ep2$ps
ep2$pw
# compute efficient portfolio not allowing short sales
ep2.ns = portfolio.optim(ret.mat, pm = mu.hat[1], shorts=F)
ep2.ns$pm
ep2.ns$ps
ep2.ns$pw
# compute tangency portfolio
tp2 = portfolio.optim(ret.mat, pm = tp$er, shorts=T)
tp2$pm
tp2$ps
tp2$pw
# compute efficient frontier with short sales
results.mat = matrix(0, 20, 6)
colnames(results.mat) = c("er","sd",colnames(ret.mat))
targetRet.vec = seq(from=gp$er, to=max(mu.hat), length=20)
for (i in 1:20) {
tmp.p = portfolio.optim(ret.mat, pm = targetRet.vec[i],
shorts=F)
results.mat[i,] = c(tmp.p$pm,tmp.p$ps,tmp.p$pw)
}
# plot efficient frontier
plot(ef$sd, ef$er, type="b", col="blue", main="Efficient Frontier",
xlab="portfolio sd", ylab="portfolio er")
points(results.mat[,"sd"], results.mat[,"er"], type="b",
col="red")
legend(x="topleft", legend=c("Short sales", "No short sales"),
lty=c(1,1), pch=c("o","o"), col=c("blue","red"))