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

« back to all changes in this revision

Viewing changes to inst/unitTests/runitPortfolioSolver.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
 
# Copyrights (C)
18
 
# for this R-port: 
19
 
#   1999 - 2007, Diethelm Wuertz, GPL
20
 
#   Diethelm Wuertz <wuertz@itp.phys.ethz.ch>
21
 
#   info@rmetrics.org
22
 
#   www.rmetrics.org
23
 
# for the code accessed (or partly included) from other R-ports:
24
 
#   see R's copyright and license files
25
 
# for the code accessed (or partly included) from contributed R-ports
26
 
# and other sources
27
 
#   see Rmetrics's copyright file
28
 
 
29
 
 
30
 
################################################################################
31
 
# FUNCTION:                    DESCRIPTION:   
32
 
#  solveShortExact              Solves Analytically Unlimited Short Portfolio 
33
 
#  solveRQuadprog               Calls Goldfarb and Idnani's QP solver
34
 
#  solveRDonlp2                 Calls Spelucci's donlp2 solver
35
 
#  solveRlpSolve                Calls linear programming solver                    
36
 
################################################################################
37
 
 
38
 
 
39
 
test.solveShortExact =
40
 
function()
41
 
42
 
    # Tod:
43
 
    NA
44
 
    
45
 
    # Return Value:
46
 
    return()
47
 
}
48
 
 
49
 
 
50
 
# ------------------------------------------------------------------------------
51
 
    
52
 
    
53
 
test.solveRQuadprog =
54
 
function()
55
 
56
 
    # Direct Access:
57
 
    data = as.timeSeries(data(smallcap.ts))
58
 
    data = data[, c("BKE", "GG", "GYMB", "KRON")]
59
 
    head(data)
60
 
    
61
 
    # Specification
62
 
    spec = portfolioSpec()
63
 
    spec
64
 
    
65
 
    # Default Constraints:
66
 
    constraints = "LongOnly"
67
 
    constraints
68
 
    
69
 
    # Quadprog:
70
 
    setTargetReturn(spec) = mean(as.matrix(data))
71
 
    ans = solveRQuadprog(data, spec, constraints)
72
 
    ans
73
 
    
74
 
    # Check Termination Error:
75
 
    setTargetReturn(spec) = mean(as.matrix(10*data))
76
 
    ans = solveRQuadprog(10*data, spec, constraints)
77
 
    ans
78
 
    
79
 
    # Return Value:
80
 
    return()
81
 
}
82
 
 
83
 
 
84
 
# ------------------------------------------------------------------------------
85
 
 
86
 
 
87
 
test.solveRQuadprog.twoAssets =
88
 
function()
89
 
90
 
    # Direct Access:
91
 
    data = as.timeSeries(data(smallcap.ts))
92
 
    data = data[, c("BKE", "GG")]
93
 
    head(data)
94
 
    
95
 
    # Specification:
96
 
    spec = portfolioSpec()
97
 
    spec
98
 
    
99
 
    # Default Constraints:
100
 
    constraints = "LongOnly"
101
 
    constraints
102
 
    
103
 
    # Quadprog:
104
 
    setTargetReturn(spec) = mean(as.matrix(data))
105
 
    ans = solveRQuadprog(data, spec, constraints) 
106
 
    ans
107
 
    
108
 
    # Check Termination Error:
109
 
    setTargetReturn(spec) = mean(as.matrix(10*data))
110
 
    ans = solveRQuadprog(10*data, spec, constraints)
111
 
    ans 
112
 
    
113
 
    # Return Value:
114
 
    return()
115
 
}
116
 
 
117
 
 
118
 
# ------------------------------------------------------------------------------
119
 
 
120
 
 
121
 
test.solverRDonlp2 =
122
 
function()
123
 
124
 
    if (FALSE) {
125
 
        
126
 
        require(Rdonlp2)
127
 
        
128
 
        # Load 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) = "Rdonlp2"
136
 
        spec
137
 
        
138
 
        # Long Only Constraints:
139
 
        constraints = NULL
140
 
        constraints
141
 
        
142
 
        # Donlp2:
143
 
        setTargetReturn(spec) = mean(as.matrix(data))
144
 
        ans = solveRDonlp2(data, spec, constraints) 
145
 
        ans
146
 
        
147
 
        # Check Termination Error:
148
 
        setTargetReturn(spec) = mean(as.matrix(10*data))
149
 
        ans10 = solveRDonlp2(10*data, spec, constraints)
150
 
        ans10
151
 
        
152
 
        # Compare:
153
 
        ans$weights
154
 
        ans10$weights
155
 
        
156
 
    }
157
 
    
158
 
    # Return Value:
159
 
    return()
160
 
}
161
 
 
162
 
 
163
 
# ------------------------------------------------------------------------------
164
 
    
165
 
 
166
 
test.solverRDonlp2.budgetConsatraints =
167
 
function()
168
 
{     
169
 
    if (FALSE) {
170
 
        
171
 
        require(Rdonlp2)
172
 
        
173
 
        # Load Data:   
174
 
        data = as.timeSeries(data(smallcap.ts))
175
 
        data = data[, c("BKE", "GG", "GYMB", "KRON")]
176
 
        head(data)
177
 
        
178
 
        # Specification:
179
 
        spec = portfolioSpec()
180
 
        setSolver(spec) = "Rdonlp2"
181
 
        setTargetReturn(spec) = mean(as.matrix(data))
182
 
        spec
183
 
        
184
 
        # Add Budget Constraints:
185
 
        constraints = c("minW[1:4]=0", "maxB[1:4]=1")
186
 
        constraints
187
 
        ans = solveRDonlp2(data, spec, constraints)
188
 
        ans$weights
189
 
        
190
 
        # Scaled Donlp2 - Add Budget Constraints:
191
 
        constraints = c("minW[1:4]=0", "maxB[1:4]=0.3")
192
 
        constraints
193
 
        ans = solveRDonlp2(data, spec, constraints)
194
 
        ans$weights
195
 
        
196
 
    }
197
 
    
198
 
    # Return Value:
199
 
    return()
200
 
}
201
 
 
202
 
 
203
 
# ------------------------------------------------------------------------------
204
 
 
205
 
 
206
 
test.solveRDonlp2.twoAssets =
207
 
function()
208
 
209
 
    if (FALSE) {
210
 
        
211
 
        require(Rdonlp2)
212
 
        
213
 
        # Direct Access:
214
 
        data = as.timeSeries(data(smallcap.ts))
215
 
        data = data[, c("BKE", "GG")]
216
 
        head(data)
217
 
        
218
 
        # Specification:
219
 
        spec = portfolioSpec()
220
 
        setTargetReturn(spec) = mean(as.matrix(data))
221
 
        
222
 
        # Default Constraints:
223
 
        constraints = "LongOnly"
224
 
        constraints
225
 
        
226
 
        # RDonlp2:
227
 
        ans = solveRDonlp2(data, spec, constraints) 
228
 
        ans 
229
 
        ans$weights
230
 
        
231
 
    }
232
 
    
233
 
    # Return Value:
234
 
    return()
235
 
}
236
 
 
237
 
 
238
 
 
239
 
# ------------------------------------------------------------------------------
240
 
 
241
 
 
242
 
test.solveRlpSolve = 
243
 
function()
244
 
{
245
 
    # Load Data:
246
 
    data = as.timeSeries(data(smallcap.ts))
247
 
    data = data[, c("BKE", "GG", "GYMB", "KRON")]
248
 
    head(data)
249
 
    
250
 
    # CVaR Specification:
251
 
    spec = portfolioSpec()
252
 
    setType(spec) = "CVaR"
253
 
    setTargetReturn(spec) = mean(colAvgs(data))
254
 
    setTargetAlpha(spec) = 0.05
255
 
    setSolver(spec) <- "lpSolve"
256
 
    spec
257
 
    
258
 
    # Constraints:
259
 
    constraints = NULL
260
 
    
261
 
    # CVaR Portfolio Optimization:  
262
 
    ans = solveRlpSolve(data, spec, constraints)
263
 
    ans$weights
264
 
    
265
 
    # Return Value:
266
 
    return()
267
 
}
268
 
 
269
 
 
270
 
################################################################################
271