6

This is just a general question regarding the maximum number of stocks I can use in the r performanceanalytics optimizer function.

My code works fine for optimizing anything up to around 110 assets but anything exceeding that gives an error. I couldn't find any documentation regarding limits around the actual number of assets. Any help is appreciated.

Further to the above, I have added reproducible code example below:

library(xts)
library(PortfolioAnalytics)
num_stocks = 300
num_periods = 200

rets = replicate(num_stocks, rnorm(num_periods))
colnames(rets) = paste0('stock', 1:num_stocks)

dates = seq(as.Date('2000-01-01'), by = 'month', length.out = num_periods) - 1




#100 stocks, returns optimal weights
equity.data = xts(rets, order.by = dates)[,1:100]

stocks <- colnames(equity.data)

# Specify an initial portfolio
portf.init <- portfolio.spec(stocks)

# Add constraints
# weights sum to 1
portf.minvar <- add.constraint(portf.init, type="full_investment")
# box constraints
portf.minvar <- add.constraint(portf.minvar, type="box", min=0.00, max=0.10)

# Add objective
# objective to minimize portfolio variance
portf.minvar <- add.objective(portf.minvar, type="risk", name="var")

optimize.portfolio(equity.data, 
               portfolio=portf.minvar, 
               optimize_method="ROI",
               trace=TRUE)


## 200 stocks, optimizer returns N/As for optimizes weights
equity.data = xts(rets, order.by = dates)[,1:200]

stocks <- colnames(equity.data)

# Specify an initial portfolio
portf.init <- portfolio.spec(stocks)

# Add constraints
# weights sum to 1
portf.minvar <- add.constraint(portf.init, type="full_investment")
# box constraints
portf.minvar <- add.constraint(portf.minvar, type="box", min=0.00, max=0.10)

# Add objective
# objective to minimize portfolio variance
portf.minvar <- add.objective(portf.minvar, type="risk", name="var")

optimize.portfolio(equity.data, 
               portfolio=portf.minvar, 
               optimize_method="ROI",
               trace=TRUE)
Tony2016
  • 247
  • 2
  • 11
  • 1
    Is this the error you get `Error in gmv_opt(R = R, constraints = constraints, moments = moments, : "package:ROI" %in% search() || requireNamespace("ROI", quietly = TRUE) is not TRUE`, just checking? – Technophobe01 Sep 06 '18 at 04:12

1 Answers1

4

I think the problem arises because of issues with calculating covariance matrices where (num_stocks = 300) > (num_periods = 200).

If I increase the number of periods to say 1000, there is no error when optimizing for 200 stocks.

Thanks all for your time

Tony2016
  • 247
  • 2
  • 11
  • Your example is a little confusing as you do not explain or state the error or show the runtime output. If someone runs the code example given they will get the ROI error, as your example depends on but does not load the ROI package. I'd recommend that you update your question and highlight the specific problem you are encountering. – Technophobe01 Sep 06 '18 at 15:34
  • Apologies Technophobe01, my R install already had the ROI package installed so I didn't encounter the "ROI" error you mentioned, so it didn't occur to me to mention it. I will make it clearer in future. – Tony2016 Sep 07 '18 at 01:02
  • 1
    So are you going to give yourself your own bounty then and close this issue? :) – Justin Sep 09 '18 at 19:21
  • I'm fairly new to this bounty but if anyone has any suggestions around this issue of number of assets exceeding the number of period data points, I'm happy to award the bounty. – Tony2016 Sep 10 '18 at 05:08