说我有一个numpy阵列,但我看不见

2024-10-02 14:16:56 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是将隐含波动率转换为分段常数波动率的代码。在下面的代码中,我得到一个错误:

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

Tags: 代码inforlennparrayglobalboot
1条回答
网友
1楼 · 发布于 2024-10-02 14:16:56

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

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

相关问题 更多 >

    热门问题