All,<div><br></div><div>I have a few thoughts on the hierarchy and how to transform weights that violate constraints and would appreciate any input or feedback.</div><div><br></div><div>The process outlined below is what I am thinking of following for the constraint mapping fn_map() function.</div>
<div><ul><li>Step 1: Test weights vector for violation of min_sum or max_sum. If violated, transform the weights vector with rp_transform() taking into account both leverage and box constraints. Another option is to normalize the weights by transforming the entire vector like what is done constrained_objective()... is one way preferable over the other?</li>
<li>Step 2: Test weights vector for violation of min or max. If violated, transform the weights vector with rp_transform() taking into account both leverage and box constraints. If we can't generate a feasible portfolio, this is because min or max is too restrictive. Try relaxing min or max. e.g., if min is violated, we could simply relax min by doing something like min <- min - 0.05 and try this N times to generate a feasible portfolio. Or we could randomly select an element of min and decrease it instead of modifying the entire vector.</li>
<li>Step 3: Test weights vector for violation of groups, cLO, or cUP. If violated, transform the weights vector with rp_transform() taking into account leverage, box, and group constraints. If we can't generate a feasible portfolio, try relaxing cLO or cUP. e.g., if cLO is violated, we could simply relax cLO by doing something like cLO <- cLO - 0.05 and try this N times to generate a feasible portfolio. Or we could randomly select an element of min and decrease it instead of modifying the entire vector.</li>
<li></li><li>Step 4: Test weights vector for violation of max_pos. If violated, transform the weights vector with rp_transform() taking into account leverage, box, group, and position limit constraints.</li></ul>Please advise if this is consistent with how you see the hierarchy in the constraint mapping function working?</div>
<div><br></div><div>Thanks,</div><div>Ross</div><div><br><div class="gmail_quote">On Tue, Jul 2, 2013 at 10:13 PM, Ross Bennett <span dir="ltr"><<a href="mailto:rossbennett34@gmail.com" target="_blank">rossbennett34@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">All,<div><br></div><div><div><div>I added an rp_constraint() function that uses logic from randomize_portfolio() to transform a weights vector element by element to satisfy (if possible) leverage *and* box constraints.</div>
<div><br></div><div>Here is a summary of where I am at with the mapping function.</div><ul><li>Box Constraints (min/max)</li><ul><li>rp_transform() takes a weight vector that violates either min_sum/max_sum leverage constraints *or* min/max box constraints and returns and weighting vector that satisfies leverage *and* box constraints.</li>
<li>txfrm_box_constraint() takes a weight vector that violates min/max box constraints and will set any weight that violates min or max to its min or max respectively. This is too simplistic and does not take into account min_sum/max_sum leverage constraints.</li>
<li>I think rp_transform() is the better option here... thoughts?</li></ul></ul><ul><li>Leverage Constraints (min_sum/max_sum)</li><ul><li>rp_transform() takes a weight vector that violates either min_sum/max_sum leverage constraints *or* min/max box constraints and returns and weighting vector that satisfies leverage *and* box constraints.</li>
<li>txfrm_weight_sum_constraint() takes a weight vector that violates min_sum/max_sum leverage constraints and normalizes the entire weights vector to satisfy leverage constraints. This is too simplistic and does not take into account min/max box constraints.</li>
<li>I think rp_transform() is the better option here... thoughts?</li></ul></ul><ul><li>Group Constraints (groups, cLO, cUP) </li><ul><li>txfrm_group_constraint() loops through the groups and checks if cLO or cUP is violated. If cLO or cUP is violated the weights of the given group are normalized to equal cLO or cUP, whichever is violated. This will likely change the sum of the weights vector and violate min_sum/max_sum so we will have to "re-transform".</li>
<li>I think txfrm_group_constraint() is a good first step because it gets us close to satisfying the group constraints.</li><li>I'm working on incorporating the group constraints into rp_transform().</li><li>I'm not seeing how to use the <span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">eval(parse(text=formula), data) code</span> to evaluate group constraints. Do you have a simple example?</li>
</ul></ul><ul><li>Diversification Constraint</li><ul><li>I'm having a hard time coming up with a straightforward solution to transform the vector of weights to meet the diversification constraint. One idea I was working on was to generate N random portfolios and select the portfolio with the closest diversification value.</li>
<li>Note that I define diversification as: diversification = 1 - sum(weights^2)</li><li>Would it be better to just handle this *like* an objective and penalize in constrained_objective()?</li></ul></ul><ul><li>Turnover Constraint</li>
<ul><li>I'm having a hard time coming up with a straightforward solution to transform the vector of weights to meet the turnover constraint. One idea I was working on was to generate N random portfolios and select the portfolio with the closest turnover value.</li>
<li>Would it be better to just handle this *like* an objective and penalize in constrained_objective()?</li></ul></ul><ul><li>Position Limit Constraint</li><ul><li>txfrm_position_limit_constraint() sets the nassets - max_pos minimum weights equal to 0.</li>
<li>An issue is that for any min_i > 0, this will violate the min box constraint and be penalized later. Would it make sense to change min_i to 0 for asset_i that is set equal to 0?</li></ul></ul></div><div>One last thing is that I have the mapping function in a loop to get each constraint object. The weights vector will be transformed in the order that the constraints were added. In order to honor the hierarchy of how we transform the weights vector, this should not be in a loop so we control the order of transformation. Is that correct?</div>
<div><br></div><div>I look forward to your feedback and comments.</div><div><br></div><div>Thanks,</div><div>Ross</div><div><div class="h5"><br><div class="gmail_quote">On Sun, Jun 30, 2013 at 11:51 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"><div lang="EN-US" link="blue" vlink="purple"><div><p><u></u> <u></u></p><p><u></u> <u></u></p><p><u></u> <u></u></p><p>
</p><div>-----Original Message-----<br>From: <a href="mailto:gsoc-porta-bounces@lists.r-forge.r-project.org" target="_blank">gsoc-porta-bounces@lists.r-forge.r-project.org</a> [mailto:<a href="mailto:gsoc-porta-bounces@lists.r-forge.r-project.org" target="_blank">gsoc-porta-bounces@lists.r-forge.r-project.org</a>] On Behalf Of Brian G. Peterson<br>
</div><div>Sent: Saturday, June 29, 2013 6:45 AM<br>To: PortfolioAnalytics<br>Subject: [GSoC-PortA] mapping function</div><p></p><p><u></u> <u></u></p><p>Based on side conversations with Ross and Peter, I thought I should talk a little bit about next steps related to the mapping function.<u></u><u></u></p>
<div><p><u></u> <u></u></p><p>Apologies for the long email, I want to be complete, and I hope that some of this can make its way to the documentation.<u></u><u></u></p><p><u></u> <u></u></p><p>The purpose of the mapping function is to transform a weights vector that does not meet all the constraints into a weights vector that does meet the constraints, if one exists, hopefully with a minimum of transformation.<u></u><u></u></p>
<p><u></u> <u></u></p><p>In the random portfolios code, we've used a couple of techniques pioneered by Pat Burns. The philosophical idea is that your optimum portfolio is most likely to exist at the edges of the feasible space.<u></u><u></u></p>
<p><u></u> <u></u></p><p>At the first R/Finance conference, Pat used the analogy of a mountain lake, where the lake represents the feasible space. With a combination of lots of different constraints, the shore of the lake will not be smooth or regular. The lake (the feasible space) may not take up a large percentage of the terrain.<u></u><u></u></p>
<p><u></u> <u></u></p><p>If we randomly place a rock anywhere in the terrain, some of them will land in the lake, inside the feasible space, but most will land outside, on the slopes of the mountains that surround the lake. The goal should be to nudge these towards the shores of the lake (our feasible space).<u></u><u></u></p>
<p><u></u> <u></u></p><p>Having exhausted the analogy, let's talk details.<u></u><u></u></p><p><u></u> <u></u></p><p>A slightly more rigorous treatment of the problem is given here:<u></u><u></u></p><p><a href="http://papers.ssrn.com/sol3/papers.cfm?abstract_id=1680224" target="_blank"><span style="color:windowtext;text-decoration:none">http://papers.ssrn.com/sol3/papers.cfm?abstract_id=1680224</span></a><u></u><u></u></p>
<p><u></u> <u></u></p></div><p><b><i>[Doug] This is the 2010 paper, which I just read much of. Very nice paper. I find Burns papers pretty useless except for pointing out nice statistical aspect and promoting PortfolioProbe. For example in the paper you sent, he does not say clearly what he is doing in generating the random portfolios and what he means by out-of-sample. As you once mentioned, I guess you got most details through direct conversation with him.<u></u><u></u></i></b></p>
<p><b><i><u></u> <u></u></i></b></p><p><b><i>Then I found the new Shaw 2011 paper at <a href="http://papers.ssrn.com/sol3/papers.cfm?abstract_id=1856476" target="_blank">http://papers.ssrn.com/sol3/papers.cfm?abstract_id=1856476</a>. <u></u><u></u></i></b></p>
<p><b><i>It has some nice additional material, e.g., Section 4.3 on lower bounds and Section 4.4 on bounded shorting, among other. I still need to finish reading this version.<u></u><u></u></i></b></p><p><b><i><u></u> <u></u></i></b></p>
<p><b><i>In general if you accept random portfolios that violate the constraints, what is a good way to: (a) Accept only those within a certain distance, with appropriately defined metric, of the feasible regions (you don’t want to consider all infeasible solutions – I guess this the reason for min.sum, max.sum, etc.?), and (b) Assuming an appropriate metric, does one want to take a solution nearest to a vertex?<u></u><u></u></i></b></p>
<div><div><p><b><i><u></u> <u></u></i></b></p><p><b><i><u></u> <u></u></i></b></p><p><b><i><u></u> <u></u></i></b></p><p>It is possible that can use this method directly for random portfolios (and that we could add the ectra constraint types to DEoptim). If so, much of the rest of what I'll write here is irrelevant. I strongly suspect that there will be some constraint types that will still need to be 'adjusted' via a mapping method like the one laid out below, since a stochastic solver will hand us a vector that needs to be transformed at least in part to move into the feasible space. It's alsom not entirely clear to me that the methods presented in the paper can satisfy all our constraint types.<u></u><u></u></p>
<p><u></u> <u></u></p><p><u></u> <u></u></p><p>I think our first step should be to test each constraint type, in some sort of hierarchy, starting with box constraints (almost all solvers support box constraints, of course), since some of the other transformations will violate the box constraints, and we'll need to transform back again.<u></u><u></u></p>
<p><u></u> <u></u></p><p>Each constraint can be evaluated as a logical expression against the weights vector. You can see code for doing something similar with time series data in the sigFormula function in quantstrat. It takes advantage of some base R functionality that can treat an R object (in this case the weights vector) as an environment or 'frame'. This allows the columns of the data to be addressed without any major manipulation, simply by column name (asset name in the weights vector, possibly after adding names back in).<u></u><u></u></p>
<p><u></u> <u></u></p><p>The code looks something like this:<u></u><u></u></p><p>eval(parse(text=formula), data)<u></u><u></u></p><p><u></u> <u></u></p><p>So, 'data' is our weights vector, and 'formula' is an expression that can be evaluated as a formula by R. Evaluating this formula will give us TRUE or FALSE to denote whether the weights vector is in compliance or in violation of that constraint. Then, we'll need to transform the weight vector, if possible, to comply with that constraint.<u></u><u></u></p>
<p><u></u> <u></u></p><p>Specific Cases:<u></u><u></u></p><p>I've implemented this transformation for box constraints in the random portfolios code. We don't need the evaluation I'll describe next for box constraints, because each single weight is handled separately.<u></u><u></u></p>
<p><u></u> <u></u></p><p>min_sum and max_sum leverage constraints can be evaluated without using the formula, since the formula is simple, and can be expressed in simple R code. The transformation can be accomplished by transforming the entire vector. There's code to do this in both the random portfolios code and in constrained_objective. It is probably preferable to do the transformation one weight at a time, as I do in the random portfolios code, to end closer to the edges of the feasible space, while continuing to take the box constraints into account.<u></u><u></u></p>
<p><u></u> <u></u></p><p>linear (in)equality constraints and group constraints can be evaluated generically via the formula method I've described above. Then individual weights can be transformed taking the value of the constraint<u></u><u></u></p>
<p>(<,>,=) into account (along with the box constraints and leverage constraints).<u></u><u></u></p><p><u></u> <u></u></p><p>and so on...<u></u><u></u></p><p><u></u> <u></u></p><p>Challenges:<u></u><u></u></p><p>- recovering the transformed vector from a optimization solver that doesn't directly support a mapping function. I've got some tricks for this using environments that we can revisit after we get the basic methodology working.<u></u><u></u></p>
<p><u></u> <u></u></p><p>-allowing for progressively relaxing constraints when the constraints are simply too restrictive. Perhaps Doug has some documentation on this as he's done it in the past, or perhaps we can simply deal with it in the penalty part of constrained_objective()<u></u><u></u></p>
<p><u></u> <u></u></p><p>Hopefully this was helpful.<u></u><u></u></p><p><u></u> <u></u></p><p>Regards,<u></u><u></u></p><p><u></u> <u></u></p><p>Brian<u></u><u></u></p><p><u></u> <u></u></p><p>--<u></u><u></u></p><p>Brian G. Peterson<u></u><u></u></p>
<p><a href="http://braverock.com/brian/" target="_blank"><span style="color:windowtext;text-decoration:none">http://braverock.com/brian/</span></a><u></u><u></u></p><p>Ph: <a href="tel:773-459-4973" value="+17734594973" target="_blank">773-459-4973</a><u></u><u></u></p>
<p>IM: bgpbraverock<u></u><u></u></p><p>_______________________________________________<u></u><u></u></p><p>GSoC-PortA mailing list<u></u><u></u></p><p><a href="mailto:GSoC-PortA@lists.r-forge.r-project.org" target="_blank"><span style="color:windowtext;text-decoration:none">GSoC-PortA@lists.r-forge.r-project.org</span></a><u></u><u></u></p>
<p><a href="http://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/gsoc-porta" target="_blank"><span style="color:windowtext;text-decoration:none">http://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/gsoc-porta</span></a><u></u><u></u></p>
</div></div></div></div><br>_______________________________________________<br>
GSoC-PortA mailing list<br>
<a href="mailto:GSoC-PortA@lists.r-forge.r-project.org" target="_blank">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></blockquote></div><br></div></div></div>
</blockquote></div><br></div>