title: Find optimal vector that minimizes function
body: <p>I am trying to find a vector that minimizes the residual sum of squares when multiplying a matrix.</p>
  
  <p>I know of scipy's optimize package (which has a minimize function). However, there is an extra constraint for my code. The sum of all entries of w (see function below) must equal 1, and no entry of w can be less than 0. <strong>Is there a package that does this for me?</strong> If not, how can I do this?</p>
  
  <p>Trying to minimize w:</p>
  
  <pre><code>def w_rss(w,x0,x1):
      predictions = np.dot(x0,w)
      errors = x1 - predictions
      rss = np.dot(errors.transpose(),errors).item(0)
  
      return rss
  
  X0 = np.array([[3,4,5,3],
                 [1,2,2,4],
                 [6,5,3,7],
                 [1,0,5,2]])  
  
  X1 = np.array([[4],
                 [2],
                 [4],
                 [2]]) 
  
  W = np.array([[.0],
                [.5],
                [.5],
                [.0]])
  
  print w_rss(W,X0,X1)
  </code></pre>
  
  <p>So far this is my best attempt at looping through possible values of w, but it's not working properly.</p>
  
  <pre><code>def get_w(x0,x1):
  
  J = x0.shape[1]
  W0 = np.matrix([[1.0/J]*J]).transpose()
  rss0 = w_rss(W0,x0,x1)
  loop = range(J)
  for i in loop:
      W1 = W0
      rss1 = rss0
      while rss0 == rss1:
          den = len(loop)-1
          W1[i][0] += 0.01
          for j in loop:
              if i == j:
                  continue
              W1[j][0] -= 0.01/den
              if W1[j][0] &lt;= 0:
                  loop.remove(j)
          rss1 = w_rss(W1,x0,x1)
          if rss1 &lt; rss0:
              #print W1
              W0 = W1
              rss0 = rss1
          print '--'
          print rss0
          print W0
  
  return W0,rss0
  </code></pre>