title: algorithm to calculate portfolio weights with negative returns?
body: <h2>Summary</h2>
  <p>Below is the full question (a bit complicated in its full form) here's a super simplified version (which is the heart of what I'm asking). If I told you to give me the weights of 49 and 17, you would take 66 (49+17) and divide each number by 66 (49/66=74% and 17/66=26%).  My question is, if I added a negative number -27 to the above numbers, how would you weight them (if I couldn't use a negative weight)?</p>
  <p>I somehow need to represent the negative number in the overall weight but the negative weight is throwing my numbers off. 39(49 + 17 + -27), causes 39/39=1.25%, 17/39=44%, and -27/39=-69%. So in the example below, if this were stocks and I gave you $100 how would you know how much to buy?</p>
  <h2>Detailed question</h2>
  <p>I'm having a bit of trouble figuring out an algorithm (or the logic how to do this).</p>
  <pre><code>Stock       percent of return              Return
  Blue        .98                            50       
  Red         .85                            20       
  Black       .90                           -30      
  </code></pre>
  <p>It's basically providing us with a report saying we want .98% of the return of 50%, 85% of the return from 20% and 90% of the returns from -30 (the total in this case is 39%).  I'm unsure how to translate this into an actual portfolio (like how much to buy to get this amount if I do not hold the fund already)?</p>
  <p>I'm starting to feel this is not possible, but I'm basically asking if I gave you the above table (name,return and % of return I want to capture) and $100 how can you make that portfolio? If I only gave you one stock that was 50% and said I wanted 100% of its returns you could easily just buy $100 of that single stock.With two stocks you could add the total returns/weights and get the weight but how do you deal with negative returns you want to capture?</p>
  <p>I don't think I'm getting this right but here's my logic so far (If this is totally wrong, please ignore and suggest anything you like).</p>
  <pre><code>Get list of data
  total_return = multiple percent_of_return and return (do this as a recursion for all stocks in the list)
  take total of all items in total_return (using absolute numbers..no negatives so above 39 equals 42) and figure out the weight of each from that.
  multiply these weights by stock price and divide by total to give weight.
  </code></pre>
  <p>The result of this is (note:the total changed from 39% to 93%):</p>
  <pre><code>Blue 0.52688172
  red 0.182795699
  black 0.290322581
  </code></pre>
  <p>The problem, which I'm really unsure if is converting a negative weight into a absolute (positive) weight.  I'm not good with math (and worst with finance) but I'm unsure how to 'buy' a percent of negative returns (the two data points I'm given, but I can enrich it with other data if needed like stock price, etc.).  Also, just to make it clear, I do not own these portfolios already so I can't just take these weights against an existing portfolios, I am using the above information as guidance to create a new one (just need to figure out an algo to do that).</p>
  <h2>Update</h2>
  <p>I always learn from code so here's my code (in Python) and some test cases (it's horribly ugly as I'm prototyping just to understand the logic of how to do this):</p>
  <pre><code>overall_total_value = []
  def test(data_return, datacoeff, data_value):
      total_results = 0
      data_total_of_return = {}
      for x in data_return:
          current_value = data_return[x] * (datacoeff[x]*.001)
          data_total_of_return[x] = current_value
          total_results = total_results + current_value
  
      #abs
      abs_total_value = 0
      for x in data_total_of_return:
          #print x, '=', abs(data_total_of_return[x])
          abs_total_value = abs_total_value + abs(data_total_of_return[x])
      
      print abs_total_value
      weight = {}
      for x in data_total_of_return:
          weight[x] = abs(data_total_of_return[x]/abs_total_value)
      
      total_value = 0
      for x in weight:
          valuer = weight[x] * data_value[x]
          print x, weight[x], '=', valuer
          total_value = total_value + valuer
      
      print 'ABS total % return: ',total_results
      print total_value
      overall_total_value.append(total_value)
      
      print &quot;*&quot;*30
      
      
  data_return = {'Blue':50, 'Red':20, 'Black':-30}
  data_value = {'Blue':10.4, 'Red':4, 'Black':8}
  
  datacoeff = { &quot;Blue&quot;: 78 , &quot;Red&quot;: 0 , &quot;Black&quot;: 0 , }
  test(data_return, datacoeff, data_value)
  datacoeff = { &quot;Blue&quot;: 48 , &quot;Red&quot;: 75 , &quot;Black&quot;: 0 , }
  test(data_return, datacoeff, data_value)
  datacoeff = { &quot;Blue&quot;: 46 , &quot;Red&quot;: 80 , &quot;Black&quot;: 0 , }
  test(data_return, datacoeff, data_value)
  datacoeff = { &quot;Blue&quot;: 38 , &quot;Red&quot;: 100 , &quot;Black&quot;: 0 , }
  test(data_return, datacoeff, data_value)
  datacoeff = { &quot;Blue&quot;: 62 , &quot;Red&quot;: 100 , &quot;Black&quot;: 40 , }
  test(data_return, datacoeff, data_value)
  datacoeff = { &quot;Blue&quot;: 100 , &quot;Red&quot;: 20 , &quot;Black&quot;: 50 , }
  test(data_return, datacoeff, data_value)
  datacoeff = { &quot;Blue&quot;: 98 , &quot;Red&quot;: 55 , &quot;Black&quot;: 70 , }
  test(data_return, datacoeff, data_value)
  datacoeff = { &quot;Blue&quot;: 90 , &quot;Red&quot;: 75 , &quot;Black&quot;: 70 , }
  test(data_return, datacoeff, data_value)
  datacoeff = { &quot;Blue&quot;: 100 , &quot;Red&quot;: 65 , &quot;Black&quot;: 80 , }
  test(data_return, datacoeff, data_value)
  datacoeff = { &quot;Blue&quot;: 98 , &quot;Red&quot;: 70 , &quot;Black&quot;: 80 , }
  test(data_return, datacoeff, data_value)
  datacoeff = { &quot;Blue&quot;: 96 , &quot;Red&quot;: 75 , &quot;Black&quot;: 80 , }
  test(data_return, datacoeff, data_value)
  datacoeff = { &quot;Blue&quot;: 94 , &quot;Red&quot;: 80 , &quot;Black&quot;: 80 , }
  test(data_return, datacoeff, data_value)
  datacoeff = { &quot;Blue&quot;: 92 , &quot;Red&quot;: 85 , &quot;Black&quot;: 80 , }
  test(data_return, datacoeff, data_value)
  datacoeff = { &quot;Blue&quot;: 90 , &quot;Red&quot;: 90 , &quot;Black&quot;: 80 , }
  test(data_return, datacoeff, data_value)
  datacoeff = { &quot;Blue&quot;: 88 , &quot;Red&quot;: 95 , &quot;Black&quot;: 80 , }
  test(data_return, datacoeff, data_value)
  test(data_return, datacoeff, data_value)
  datacoeff = { &quot;Blue&quot;: 98 , &quot;Red&quot;: 100 , &quot;Black&quot;: 100 , }
  test(data_return, datacoeff, data_value)
  </code></pre>