这只是关于我可以在 r 性能分析优化器函数中使用的最大股票数量的一般问题。
我的代码可以很好地优化任何多达 110 个资产的东西,但任何超过这个值的东西都会产生错误。我找不到任何有关实际资产数量限制的文档。任何帮助表示赞赏。
除上述内容外,我还在下面添加了可重现的代码示例:
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)