Trying to solve the maximization function
Trying to solve the maximization function
I have a 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.
minimize
scipy
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.
funct0
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.
funct1
x0,x1,x2,x3,x4
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.
x1
x0
x2
x3
x4
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
funct0
w
mu
x1
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.
w
Is it that I might be setting the function in funct0
incorrectly?
Am I using a wrong optimization maybe?
funct0
Your help is highly appreciated.
Sorry @Nico Schertler for my query! I have edited my text
– ge00rge
Jun 30 at 8:38
The signs of the mu vector look suspicious.
– Erwin Kalvelagen
Jun 30 at 8:50
It seems as if the found maximizer is actually correct. The maximizer of the unconstrained problem would be
(-36.1, 29.3, 31.3, -56.9, 0.08)
, which is way outside of your constraints.– Nico Schertler
Jun 30 at 9:05
(-36.1, 29.3, 31.3, -56.9, 0.08)
Ran the optimization through Mathematica and checked some close-by function values around the maximizer you stated (the neighboring values are all smaller and since it is a quadratic function, it is very likely that this is the real maximizer unless I chose some bad offset vectors). The unconstrained minimizer is also the solution of the linear system
lambda * Sigma * w = mu
.– Nico Schertler
Jun 30 at 9:16
lambda * Sigma * w = mu
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
So, what is it? The title and introduction says that your problem is unconstrained, but your code says that you have linear and box constraints.
– Nico Schertler
Jun 30 at 8:34