jmbejara / comp-econ-sp18

Main Course Repository for Computational Methods in Economics (Econ 21410, Spring 2018)
16 stars 23 forks source link

My graph looks like a straight line... #11

Closed alykhanb96 closed 6 years ago

alykhanb96 commented 6 years ago
# TODO
import numpy
mean_grid = numpy.linspace(-.01, 2, num = 100, endpoint = True)
#print(mean_grid)
mg = mean_grid
var_needed = np.empty(100)
j = 0
for i in mg:
  mup = i
  cons = [lambda f: (f.T @ mu) - mup, lambda f: (f.T @ np.full((5,1),1)) - 1]
  var = scipy.optimize.fmin_slsqp(lambda f: f.T@sigma@f, w, eqcons  = cons)
  var_needed[j] = np.amin(var)
  j= j+1
print(var_needed)
plt.plot(var_needed, mean_grid)

plt.xlabel('Portfolio Return Variance')
plt.ylabel('Mean Portfolio Return')

returns a graph with a straight line... please help

conniexu75 commented 6 years ago

are you using the rets dataset? I had this problem when I was using prices

alykhanb96 commented 6 years ago

@conniexu75 I think so. Do you mean to calculate sigma initially?

u= np.mean(rets, axis= 0)
mu
sigma = np.cov(rets, rowvar = False)
sigma

Are we supposed to use rets somewhere else as well?

conniexu75 commented 6 years ago

ohh yeah that's what I meant, nevermind

jmbejara commented 6 years ago

The line involving np.amin(var) appears to be the error. The output of scipy.optimize.fmin_slsqp is the portfolio weights. You need to calculate the variance associated with the optimal weights after you've solved for the optimal weights.

alykhanb96 commented 6 years ago

thanks!

jmbejara commented 6 years ago

Additionally, you could use the full_output toggle and then you could get the value of the objective directly from the optimizer without having to recompute it. Check out the documentation here: https://docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.optimize.fmin_slsqp.html

You're welcome! Let me know if you have any other questions.