gpt4 book ai didi

python - 说我有一个 numpy 数组,但我看不到一个

转载 作者:行者123 更新时间:2023-12-01 07:35:13 26 4
gpt4 key购买 nike

我有下面的代码,可将隐含波动率转换为分段恒定波动率。在下面的代码中我收到一个错误:

对于 j,枚举中的 _vol(_boot_vol,2):
类型错误:“numpy.float64”对象不可迭代

但是 _vol_boot_vol 都不是 numpy 数组。需要你的智慧来解决这个问题

代码:

termstruct = np.array([0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]);
forwardcurve = np.array([0.0112, 0.0118, 0.0123, 0.0127, 0.0132, 0.0137, 0.0145,0.0154, 0.0163, 0.0174]);
capletvols = np.array([0.2366, 0.2487, 0.2573, 0.2564, 0.2476, 0.2376, 0.2252,0.2246, 0.2223]);
num_times = len(termstruct);
tau= np.diff(termstruct);
class computevol:
def _caliberatevol():
global termstruct
global forwardcurve
global tau
global capletvols
_vols = np.zeros((len(forwardcurve),len(termstruct)))
_boot_vol = []
for i , _capvol in enumerate(capletvols,2):
_boot_vol = _capvol**2 * termstruct[i-1]
for j, _vol in enumerate(_boot_vol,2):
_boot_vol -= _vol**2*tau[j-1]
_boot_vol.append(_boot_vol,np.sqrt(_boot_vol/tau(0)))
_vols[1:,1] = _boot_vol
for i in range(2,len(termstruct)):
_vols[i:,i] = _boot_vol[:-i+1]

return _vols

最佳答案

需要在两者之间使用临时变量

class computevol:
def _caliberatevol():
global termstruct
global forwardcurve
global tau
global capletvols
_vols = np.zeros((len(forwardcurve),len(termstruct)))
_boot_vol = []
for i , _capvol in enumerate(capletvols,2):
_temp= _capvol**2 * termstruct[i-1]
for j, _vol in enumerate(_boot_vol,2):
_temp -= _vol**2*tau[j-1]
_boot_vol.append(np.sqrt(_temp/tau[0]))
_vols[1:,1] = _boot_vol
for i in range(2,len(termstruct)):
_vols[i:,i] = _boot_vol[:-i+1]

return _vols

关于python - 说我有一个 numpy 数组,但我看不到一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57018357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com