2

Let's see if I can get this solved with your help. It seems that the R package PortfolioAnalytics used to optimise a portfolio has a bug. It is most likely me, but to be sure I want to see if anyone else ran into the same issue and possibly a solution.

When adding a constraint for min and max of allocation per asset I receive the following error:

Error in sample.int(length(x), size, replace, prob) : 
  invalid first argument

Here is the code for the said error:

c.min = c(0.10,0.15,0.10,0.50,0.15,0,0,0,0,0)
c.max = c(.20,.20,.20,.20,.20,.20,.20,.20,.20,.20)
port_spec <- add.constraint(portfolio = port_spec,type = "box",enabled = TRUE,min = c.min,
                            max = c.max) #Min Position & Max position

10 is the correct number of assets, and the code runs fine when I use the following:

port_spec <- add.constraint(portfolio = port_spec,type = "box",enabled = TRUE,min = .00, max = .25) #Min Position & Max position

Furthermore, I am using the exact syntax that the publisher request, via the following link: publishers doc

Has anyone seen this issue before?

Here is the entire code used for the portfolio optimization:

port_spec <- NULL
port_spec <- portfolio.spec(colnames(returns.monthly))

## Add constraints 
#add constraint "full_invested" portfolio must equal 100% allocation
port_spec <- add.constraint(portfolio = port_spec,type = "weight_sum",min_sum = .97, max_sum = 1.01) 
port_spec <- add.constraint(portfolio = port_spec,type = "long_only")#add constriant "long_only" not allowing the portfolio to go short

c.min = c(0.10,0.15,0.10,0.50,0.15,0,0,0,0,0)
c.max = c(.20,.20,.20,.20,.20,.20,.20,.20,.20,.20)
port_spec <- add.constraint(portfolio = port_spec,type = "box",enabled = TRUE,min = c.min,
                            max = c.max) #Min Posiyion & Max position
port_spec <- add.constraint(port_spec,type = "return",return_target=mean.return)
port_spec <- add.constraint(port_spec,type = "risk",risk_target=mean.risk)
##Add portfolio objectives 

#port_spec <- add.objective(portfolio = port_spec,type = "return",name = "sr_annualized",arguments = list(scale = 12,rfr = .02))
port_spec <- add.objective(portfolio = port_spec,type = "return",name = "mean")
#port_spec <- add.objective(portfolio = port_spec,type = "risk",name = "StdDev")
#port_spec <- add.objective(portfolio = port_spec,type = "risk",name = "ES",arguments = list(p=0.90,method = "gaussian"))



# Add dummy objectives for plotting
port_spec <- add.objective(portfolio = port_spec,type = "risk",name = "StdDev",multiplier=0)
#port_spec <- add.objective(portfolio = port_spec,type = "return",name = "mean",multiplier=0)

print(port_spec)
## Run optimization 
opt <- optimize.portfolio(returns.monthly, portfolio = port_spec,optimize_method="DEoptim",trace = TRUE)
micstr
  • 5,080
  • 8
  • 48
  • 76
Aaron Soderstrom
  • 599
  • 1
  • 6
  • 12

1 Answers1

0

Nevermind, I found my issue, if you sum the min it is = 1, not allowing for optimization. Um, DUH!!!

Sorry

Aaron Soderstrom
  • 599
  • 1
  • 6
  • 12
  • 1
    lol, I'm glad you figured it out. Sometimes you just need to post the question to make you look at it with fresh eyes. – lebelinoz Oct 18 '17 at 22:53