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