carlosrjbr10
carlosrjbr10

Reputation: 23

SciPy's minimize is not iterating at all

I am trying to minimize a function that basically looks like this:

enter image description here

In reality it has two independent variables, but since x1 + x2 = 1, they're not REALLY independent.

now here's the objective function

def calculatePVar(w,covM):
    w = np.matrix(w)
    return (w*covM*w.T) [0,0]

wnere w is a list of the weights of each asset and covM is the covariance matrix that is returned by .cov() of pandas

Here's where the optimization function is called:

w0 = []
for sec in portList:
    w0.append(1/len(portList))

bnds = tuple((0,1)  for x in w0)
cons = ({'type': 'eq', 'fun': lambda x:  np.sum(x)-1.0})
res= minimize(calculatePVar, w0, args=nCov, method='SLSQP',constraints=cons, bounds=bnds)
weights = res.x

now there is a clear minimum to the function but minimize will just spit out the initial values as the result and it does say "Optimization terminated sucessfully". Any suggestions?

optimization results:

enter image description here

P.S. images as links because I don't meet the reqs!

Upvotes: 2

Views: 5791

Answers (2)

Finncent Price
Finncent Price

Reputation: 817

I had a similar problem and the issue turned out to be that the function and the constraint were outputting numpy arrays with a single element. Changing the output of those two functions to be floats solved the problem.

A very simple solution to a perplexing problem.

Upvotes: 1

SuperKogito
SuperKogito

Reputation: 2966

Your code had just some confusing variables so I just cleared that out and simplified some lines, now the minimization works correctly. However, the question now is: if the results are correct? and do they make sense? and that is for you to judge:

import numpy as np 
from scipy.optimize import minimize

def f(w, cov_matrix):
    return (np.matrix(w) * cov_matrix * np.matrix(w).T)[0,0]

cov_matrix = np.array([[1, 2, 3],
                       [4, 5, 6],
                       [7, 8, 9]])
p    = [1, 2, 3]
w0   = [(1/len(p))  for e in p]
bnds = tuple((0,1)  for e in w0)
cons = ({'type': 'eq', 'fun': lambda w:  np.sum(w)-1.0})

res  = minimize(f, w0, 
                args        = cov_matrix, 
                method      = 'SLSQP',
                constraints = cons, 
                bounds      = bnds)
weights = res.x
print(res)
print(weights)

Update:

Based on your comments, it seems to me that -maybe- your function has has multiple minima and that's why scipy.optimize.minimize gets trapped in there. I suggest scipy.optimize.basinhopping as an alternative, this would use a random step to go over most of the minima of your function and it will still be fast. Here is the code:

import numpy as np 
from scipy.optimize import basinhopping


class MyBounds(object):
     def __init__(self, xmax=[1,1], xmin=[0,0] ):
         self.xmax = np.array(xmax)
         self.xmin = np.array(xmin)

     def __call__(self, **kwargs):
         x = kwargs["x_new"]
         tmax = bool(np.all(x <= self.xmax))
         tmin = bool(np.all(x >= self.xmin))
         return tmax and tmin

def f(w):
    global cov_matrix
    return (np.matrix(w) * cov_matrix * np.matrix(w).T)[0,0]

cov_matrix = np.array([[0.000244181, 0.000198035],
                       [0.000198035, 0.000545958]])

p    = ['ABEV3', 'BBDC4']
w0   = [(1/len(p))  for e in p]
bnds = tuple((0,1)  for e in w0)
cons = ({'type': 'eq', 'fun': lambda w:  np.sum(w)-1.0})

bnds = MyBounds()
minimizer_kwargs = {"method":"SLSQP", "constraints": cons}
res  = basinhopping(f, w0, 
                    accept_test  = bnds)
weights = res.x
print(res)
print("weights: ", weights)

Output:

                        fun: 2.3907094432990195e-09
 lowest_optimization_result:       fun: 2.3907094432990195e-09
 hess_inv: array([[ 2699.43934183, -1184.79396719],
       [-1184.79396719,  1210.50404805]])
      jac: array([1.34548553e-06, 2.00122166e-06])
  message: 'Optimization terminated successfully.'
     nfev: 60
      nit: 6
     njev: 15
   status: 0
  success: True
        x: array([0.00179748, 0.00118076])
                    message: ['requested number of basinhopping iterations completed successfully']
      minimization_failures: 0
                       nfev: 6104
                        nit: 100
                       njev: 1526
                          x: array([0.00179748, 0.00118076])
weights:  [0.00179748 0.00118076]

Upvotes: 3

Related Questions