File: solve-Ripop.R

package info (click to toggle)
fportfolio 4023.84-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,876 kB
  • sloc: makefile: 2
file content (148 lines) | stat: -rw-r--r-- 4,574 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR Description. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General
# Public License along with this library; if not, write to the
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA


################################################################################
# FUNCTION:                    DESCRIPTION:
#  solveRipop                   Portfolio interface to solver Ripop
#  .ripopArguments              Returns arguments for solver
################################################################################


solveRipop <-
    function(data, spec, constraints)
{
    # A function implemented by Diethelm Wuertz
    
    # Description:
    #   Portfolio interface to solver Ripop
    
    # Example:
    #    data <- 100 * LPP2005.RET[, 1:6]
    #    spec <- portfolioSpec()
    #    setTargetReturn(spec) <- mean(data)
    #    setSolver(spec) <- "solveRipop"
    #    solveRipop(data, spec)  

    # FUNCTION:

    # Transform Data:
    Data <- portfolioData(data, spec)
    data <- getSeries(Data)
    nAssets <- getNAssets(Data)

    # Compile Arguments for Solver:
    args <- .ripopArguments(data, spec, constraints)

    # Optimize Portfolio:
    ans <- try(ipopQP(
        objective = args$objective, 
        par.lower = args$par.lower, 
        par.upper = args$par.upper, 
        eqA = args$eqA, 
        eqA.bound = args$eqA.bound, 
        ineqA = args$ineqA, 
        ineqA.lower = args$ineqA.lower, 
        ineqA.upper = args$ineqA.upper, 
        control = list()), 
        silent = TRUE)
        
    if (inherits(ans, "try-error")) {
        # When Optimization Failed:
        ans <- list(
            opt = list(dvec=NA, Dmat=NULL),
            objective = 1e99,
            status = 1,
            message = "error",
            weights = rep(0, times=nAssets))
        run <- "failed"
    } else {
        # Set Tiny Weights to Zero:
        ans$weights <- .checkWeights(ans$solution)
        ans$solution <- NULL
        run <- "passed"
    }
    ans$solver <- "solveRipop"
    ans$solution <- ans$weights
    attr(ans$weights, "invest") <- sum(ans$weights)
    attr(ans$opt, "args") <- args

    # Class:
    class(ans) <- c("solver", "list")

    # Return Value:
    ans
}
       

# -----------------------------------------------------------------------------


.ripopArguments <-
    function(data, spec, constraints)
{
    # A function implemented by Diethelm Wuertz
    
    # Description:
    #   Returns ipopQP conform arguments for the solver

    # FUNCTION:

    # Data and Constraints as S4 Objects:
    Data <- portfolioData(data)
    data <- getSeries(Data)
    nAssets <- getNAssets(Data)
    Sigma <- getSigma(Data)

    # Box Constraints:
    par.lower <- minWConstraints(data, spec, constraints)
    par.upper <- maxWConstraints(data, spec, constraints)
        
    # Set up Equality Constraints:
    eqsumW <- eqsumWConstraints(data, spec, constraints)
    eqA <- eqsumW[, -1]
    eqA.bound <- eqsumW[, 1]
    
    # Set up Inequality Constraints:
    minsumW <- minsumWConstraints(data, spec, constraints)
    maxsumW <- maxsumWConstraints(data, spec, constraints)
    ineqA <- NULL
    if(!is.null(minsumW)) ineqA = rbind(ineqA, minsumW[, -1])
    if(!is.null(maxsumW)) ineqA = rbind(ineqA, maxsumW[, -1])
    ineqA.lower <- ineqA.upper <- NULL
    if(!is.null(minsumW)) {
        ineqA.lower = c(ineqA.lower, +minsumW[, 1])
        ineqA.upper = c(ineqA.upper, rep(sum(par.upper), times=length(minsumW[, 1]))) }
    if(!is.null(maxsumW)) {
        ineqA.lower = c(ineqA.lower, rep(sum(par.lower), times=length(maxsumW[, 1])))
        ineqA.upper = c(ineqA.upper, maxsumW[, 1]) }

    # Return Value:
    list(
        objective = list(dvec = rep(0, nAssets), Dmat = Sigma), 
        par.lower = par.lower,
        par.upper = par.upper,
        eqA = eqA,
        eqA.bound = eqA.bound ,
        ineqA = ineqA,
        ineqA.lower = ineqA.lower,
        ineqA.upper = ineqA.upper
        )
}


################################################################################