braverock / PortfolioAnalytics

80 stars 46 forks source link

random.portfolio fails without constraints and no weight sequence specified #12

Open sstoeckl opened 6 years ago

sstoeckl commented 6 years ago

See the code below. Is that intended?

require(PortfolioAnalytics)
data(edhec)
R <- edhec[, 1:4]
pspec <- portfolio.spec(assets=colnames(R))
pspec <- add.constraint(portfolio=pspec, type="full_investment")
rp1 <- random_portfolios(portfolio=pspec, permutations=5000,rp_method='sample')
#> Error in seq.default(from = round(min, rounding), to = round(max, rounding), : 'from' must be a finite number

Created on 2018-09-20 by the reprex package (v0.2.1.9000)

sstoeckl commented 6 years ago

I just realized that this happens if no weight (box) constraints are specified for the portfolio object. In this case generatesequence() returns an error. A solution could be to assume any weight bound (e.g. min=-n_assets,max=+n_assets) and give a warning. However, when I try to add something like this: if(min(constraints$min)==-Inf){ constraints$min <- rep(-nassets,nassets) warning("random_portfolio needs to sample between bounded weights. As no box constraints were set, we specify minimum weights as -number of assets.") } if(max(constraints$max)==Inf){ constraints$max <- rep(nassets,nassets) warning("random_portfolio needs to sample between bounded weights. As no box constraints were set, we specify maximum weights as number of assets.") } I get various other problems in rp_transform not finding feasible weights etc...

priyasaivasan commented 3 years ago

Did you resolve this issue? I am having a similar problem.