[R-SIG-Finance] fPortfolio - SOCP not available?

Corwin Joy corwin.joy at gmail.com
Thu Dec 9 01:41:44 CET 2010


The method given by Diethelm Wuertz in the referenced post to obtain a
maximum return portfolio for a given level of risk works pretty well. 
However, I found it useful to tune the optimizer parameter so that the
solver is only solving to the accuracy I need and not running for a long
time.  Here is what this looks like:

require(fPortfolio)
require(fPortfolioSolver)

#
----------------------------------------------------------------------------------------------
# Portfolio solver using RSCOP.  Adapted from solveRsocp to use control
parameters from portfolio spec
#
----------------------------------------------------------------------------------------------
SolveRsocpWithControl<-
function (data, spec, constraints) 
{
    Data = portfolioData(data, spec)
    trace = getTrace(spec)
    if (trace) 
        cat("\nPortfolio Optimiziation:\n Using Rsocp ...\n\n")
    nAssets = getNAssets(Data)
    args <- .rsocpArguments(data, spec, constraints)
    Control <- getControl(spec)
    ans <- .rsocp(f = args$f, A = args$A, b = args$b, C = args$C, 
        d = args$d, N = args$N, targetRisk = args$targetRisk, 
        mu = args$mu, Scale = args$Scale, control = Control)
    class(ans) = c("solveRfoo", "list")
    ans
}

#
----------------------------------------------------------------------------------------------
# Compute weights to give maximum return for desired volatility
# Input
#    rets: log-return history of assets to optimize as a time series object
#    target_vol: target level for desired volatility, expressed as an
annualized volatility
#
# Return
#    vector of weights to give maximum return given desired volatility
#
----------------------------------------------------------------------------------------------
maxReturnGivenVol<-function(rets, target_vol)
{  
  portSpec<-portfolioSpec()
  setTargetRisk(portSpec) <- (target_vol / sqrt(12)) ^ 2
  setSolver(portSpec)="SolveRsocpWithControl"
  
  # Adjust accuracy to appropriate level
  # setContol does not seem to be implemented
  # setControl(portSpec) = .rsocpControl(abs.tol = 1e-5, rel.tol = 1e-4)  
  portSpec at optim$control <- .rsocpControl(abs.tol = 1e-5, rel.tol = 1e-4) 
    
  eff<-efficientPortfolio(data=rets, spec=portSpec, constraints="LongOnly")
  # print(eff)
  # weightsPieCutoff(eff) # Pie chart with min weight cutoff

  w<-getWeights(eff)
  names(w)<-getNames(eff)
  w
}
-- 
View this message in context: http://r.789695.n4.nabble.com/fPortfolio-SOCP-not-available-tp2019667p3079413.html
Sent from the Rmetrics mailing list archive at Nabble.com.



More information about the R-SIG-Finance mailing list