~ubuntu-branches/ubuntu/trusty/fportfolio/trusty

« back to all changes in this revision

Viewing changes to R/methods-plot.R

  • Committer: Bazaar Package Importer
  • Author(s): Dirk Eddelbuettel
  • Date: 2008-12-04 11:36:54 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20081204113654-gr832nfs44blp5ph
Tags: 280.74-1
* New upstream release
* Finally uploading as r-cran-rglpk is out of NEW after five weeks

* debian/control: Updated (Build-)Depends: and Suggests:

* debian/control: Set (Build-)Depends: to current R version
* debian/control: Set Standards-Version: to current version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
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.
 
6
#
 
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.
 
11
#
 
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,
 
15
# MA 02111-1307 USA
 
16
 
 
17
 
 
18
################################################################################
 
19
# FUNCTION:                     PRINT AND PLOT METHODS:
 
20
#  plot.fPORTFOLIO               S3 Plot method for 'fPORTFOLIO' objects
 
21
################################################################################
 
22
 
 
23
 
 
24
plot.fPORTFOLIO <-
 
25
    function(x, which = "ask", control = list(), ...)
 
26
{
 
27
    # A function implemented by Rmetrics
 
28
 
 
29
    # Description:
 
30
    #   Plot method for an object of class 'fPORTFOLIO'
 
31
 
 
32
    # Note:
 
33
    #   This method can also be used for plotting graphs fitted by
 
34
    #   the function 'garch' from the contributed R package 'tseries'.
 
35
 
 
36
    # FUNCTION:
 
37
 
 
38
    # Control Parameters:
 
39
    Statistics = getStatistics(x)
 
40
 
 
41
    # Use default, if xlim and ylim is not specified ...
 
42
    mu = Statistics$mu
 
43
    Sigma = Statistics$Sigma
 
44
    N = length(mu)
 
45
    yLim = range(mu) + 0.25*c(-diff(range(mu)), diff(range(mu)))
 
46
 
 
47
    # First, take care that all assets appear on the plot ...
 
48
    # sqrtSig = sqrt(diag(Sigma))
 
49
    # xLimAssets = c(
 
50
    #    min(sqrtSig),
 
51
    #    max(sqrtSig))+ c(-0.4*diff(range(sqrtSig)), 0.1*diff(range(sqrtSig)))
 
52
    xRange = range(frontierPoints(x)[, 1])
 
53
    xDiff = diff(xRange)
 
54
    xLimAssets = c(xRange[1] - 2.5*xDiff/10, xRange[2] + xDiff/10)
 
55
 
 
56
    # ... second take care that the whole frontier appears on the plot:
 
57
    fullFrontier = frontierPoints(x)
 
58
    xLimFrontier = range(fullFrontier[, 1])
 
59
    xLim = range(c(xLimAssets, xLimFrontier))
 
60
 
 
61
    # Control List:
 
62
    # YC: merge with user control list
 
63
    con <- c(control, frontierPlotControl())
 
64
    # YC: remove double entries and keep user args
 
65
    con <- con[unique(names(con))]
 
66
    attr(x, "control") <- con
 
67
 
 
68
    # Plot:
 
69
    interactivePlot(
 
70
        x,
 
71
        choices = c(
 
72
            "Plot Efficient Frontier",
 
73
            "Add Minimum Risk Portfolio",
 
74
            "Add Tangency Portfolio",
 
75
            "Add Risk/Return of Single Assets",
 
76
            "Add Equal Weights Portfolio",
 
77
            "Add Two Asset Frontiers [LongOnly Only]",
 
78
            "Add Monte Carlo Portfolios",
 
79
            "Add Sharpe Ratio [Markowitz PF Only]"),
 
80
        plotFUN = c(
 
81
            ".fportfolio.plot.1", ".fportfolio.plot.2", ".fportfolio.plot.3",
 
82
            ".fportfolio.plot.4", ".fportfolio.plot.5", ".fportfolio.plot.6", 
 
83
            ".fportfolio.plot.7", ".fportfolio.plot.8"),
 
84
        which = which)
 
85
 
 
86
    # Return Value:
 
87
    invisible(x)
 
88
}
 
89
 
 
90
 
 
91
# ------------------------------------------------------------------------------
 
92
 
 
93
 
 
94
.fportfolio.plot.1 <-
 
95
    function(x)
 
96
{
 
97
    # Description:
 
98
    #   Plot Efficient Frontier
 
99
 
 
100
    # FUNCTION:
 
101
 
 
102
    # Control:
 
103
    con = attr(x, "control")
 
104
    Type = getType(x)
 
105
    if (Type == "MV") {
 
106
        xLab = "Mean-Var Target Risk"
 
107
    } else if (Type == "CVaR") {
 
108
        xLab = "-CVaR Target Risk"
 
109
    }
 
110
 
 
111
    # Plot:
 
112
    frontierPlot(
 
113
        object = x, xlim = con$xlim, ylim = con$ylim, 
 
114
        pch = 19, cex = 0.75, title = FALSE,
 
115
        las = ifelse(is.null(con$las), 0, con$las))
 
116
    title(
 
117
        main = ifelse(is.null(con$main), "Efficient Frontier", con$main),
 
118
        xlab = ifelse(is.null(con$xlab), xLab, con$xlab),
 
119
        ylab = ifelse(is.null(con$ylab), "Target Return", con$ylab))
 
120
 
 
121
}
 
122
 
 
123
 
 
124
# ------------------------------------------------------------------------------
 
125
 
 
126
 
 
127
.fportfolio.plot.2 <-
 
128
    function(x)
 
129
{
 
130
    # Description:
 
131
    #   Add Minimum Risk Portfolio
 
132
 
 
133
    # FUNCTION:
 
134
 
 
135
    # Control:
 
136
    con = attr(x, "control")
 
137
 
 
138
    # Plot:
 
139
    minvariancePoints(
 
140
        object = x,
 
141
        col = con$minvariance.col,
 
142
        cex = con$minvariance.cex,
 
143
        pch = 19)
 
144
}
 
145
 
 
146
 
 
147
# ------------------------------------------------------------------------------
 
148
 
 
149
 
 
150
.fportfolio.plot.3 <-
 
151
    function(x)
 
152
{
 
153
    # Description:
 
154
    #   Add Tangency Portfolio
 
155
 
 
156
    # FUNCTION:
 
157
 
 
158
    # Control:
 
159
    con = attr(x, "control")
 
160
 
 
161
    # Plot:
 
162
    tangencyPoints(
 
163
        object = x,
 
164
        col = con$tangency.col,
 
165
        cex = con$tangency.cex,
 
166
        pch = 17)
 
167
    tangencyLines(
 
168
        object = x,
 
169
        col = con$tangency.col,
 
170
        cex = con$tangency.cex)
 
171
}
 
172
 
 
173
 
 
174
# ------------------------------------------------------------------------------
 
175
 
 
176
 
 
177
.fportfolio.plot.4 <-
 
178
    function(x)
 
179
{
 
180
    # Description:
 
181
    #   Add Risk/Return of Single Assets
 
182
 
 
183
    # FUNCTION:
 
184
 
 
185
    # Control:
 
186
    con = attr(x, "control")
 
187
 
 
188
    # Plot:
 
189
    Palette = match.fun(con$singleAsset.col)
 
190
    col = Palette(getNAssets(x))
 
191
    singleAssetPoints(
 
192
        object = x,
 
193
        col = col,
 
194
        cex = con$singleAsset.cex,
 
195
        pch = 18)
 
196
}
 
197
 
 
198
 
 
199
# ------------------------------------------------------------------------------
 
200
 
 
201
 
 
202
.fportfolio.plot.5 <-
 
203
    function(x)
 
204
{
 
205
    # Description:
 
206
    #   Add Equal Weights Portfolio
 
207
 
 
208
    # FUNCTION:
 
209
 
 
210
    # Control:
 
211
    con = attr(x, "control")
 
212
 
 
213
    # Plot:
 
214
    equalWeightsPoints(
 
215
        object = x,
 
216
        col = con$equalWeights.col,
 
217
        cex = con$equalWeights.cex,
 
218
        pch = 15)
 
219
}
 
220
 
 
221
 
 
222
# ------------------------------------------------------------------------------
 
223
 
 
224
 
 
225
.fportfolio.plot.6 <-
 
226
    function(x)
 
227
{
 
228
    # Description:
 
229
    #   Add Two Asset Frontiers [0-1 PF Only]
 
230
 
 
231
    # FUNCTION:
 
232
 
 
233
    # Control:
 
234
    con = attr(x, "control")
 
235
 
 
236
    # Lines:
 
237
    lines(frontierPoints(object = x), col = "grey")
 
238
    twoAssetsLines(object = x, col = con$twoAssets.col)
 
239
 
 
240
    # Points:
 
241
    Palette = match.fun(con$singleAsset.col)
 
242
    col = Palette(getNAssets(x))
 
243
    singleAssetPoints(
 
244
        object = x,
 
245
        col = col,
 
246
        cex = con$singleAsset.cex,
 
247
        pch = 18)
 
248
}
 
249
 
 
250
 
 
251
# ------------------------------------------------------------------------------
 
252
 
 
253
 
 
254
.fportfolio.plot.7 <-
 
255
    function(x)
 
256
{
 
257
    # Description:
 
258
    #   Add Monte Carlo Portfolios
 
259
 
 
260
    # FUNCTION:
 
261
 
 
262
    # Control:
 
263
    con = attr(x, "control")
 
264
 
 
265
    # Plot:
 
266
    monteCarloPoints(
 
267
        object = x,
 
268
        col = con$monteCarlo.col,
 
269
        cex = con$monteCarlo.cex,
 
270
        mcSteps = con$mcSteps)
 
271
}
 
272
 
 
273
 
 
274
# ------------------------------------------------------------------------------
 
275
 
 
276
 
 
277
.fportfolio.plot.8 <-
 
278
    function(x)
 
279
{
 
280
    # Description:
 
281
    #   Add Sharpe Ratio [MV PF Only]
 
282
    # FUNCTION:
 
283
 
 
284
    # Control:
 
285
    con = attr(x, "control")
 
286
 
 
287
    # Plot:
 
288
    sharpeRatioLines(
 
289
        object = x,
 
290
        col = con$sharpeRatio.col,
 
291
        cex = con$sharpeRatio.cex,
 
292
        lty = 3)
 
293
}
 
294
 
 
295
 
 
296
################################################################################
 
297