我有下面的代码将隐含波动率转换成分段恒定波动率。在下面的代码中,我得到一个错误:
for j, _vol in enumerate(_boot_vol,2):
TypeError: 'numpy.float64' object is not iterable
但是_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
答案 0 :(得分:1)
需要在两者之间使用一个临时变量
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