2
# This library is free software; you can redistribute it and/or
3
# modify it under the terms of the GNU Library General Public
4
# License as published by the Free Software Foundation; either
5
# version 2 of the License, or (at your option) any later version.
7
# This library is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR Description. See the
10
# GNU Library General Public License for more details.
12
# You should have received a copy of the GNU Library General
13
# Public License along with this library; if not, write to the
14
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19
# 1999 - Diethelm Wuertz, GPL
20
# 2007 - Rmetrics Foundation, GPL
21
# Diethelm Wuertz <wuertz@itp.phys.ethz.ch>
22
# for code accessed (or partly included) from other sources:
23
# see Rmetric's copyright and license files
26
################################################################################
27
# FUNCTION: EDUCATIONAL PORTFOLIO SLIDERS:
28
# weightsSlider Weights Slider
29
################################################################################
32
.counterWeightsSlider <- NA
35
# ------------------------------------------------------------------------------
39
function(object, control = list(), ...)
41
# A function implemented by Rmetrics
44
# Interactive view of Portfolio Weights
50
nFrontierPoints <- length(getTargetRisk(object)[ ,1])
51
dim = dim(getWeights(object))[2]
53
# Use default, if xlim and ylim is not specified ...
54
mu = getStatistics(object)$mu
55
Sigma = getStatistics(object)$Sigma
56
yLim = range(mu) + 0.25*c(-diff(range(mu)), diff(range(mu)))
58
# First, take care that all assets appear on the plot ...
59
sqrtSig = sqrt(diag(Sigma))
60
xLimAssets = c(min(sqrtSig), max(sqrtSig))+
61
c(-0.4*diff(range(sqrtSig)), 0.1*diff(range(sqrtSig)))
63
# ... second take care that the whole frontier appears on the plot:
64
fullFrontier = frontierPoints(object)
65
xLimFrontier = range(fullFrontier[, 1])
66
xLim = range(c(xLimAssets, xLimFrontier))
67
xLim[1] = xLim[1]-diff(xLim)/5
72
sliderFlag = "weights",
73
runningPoint.col = "red",
74
minvariance.col = "red",
75
tangency.col = "steelblue",
76
singleAsset.col = rainbow(dim),
80
runningPoint.cex = 1.5,
87
con[(Names <- names(control))] <- control
90
refresh.code = function(...)
93
.counterWeightsSlider <- getRmetricsOptions(".counterWeightsSlider") + 1
94
setRmetricsOptions(.counterWeightsSlider = .counterWeightsSlider)
95
if (.counterWeightsSlider < 1) return ()
98
N = .sliderMenu(no = 1)
103
# Plot 1 - Frontier Plot:
105
frontier = frontierPoints(object)
107
fPoint = frontier[N, ]
109
frontierPlot(object, xlim = con$xlim, ylim = con$ylim,
110
xlab = "", ylab = "", pch = 19, cex = 0.7, title = FALSE)
112
mtext("Target Risk", side = 1, line = 2, adj = 1, cex = 0.7)
113
mtext("Target Return", side = 2, line = 2, adj = 1, cex = 0.7)
115
points(fPoint[1], fPoint[2], col = con$runningPoint.col, pch = 19,
116
cex = con$runningPoint.cex)
118
tangencyLines(object, col = con$tangency.col, pch = con$tangency.pch)
119
tangencyPoints(object, col = con$tangency.col)
121
singleAssetPoints(object, col = con$singleAsset.col,
122
cex = con$singleAsset.cex, pch = con$singleAsset.pch)
124
minvariancePoints(object, col = con$minvariance.col,
125
cex = con$minvariancePlot.cex, pch = con$minvariance.pch)
128
"Return =", signif(fPoint[2], 2), "|",
129
"Risk = ", signif(fPoint[1], 2))
131
Title = "Efficient Frontier"
132
mtext(Title, adj = 0, line = 2.5, font = 2, cex = 0.7)
137
# Plot 2 - Weights Pie:
138
weightsPie(object, pos = N)
140
# Plot 3 - Weights Plot:
142
abline(v = N, col = "black")
144
# Plot 4 - Single Weights Plot:
145
weightsLinePlot(object)
146
abline(v = N, col = "black")
151
setRmetricsOptions(.counterWeightsSlider = 0)
152
Start <- which.min(getTargetRisk(object)[ , 1])
153
.sliderMenu(refresh.code, title = "Weights Slider",
156
maxima = c( nFrontierPoints),
157
resolutions = c(con$sliderResolution),
165
################################################################################