-1

I want to estimate parameters 'k,ru,sigma' that maximumize the function 'func' ('ru' means r upperba)

The'func'formula is compex, so I want to upload the image to show this fomula, but i have no enough reputation.

    import numpy as np

sigma,k,ru=0.01,0.001,5

p0=np.array([[0.01,0.01,6]])

p=np.array([[sigma,k,ru]])

def func(p,r):
   T=91/365
   y=1/(np.sqrt(2*(np.pi)*p[0]**2/(2*p[1])*(1-np.exp(-(2*p[1]*T)))))*np.exp((r-p[2]-np.exp(-(p[1]*T))*(r-p[2]))**2/(p[0]**2/((-4)*p[1])*(1-np.exp(-(2*p[1]*T)))))    
   return -y


from scipy.optimize import minimize

r=np.array([[1.45,2.5,2.6,1.67,1.2]])

# r has 1350 datas like this

res=minimize(func,p0,args=(r))
Traceback (most recent call last):

  File "<ipython-input-9-b94a05d2ede8>", line 1, in <module>
    res=minimize(func,p0,args=(r))

  File "C:\Users\hyun su\Anaconda3\lib\site-packages\scipy\optimize\_minimize.py", line 419, in minimize
    return _minimize_bfgs(fun, x0, args, jac, callback, **options)

  File "C:\Users\hyun su\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 837, in _minimize_bfgs
    gfk = myfprime(x0)

  File "C:\Users\hyun su\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 282, in function_wrapper
    return function(*(wrapper_args + args))

  File "C:\Users\hyun su\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 616, in approx_fprime
    return _approx_fprime_helper(xk, f, epsilon, args=args)

  File "C:\Users\hyun su\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 556, in _approx_fprime_helper
    grad[k] = (f(*((xk + d,) + args)) - f0) / d[k]

ValueError: setting an array element with a sequence.

How can i solve this?

3
  • Why are p and p0 2-dimensional? Are you used to a language that doesn't support arbitrary-dimension arrays? Commented May 27, 2015 at 20:31
  • p and p0 should be 1-dimensional array? then p =np.array([sigma,k,rup]) is right? Commented May 28, 2015 at 3:59
  • Possible duplicate of Understanding the error for scipy.optimize.minimize() function Commented May 22, 2017 at 10:41

1 Answer 1

0

func here takes in a vector but it must be a scalar function of one or more variables as indicated in the scipy.optimize.minimize doc

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

1 Comment

Then how can I change Func to scalar fuction?

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.