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

« back to all changes in this revision

Viewing changes to inst/unitTests/runit.portfolioFrontier.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 PURPOSE. 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:
 
20
#  test.portfolioFrontier.MV.Short
 
21
#  test.portfolioFrontier.MV.LongOnly
 
22
#  test.portfolioFrontier.MV.LongOnly.twoAssets
 
23
#  test.portfolioFrontier.MV.LongOnly.Rdonlp2
 
24
#  test.portfolioFrontier.MV.LongOnly.boxGroupConstraints
 
25
#  test.portfolioFrontier.MV.LongOnly.covRiskBudgetConstraints
 
26
#
 
27
#  test.portfolioFrontier.CVaR.Short
 
28
#  test.portfolioFrontier.CVaR.LongOnly
 
29
#  test.portfolioFrontier.CVaR.LongOnly.TwoAssets
 
30
################################################################################
 
31
 
 
32
 
 
33
test.portfolioFrontier.MV.Short <-
 
34
    function()
 
35
{
 
36
    # Data:
 
37
    data = as.timeSeries(data(smallcap.ts))
 
38
    data = data[, c("BKE", "GG", "GYMB", "KRON")]
 
39
    head(data)
 
40
 
 
41
    # Specification:
 
42
    spec = portfolioSpec()
 
43
    setSolver(spec) = "solveRshortExact"
 
44
    setNFrontierPoints(spec) = 10
 
45
    spec
 
46
 
 
47
    # Constraints:
 
48
    constraints = "Short"
 
49
    constraints
 
50
 
 
51
    # Frontier:
 
52
    Frontier = portfolioFrontier(data, spec, constraints)
 
53
    Frontier
 
54
 
 
55
    # Return Value:
 
56
    return()
 
57
}
 
58
 
 
59
 
 
60
# ------------------------------------------------------------------------------
 
61
 
 
62
 
 
63
test.portfolioFrontier.MV.LongOnly <-
 
64
    function()
 
65
{
 
66
    # Data:
 
67
    data = as.timeSeries(data(smallcap.ts))
 
68
    data = data[, c("BKE", "GG", "GYMB", "KRON")]
 
69
    head(data)
 
70
 
 
71
    # Specification:
 
72
    spec = portfolioSpec()
 
73
    setNFrontierPoints(spec) = 5
 
74
    spec
 
75
 
 
76
    # Constraints:
 
77
    constraints = "LongOnly"
 
78
    constraints
 
79
 
 
80
    # Portfolio Frontier:
 
81
    Frontier = portfolioFrontier(data, spec, constraints)
 
82
    Frontier
 
83
 
 
84
    # Return Value:
 
85
    return()
 
86
}
 
87
 
 
88
 
 
89
# ------------------------------------------------------------------------------
 
90
 
 
91
 
 
92
test.portfolioFrontier.MV.LongOnly.twoAssets <-
 
93
    function()
 
94
{
 
95
    # Data:
 
96
    data = as.timeSeries(data(smallcap.ts))
 
97
    data = data[, c("BKE", "GG")]
 
98
    head(data)
 
99
 
 
100
    # CVaR Specification:
 
101
    spec = portfolioSpec()
 
102
    setType(spec) = "MV"
 
103
    spec
 
104
 
 
105
    # Constraints:
 
106
    constraints = "LongOnly"
 
107
    constraints
 
108
 
 
109
    # CVaR Portfolio:
 
110
    Frontier = portfolioFrontier(data, spec, constraints)
 
111
    Frontier
 
112
 
 
113
    # Return Value:
 
114
    return()
 
115
}
 
116
 
 
117
 
 
118
# ------------------------------------------------------------------------------
 
119
 
 
120
 
 
121
test.portfolioFrontier.MV.LongOnly.Rdonlp2 <-
 
122
    function()
 
123
{
 
124
    if (FALSE) {
 
125
    
 
126
    if (require(Rdonlp2)) {
 
127
 
 
128
        # Data:
 
129
        data = as.timeSeries(data(smallcap.ts))
 
130
        data = data[, c("BKE", "GG", "GYMB", "KRON")]
 
131
        head(data)
 
132
 
 
133
        # Specification:
 
134
        spec = portfolioSpec()
 
135
        setSolver(spec) = "solveRdonlp2"
 
136
        setNFrontierPoints(spec) = 5
 
137
        spec
 
138
 
 
139
        # Constraints:
 
140
        constraints = "LongOnly"
 
141
        constraints
 
142
 
 
143
        # Portfolio Frontier:
 
144
        Frontier = portfolioFrontier(data, spec, constraints)
 
145
        Frontier
 
146
 
 
147
    }
 
148
    
 
149
    }
 
150
    NA
 
151
 
 
152
    # Return Value:
 
153
    return()
 
154
}
 
155
 
 
156
 
 
157
# ------------------------------------------------------------------------------
 
158
 
 
159
 
 
160
test.portfolioFrontier.MV.LongOnly.boxGroupConstraints <-
 
161
    function()
 
162
{
 
163
    # Load Data:
 
164
    Data = as.timeSeries(data(smallcap.ts))
 
165
    Data = Data[, c("BKE", "GG", "GYMB", "KRON")]
 
166
    head(Data)
 
167
 
 
168
    # Set Specifications:
 
169
    Spec = portfolioSpec()
 
170
    setNFrontierPoints(Spec) <- 10
 
171
 
 
172
    # Set Constraints:
 
173
    Constraints = c(
 
174
        "minW[1:4]=0.1",
 
175
        "maxW[1:4]=0.9",
 
176
        "minsumW[1:2]=0.2",
 
177
        "maxsumW[3:4]=0.8")
 
178
    ans = portfolioFrontier(Data, Spec, Constraints)
 
179
 
 
180
    # Plot:
 
181
    par(mfrow = c(2, 2), cex = 0.7)
 
182
    weightsPlot(ans)
 
183
    weightedReturnsPlot(ans)
 
184
    covRiskBudgetsPlot(ans)
 
185
 
 
186
    # Get Weights:
 
187
    getWeights(ans)
 
188
 
 
189
    # Get Risk Budgets:
 
190
    # getRiskBudgets(ans)
 
191
 
 
192
    # Return Value:
 
193
    return()
 
194
}
 
195
 
 
196
 
 
197
# ------------------------------------------------------------------------------
 
198
 
 
199
 
 
200
test.portfolioFrontier.MV.LongOnly.covRiskBudgetConstraints.Rdonlp2 <-
 
201
    function()
 
202
{
 
203
    if (FALSE) {
 
204
    
 
205
    if (require(Rdonlp2)) {
 
206
 
 
207
        # Data:
 
208
        data = as.timeSeries(data(smallcap.ts))
 
209
        data = data[, c("BKE", "GG", "GYMB", "KRON")]
 
210
        head(data)
 
211
 
 
212
        # Specification:
 
213
        spec = portfolioSpec()
 
214
        setSolver(spec) = "solveRdonlp2"
 
215
        setNFrontierPoints(spec) = 10
 
216
        spec
 
217
 
 
218
        # Add Risk Budgets:
 
219
        constraints = c("minW[1:4]=0", "maxB[1:4]=0.8")
 
220
        constraints
 
221
 
 
222
        # Frontier:
 
223
        Frontier = portfolioFrontier(data, spec, constraints)
 
224
        Frontier
 
225
 
 
226
    }
 
227
    
 
228
    }
 
229
    NA
 
230
 
 
231
    # Return Value:
 
232
    return()
 
233
}
 
234
 
 
235
 
 
236
# ------------------------------------------------------------------------------
 
237
 
 
238
 
 
239
test.portfolioFrontier.MV.LongOnly.covRiskBudgetConstraints <-
 
240
    function()
 
241
{
 
242
    if (FALSE) {
 
243
    
 
244
    if (require(Rdonlp2)) {
 
245
 
 
246
        # Load Data:
 
247
        Data = as.timeSeries(data(smallcap.ts))
 
248
        Data = Data[, c("BKE", "GG", "GYMB", "KRON")]
 
249
        head(Data)
 
250
 
 
251
        # Set Specifications:
 
252
        Spec = portfolioSpec()
 
253
        setSolver(Spec) = "solveRdonlp2"
 
254
        setNFrontierPoints(Spec) <- 10
 
255
 
 
256
        # Set Constraints:
 
257
        Constraints = c(
 
258
            "minW[1:4]=0.1",
 
259
            "maxW[1:4]=0.9",
 
260
            "minsumW[1:4]=0.0",
 
261
            "maxsumW[1:4]=1.0",
 
262
            "minB[1:4]=0.05",
 
263
            "maxB[1:4]=0.35")
 
264
 
 
265
        # still to be done ...
 
266
 
 
267
        ## setTargetReturn(Spec) <- mean(series(Data))
 
268
        ## solveRdonlp2(Data, Spec, Constraints)
 
269
 
 
270
        ## ans = .efficientConstrainedMVPortfolio(Data, Spec, Constraints)
 
271
 
 
272
        ## efficientPortfolio(Data, Spec, Constraints)
 
273
 
 
274
        ## ans = portfolioFrontier(Data, Spec, Constraints)
 
275
 
 
276
        # Plot:
 
277
        ## par(mfrow = c(2, 2), cex = 0.7)
 
278
        ## weightsPlot(ans)
 
279
        ## attributesPlot(ans)
 
280
        ## covRiskBudgetsPlot(ans)
 
281
 
 
282
        # Get Weights:
 
283
        ## getWeights(ans)
 
284
 
 
285
        # Get Risk Budgets:
 
286
        # getRiskBudgets(ans)                               # Does not work !!!!
 
287
 
 
288
    }
 
289
    
 
290
    }
 
291
    NA
 
292
 
 
293
    # Return Value:
 
294
    return()
 
295
}
 
296
 
 
297
 
 
298
################################################################################
 
299
 
 
300
 
 
301
test.portfolioFrontier.CVaR.Short <-
 
302
    function()
 
303
{
 
304
    NA
 
305
    
 
306
    # Return Value:
 
307
    return()
 
308
}
 
309
 
 
310
 
 
311
# ------------------------------------------------------------------------------
 
312
 
 
313
 
 
314
test.portfolioFrontier.CVaR.LongOnly <-
 
315
    function()
 
316
{
 
317
    # Data:
 
318
    data = as.timeSeries(data(smallcap.ts))
 
319
    data = data[, c("BKE", "GG", "GYMB", "KRON")]
 
320
    head(data)
 
321
 
 
322
    # CVaR Specification:
 
323
    spec = portfolioSpec()
 
324
    setType(spec) = "CVaR"
 
325
    setNFrontierPoints(spec) = 10
 
326
    spec
 
327
 
 
328
    # Constraints:
 
329
    constraints = "LongOnly"
 
330
    constraints
 
331
 
 
332
    # CVaR Portfolio Optimization:
 
333
    Frontier = portfolioFrontier(data, spec, constraints)
 
334
    Frontier
 
335
 
 
336
    # Return Value:
 
337
    return()
 
338
}
 
339
 
 
340
 
 
341
# ------------------------------------------------------------------------------
 
342
 
 
343
 
 
344
test.portfolioFrontier.CVaR.LongOnly.TwoAssets <-
 
345
    function()
 
346
{
 
347
    # Data:
 
348
    data = as.timeSeries(data(smallcap.ts))
 
349
    data = data[, c("BKE", "GG")]
 
350
    head(data)
 
351
 
 
352
    # CVaR Specification:
 
353
    spec = portfolioSpec()
 
354
    setType(spec) = "CVaR"
 
355
    spec
 
356
 
 
357
    # Constraints:
 
358
    constraints = "LongOnly"
 
359
    constraints
 
360
 
 
361
    # CVaR Portfolio:
 
362
    Frontier = portfolioFrontier(data, spec, constraints)
 
363
    Frontier
 
364
 
 
365
    # Return Value:
 
366
    return()
 
367
}
 
368
 
 
369
 
 
370
################################################################################
 
371