I am trying to using scipy
minimize
function for the following optimization:
V = np.matrix(pd.read_csv('V.csv'))`
R = np.matrix(pd.read_csv('R.csv', index_col = 'Ticker'))`
w0= list()
for i in range(0, 84):
w0.append(1/84)
def calculate_portfolio_var(w,V):
w = np.matrix(w)
return (w*V*w.T)[0,0]
cons = ({'type': 'eq', 'fun': lambda x: np.sum(x)-1.0})
myBound = [(0, 1) for i in range(0, 84)]
res= minimize(calculate_portfolio_var, w0, args=V, method='SLSQP',constraints=cons, bounds = myBound)
where V
is the variance-covariance matrix, R
is the series of annualized return of stocks.
In addition to the 2 constraints (cons
and myBound
), I want an additional constraint that the result portfolio return, which is the weighted average of the result weights and stock returns, be equal to certain number and the number of stocks to be less than equal to certain number..
For example, it should look like:
cons = ({'type': 'eq', 'fun': lambda x: np.sum(x)-1.0},
{'type': 'eq', PortfolioReturn = 10%,
{'type': 'ineq', number of result stocks <= 40)
I am not so familiar with the Scipy minimize, and I would appreciate if someone can help me.