Looking forward to solve the maximization of a unconstained function using python

118 Views Asked by At

I have a unconstrained maximization problem I would like to solve:

enter image description here

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.

1

There are 1 best solutions below

1
On BEST ANSWER

It is hard to write code in comments, so here it is in an answer:

from cvxpy import *
import numpy as np

mu=np.array([-0.241035,0.03557551,-0.00410642,-0.43985304,-0.24741543])
E=np.array([[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]])

w = Variable(5)
gamma = 42.74650697
ret = mu.T*w
risk = quad_form(w, E)
prob = Problem(Maximize(ret - gamma*risk/2), 
               [sum(w) == 1, w>=0])
prob.solve()
print(w.value)

and the answer is just like yours:

[1.45315326e-09 9.99999988e-01 7.26139115e-09 6.48572952e-10 1.20232620e-09]

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

[1.14078383e-01 5.84883795e-01 1.97874903e-01 3.00093159e-08 1.03162887e-01]