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 PURPOSE. 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 - 2007, Diethelm Wuertz, GPL
20
# Diethelm Wuertz <wuertz@itp.phys.ethz.ch>
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
27
# see Rmetrics's copyright file
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
################################################################################
39
test.solveShortExact =
50
# ------------------------------------------------------------------------------
57
data = as.timeSeries(data(smallcap.ts))
58
data = data[, c("BKE", "GG", "GYMB", "KRON")]
62
spec = portfolioSpec()
65
# Default Constraints:
66
constraints = "LongOnly"
70
setTargetReturn(spec) = mean(as.matrix(data))
71
ans = solveRQuadprog(data, spec, constraints)
74
# Check Termination Error:
75
setTargetReturn(spec) = mean(as.matrix(10*data))
76
ans = solveRQuadprog(10*data, spec, constraints)
84
# ------------------------------------------------------------------------------
87
test.solveRQuadprog.twoAssets =
91
data = as.timeSeries(data(smallcap.ts))
92
data = data[, c("BKE", "GG")]
96
spec = portfolioSpec()
99
# Default Constraints:
100
constraints = "LongOnly"
104
setTargetReturn(spec) = mean(as.matrix(data))
105
ans = solveRQuadprog(data, spec, constraints)
108
# Check Termination Error:
109
setTargetReturn(spec) = mean(as.matrix(10*data))
110
ans = solveRQuadprog(10*data, spec, constraints)
118
# ------------------------------------------------------------------------------
129
data = as.timeSeries(data(smallcap.ts))
130
data = data[, c("BKE", "GG", "GYMB", "KRON")]
134
spec = portfolioSpec()
135
setSolver(spec) = "Rdonlp2"
138
# Long Only Constraints:
143
setTargetReturn(spec) = mean(as.matrix(data))
144
ans = solveRDonlp2(data, spec, constraints)
147
# Check Termination Error:
148
setTargetReturn(spec) = mean(as.matrix(10*data))
149
ans10 = solveRDonlp2(10*data, spec, constraints)
163
# ------------------------------------------------------------------------------
166
test.solverRDonlp2.budgetConsatraints =
174
data = as.timeSeries(data(smallcap.ts))
175
data = data[, c("BKE", "GG", "GYMB", "KRON")]
179
spec = portfolioSpec()
180
setSolver(spec) = "Rdonlp2"
181
setTargetReturn(spec) = mean(as.matrix(data))
184
# Add Budget Constraints:
185
constraints = c("minW[1:4]=0", "maxB[1:4]=1")
187
ans = solveRDonlp2(data, spec, constraints)
190
# Scaled Donlp2 - Add Budget Constraints:
191
constraints = c("minW[1:4]=0", "maxB[1:4]=0.3")
193
ans = solveRDonlp2(data, spec, constraints)
203
# ------------------------------------------------------------------------------
206
test.solveRDonlp2.twoAssets =
214
data = as.timeSeries(data(smallcap.ts))
215
data = data[, c("BKE", "GG")]
219
spec = portfolioSpec()
220
setTargetReturn(spec) = mean(as.matrix(data))
222
# Default Constraints:
223
constraints = "LongOnly"
227
ans = solveRDonlp2(data, spec, constraints)
239
# ------------------------------------------------------------------------------
246
data = as.timeSeries(data(smallcap.ts))
247
data = data[, c("BKE", "GG", "GYMB", "KRON")]
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"
261
# CVaR Portfolio Optimization:
262
ans = solveRlpSolve(data, spec, constraints)
270
################################################################################