Scipy Interpolate PiecewisePolynomial - 输入数据错误

时间:2016-04-16 19:54:42

标签: python scipy interpolation

您好我正在使用以下代码尝试使用scipy的插值。

我收到以下错误:

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 3.4.1\helpers\pydev\pydevd.py", line 1733, in <module>
    debugger.run(setup['file'], None, None)
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 3.4.1\helpers\pydev\pydevd.py", line 1226, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:/Users/%user%/PycharmProjects/price_options_multithread/curve_interpolation.py", line 81, in <module>
    tck = interpolate.PiecewisePolynomial(moneyness_,vols_, orders = 2)
  File "C:\Users\%user%\Miniconda\lib\site-packages\scipy\interpolate\polyint.py", line 731, in __init__
    self._set_yi(yi0)
  File "C:\Users\%user%\Miniconda\lib\site-packages\scipy\interpolate\polyint.py", line 131, in _set_yi
    self._y_axis = (axis % yi.ndim)
ZeroDivisionError: integer division or modulo by zero

使用代码将插值读数调用为:

tck = interpolate.PiecewisePolynomial(X,Y, orders = 2)

Y是:

[0.2015, 0.3469, 0.2985, 0.2113, 0.19989999999999997, 0.4262, 0.21355000000000002, 0.22260000000000002, 0.21194999999999997, 0.1846, 0.2627, 0.2058, 0.2276, 0.21715, 0.23099999999999998, 0.20235, 0.2165, 0.21165, 0.3836, 0.19594999999999999, 0.20450000000000002, 0.20375, 0.20145000000000002, 0.23525000000000001, 0.2242, 0.22645, 0.27455, 0.22425, 0.2232, 0.1977, 0.19635000000000002, 0.21995, 0.30325, 0.22565]

而X是:

[0.91157701638785, 0.586013796249332, 0.820419314749065, 0.937622073998931, 1.17202759249866, 0.520901152221628, 0.872509429971228, 0.989712189221094, 1.0548248332488, 1.02877977563772, 0.807396785943524, 0.92459954519339, 1.30225288055407, 1.01575724683218, 0.859486901165687, 0.976689660415553, 1.00273471802663, 1.23714023652637, 0.455788508193925, 1.10691494847096, 0.846464372360146, 0.963667131610013, 1.09389241966542, 0.781351728332443, 0.898554487582309, 1.08086989085988, 0.716239084304739, 0.833441843554605, 0.950644602804472, 1.04180230444326, 1.1199374772765, 1.06784736205434, 0.651126440277035, 0.885531958776768]

type(X)type(Y)提供:<type 'list'>

如果我在excel中进行多项式插值,它看起来像如下: enter image description here

关于产生此错误的内容的任何指示都会非常有用。

谢谢!

1 个答案:

答案 0 :(得分:0)

使用python进行并行拟合最好使用numpy polyfit完成。

希望将来可以帮助其他人。

这与源错误无关 - 但我希望能帮助其他人。