[R-SIG-Finance] fPortfolio question and edited code

Diethelm Wuertz wuertz at itp.phys.ethz.ch
Tue Oct 6 02:50:38 CEST 2009


Lorenzo Bertolini wrote:
> 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).
>   
why ????

"Partial" (is still untested and) was thought to work together wit 
solveRquadprog together setting the meq
argument properly. Unfortunately it is not well documented. This is on 
the ToDo list.

> 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")
>   
do not forget to define the Data object Used later ...

Data = portfolioData(data, spec)

> Return <- function(weights) (getMu(Data) %*% weights)
> Risk <- function(weights) (sqrt(weights %*% getSigma(Data) %*% weights))
>   
in the previous 2 lines you called the Data object ...
> 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)
>   

Title:
 MV Efficient Portfolio
 Estimator:         covEstimator
 Solver:            solveRdonlp2
 Optimize:          minRisk
 Constraints:       minW maxW minsumW maxsumW

Portfolio Weights:
    SBI     SPI     SII     LMI     MPI     ALT
-0.2954  0.0018 -0.0001 -0.0045  0.0000  1.1888

Covariance Risk Budgets:
   SBI    SPI    SII    LMI    MPI    ALT
0.0129 0.0014 0.0000 0.0002 0.0000 0.9854

Target Return and Risks:
  mean     mu    Cov  Sigma   CVaR    VaR
0.0010 0.0010 0.0068 0.0068 0.0161 0.0110



Diethelm Wuertz

> ... 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]]
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>
>



More information about the R-SIG-Finance mailing list