I have a unconstrained maximization problem I would like to solve:
In order to do so I am looking forward to use the minimize function from the scipy library.
from scipy.optimize import minimize
Before I go into what I tried, I will define each of the variables:
mu=[[-0.241035],
[ 0.03557551],
[-0.00410642],
[-0.43985304],
[-0.24741543]]
landa= 42.74650697 # is a scalar
E =[[0.000167,0.000032,0.000082,0.000055,0.000055],
[0.000032,0.000131,0.000019,0.000043,0.000032],
[0.000082,0.000019,0.000273,0.000110,0.000086],
[0.000055,0.000043,0.000110,0.000229,0.000131],
[0.000055,0.000032,0.000086,0.000131,0.000165]]
In funct0 I set the Maximization function that appears in the image attached and define weights matrix.
def funct0(x):
x0,x1,x2,x3,x4=x
weights= np.array([x0,x1,x2,x3,x4])
return -1*(np.matmul(weights.T , mu) - np.matmul(np.matmul (landa*weights.T, E ),weights) /2)
In funct1 I set the bounds and the constraint because I want the variables in weights x0,x1,x2,x3,x4 to sum up to 1.
def funct1():
x0=np.array([1,1,1,1,1])
cons = ({'type': 'eq', 'fun': lambda x: sum(x) - 1})
res=minimize(funct0, x0, bounds=[[0,None] for i in range(len(x0))],options={"disp": False}, constraints=cons)
return res.x
print(funct1())
When executing this script , it outputs
[ 0 1 0 0 0]
I am not sure the maximization is correct because it assigns all to x1 variable, while the rest x0 x2 x3 x4 gets assigned 0 values.
It would made sense to me if the function set in funct0 was a linear function of w and mu , because doing so it assigns all to x1 that corresponds to its highest value in mu
I was thinking my output would be a more "diversified" assignment of values among the w variables in the matrix given the non linearity of the function.
Is it that I might be setting the function in funct0 incorrectly? Am I using a wrong optimization maybe?
Your help is highly appreciated.

It is hard to write code in comments, so here it is in an answer:
and the answer is just like yours:
Your penalization is apparently not enough to overcome the dominating effect of the only positive coefficient in $\mu$. If I change $\gamma$ to 10000 then I get