-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?

hyunsu
  • 1
  • 2
  • Why are `p` and `p0` 2-dimensional? Are you used to a language that doesn't support arbitrary-dimension arrays? – user2357112 May 27 '15 at 20:31
  • p and p0 should be 1-dimensional array? then p =np.array([sigma,k,rup]) is right? – hyunsu May 28 '15 at 03:59
  • Possible duplicate of [Understanding the error for scipy.optimize.minimize() function](http://stackoverflow.com/questions/22844347/understanding-the-error-for-scipy-optimize-minimize-function) – MB-F May 22 '17 at 10:41

1 Answers1

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

farhawa
  • 10,120
  • 16
  • 49
  • 91