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,
18
################################################################################
19
# FUNCTION: DESCRIPTION:
20
# portfolioSpec Specifies a portfolio to be optimized
21
# .checkWeights Forces tiny weights to zero
22
# .checkSpecVsConstraints Stops if spec and constraints do not match
23
################################################################################
29
type = "MV", # Alt: "LPM", "CVaR"
30
optimize = "minRisk", # Alt: "maxReturn"
31
estimator = "covEstimator", # Alt: "shrinkEstimator",
34
params = list(alpha = 0.05, a = 1)),
43
solver = "solveRquadprog", # Alt: "solveRdonlp2"
47
params = list(meq = 2),
53
# Specifies a portfolio to be optimized
56
# portfolioSpec(portfolio = list(targetReturn = 1.5))
61
# model.type = c("MV", "CVaR")
62
# model.estimator.mean = "mean"
63
# model.estimator.cov = c("cov", "mcd", "Mcd", "shrink")
64
# optim.solver = c("solveRquadprog", "solveRdonlp2", "solveRglpk")
68
# stopifnot(model$type %in% model.type)
69
# stopifnot(model$estimator[1] %in% model.estimator.mean)
70
# stopifnot(model$estimator[2] %in% model.estimator.cov)
71
# stopifnot(optim$solver %in% optim.solver)
77
estimator = "covEstimator",
80
model$type = model$type[1]
81
Model[(Names <- names(model))] <- model
91
Portfolio[(Names <- names(portfolio))] <- portfolio
93
# Check Portfolio - weights, targetReturn, targetRisk:
94
# ... at least two of them must be set to NULL!
96
if(!is.null(portfolio$weights)) checkPortfolio = checkPortfolio + 1
97
if(!is.null(portfolio$targetReturn)) checkPortfolio = checkPortfolio + 1
98
stopifnot(checkPortfolio <= 1)
102
solver = "solveRquadprog",
104
Optim[(Names <- names(optim))] <- optim
109
portfolio = Portfolio,
114
# ------------------------------------------------------------------------------
118
function(weights, eps = sqrt(.Machine$double.eps))
120
# A function implemented by Diethelm Wuertz
123
# Sets tiny weights to zero
126
# weights - a numeric vector of portfolio weights
127
# eps - a numeric value, lower bounds of weigths
132
for(i in 1:length(weights)) {
133
if(abs(weights[i]) < eps) weights[i] = 0
141
# ------------------------------------------------------------------------------
144
.checkSpecVsConstraints <-
145
function(spec, constraints)
147
# A function implemented by Diethelm Wuertz
150
# Stops if spec versus constraints do mot match
153
# spec - portfolio specification as fPFOLIOSPEC object
154
# constraints - as charvec or as fPFOLIOSPEC object
159
if(class(constraints) == "fPFOLIOCON")
160
constraints = constraints@stringConstraints
161
if(any(constraints == "Short")) {
162
stopifnot(getSolver(spec) == "solveRshortExact")
170
# ------------------------------------------------------------------------------
173
.checkTargetReturn <-
177
# Check if target Return is defined
180
# spec - specification object
185
targetReturn = getTargetReturn(spec)
186
if(is.null(targetReturn))
187
stop("The target return is not available")
190
invisible(targetReturn)
195
################################################################################