title: Choose weights that minimize portfolio variance
body: <p>I am looking for a method that chooses the weights that minimize the portfolio variance.</p>
  
  <p>For example:</p>
  
  <p>I have 3 assets; their returns are given in an array below:</p>
  
  <pre><code>import numpy as np
  x = np.array([[0.2,-0.1,0.5,-0.2],[0, -0.9, 0.8, 0.2],[0.4,0.5,-0.3,-.01]])
  </code></pre>
  
  <p>I can weight them how I want to as long as sum of their weights adds to 1. I am looking for such weights that minimize variance of the portfolio.</p>
  
  <p>Here are two examples of randomly chosen weights:</p>
  
  <pre><code>weight_1 = [0.3,0.3,0.4]
  
  weighted_x_1 = [ele_x*ele_w for ele_x,ele_w in zip (x,weight_1)]
  
  var_1 = np.var(sum(weighted_x_1))
  
  
  weight_2 = [-0.2,0.4,0.8]
  
  weighted_x_2 = [ele_x*ele_w for ele_x,ele_w in zip (x,weight_2)]
  
  var_2 = np.var(sum(weighted_x_2))
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>&gt;&gt;&gt; var_1
  0.02351675000000001
  &gt;&gt;&gt; var_2
  0.012071999999999999
  </code></pre>
  
  <p>The second way is better.</p>
  
  <p>Is there a Python (or a Python library) method that could do this for me? If not any suggestions on what method I should use to do the above are welcome.</p>
  
  <p>Thank You in Advance</p>