curve_fit python函数调用不是一个正确的浮点数组

时间:2015-10-01 16:34:42

标签: python

当我尝试在Python中运行curve_fit时,我的代码收到以下错误。 "函数调用不是一个正确的浮点数数组"。我读到它可能是由于没有输入数组引起的,所以我将列表转换为数组,但我仍然得到同样的错误。

#Calculate initial price
def initial_price(stocks, V, T, M, N, K, r):
  dt = float(T)/M
  for i in range(M - 1, 0, -1): 
      #Define x data
      xdata = list()
      for k in range(0, N):
        xdata.append(stocks[k * M + (i - 1)])
      #Convert list into array
      xdata = np.asarray(xdata)
      #Define y data
      ydata = list()
      for k in range(0, N): 
        ydata.append(exp(-r * dt) * V[k * M + i])
      #Convert list into array
      ydata = np.asarray(ydata)
      #get optimal values
      popt, pcov = curve_fit(cubic, xdata, ydata)
      for k in range(0, N): 
        V[k * M + (i - 1)] = \
        max(C_payoff(stocks[k * M + (i - 1)], 
        cubic(stocks[k * M + (i - 1)], popt[0], 
        popt[1], popt[2], popt[3])))
  #Compute initial price as discounted weighted average
  #over initial prices for all paths
  sum = 0.0
  for k in range(0, N): 
    sum = sum + V[k * M]
  V0 = exp(-r * dt) * (1.0/N) * sum
  return V0





#Define a cubic polynomial function 
def cubic(x, a0, a1, a2, a3): 
  return a0 + a1*x + a2 * x ** 2 + a3 * x ** 3

0 个答案:

没有答案