DeaR List,

I have been going through the fPortfolio package and changed some code in
the eqsumWConstraints function.
Now, if I use the String "Free" (instead of e.g. c("LongOnly","Partial")), I
can specify weights constraints with
more flexibility (also e.g. "eqsumW[1:6]=0", or "eqsumW[1:6]=-1" , which
were problematic before).

I have pasted the modified eqsumWConstraints() below and marked the changed
parts.

I have an actual problem which I find difficult to resolve, maybe someone
can point me out in the right direction:

I want to replicate the example in Diethelm Wurz' Chicago presentation in
April (ppt slide 18):

# Specification:
data = LPP2005.RET[, 1:6]
spec <- portfolioSpec()
setTargetReturn(spec) <- 4*mean(data) # 17.2%
setObjective(spec) = c("Objective", "Return", "Risk")
Return <- function(weights) (getMu(Data) %*% weights)
Risk <- function(weights) (sqrt(weights %*% getSigma(Data) %*% weights))

Objective <- function(weights) Risk(weights)

setSolver(spec) <- "solveRdonlp2"
# 130/30 Extension Constraints:

lowerExtension <- function(w) sum(w[w<0])
upperExtension <- function(w) sum(w[w>0])

cons <- c(
   "minW[1:nAssets] = rep(-0.30, times = nAssets)",
   "maxW[1:nAssets] = rep( 1.30, times = nAssets)",
   "minsumW[1:nAssets] = -0.30",
   "maxsumW[1:nAssets] = 1.30",
   "listF = list(lowerExtension, upperExtension)",
   "minF = c(-0.30, 0.00)",
   "maxF = c( 0.00, 1.30)")

# Portfolio:
efficientPortfolio(data, spec, cons)

... I get the error message: Error in UseMethod("getSigma") : no applicable
method for "getSigma"

I loaded fPortfolioSolver and can trace the origin of the error back to:


[...]
debug(Rdonlp2::donlp2)

[...]
debug: tryCatch(ans <- .Call("call_donlp2", as.double(par),
as.integer(num.lin),
    as.integer(num.nlin), fsilent, name, nchar(name), as.double(lbd),
    as.double(ubd), as.double(conmat), control, accfun, confun,
    environment(confun), PACKAGE = "Rdonlp2"), finally = .Call("teardown",
    0, PACKAGE = "Rdonlp2"))
Browse[1]>
Error in UseMethod("getSigma") : no applicable method for "getSigma"


Can someone give me a clue how to proceed? I would be very happy about that
;-)

Best regards,

Lorenzo Bertolini



Here is the edited eqsumWConstraints() function:


eqsumWConstraints <-
    function(data, spec = portfolioSpec(), constraints = "LongOnly")
{
    # Description:
    #   Returns a list with group equal matrix and vectors constraints

    # Details:
    #   Takes care of "eqsumW" strings
    #   A_eq W = c_eq

    # Arguments:
    #   data - a timeSeries or a fPFOLIODATA object
    #   spec - a fPFOLIOSPEC object
    #   constraints - a constraints string

    # Example:
    #   data = as.timeSeries(data(LPP2005REC))[, 1:6]
    #   spec = portfolioSpec(); setTargetReturn(spec) = mean(data)
    #   constraints = "eqsumW[1:6]=1"
    #   eqsumWConstraints(data, spec, constraints)
    #   eqsumWConstraints(data, spec, constraints = "LongOnly")
    #   eqsumWConstraints(data, spec, constraints = c("LongOnly","Partial"))

    # FUNCTION:

    # Get Statistics:
    data = portfolioData(data, spec)
    targetReturn = getTargetReturn(spec)[1]
    if (is.null(targetReturn)) {
        targetReturn = NA
        stop("Target Return is Missing")
    }

    # Get Specifications:
    mu <- getMu(data)
    nAssets <- getNAssets(data)
    assetsNames <- getNames(data)

    # Target Return:
    Aeq <- matrix(mu, byrow = TRUE, ncol = nAssets)

    # Full or partial Investment?
    if ("partial" %in% tolower(constraints))
        fullInvest = FALSE else fullInvest = TRUE

    if ("free" %in% tolower(constraints))                        ## added
this
        fullInvest = FALSE else fullInvest = TRUE                    ##
added this

    # Full Investment:
    #   - negative to handle better partial Investment in Rquadprog:
    if (fullInvest) Aeq <- rbind(Aeq, -rep(1, nAssets))

    # Dimension Names:
    colnames(Aeq) <- assetsNames
    if (fullInvest)
        rownames(Aeq) <- c("Return", "Budget")
    else
        rownames(Aeq) <- "Return"

    # RHS Vector:
    if (fullInvest)
        ceq <- c(Return = targetReturn, Budget = -1)
    else
        ceq <- c(Return = targetReturn)

    # Extract and Compose Matrix and Vector:
    what6 = substr(constraints, 1, 6)
    if (!is.null(constraints)) {
        nC = length(constraints)
        for (i in 1:nC) {
            if (what6[i] == "eqsumW")  {
                eqsumW = rep(0, times = nAssets)
                names(eqsumW) <- assetsNames

        alternateTextA <-strsplit(constraints[i], "=")[[1]][1]            ##
added this
        alternateTextB <-"=1"                            ## added this
        alternateText <- paste(alternateTextA,alternateTextB,sep="")
## added this
                eval(parse(text = alternateText))                    ##
changed this
                #####eval(parse(text = constraints[i]))
##

                Aeq = rbind(Aeq, eqsumW = sign(eqsumW))
                a = strsplit(constraints[i], "=")[[1]][2]
                ceq = c(ceq, eqsumW = as.numeric(a))
            }
        }
    }

    # Return Value:
    cbind(ceq, Aeq)
}

	[[alternative HTML version deleted]]

