worldexplorer95
worldexplorer95

Reputation: 137

python - Optimization with true/false constraint

I am currently working on a constrained optimization problem in Python and while I am able to formulate my problem I get the following error : 'Singular matrix C in LSQ subproblem'.

I believe this happens because two of my constraints(equality) are not continuous or something else related to them , since the optimizer works without them.

An example follows below :

vol_tgt = 0.1

sign_vec =

---------------+----+
| XLK US Equity |  1 |
| XOP US Equity |  1 |
| KRE US Equity |  1 |
| KBE US EQUITY |  1 |
| XLK US EQUITY |  1 |
| XLE US EQUITY |  1 |
| XLF US EQUITY |  1 |
| XRT US EQUITY |  1 |
| XLU US EQUITY |  1 |
| XLY US EQUITY |  1 |
| XLV US EQUITY |  1 |
| STS FP EQUITY |  1 |
| STR FP EQUITY |  1 |
| STZ FP EQUITY |  1 |
| STW FP EQUITY |  1 |
| STQ FP EQUITY |  1 |
| STN FP EQUITY | -1 |
+---------------+----+

return_vec =

+---------------+--------------+
| XLK US Equity | 0.005951589  |
| XOP US Equity | 0.024262624  |
| KRE US Equity | 0.007112154  |
| KBE US EQUITY | 0.003097968  |
| XLK US EQUITY | 0.005951589  |
| XLE US EQUITY | 0.019948716  |
| XLF US EQUITY | 0.003813095  |
| XRT US EQUITY | -0.001202198 |
| XLU US EQUITY | 0.003021156  |
| XLY US EQUITY | 0.002821742  |
| XLV US EQUITY | 0.004961415  |
| STS FP EQUITY | 0.000827929  |
| STR FP EQUITY | 0.005422823  |
| STZ FP EQUITY | -0.003453351 |
| STW FP EQUITY | -0.001449392 |
| STQ FP EQUITY | 0.015776843  |
| STN FP EQUITY | 0.000937061  |
+---------------+--------------+

The code is the following :

### define necessary functions ###
def optimization_function(weights,returns , vol_tgt, signs) :
    return - np.sum(np.log(np.abs(weights)))  #multiply by -1 since we wish to maximize but we give the problem 
        #to a minimizer

def portfolio_vol(weights,returns , vol_tgt, signs) : # inequality
    portf_return = np.dot(weights.T,returns)
    return np.sqrt(portf_return) - vol_tgt

def absolute_exposure(weights,returns , vol_tgt, signs) :
    return np.sum(np.abs(weights)) - 1

def positive_weights(weights,returns , vol_tgt, signs) :
    return float(np.sum(weights[signs == 1] <= 0))

def negative_weights(weights,returns , vol_tgt, signs) :
    return float(np.sum(weights[signs == -1] >= 0))

weights = sp.fmin_slsqp(optimization_function,lol,args=(return_vec,vol_tgt,sign_vec,),
                                    ieqcons = [portfolio_vol,],eqcons=[absolute_exposure,positive_weights,])

The troublesome functions are positive_weights and negative_weights. Without them I have no issues. Is there a way to fix this?

Thank you in advance.

Upvotes: 0

Views: 409

Answers (1)

Armali
Armali

Reputation: 19375

It seems much more natural to represent those as inequality constraints. For example, return weights[signs == 1].min() and constrain that to be nonnegative. (Unless the distinction between weight 0 and weight 1e-308 is actually crucial, in which case I guess you could subtract a tiny number before returning it.) – user2357112

Upvotes: 1

Related Questions