Maximum number of assets in R package 'performanceanalytics' optimizer
Asked Answered
A

1

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)
Acquisition answered 1/9, 2018 at 0:30 Comment(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?Sitra
A
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

Acquisition answered 6/9, 2018 at 7:5 Comment(4)
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.Sitra
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.Acquisition
So are you going to give yourself your own bounty then and close this issue? :)Manchester
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.Acquisition

© 2022 - 2024 — McMap. All rights reserved.