我正在尝试使用包含不同选项数据的pandas数据框来计算隐含波动率。对于我使用mibian的隐含波动率。这是代码:
optionsData.to_dict():
{'callclose': {0: Decimal('4'),
1: Decimal('2.62'),
2: Decimal('2.64'),
3: Decimal('1.7'),
4: Decimal('1.35')},
'daystoexpiration': {0: 43L, 1: 43L, 2: 43L, 3: 43L, 4: 43L},
'expiration': {0: datetime.date(2013, 2, 16),
1: datetime.date(2013, 2, 16),
2: datetime.date(2013, 2, 16),
3: datetime.date(2013, 2, 16),
4: datetime.date(2013, 2, 16)},
'impvol': {0: nan, 1: nan, 2: nan, 3: nan, 4: nan},
'putclose': {0: Decimal('0.54'),
1: Decimal('0.65'),
2: Decimal('0.76'),
3: Decimal('1.08'),
4: Decimal('1.56')},
'strike': {0: Decimal('39'),
1: Decimal('40'),
2: Decimal('41'),
3: Decimal('42'),
4: Decimal('43')},
'symbol': {0: 'A', 1: 'A', 2: 'A', 3: 'A', 4: 'A'},
'underlyingclose': {0: Decimal('42.86'),
1: Decimal('42.86'),
2: Decimal('42.86'),
3: Decimal('42.86'),
4: Decimal('42.86')}}
optionsData = optionsData.T
def calcvol(info):
print info.name
print 'Starting procedure.'
tempmb = mb.BS([info['underlyingclose'],
info['strike'],
.25,
info['daystoexpiration']],
callPrice=float(info['callclose']),
putPrice=info['putclose'])
print 'mb created'
impvol = tempmb.impliedVolatility
print 'implied vol calculated'
info['impvol'] = impvol
print 'impvol set'
del impvol, tempmb
print 'vars deleted'
return info
a = optionsData.apply(calcvol)
当我完成所有这些操作时,它会在optionsData中的第一个元素上设置impvol,但似乎随后给了我这个错误:
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-9-116f3c010b9c> in <module>()
----> 1 a = optionsData.apply(calcvol)
C:\Python27\lib\site-packages\pandas-0.10.0-py2.7-win32.egg\pandas\core\frame.pyc in apply(self, func, axis, broadcast, raw, args, **kwds)
4079 return self._apply_raw(f, axis)
4080 else:
-> 4081 return self._apply_standard(f, axis)
4082 else:
4083 return self._apply_broadcast(f, axis)
C:\Python27\lib\site-packages\pandas-0.10.0-py2.7-win32.egg\pandas\core\frame.pyc in _apply_standard(self, func, axis, ignore_failures)
4154 # no k defined yet
4155 pass
-> 4156 raise e
4157
4158 if len(results) > 0 and _is_sequence(results[0]):
ZeroDivisionError: ('float division by zero', u'occurred at index 1')
0
Starting procedure.
mb created
implied vol calculated
impvol set
vars deleted
0
Starting procedure.
mb created
implied vol calculated
impvol set
vars deleted
1
Starting procedure.
我必须错过一些简单的事情。我已经尝试将float()中的每个值包装起来,因为我将它们传递给mibian,但仍然会遇到同样的问题。我非常感谢任何指导。
另外,如果您知道使用数据框架计算隐含波动率的更有效方法,我会全力以赴。
答案 0 :(得分:1)
正如hayden评论的那样,我确实单独尝试了这个功能,但没有足够大的样本量。
显然,对于这些变量的某些值,它将接近于零。我更改了calcvol
函数以尝试使用mibian来获取波动率,如果遇到ZeroDivisionError
,则将impvol
设置为NaN
。这将帮助我找出导致这种骚动的原因。