python - Ineq and eq constraints with scipy.optimize.minimize() -
i attempting understand behavior of constraints in scipy.optimize.minimize : first, create 4 assets , 100 scenarios of returns. average returning funds in order best worse d > b > > c #seed first np.random.seed(1) df_returns = pd.dataframe(np.random.rand(100,4) - 0.25, columns =list('abcd')) df_returns.head() b c d 0 0.167022 0.470324 -0.249886 0.052333 1 -0.103244 -0.157661 -0.063740 0.095561 2 0.146767 0.288817 0.169195 0.435220 3 -0.045548 0.628117 -0.222612 0.420468 4 0.167305 0.308690 -0.109613 -0.051899 and set of weights weights = pd.series([0.25, 0.25, 0.25, 0.25], index=list('abcd')) 0 0.25 b 0.25 c 0.25 d 0.25 we create objective function: def returns_objective_function(weights, df_returns): result = -1. * (df_returns * weights).mean().sum() return result and constraints , bounds cons = ({'type': 'eq', 'fun&