0

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.

JungleDiff
  • 3,221
  • 10
  • 33
  • 57

2 Answers2

0

What about

constraints=({'type': 'eq', 'fun': lambda x:  np.sum(x) - 1.0},
             {'type': 'eq', 'fun': calculate_portfolio_var(x, V) - 0.1, 'args':V})

for the PortfolioReturn? For the number of result stocks, would you mind explaining to which variable it is related? I mean, it looks like it is R, but R is not used anywhere in the minimization problem.

Patol75
  • 4,342
  • 1
  • 17
  • 28
0

The constraint that limits the number of instruments in a portfolio is called a cardinality constraint. This leads to a model with discrete variables. These type of portfolio models are typically solved with MIQP (Mixed Integer Quadratic Programming) solvers. MIQP solvers are readily available for Python, but SciPy does not have one.

Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39