[R-SIG-Finance] PortfolioAnalytics group constraints
rossbennett34 at gmail.com
Sat Jun 13 13:34:01 CEST 2015
On Thu, Jun 11, 2015 at 11:53 AM, Peter <peter.michaels at gmail.com> wrote:
> I am trying to set-up a group constraint within PortfolioAnalytics, but I
> run into a problem if the current portfolio doesn't have weights in each of
> the groups. As an example, if I set a +/- 5% sector constraint on all 10
> sectors, but my current portfolio is only in 7 of the 10 sectors, the
> optimization step fails.
> The groups within the constraint is a list and the empty groups have an
> index of 0, since an empty list can't be accepted.
> sectiors <- c('s1','s2','s3','s4')
> sector.weights.max <- list(s1 = 0.3, s2 = 0.25, s3 = 0.40, s4=0.25)
> sector.weights.min <- list(s1 = 0.2, s2 = 0.15, s3 = 0.30, s4=0.15)
Your sector.weights.max and sector.weights.min objects should be vectors,
not named lists.
sector.weights.max <- c(0.3, 0.25, 0.40, 0.25)
sector.weights.min <- c(0.2, 0.15, 0.30, 0.15)
> sector.groups <- list(s1 = list(1,2,5), s2=list(3,4), s3=list(0),
Your sector.groups object should be a list of vectors. The groups should be
a list of vectors of the *index* of the assets. Recall that R has 1-based
indexing so defining a vector with an index of 0 will fail. See
?group_constraint for a few examples.
> pspec <- add.constraint(portfolio = pspec,
> type = 'group',
> groups = sector.groups,
> group_labels = sectors,
> group_min = sector.weights.min,
> group_max = sector.weights.max)
> Depending on the optimization algorithm I try, a different problem arises.
> When I try to generate a sequence of random portfolios, this is the error:
> rp = random_portfolios(pspec, permutations = 2000, rp_method = 'sample',
> eliminate = TRUE)
> Error in seq.default(from = round(min, rounding), to = round(max,
> rounding), :
> 'from' cannot be NA, NaN or infinite
> In addition: Warning messages:
> 1: In min(min_box) : no non-missing arguments to min; returning Inf
> 2: In max(max_box) : no non-missing arguments to max; returning -Inf
> How can I specify an empty group in the constraint, but still define a
> min/max constraint since a rebalance may include a position within that
> group. Is that possible?
I'm not sure I entirely understand the question. By defining an empty
group, you are stating that no assets belong in that group so how can a
rebalance include a position in an undefined group? Do you have a more
complete minimal reproducible example?
> Thank you,
> [[alternative HTML version deleted]]
> R-SIG-Finance at r-project.org mailing list
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.
[[alternative HTML version deleted]]
More information about the R-SIG-Finance