title: Trying to calculate correct returns and set a constraints on the max and min invested in each asset using 'quad_form'
body: <p>I am trying to hack together some code that looks like it should print our risk and returns of a portfolio, but the first return is 0.00, and that can't be right. Here's the code that I'm testing.</p>
  <pre><code>import pandas as pd
   
  # initialize list of lists
  data = [[130000, 150000, 190000, 200000], [100000, 200000, 300000, 900000], [350000, 450000, 890000, 20000], [400000, 10000, 500000, 600000]]
   
  # Create the pandas DataFrame
  data = pd.DataFrame(data, columns = ['HOSPITAL', 'HOTEL', 'STADIUM', 'SUBWAY'])
   
  # print dataframe.
  data
  </code></pre>
  <p>That gives me this data frame.</p>
  <p><a href="https://i.stack.imgur.com/OHWsD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OHWsD.png" alt="enter image description here" /></a></p>
  <pre><code>symbols = data.columns
  
  # convert daily stock prices into daily returns
  returns = data.pct_change()
  
  r = np.asarray(np.mean(returns, axis=1))
  r = np.nan_to_num(r)
  
  C = np.asmatrix(np.cov(returns))
  C = np.nan_to_num(C)
  
  # print expected returns and risk
  for j in range(len(symbols)):
      print ('%s: Exp ret = %f, Risk = %f' %(symbols[j],r[j], C[j,j]**0.5))
  </code></pre>
  <p>The result is this.</p>
  <p><a href="https://i.stack.imgur.com/2EEVx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2EEVx.png" alt="enter image description here" /></a></p>
  <p>The hospital risk and return can't be zero. That doesn't make sense. Something is off here, but I'm not sure what.</p>
  <p>Finally, I am trying to optimize the portfolio. So, I hacked together this code.</p>
  <pre><code># Number of variables
  n = len(data)
  
  # The variables vector
  x = Variable(n)
  
  # The minimum return
  req_return = 0.02
  
  # The return
  ret = r.T*x
  
  # The risk in xT.Q.x format
  risk = quad_form(x, C)
  
  # The core problem definition with the Problem class from CVXPY
  prob = Problem(Minimize(risk), [sum(x)==1, ret &gt;= req_return, x &gt;= 0])
  
  try:
      prob.solve()
      print (&quot;Optimal portfolio&quot;)
      print (&quot;----------------------&quot;)
      for s in range(len(symbols)):
         print (&quot; Investment in {} : {}% of the portfolio&quot;.format(symbols[s],round(100*x.value[s],2)))
      print (&quot;----------------------&quot;)
      print (&quot;Exp ret = {}%&quot;.format(round(100*ret.value,2)))
      print (&quot;Expected risk    = {}%&quot;.format(round(100*risk.value**0.5,2)))
  except:
      print (&quot;Error&quot;)
  </code></pre>
  <p>It seems to run but I don't know how to add a constraint. I want to invest at least 5% in every asset and don't invest more than 40% in any one asset. How can I add a constraint to do that?</p>
  <p>The idea comes from this link.</p>
  <p><a href="https://tirthajyoti.github.io/Notebooks/Portfolio_optimization.html" rel="nofollow noreferrer">https://tirthajyoti.github.io/Notebooks/Portfolio_optimization.html</a></p>