[R-SIG-Finance] fPortfolio (portfolioFrontier function)

Mark Gordon mgordon at mcphedge.com
Wed Dec 29 14:50:56 CET 2010


 I ran into the same problem with the function minriskPortfolio when I
started using R in September.  I posted the problem, but didn't get any
response.  Since that time several other users have posted with the same
problem:

Execution stopped:
   The minimum risk portfolio could not be computed
Possible Reason:
   Your portfolio constraints may be too restrictive.
Status Information:
   status=1 from solver solveRquadprog
Error:
   returned from Rmetrics

even though the constraints are not too restrictive and that a solution
really exists.

 I have been able to diagnose the problem.  I hope that you will be able
to correct the bug soon in the package.
 
 Within minriskPortfolio there is this line of code:

	 portfolio <- optimize(targetRiskFun, interval =
range(getMu(Data)), 
      	data = Data, spec = spec, constraints = constraints, 
        	tol = .Machine$double.eps^0.5)
 
 The parameter "interval" specificies the range of returns to search for
determining an optimal portfolio.  The "range" function returns an array
containing the returns for the best and worst assets in the dataset.
These returns are feasible only if there is no constriant on owning 100%
of the best or worst asset, respectively.  While iterating, the
optimizatino function will sometimes chose as an iterant a non-feasible
return.

 I worked around this by writing my own range function which returns the
best and worst returns subject to my constraints.  I look to create the
minimum return by first setting weights to their minimum constraints
then by choosing a maximum weight on the lowest returning asset, then
choosing the maximum weight on the second lowest returning asset et
cetera until I have my weights equal to 100%.  The calculation for
maximum return is similar.  I am not an R-maven, so I am sure that it is
possible to write more concise code but here is what I did and it works
for me:

MCPrange<-function(data, constraints)
{
mu=getMu(data)
maxCon<-maxWConstraints(data, constraints=constraints)
minCon<-minWConstraints(data, constraints=constraints)
Excess<-maxCon-minCon
summin=sum(minCon)

temp<-sort(mu, index.return=TRUE)
smu=temp$x
sExcess=Excess[temp$ix]
sminCon=minCon[temp$ix]
i=1
while (summin+sum(sExcess[1:i])<1) i=i+1
sExcess[i]=sExcess[i]+1-sum(sExcess[1:i])-summin
sExcess[(i+1):length(sExcess)]=0
min=sum((sExcess+sminCon)*smu)

temp<-sort(mu, decreasing=TRUE, index.return=TRUE)
smu=temp$x
sExcess=Excess[temp$ix]
sminCon=minCon[temp$ix]
i=1
while (summin+sum(sExcess[1:i])<1) i=i+1
sExcess[i]=sExcess[i]+1-sum(sExcess[1:i])-summin
sExcess[(i+1):length(sExcess)]=0
max=sum((sExcess+sminCon)*smu)

return(c(min, max))
}

  I substituted MCPrange for the range function in my version of
minriskPortfolio and use that version in all of my optimization
functions.

Mark Gordon
Meridian Capital Partners



More information about the Rmetrics-core mailing list