[GSoC-PortA] Constraints and Objectives Separately in Optimize.Portfolio

Ross Bennett rossbennett34 at gmail.com
Mon Jul 29 22:21:56 CEST 2013


Doug,

I have not implemented that yet. We did have some initial discussions, but
nothing further. I don't recall doing this in class and haven't been able
to find any good examples. I assume this would still be in the framework of
maximizing return, maximizing quadratic utility, or minimizing variance and
simply adding betas as a "constraint" in the A matrix, correct?

Here is a quick snippet of code, is this how it would be implemented for
the example below?

library(PortfolioAnalytics)
library(Rglpk)

data(edhec)
ret <- edhec[, 1:4]

# maximize return
# subject to:
# w'1 = 1
# w' betas <= 1.2
# w_min >= 0
# w_max <= 1

# Linear objective vector
objL <- colMeans(ret)

betas <- c(0.8, 0.2, 1.1, 1.5)
# A matrix
Amat <- rbind(rep(1, 4),
              betas,
              diag(4),
              diag(4))
rhs <- c(1, 1.2, rep(0, 4), rep(1, 4))
dir <- c("==", "<=", rep(">=", 4), rep("<=", 4))

opt <- Rglpk_solve_LP(obj=objL, mat=Amat, dir=dir, rhs=rhs, max=TRUE)
opt

Thanks,
Ross


On Mon, Jul 29, 2013 at 10:47 AM, Doug Martin <martinrd at comcast.net> wrote:

> Ross,
>
> Do I recall correctly that you implemented a general linear inequality
> constraint
> whereby the user specifies a row vector of the A matrix.   In that case the
> user
> can construct rows of the A matrix for any risk factor neutralization or
> constraint
> they like, including portfolio market exposures via single factor model
> betas,
> and use of whatever other fundamental factor model "beta" types they wish,
> i.e.,
> columns of the B matrix.
>
> A related question:  Have you implemented a mapping function for a general
> linear inequality constraint?  For this I think you just need the pattern
> of
> plus
> and minus signs of the row vector entries in the A matrix.  Brian, is that
> correct?
>
> Doug
>
>
>
> -----Original Message-----
> From: gsoc-porta-bounces at lists.r-forge.r-project.org
> [mailto:gsoc-porta-bounces at lists.r-forge.r-project.org] On Behalf Of Brian
> G. Peterson
> Sent: Sunday, July 28, 2013 11:31 AM
> To: gsoc-porta at r-forge.wu-wien.ac.at
> Subject: Re: [GSoC-PortA] Constraints and Objectives Separately in
> Optimize.Portfolio
>
> I think it is worth supporting, and I don't think it will be too hard.
>
> If we allow optimize.portfolio to take portfolio( the default) as well as
> constraints and objectives arguments (with default NULL) we can
> reconstitute
> a 'full' portfolio object with constraints and objectives included inside
> optimize.portfolio.  I'd suggest that the behavior in this case would be to
> append the constraints list/object or objectives list/object to the slots
> in
> the portfolio specification.
>
> I think this is important because while constraints, by definition, usually
> require some knowledge of the portfolio, objectives, again by definition,
> can often be completely independent of the portfolio specification.
>
> Another related use case which we should begin discussing is the ability to
> store multiple constraint and objective sets.  See for example the various
> benchmark optimizations Peter and I used in the 2012 R/Finance seminar.
>
> To understand the solution space, it is very common for an investor to
> calculate GMV, Markowitz, minETL, risk budget, etc portfolios.  How should
> we represent this multiple specification model?  A list of portfolio
> specifications?  I don't have an implementation answer right now, I'm just
> aware of the user case for supporting it.
>
> Regards,
>
> Brian
>
> On 07/28/2013 01:17 PM, Doug Martin wrote:
> > Ross,
> >
> > Earlier this week Brian and I discussed the desirability of being able
> > to specify constraints and objectives separately in
> > optimize.portfolio, and agreed that this is worth doing.  Brian, please
> confirm.
> >
> > Thanks,
> >
> > Doug
> >
> > *From:*Ross Bennett [mailto:rossbennett34 at gmail.com]
> > *Sent:* Thursday, July 18, 2013 9:05 AM
> > *To:* Doug Martin
> > *Cc:* Guy Yollin
> > *Subject:* Constraints and Objectives Separately in Optimize.Portfolio
> >
> > It is doable, but it won't be an easy fix because
> > optimize.portfolio_v2, constrained_objective_v2, and the subfunctions
> > are designed to operate on a single portfolio object.
> >
> > It is possible to change constraints or objectives without recreating
> > the entire portfolio object. You can do this for constraints as well
> > as objectives. See example below, will this suffice?
> >
> > # Load necessary packages
> >
> > library(PortfolioAnalytics)
> >
> > library(ROI)
> >
> > library(ROI.plugin.glpk)
> >
> > # Load the edhec data
> >
> > data(edhec)
> >
> > ret <- edhec[, 1:4]
> >
> > funds <- colnames(ret)
> >
> > # Specify a portfolio object
> >
> > pspec <- portfolio.spec(assets=funds)
> >
> > # Add constraints
> >
> > pspec <- add.constraint(portfolio=pspec, type="weight_sum",
> >
> >                          min_sum=0.99, max_sum=1.01, enabled=TRUE)
> >
> > pspec <- add.constraint(portfolio=pspec, type="box", min=0.1, max=0.4,
> > enabled=TRUE)
> >
> > pspec <- add.objective_v2(portfolio=pspec, type="return", name="mean",
> > enabled=TRUE)
> >
> > opt <- optimize.portfolio_v2(R=ret, portfolio=pspec,
> > optimize_method="ROI")
> >
> > opt$weights
> >
> > # The box constraints is the 2nd element (indexnum=2) in the
> > constraints list
> >
> > # We can update this in place by specifying the indexnum argument in
> > add.constraint
> >
> > # The indexnum depends on the order the constraint was added
> >
> > pspec$constraints
> >
> > # Now say we wanted to change the box constraints to for specific per
> > asset weights
> >
> > pspec <- add.constraint(portfolio=pspec, type="box",
> >
> >                          min=c(0.1, 0.05, 0.1, 0.15),
> >
> >                          max=c(0.4, 0.4, 0.5, 0.45),
> >
> >                          indexnum=2, enabled=TRUE)
> >
> > opt_new <- optimize.portfolio_v2(R=ret, portfolio=pspec,
> > optimize_method="ROI")
> >
> > opt_new$weights
> >
> > Ross
> >
> > On Thu, Jul 18, 2013 at 10:05 AM, Doug Martin <martinrd at comcast.net
> > <mailto:martinrd at comcast.net>> wrote:
> >
> > Do you think you can fairly easily make the fix I suggested concerning
> > having optimize.portfolio take separate constraint and objective
> > arguments, in case the is not already possible?
> >
> > Doug
> >
> > *From:*Ross Bennett [mailto:rossbennett34 at gmail.com
> > <mailto:rossbennett34 at gmail.com>]
> > *Sent:* Thursday, July 18, 2013 7:24 AM
> >
> >
> > *To:* Doug Martin
> > *Cc:* Guy Yollin
> > *Subject:* Re: Recurrent Problem
> >
> > Excellent, glad to hear that fixed the problem.
> >
> > Ross
> >
> > On Thu, Jul 18, 2013 at 9:16 AM, Doug Martin <martinrd at comcast.net
> > <mailto:martinrd at comcast.net>> wrote:
> >
> > I did as you suggested and that fixed the problem.  Thanks!  Doug
> >
> > *From:*Ross Bennett [mailto:rossbennett34 at gmail.com
> > <mailto:rossbennett34 at gmail.com>]
> > *Sent:* Thursday, July 18, 2013 4:18 AM
> > *To:* Doug Martin
> > *Cc:* Guy Yollin
> > *Subject:* Re: Recurrent Problem
> >
> > Doug,
> >
> > That does seem to be the exact problem you were running into before. I
> > found this discussion about dependencies for the methods package.
> > http://grokbase.com/t/r/r-devel/1272tva0c2/rd-dependency-problem-for-h
> > asarg
> >
> > I'm not familiar with WinEdt and Stangle, but it might be calling
> > Rscript to run the R code chunks and the methods package doesn't load
> > with Rscript. Can you try putting require(methods) in the first R code
> > chunk?
> >
> > Hope that helps,
> >
> > Ross
> >
> > On Wed, Jul 17, 2013 at 10:14 PM, Doug Martin <martinrd at comcast.net
> > <mailto:martinrd at comcast.net>> wrote:
> >
> > Ross,
> >
> > When I run the attached .Rnw file from WinEdt it fails and I get the
> > following _Err.log message:
> >
> > Error:  chunk 1
> >
> > Error in add.constraint(portfolio = pspec, type = "full_investment",
> > enabled = TRUE) :
> >
> >    could not find function "hasArg"
> >
> > Execution halted
> >
> > I believe this was the same error message I got when I tried to run
> > your Vignette (remember the "could not find function "hasArg".
> >
> > If you run Stangle on the .Rnw file and execute it, it runs fine.
> >
> > I tried R2.15.1 as well as 3.0.1 and have the same problem.
> >
> > Any idea how to fix this problem?
> >
> > Doug
> >
> > P.S. I am running other R code with Sweave in the latex document for
> > Chapter 1 with no problem.
> >
>
>
> --
> Brian G. Peterson
> http://braverock.com/brian/
> Ph: 773-459-4973
> IM: bgpbraverock
> _______________________________________________
> GSoC-PortA mailing list
> GSoC-PortA at lists.r-forge.r-project.org
> http://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/gsoc-porta
>
> _______________________________________________
> GSoC-PortA mailing list
> GSoC-PortA at lists.r-forge.r-project.org
> http://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/gsoc-porta
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/gsoc-porta/attachments/20130729/aeda75c8/attachment-0001.html>


More information about the GSoC-PortA mailing list