[R-SIG-Finance] fPortfolio - Problem with constraints

Yaakov Moser ymoser at gmail.com
Sun May 9 12:24:14 CEST 2010


Hi all,


I am looking for help with a problem with finding the efficient frontier 
for a portfolio subject to quite restrictive constraints.

I am using fPortfolio - see the sample code and data below.


The problem is this - the portfolio returned does not quite reach the 
ends of the efficient frontier.

In the example given, for example, the maximum risk portfolio is not 
quite where it should be.

portfolioFrontier  gives weights -   0.1881       0.2000       
0.3019       0.0600       0.2500

while it should be -  0.2000       0.2000       0.3000       
0.0600       0.2400


As can be seen, there is some discrepancy.


About a year ago Diethelm kindly suggest a maxriskPortfolio function, 
which I tried here, but it gives a portfolio with short selling for some 
reason...

maxriskPortfolio -  0.20        45.32         0.30         0.06       -44.88


A similar problem exists with the minimum risk portfolio, but this can 
be solved by using the minvariancePortfolio function

portfolioFrontier -  0.0037       0.0420       0.8943       0.0600       
0.0000

minvariancePortfolio-  0.0000       0.1564       0.7836       
0.0600       0.0000


Can anyone help with the max risk portfolio?


Also, if I have the min risk and max risk porfolios and I want to find a 
portfolio with a given risk between them, how could I do this?


Finally, I note that the portfolioFrontier function is sensitive to the 
dataset (Data in my example below), even though the adjustedCovEstimator 
function doesn't need it at all..

In some cases (and I have not discovered what the rules are), the 
portfolioFrontier won't return a result.

Deleting some data from the Data resolves the problem..


Thanks


Yaakov Moser


> library(fPortfolio)
> adjustedCovEstimator <- function(x, spec = NULL, ...)
> {
>   tempEstimator <- covEstimator(x)
>   
> tempEstimator$mu[adjustedReturnsIndex==TRUE]<-adjustedReturns[adjustedReturnsIndex==TRUE]
>   tempEstimator$Sigma<-adjustedCovariance
>   list(mu=tempEstimator$mu,Sigma=tempEstimator$Sigma)
> }
>
> Data=as.timeSeries(read.csv("C:\\TSTest.csv"))
> Spec <- portfolioSpec()
> constraints = c("minW[1]=0", "maxW[1]=0.2", "minW[2]=1E-06", 
> "maxW[2]=0.2", "minW[3]=0.3", "maxW[3]=1", "minW[4]=0.06", 
> "maxW[4]=1", "minW[5]=0", "maxW[5]=0.25")
> adjustedReturns = c(0.072,0.027,0.002,0.004,0.026)
> adjustedReturnsIndex = c(TRUE,TRUE,TRUE,TRUE,TRUE)
>
> setEstimator(Spec)<-"adjustedCovEstimator"
> adjustedCovariance=as.matrix(read.csv("c:\\adjustedCovarianceTestFail.csv"))
> adjustedPortfolio = portfolioFrontier(Data, Spec, constraints)
> adjustedPortfolio
>
> globminPortfolio<-minvariancePortfolio(Data,Spec,constraints)
> print(globminPortfolio)
>
>
> maxriskPortfolio <-
> function (data, spec = portfolioSpec(), constraints = "long-only")
> {
>    Data = portfolioData(data, spec)
>    data <- getSeries(Data)
>    targetRiskFun <- function(x, data, spec, constraints) {
>        setTargetReturn(spec) = x[1]
>        Solver = match.fun(getSolver(spec))
>        ans = Solver(data, spec, constraints)
>        # DW:
>        # Take care that the status ans$status is always 0
>        #  If the solver fails set the value of the risk to the global
>        #    min risk portfolio!
>        # Use the function try() that the calculation does not break!
>        targetRisk = -ans$objective
>        attr(targetRisk, "weights") <- ans$weights
>        attr(targetRisk, "status") <- ans$status
>        return(targetRisk)
>    }
>
>    # DW:
>    # Take care that the interval range may be large enough if short 
> selling
>    # is allowed, that requires an adaption of the range!
>    # DW:
>    # Increase the tolerance to be sure that optimize has converged!
>    portfolio <- optimize(targetRiskFun, interval = range(getMu(Data)),
>        data = Data, spec = spec, constraints = constraints,
>        tol = .Machine$double.eps^0.5)
>
>    setWeights(spec) <- attr(portfolio$objective, "weights")
>    setStatus(spec) <- attr(portfolio$objective, "status")
>    portfolio = feasiblePortfolio(data, spec, constraints)
>    portfolio at call = match.call()
>    portfolio at title = "Maximum Risk Portfolio"
>    portfolio
> }
>
> maxriskPortfolio(Data, Spec, constraints)
TSTest.csv:

> returnsData1,returnsData2,returnsData3,returnsData4,returnsData5
> 01/01/1990,0.110279651,0.019190915,0.003901142,-0.014044147,0.009070051

adjustedCovarianceTestFail.csv:

> covData1,covData2,covData3,covData4,covData5
> 0.07177011,0.008122796,-0.000179875,9.74E-05,0.003777439
> 0.008122796,0.002606836,0.000750827,0.000486699,0.001584018
> -0.000179875,0.000750827,0.001071123,0.001123975,0.001366914
> 9.74E-05,0.000486699,0.001123975,0.002925277,0.003458666
> 0.003777439,0.001584018,0.001366914,0.003458666,0.005272605
 > R.Version()$version.string
[1] "R version 2.8.1 (2008-12-22)"

 > sessionInfo()
R version 2.8.1 (2008-12-22)
i386-pc-mingw32

locale:
LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United 
Kingdom.1252;LC_MONETARY=English_United 
Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
  [1] fPortfolio_2100.77 Rsymphony_0.1-8    Rglpk_0.2-9        
quadprog_1.4-11
  [5] fAssets_2100.77    robustbase_0.4-5   fCopulae_2100.76   adapt_1.0-4
  [9] sn_0.4-12          mnormt_1.3-3       fBasics_2100.77    
timeSeries_2100.83
[13] timeDate_290.85    MASS_7.2-45



More information about the R-SIG-Finance mailing list