Reputation: 21
I am using SciPy to minimize the variance:
port_returns=[]
port_variance=[]
for p in range(4000):
weights = np.random.random(5)
weights /=np.sum(weights)
port_returns.append(np.sum(returns.mean()*245*weights))
port_variance.append(np.sqrt(np.dot(weights.T, np.dot(returns.cov()*245, weights))))
def min_variance(weights):
return np.array(port_variance)
cons = {'type':'eq', 'fun':lambda x: np.sum(x)-1}
bnds = tuple((0,1) for x in range(245))
optv = sco.minimize(min_variance, 245*[1.0/245,], method='SLSQP',
bounds=bnds, constraints=cons)
I tried to run this function but got the error below:
File "D:\Python27\lib\site-packages\scipy\optimize\_minimize.py", line 458, in minimize
constraints, callback=callback, **options)
File "D:\Python27\lib\site-packages\scipy\optimize\slsqp.py", line 370, in _minimize_slsqp
raise ValueError("Objective function must return a scalar")
ValueError: Objective function must return a scalar
How can I return a scalar value?
Upvotes: 2
Views: 3787