[GSoC-PortA] Constraints and Objectives Separately in Optimize.Portfolio
Brian G. Peterson
brian at braverock.com
Sun Jul 28 20:30:53 CEST 2013
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-hasarg
>
> 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
More information about the GSoC-PortA
mailing list