2

I have a problem with scipy.brute, I set the maximization in this way

import numpy as np
from scipy.optimize import brute
from scipy.optimize import fmin
from scipy.stats import norm 

lambmarket=np.array([1.1076, 0.0615, 0.15238, 0.16265, 0.1761301, 0.193762, 0.0778772, 0.079162, 0.07505194, 0.071973])
xnew = np.array([0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 7.0, 10.0, 20.0, 30.0])

def optimization(key):
    if key=='inc':
         ranges=slice(0,2,0.2), slice(0.001,2.998,0.5)
         finalB =brute(incmse, ranges, full_output=True,finish=None, disp=True)
         finalA=fmin(incmse, finalB[0], xtol=0.001, ftol=0.001, maxiter=500, maxfun=500)

    return finalA

where

def incmse(*p):

    v, sigma =p[0]
    print v,sigma

    msqdiff=[(( lambmarket[t]  - s_t(0.,xnew[t], v, 0.003, sigma) )**2)  for t in range(0,len(xnew))]

    print sum(msqdiff)
    return msqdiff


def s_t(t,T,v,r,sigma):
    m=r-0.5*sigma**2
    gamma=1+2*m/(sigma**2)
    nu=m+sigma**2
    delta=m-gamma*sigma**2
    beta=-m*gamma+(gamma**2)*(sigma**2)/2
    s = lambda t,T,v: norm.cdf((-v-m*(T-t))/(sigma*np.sqrt(T-t))) - np.exp(v+r*(T-t)) * norm.cdf((-v-nu*(T-t))/(sigma*np.sqrt(T-t))) \
        + np.exp(( 1-gamma )*v)/gamma * norm.cdf((m*(T-t)-v)/(sigma*np.sqrt(T-t))) \
        - np.exp(v+(T-t)*beta)/gamma * norm.cdf((delta*(T-t)-v)/(sigma* np.sqrt(T-t)))

    lamb = ((1/0.01) * (s(t,T+0.01,v)-s(t,T,v)))/(1-s(t,T,v))


    return lamb

It can be executed by

 optimization('inc')

(lambmarket and xnew are global) from the prints I know that the error comes after all the brute force iterations have finished...is there someone that can explain me?

(I looked for similar threads and tried to solve by myself but I still can't understand what the problem is)

Full error output

param=optimization(key)
File "/home/myway/calibration.py", line 129, in optimization
finalB =brute(incmse, ranges, full_output=True,finish=None, disp=True)
File "/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 2542, in brute
Jout = vecfunc(*grid)
File "/usr/lib/python2.7/dist-packages/numpy/lib/function_base.py", line 1573, in __call__
return self._vectorize_call(func=func, args=vargs)
File "/usr/lib/python2.7/dist-packages/numpy/lib/function_base.py", line  1643, in _vectorize_call
copy=False, subok=True, dtype=otypes[0])
ValueError: setting an array element with a sequence.
7
  • Please transform this into a minimal reproducible example. We don't know what lambmarket is for instance. Commented Sep 21, 2015 at 19:06
  • edited, it should be compliant now Commented Sep 21, 2015 at 19:16
  • There are still undefined variables. (e.g. xnew, r) Commented Sep 21, 2015 at 19:34
  • 1
    return msqdiff should be return sum(msqdiff), right? Otherwise you would return a vector. Commented Sep 21, 2015 at 20:17
  • 1
    Glad you solved it. You may want to post an answer explaining the problem + the solution. This error is rather difficult to figure out, when you see it the first time. Commented Sep 21, 2015 at 20:24

1 Answer 1

2

As pointed out by cel, the problem is simply I was (unintendly) returning a vector instead of a single value, the function incmse must be modified as follows

def incmse(*p):

     v, sigma =p[0]
     print v,sigma

     msqdiff=[(( lambmarket[t]  - s_t(0.,xnew[t], v, 0.003, sigma) )**2)  for t in range(0,len(xnew))]

     print sum(msqdiff)
     return sum(msqdiff)

Now a single value is passed to scipy.optimize.brute as required for it to work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.