-
Notifications
You must be signed in to change notification settings - Fork 0
/
optim_weights.R
154 lines (120 loc) · 5.36 KB
/
optim_weights.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
##==========================================================================================
# Assign xts object to a new variable
# Then make it a timeSeries object before pluging into portfolioData
ret <- monthly_log_ret %>% timeSeries()
# Create a data opject to use in fportfolio
ret_dat <- portfolioData(data = ret)
##==========================================================================================
# Compute a feasible portfolio
ewSpec <- portfolioSpec()
nAssets <- ncol(ret)
setWeights(ewSpec) <- rep(1/nAssets, times = nAssets)
ewPortfolio <- feasiblePortfolio(
data = ret,
spec = ewSpec,
constraints = "LongOnly")
print(ewPortfolio)
col <- divPalette(ncol(ret), "RdBu")
weightsPie(ewPortfolio, radius = 0.7, col = col)
mtext(text = "Equally Weighted MV Portfolio", side = 3, line = 1.5,
font = 2, cex = 0.7, adj = 0)
weightedReturnsPie(ewPortfolio, radius = 0.7, col = col)
mtext(text = "Equally Weighted MV Portfolio", side = 3, line = 1.5,
font = 2, cex = 0.7, adj = 0)
covRiskBudgetsPie(ewPortfolio, radius = 0.7, col = col)
mtext(text = "Equally Weighted MV Portfolio", side = 3, line = 1.5,
font = 2, cex = 0.7, adj = 0)
##==========================================================================================
# Compute a minimum risk efficient portfolio
minriskSpec <- portfolioSpec()
targetReturn <- getTargetReturn(ewPortfolio@portfolio)["mean"]
setTargetReturn(minriskSpec) <- targetReturn
minriskPortfolio <- efficientPortfolio(
data = ret,
spec = minriskSpec,
constraints = "LongOnly")
print(minriskPortfolio)
col = qualiPalette(ncol(ret), "Dark2")
weightsPie(minriskPortfolio, radius = 1, col = col)
mtext(text = "Minimal Risk MV Portfolio", side = 3, line = 1.5,
font = 2, cex = 1, adj = 0)
weightedReturnsPie(minriskPortfolio, radius = 0.7, col = col) > mtext(text = "Minimal Risk MV Portfolio", side = 3, line = 1.5,
font = 2, cex = 0.7, adj = 0)
covRiskBudgetsPie(minriskPortfolio, radius = 0.7, col = col)
mtext(text = "Minimal Risk MV Portfolio", side = 3, line = 1.5,
font = 2, cex = 0.7, adj = 0)
##==========================================================================================
# Compute global minimum variance portfolio
globminSpec <- portfolioSpec()
globminPortfolio <- minvariancePortfolio(
data = ret,
spec = globminSpec,
constraints = "LongOnly")
print(globminPortfolio)
col3 <- seqPalette(ncol(ret), "YlGn")
weightsPie(globminPortfolio, box = FALSE, col = col3)
mtext(text = "Global Minimum Variance MV Portfolio", side = 3,
line = 1.5, font = 2, cex = 1, adj = 0)
weightedReturnsPie(globminPortfolio, box = FALSE, col = col3)
mtext(text = "Global Minimum Variance MV Portfolio", side = 3,
line = 1.5, font = 2, cex = 1, adj = 0)
covRiskBudgetsPie(globminPortfolio, box = FALSE, col = col3)
mtext(text = "Global Minimum Variance MV Portfolio", side = 3,
line = 1.5, font = 2, cex = 1, adj = 0)
##==========================================================================================
# Compute a tangency portfolio
tgSpec <- portfolioSpec()
setRiskFreeRate(tgSpec) <- 0
tgPortfolio <- tangencyPortfolio(
data = ret,
spec = tgSpec,
constraints = "LongOnly")
print(tgPortfolio)
col <- seqPalette(ncol(ret), "BuPu")
weightsPie(tgPortfolio, box = FALSE, col = col)
mtext(text = "Tangency MV Portfolio", side = 3, line = 1.5,
font = 2, cex = 0.7, adj = 0)
weightedReturnsPie(tgPortfolio, box = FALSE, col = col)
mtext(text = "Tangency MV Portfolio", side = 3, line = 1.5,
font = 2, cex = 0.7, adj = 0)
covRiskBudgetsPie(tgPortfolio, box = FALSE, col = col)
mtext(text = "Tangency MV Portfolio", side = 3, line = 1.5,
font = 2, cex = 0.7, adj = 0)
##==========================================================================================
# Create bar plot instead of pie chart
col = rampPalette(ncol(ret), "purple2green")
weights <- 100 * as.vector(getWeights(tgPortfolio))
names <- as.vector(colnames(ret))
barplot(height = weights, names.arg = names,
horiz = TRUE, las = 1, col = col)
title(main = "Weights of Long-Only Tangency Portfolio",
xlab = "Weights %")
##==========================================================================================
# Compur efficient frontier
portSpec <- portfolioSpec()
setNFrontierPoints(portSpec) <- 5
longFrontier <- portfolioFrontier(ret, portSpec)
print(longFrontier)
#interactive plot the efficient frontier
longFrontier <- portfolioFrontier(ret)
# plot(longFrontier)
setNFrontierPoints(portSpec) <- 25
longFrontier <- portfolioFrontier(ret, portSpec)
text <- "Mean-Variance Portfolio - Long Only Constraints"
tailoredFrontierPlot(object = longFrontier, risk = "Cov")
mtext(text, side = 3, line = 3, font = 2, cex = 0.9)
text <- "Mean-Variance Portfolio - Long Only Constraints"
weightsPlot(longFrontier)
mtext(text, side = 3, line = 3, font = 2, cex = 0.9)
weightedReturnsPlot(longFrontier)
covRiskBudgetsPlot(longFrontier)
# Efficient frontier
par(mfrow = c(1, 1))
set.seed(1953)
frontierPlot(object = longFrontier, pch = 19, xlim = c(-0.002, 0.045),
cex = 0.5, ylim = c(-0.001, 0.004))
monteCarloPoints(object = longFrontier, mcSteps = 1000, pch = 19,
cex = 0.5)
twoAssetsLines(object = longFrontier, col = "orange", lwd = 1)
frontier <- frontierPoints(object = longFrontier)
lines(frontier, col = "red", lwd = 2)